Hello guys, hope you're all doing great!
I have a problem that I can't solve and I would love it if anybody could take a look at it.
I have a tick-type futures strategy that also trades in the extended market. Even though I have a strict SL&TP there were multiple occasions where the profit/loss was bigger than the normal +/- 200. I took a look at the code and it seems like the positions do not close /liquidate the right way. Because as soon as the market opens up again the positions close and it loses a lot of money. Maybe it has to do something with the stop market order or limit order.
For solving purposes, I only included the buying and selling. To show this problem I added an image of a chart that can be found on the backtest under"PROFIT"(shows last trade profit). Normally this number should be somewhere between -/+200 but sometimes it is -2k.
It might have to do something with the way it receives trade bars but I don't understand how to rewrite the code so that it doesn't happen anymore…
This chart shows liquidation(red, happens multiple times??), BarHandler before(green) and after(blue) StopTrading barrier that activates after the liquidation process
The objective: close open positions before market close (dates from GetMarketHours with extended market hours)
Code to get next open and close(extended):
hours = self.Securities[self.future.Symbol].Exchange.Hours
if hours.IsDateOpen(self.Time):
self.StopTrading = False
#get next open/close date
self.todayOpen = hours.GetNextMarketOpen(self.Time, True)
self.todayClose = hours.GetNextMarketClose(self.Time, True)
#liquidate position 15 minutes before close if unrealized profit > 30
if self.todayClose - timedelta(minutes=15) < self.Time and self.StopTrading is not True:
if self.Portfolio[self.future.Symbol].UnrealizedProfit > 30:
self.StopTrading = True
#liquidate position 2 minutes before close
if self.todayClose - timedelta(minutes=2) < self.Time and self.StopTrading is not True:
self.StopTrading = True
self.Liquidate()
self.Plot("CLOSING", "Liquidation_signal",0.5)
self.Transactions.CancelOpenOrders()
#activate trading 2 minutes before open
if self.todayOpen - timedelta(minutes=2) < self.Time:
selfStopTrading = False
I also tried it with scheduled events:
self.Schedule.On(self.DateRules.EveryDay(self.future.Symbol), self.TimeRules.BeforeMarketClose(self.future.Symbol, 1, True), self.CloseMarket)
self.Schedule.On(self.DateRules.EveryDay(self.future.Symbol), self.TimeRules.AfterMarketOpen(self.future.Symbol, 0, True), self.OpenMarket)
def OpenMarket(self):
self.StopTrading = False
def CloseMarket(self):
self.StopTrading = True
self.Transactions.CancelOpenOrders()
self.Liquidate()
self.Plot("CLOSING", "Liquidation_signal",1.5)
Both do not work.
I would be very thankful if someone could help me with this 😊
Thanks in advance!
Alexandre Catarino
Hi Nico Xenox ,
The fill model for the futures security type is the FutureFillModel which implements the LimitFill and StopMarketFill methods of the base FillModel. For a tick-resolution subscription, it will consider the last bid/ask for sell/buy orders as the trigger price, and the same price will be used to define the fill price in the worst-case scenario assumption. All of these results in filling with the bid/ask price, so your algorithm will rarely get the $200 profit or loss. The limit orders should fill with the limit price. It's a known issue that we are addressing in the EquityFillModel and will extend to other security types. Meanwhile, you can create a custom fill model that fills at the limit price.
I don't understand the problem with liquidation, Could you please expand on this paragraph?
This chart shows liquidation(red, happens multiple times??), BarHandler before(green) and after(blue) StopTrading barrier that activates after the liquidation process
What are we seeing? Why is it wrong? What should we see?
Best regards,
Alex
Nico Xenox
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!