Back

Stop Loss and Limit Order

I am trying to use a stop loss and limit order, but for some reason they are being canceled the same moment I attempt to place them. Why is this happening?

 

Also, if a limit order is executed, will it automatically cancel the stop loss or must I do this myself?

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.


Hi Michael,

It looks like you're trying to implement a moving average crossover algorithm. First thing to note is that it is good practice to add a threshold to the moving average when we are determining if a crossover occurred. We do this because the price can oscillate around the moving average, which would produce many false signals.

Instead of using a boolean, self.over, to keep track of whether a crossover occurred, we can use rolling windows to keep track of the previous prices and compare them to the moving average. This method is easily scalable to multiple symbols. We can create a rolling window to hold the latest 2 prices for our security.

self.priceWindow = RollingWindow[float](2)

and then we can compare the prices to the moving average to determine if a crossover has occurred.

## long crossover
iif self.priceWindow[0] > ma * (1 + self.threshold) and self.priceWindow[1] < ma * (1 - self.threshold):

Lastly, we will need to update our stop loss and limit orders as each fills since they are not automatically cancelled. We can do this in the OnOrderEvents method, which is fired each time an orderevent occurs. Inside this method, we can check if our stop loss is filled and cancel our limit order, and likewise if our limit is filled.

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