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?

  1. ... Buy:
  2. self.entry_ticket = self.LimitOrder(self.Symbol, quantity, price))
  3. self.entry_ticket_time = self.Time.hour
  4. def OnOrderEvent(self, orderEvent):
  5. if orderEvent.Status != OrderStatus.Filled:
  6. return
  7. if self.entry_ticket is not None:
  8. if orderEvent.OrderId == self.entry_ticket.OrderId:
  9. price = orderEvent.FillPrice
  10. quantity = orderEvent.Quantity
  11. self.LimitOrder(orderEvent.Symbol, -quantity, priceL)
  12. self.StopMarketOrder(orderEvent.Symbol, -quantity, priceS)
  13. else:
  14. self.entry_ticket = None
+ Expand

Author

Alex Veraart

January 2023