Hi,
I'm new to QC and get my first experiences with Phyton. Now i'm trying to implement a simple stop loss via stop order to my algo. I buy the assets via SetHoldings in OnData.
Then I want to add a stop order in the OrderEvent:
def OnOrderEvent(self, event):
self.AveragePrice = self.Portfolio[event.Symbol].AveragePrice
self.StopMarketOrder(event.Symbol, -self.Portfolio[event.Symbol].Quantity, self.AveragePrice*0.8)
The backtest with this code is not possible: It doesnt bring an compiler error etc but it crashes. The backtest starts and cant be finished. Is there an referencing problem here?
Thanks for your help, Chris
Louis Szeto
Hi Chris
OnOrderEvent is called not only when an order is filled, but whenever a trade status changes, including a trade submission. Since you're submitting a stop market order within it, it will be called recursively as an infinite loop.
Since we only want a stop order when
we'll have to include an if condition to filter what we want:
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.
Toaster
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!