Back

How to detect that position was closed / liquidated

Say, I create MarketOrder, broker fills it and I have open position, I add Stop Loss and Take Profit orders.

How to find out that this position was closed and remove Stop Loss and Take Profit order?

P. S. I saw other subjects about SL / TP orders, like this one, but it doesn't answer my question

https://www.quantconnect.com/forum/discussion/1700/support-for-one-cancels-the-other-order-oco-
Update Backtest








How is your need different from the OCO? Do you want to cancel both?
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.


I tried to describe possible scenario, maybe I just need to add more details.

Problem: Long position opened with SL / TP, then I open Short position of the same volume, result - positions compensate each other, so I don't have positions at all, but SL / TP orders are still active. OCO cancel orders, so OCO can detect when order gets filled / invalid / cancelled, but in my case initial market order will be closed by NEW UNRELATED POSITION. So, I need some mechanism to define connection between the first 3 orders (Market, SL, TP) and new order that will remove initial market order. Link in the post describes how to deal with 3 orders, it doesn't answer how to cancel existing orders when OCO relations are already defined.

0

When you place a trade you receive an OrderTicket for you to access the order. This allows you to update and cancel the order like this

ticket.Cancel()

See this algorithm about order tickets

https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/OrderTicketDemoAlgorithm.cs
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.


Yes, I can call "Cancel" method. My question is, WHEN I should call it?

  • Long position with 5 contracts of MSFT was opened at 9am, it has SL / TP
  • Short position with 5 contracts of MSFT was opened at 3pm

Second order is closing initial Long position, but I still have SL / TP active. OCO can define relation between Long / SL and TP, how to define relation between these orders and future Short order? All existing examples show how to connect 3 initial orders - Long / SL / TP, but once Long is filled it's not an order anymore, it's an open position that doesn't have any order status, so it's not clear when to close SL / TP.

0

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