Hello community,
Over the last few weeks I've been trying hard to get a grip on how QuantConnect and Python work, but it seems my brain doesn't make simple connections. I consider myself a knowledge person in general, not in the last place since I managed to start a successful business in my early 30s, but getting a grip on Python / QuantConnect seems harder than I want to admit. I already followed some boot camp courses, and I'll continue the boot camp later on, but still some basics are hard for me to grasp.
In specific, what I'm trying to do is build a realtively simple RSI strategy, which I'm planning on extending later with some Larry Connors additions ;-) However, what I'm struggling with is setting a stop loss for every market order if the initial price of the MarketOrder will get 10% below the price that I bought the stock for.
I hope anyone can help me out with this.
Jimmy
HughStryker
Hi Jimmy,
Fellow noob here, but I've overcome many hurdles including this one on my journey thus far - it's worth persevering. Do you have an algo you're working on that you can attach to this thread for context? I think it matters because order types vary by broker (e.g. some brokers support self.StopMarketOrder, others do not).
Derek Melchin
Hi JimmyNeedles,
To create a stop loss order, create a StopMarketOrder after a MarketOrder.
def OnData(self, data): if self.stop_loss_ticket is None: self.entry_ticket = self.MarketOrder("SPY", 1) stop_price = self.entry_ticket.AverageFillPrice * 0.9 self.stop_loss_ticket = self.StopMarketOrder("SPY", -1, stop_price)
After the stop loss is hit, we remove the stop loss ticket to allow a new entry order to take place.
def OnOrderEvent(self, orderevent): if orderevent.Status != OrderStatus.Filled: return if self.entry_ticket is not None and \ self.entry_ticket.OrderId == orderevent.OrderId: self.entry_ticket = None if self.stop_loss_ticket is not None and \ self.stop_loss_ticket.OrderId == orderevent.OrderId: self.stop_loss_ticket = None
See the attached backtest for a working example.
Best,
Derek Melchin
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.
JimmyNeedles
Thank you, both! I got it to work :)
JimmyNeedles
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!