Back

Implementing a complicated order sequence with a stop buy order as trigger

I Am trying to implement the following order logic:

  • Once a stop buy price above the current price is reached, buy 100 of a particular security
  • Once the stop buy order has been triggered
    • trigger a stop loss order
    • trigger a profit target order 1
    • trigger a profit target order 2

The following pseudocode represents what I have come up with, so far:

def Initialize(self):
self.symbol = 'SPY'
self.stopBuyPrice = None
self.stopLossPrice = None
self.profitTarget1 = None
self.profitTarget = None


def OnData(self, data):
self.stopBuyPrice = data[self.symbol].CLose + 3
self.stopLossPrice = self.stopBuyPrice - 10
self.profitTarget1 = self.stopBuyPrice + 10
self.profitTarget2 = self.stopBuyPrice + 20

#Buy 100 once stopBuyPrice is reached
self.ticket = # ?

def OnOrderEvent(self, orderEvent):
if self.ticket.OrderId == orderEvent.OrderId and orderEvent.OrderStatus.Filled:
#Enter stop loss order
self.StopMarketOrder(self.symbol,100,self.stopLossPrice)
#Enter limit order 1
self.LimitOrder(self.symbol,50,self.profitTarget1)
#Enter limit order 2
self.LimitOrder(self.symbol,50,self.profitTarget2)

 

Is this the correct way to do this and how can I realize a stop buy order, i.e. an order that should be triggered once the security price reaches a fixed value above the current price?

Update Backtest







0

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.


Hey Filib,

The quantities for your exit orders need to be negative if you are entering long with your stop buy. You also need to make sure to update your stop loss as your profit limit orders are reached. Otherwise, you may end up in a net short position if all your orders are filled. Likewise, you need to cancel your profit limit orders if your stop loss is filled. To be able to update your orders, you need to keep track of their respective order tickets.

Here's an implementation of this sequence of orders.

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed