Making a trailing stop with StopMarketOrder()?

Back

We are trying to implement trailing stops for Long and Short orders.

We were doing simple if/else statements to keep track of a the high-water mark (or low water mark in a short), but it seems the correct way would be using the StopMarketOrder() function, and then doing an UpdateOrderFields() as this will be more accurate (and stateless). What confuses me, si that all the documentation around StopMarketOrder() says you set it HIGHER than the current market price, which isn't what we want a trailing stop for (we want it as a way to cap our downside).

Is this correct? Or have I misunderstood the StopMarketOrder() purpose?

Thanks!
 

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.


Somebody may correct me, but both, LimitOrder and StopMarketOrder, can be used below and above the price, the main difference between them is that LimitOrder is a "execute only with profit for me" order and StopMarketOrder is a "execute by any mean" order. Depending on a volume sign, positive or negative, they both can open long and short position.

 

 

// current price is $80
// this order says to broker - I agree to buy for any price from 0 to $70
// if price is higher than $70 - do nothing
LimitOrder(symbol, 1, 70.00)

// current price is $60
// this order says to broker - I agree to sell for any price from $70 to infinity
// if price is cheaper than $70 - do nothing
LimitOrder(symbol, -1, 70.00)

// current price is $60
// this order says to broker - when price go to $70 or above
// execute order as fast as possible, even if I have to pay more than $70
StopMarketOrder(symbol, 1, 70.00)

// current price is $80
// this order says to broker - when price go to $70 or below
// execute order as fast as possible, even if I lose money by selling it cheap
StopMarketOrder(symbol, -1, 70.00)

1

Ok, I take my words back. Looks like people at Quant Connect imagine order management differently. Explained here.

0

We modelled StopMarket, StopLimit and Limit orders from Interactive Brokers. 

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.


Drew Baker we'll post an example here tomorrow. The example in the documentation shows a lower price.

// Closing out a long position on market drop
var stopPrice = close * .9975m;
var newTicket = StopMarketOrder("IBM", -100, stopPrice);
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.


https://www.youtube.com/watch?v=S2NMtBDGJqM

I found this video helpful in understanding this. From what I can tell, a "stop order" in the video is roughly the same as QC's StopMarketOrder and a "stop limit order" is roughly the same as QC's StopLimitOrder. 

I still dont understand what positive quantities mean in a stop order though, still trying to understand this.

0

Hi Aadi,

 

If you're referring to the positive number in an order such as the one below, then it is telling the algorithm to place a StopMarketOrder to buy 1 share of the symbol. If you wanted to sell, then you would pass -1 as the numeric argument for order quantity.

StopMarketOrder(symbol, 1, 70.00)

 

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.


What if in my StopMarketOrder, instead of wanting to sell a fixed quantity, I want to liquidate the entire holding if the set Stop is hit?

0

In other words, does anyone know how I can incorporate self.SetHoldings() into a StopMarketOrder?

0

Sorry, I meant how to incorporate self.Liquidate() not self.SetHoldings().

0

Vncne  have you found the solution? I also need some kind of "Sell evrything if price goes up 7% or down  10%. Is it possible to set two stopMarketOrders? 

 

0

Hi Vncne & Crypto Hai,

We can set our StopMarketOrder to liquidate all of our current holdings by utilizing the Portfolio object.

self.StopMarketOrder(self.spy, -self.Portfolio[self.spy].Quantity, data[self.spy].Close * 0.7)

See the attached backtest for reference.

If the holdings quantity adjusts after submitting this order, we can update the order quantity to ensure we liquidate everything. Refer to the docs here.

Best,
Derek Melchin

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.


0
Update Backtest






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...

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!