Inspired from
Is it possible to set OCO orders for multi-leg spread as well?
Thanks!
QUANTCONNECT COMMUNITY
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.
Mia Alissi
To set up One Cancels the Other (OCO) orders for a multi-leg spread in QuantConnect, you can follow these steps:
Define the Multi-Leg Strategy: Use the
Leg
class to define each leg of your strategy, specifying the symbols and quantities.Place Initial Orders: Use the
combo_leg_limit_order
orcombo_limit_order
methods to place initial orders for your multi-leg strategy. These methods allow you to specify limit prices for each leg or a compound limit price for the entire combo.Monitor Order Events: Implement the
on_order_event
method to monitor order events and track the fill status of each leg in your strategy.Calculate OCO Levels: Once an order is filled, calculate the target price levels for the OCO orders based on the fill price of the entry trade.
Place OCO Orders: Submit new limit or market orders for the remaining legs to create OCO orders. Ensure that when one order fills, the other is canceled.
Simulate OCO Behavior: If necessary, simulate OCO behavior by tracking asset prices in the
on_data
method and manually canceling orders when the target price levels are reached.Update Orders: Continuously update the limit prices of the OCO orders if neither order has filled, using the
apply_update_order_request
method to adjust the order state as needed.Here's a basic example of how you might implement this:
This example demonstrates how to set up a basic multi-leg strategy with OCO orders. Adjust the logic and parameters to fit your specific trading strategy and requirements.
Sources: Other Order Types - QuantConnect.com, Combo Leg Limit Orders - QuantConnect.com, Combo Market Orders - QuantConnect.com, Combo Order Ticket Demo Algorithm, Combo Orders Fill Model Algorithm
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.
Wang Yi
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!