Hello,
Assume that I enter a position using a simple market order such as
self.Buy(option_symbol, 1)
I would like to combine it with a stop loss order that will exit (sell) in case that the position is in 50% loss - how should the stop loss order look like?
Thank youÂ
Mia Alissi
To set a stop loss order that will exit the position at a 50% loss, you would need to set the stop price at 50% of the purchase price. However, the exact purchase price might not be known at the time of placing the order, especially if the order doesn't execute immediately.
You can get the fill price in the
OnOrderEvents
event handler. Once you have the entry fill price, you can calculate the price level for the stop loss order. Here is an example of how you might do this in Python:In this example,
self.ticket
is the order ticket of your entry order (the market order you mentioned). When the entry order is filled, a stop market order is placed with a stop price at 50% of the fill price.Please note that this is a simplified example and you might need to adjust it according to your specific needs. For example, you might want to add checks to ensure that the stop loss order is only placed once, or to update the stop price under certain conditions.
Sources:
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.
Nathan Sela
Thank you Mia, I tried to implement your code, however, the OnOrderEvent() method is called before the ticket is returned from the original Buy() method so the ticket is still None in OnOrderEvent(), what am I doing wrong?Â
Mia Alissi
The issue you're experiencing might be due to the asynchronous nature of order events. When you place an order, the
OnOrderEvent
method can be called before the order ticket is returned and assigned toself.ticket
.To handle this, you can use a flag to indicate whether the entry order has been placed. Here's an example:
In this example,
self.entry_placed
is a flag that is set toTrue
when the entry order is placed. In theOnOrderEvent
method, the stop loss order is only placed ifself.entry_placed
isTrue
and the filled order is the entry order. After the stop loss order is placed,self.entry_placed
is reset toFalse
.This should ensure that the stop loss order is placed correctly. However, please note that this is a simplified example and you might need to adjust it according to your specific needs.
Sources:
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.
Nathan Sela
I still do not see how this will work in all cases - if by the asynchronous nature of order events OnOrderEvent() is called before the ticket gets its value that the new flag of entry_placed will remain False and the stop order will not be placed…
Mia Alissi
You're correct, Nathan. The asynchronous nature of order events can indeed cause the
OnOrderEvent
method to be called before the order ticket is returned from theBuy
method. This is because order events are triggered asynchronously as they occur.One way to handle this is to keep track of the order IDs and their corresponding stop prices in a dictionary. When an order is filled, you can look up the stop price in the dictionary using the order ID from the order event. Here's an example:
In this example,
self.stop_prices
is a dictionary that maps order IDs to stop prices. When an order is placed, an entry is added to the dictionary with the order ID as the key and the stop price as the value. When an order is filled, the stop price is looked up in the dictionary using the order ID from the order event, and a stop market order is placed with the retrieved stop price. After the stop market order is placed, the entry is removed from the dictionary.This should ensure that the stop market order is placed correctly even if the
OnOrderEvent
method is called before the order ticket is returned from theBuy
method.Sources:
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.
Nathan Sela
How about simply placing the stop loss order together with the original order. If the original is a market order then I know the fill price - something like that:
self.Buy(option_to_trade.Symbol, 1)
entry_midprice = 0.5 * (option_to_trade.AskPrice + option_to_trade.BidPrice)
self.StopMarketOrder(option_to_trade.Symbol, -1, 0.5 * entry_midprice)
Â
Will that work?
Â
Mia Alissi
Yes, Nathan, your approach should work in most cases. You're placing a market order to buy the option and then immediately placing a stop market order to sell the option if its price falls to 50% of the entry midprice. Here's your code:
However, please note a few things:
The
entry_midprice
is calculated based on the current ask and bid prices, which might not exactly match the fill price of the market order. The fill price depends on the market conditions at the time the order is executed.If the market order doesn't execute immediately (for example, due to low liquidity), the stop market order might be placed with an incorrect stop price.
If the market gaps down past your stop price before the stop market order is executed, the order might be filled at a worse price than the stop price you set. There's no guarantee that a stop market order fills at the stop price.
Sources:
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.
Nathan Sela
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!