Hi All,
When implementing a simple stop loss / profit take, and timed exit for a trade I am running into an issue where all three execute simultaneously. despite being restricted by conditions (see order screen cap).
Could someone explain to me why this is happening, and how to fix it? I have attached the backtest code. Thanks in advance!
class TradeStrategyTest(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021,3, 1) #Set Start Date
self.SetEndDate(2021,6,14) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddEquity("OCGN", Resolution.Minute)
self.RSI1 = self.RSI("OCGN", 15)
self.RSI2 = self.RSI("OCGN", 2)
self.SetWarmUp(14)
self.Securities["OCGN"].FeeModel = ConstantFeeModel(2.00)
self.ticket = None # Flag for position status
def OnData(self, data):
if self.IsWarmingUp or not data.Bars.ContainsKey("OCGN") or not self.RSI1.IsReady or not self.RSI2.IsReady:
return
# Buy Conditions
if not self.Securities["OCGN"].Invested and self.ticket is None:
if ((self.RSI1.Current.Value <=70 and self.RSI1.Current.Value >=20)):
self.ticket = self.MarketOrder("OCGN", 1)
self.StopMarketOrder("OCGN", -1, self.Securities["OCGN"].Close* 0.95*0.99)
self.LimitOrder("OCGN", -1, self.Securities["OCGN"].Close*1.03)
elif self.Portfolio["OCGN"].Invested:
self.ticket is not None and self.UtcTime > self.ticket.Time + timedelta(minutes = 15)
self.Liquidate()
self.ticket = None
Order Screen Cap:
Louis Szeto
Hi Chris
It is not execute simutaneously. They are just submitted simutaneously, not filled, you could see there is only 1 fulfilled while other 2 cancelled. So why It kept liquidating minute-wise:
You forget to set “if” condition in this line.
Cheers
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.
Christopher Phillips
Hi Louis Szeto ,
Thanks so much for the help! I added the ‘IF’ and it fixed some of the issues.
I still see the stop and take profit orders executing when they shouldn't (I have attached a screen shot).
It looks like it is submitting orders at end of day and they are getting filled the next day on market open.
Do you know what is going on here? It's almost like it is shorting instead.
Thanks again for the help, you rock!
Louis Szeto
Hi Chris
If you want everything settled before market close. You can just schedule an event on some minutes before market close to liquidate all positions and cancel all open orders.
Also you should set a condition in OnData method to check if it is in the last minutes of the market to stop trading. For example:
Cheers
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.
Christopher Phillips
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!