I tried to write a function that blocks new trades being placed for 30 minutes after a stop or take profit is hit, as often the trade criteria are still met immediately after exiting a position on shorter time frames, which can create a terrible loop of repeated losses, or cause an entry near the end of a breakout after tp is hit. I was using a scheduled event every minute to check against the time of my most recent tickets, but I am not sure how to do that outside of OnOrderEvent, which is the logic I was piggypacking off of? Or if there is a simpler way that I am missing.? Obviously, this code does not run as ‘orderEvent.OrderId’ does not exist outside of OnOrderEvent
self.Schedule.On(everyday, self.TimeRules.Every(timedelta(minutes=1)), self.TradeBuffer)
def TradeBuffer(self):
for symbol, data in self.symbolData.items():
if data.stopmarketorderticket != None and data.stopmarketorderticket.OrderId == orderEvent.OrderId:
if self.UtcTime >= (data.stopmarketorderticket.Time + timedelta(minutes=30)):
data.longTrade = True
if data.limitorderticket != None and data.limitorderticket.OrderId == orderEvent.OrderId:
if self.UtcTime >= (data.limitorderticket.Time + timedelta(minutes=30)):
data.longTrade = True
Vladimir
Victoria Butler
Attached backtest blocks new trades being placed for X minutes after a Stop Loss or Take Profit is hit.
if not self.wait > X: return
If you are satisfied with my answer, please accept it and don't forget to like it.
Victoria Butler
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!