Hi all,
A happy new year. A new year with struggles with .the algorithms.
When I open a trade, I immediately set a stop-loss and limit order. I work with a hourly resolution for multiple equities. It is possible that I open two (or more) trades at the same time, but then the code doesn't place limits for both trades.
So next part works for when I open one trade, but does nothing when I make two trades. I think it has something to do with the entry_ticket or with the OrderId.
How can I set limit/stoploss for open trades or for multiple trades when I open them at the same time?
+ Expand
... Buy:
self.entry_ticket = self.LimitOrder(self.Symbol, quantity, price))
self.entry_ticket_time = self.Time.hour
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled:
return
if self.entry_ticket is not None:
if orderEvent.OrderId == self.entry_ticket.OrderId:
price = orderEvent.FillPrice
quantity = orderEvent.Quantity
self.LimitOrder(orderEvent.Symbol, -quantity, priceL)
self.StopMarketOrder(orderEvent.Symbol, -quantity, priceS)
else:
self.entry_ticket = None
Derek Melchin
Hi Alex,
To create take-profit and stop-loss orders, see One Cancels the Other Orders > Place Active Orders. The example there only works for 1 trade, like the above code snippet. To make it work with multiple trades, we'd just need to define a class that encapsulates the ticket, stop_loss, and take_profit members of each trade. When we open a trade, we add it to the collection. When a trade is closed, we remove it from the collection.
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.
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!