Dear Team,
I am on my aborted strategy use to QC not having the functionality I need. So hope QC will come through this time.
This time I am looking at Currencies. I do not care which broker the trades are executed on. But of course QC will need to support the brokers platform. So may need some advise on which broker offers the features I need.
I could write my strategy in MQL4 for MT4. But want to improve my Python skills and get a Python strategy running on QC.
The strategy needs to be able to open long or shot positions at various prices and quantities with different stop loss and take profits depending on how the underlying currency moves. Also it needs to be able to track each opened position and close each open position individually in any order depending on how the currency moves.
I am a non US citizen. So should not be limited by US Forex regulations. That truly suck.
Does QC support having multiple individual long and short positions open at the same time. With the ability to track the open positions value and the ability to close each position individually?
Think of each position like separate options contract on the same underlying. In fact my strategy is an adaption of an Options strategy.
That will allow me to run one long or short strategy on a currency at a time. However I want to be able to do both long and short strategies on the same currency at the same time. To do this I need to be able to determine which option positions and which orders belong to which strategy. In MT4 this is done with a property called the Magic Number that you assign to each position to identify which strategy it belongs to.
A not about my strategy building style. When I write strategies. I write them in a way so that if the strategy or server fails. Once it is up and running the strategy can workout where it is by looking at what positions and orders are open and continue where it left of. This I do at the beginning of the ondata() function in QC.
Here is a code sample that does the open orders position analysis can you tell me how to adjust the code to ensure it works. I found some code in someones strategy and adapted it to the open position analysis. But can not find any reference to some of the properties used in the code.
self.bull_strategy_id = "Bullish"
self.bear_strategy_id = "Bearish"
self.brokerage_model = None
# 'fxcm', 'oanda', 'ig', 'td', 'alpaca', 'ib'
if self.brokerage_model == 'fxcm':
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
elif self.brokerage_model == 'oanda':
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
elif self.brokerage_model == 'ig':
self.SetBrokerageModel(BrokerageName.IG)
elif self.brokerage_model == 'td':
self.SetBrokerageModel(BrokerageName.TradierBrokerage)
elif self.brokerage_model == 'alpaca':
self.SetBrokerageModel(BrokerageName.Alpaca)
elif self.brokerage_model == "ib":
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.Log(f'Brokerage model set to {self.brokerage_model}')
self.tickers = ['AUDGBP', 'AUDNZD', 'AUDUSD', 'AUDJPY']
self.profit_loss = {}
self.tickets = {}
for ticker in self.tickers:
self.AddData(MinuteBar, ticker)
self.profit_loss[ticker] = []
unfilled_orders_list = None
def OnData(self, data):
for symbol in self.Securities:
open_positions = 0
open_long_positions = 0
open_short_positions = 0
unfilled_orders = False
stop_price = None
take_profit = None
stop_loss = None
unfilled_order_stop_price = None
unfilled_order_take_profit = None
unfilled_order_stop_loss = None
lots_multiplier = None
opening_units = None
security = self.Securities[symbol]
first_position = True
for holding in self.Portfolio.Values:
if holding.StrategyId == self.bull_strategy_id:
open_positions += 1
if holding.IsLong:
open_long_positions += 1
elif holding.IsShort:
open_short_positions += 1
if first_position:
stop_price = holding.StopPrice
take_profit = holding.TakeProfit
stop_loss = holding.StopLoss
self.Log("Open Positions: " + str(open_positions))
self.Log("Open Long Positions: " + str(open_long_positions))
self.Log("Open Short Positions: " + str(open_short_positions))
first_position: = False
if security.OrderCount > 0:
for order in security.OpenOrders:
if order.StrategyId == self.bull_strategy_id:
unfilled_orders = True
unfilled_order_stop_price = order.StopPrice
unfilled_order_take_profit = order.TakeProfit
unfilled_order_stop_loss = order.StopLoss
self.Log("Unfilled Orders: " + str(unfilled_orders))
self.Log("Unfilled Orders Stop Price: " + str(unfilled_orders_ StopPrice))
self.Log("Unfilled Orders Take Profit: " + str(unfilled_orders_TakeProfit))
self.Log("Unfilled Orders Stop Loss: " + str(unfilled_orders_StopLoss))
self.Log("Unfilled Order Entry Price : " + str(unfilled_order_entry_price))
self.Log("Unfilled Order Profit Target: " + str(unfilled_order_profit_target))
self.Log("Unfilled Order Hedged Price: " + str(unfilled_order_hedged_price))
self.Log("Unfilled Order Hedged Target: " + str(Unfilled Order Hedged Target))
break
if open_positions == 0:
self.Log("No open positions")
if not unfilled_orders:
self.Log("No open positions and No unfilled orders")
entry_price = security.Close + 0.00001
Profit_Target_price = entry_price + (3 * self.pip_units)
hedged_price = entry_price - (1 * self.pip_units)
Hedged_Target_price = entry_price - (4 * self.pip_units)
else:
self.Log("No open positions and has unfilled order")
if abs(unfilled_order_entry_price - self.Securities[Security].Price) > 0.0001:
unfilled_orders_list = self.Transactions.GetOpenOrders(x => x.Security.Symbol == security and x.StrategyId == self.bull_strategy_id)
self.Transactions.CancelOpenOrders(unfilled_orders_list)
if not unfilled_orders:
self.Log("No unfilled orders")
lots_multiplier = self.lookup_table[open_positions]
opening_units = self.lot_units * lots_multiplier
if open_positions % 2 != 0 and not unfilled_orders:
self.Log("Odd number of open positions and no unfilled orders")
self.StopBuy(symbol, opening_units, entry_price, True, entry_price, primary_target_price, secondary_target_price)
else: # elif open_positions % 2 == 0 and not unfilled_orders:
self.Log("Even number of open positions and no unfilled orders")
self.BuyStop("AAPL", 100, "MyStrategy", limitPrice=90, stopPrice=95, takeProfit=110, stopLoss=80, quantity=10)
Jackson Dude
Thank you for the reply Alex. Yes I had read the page on Stop Market orders. But was hoping that they were a bit more feature rich. As someone had mention other parameters, but could not find in the reference pages.
From what you said Orders do not have Take Profit, Stop Loss, Strategy ID properties like other platforms. MT4 20 year old to name one. I guess will have to use the OnOrder even to please Stop and Limit orders for the TP and SL and in turn monitor those.
A feature request is to add Take Profit, Stop Loss, Strategy ID options to all order types
The other questions above were:
Does QC support and multiple option positions on the same underlying asset?
Does QC support and multiple option positions on the same underlying asset long and short at the same time?
Does QC support and multiple option positions on the same underlying asset and closing the position in any order?
If so can you provide example of the code to reference the positions on the same asset.
These are features that have been around in MT4 for two decades. Not used by many traders unless you are a hedge trader like me.
Kind Regards
J
Louis Szeto
Hi Jason
Yes, multi-leg options strategy are available via OptionStrategies class and/or Combo orders. You may take a look at the doc here for option strategies' implementations, and here for ordering combo order (you can use them for multi-leg options, or even pack your whole strategy's orders like option-equity/TP+SL mix).
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jackson Dude
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!