I have been trying to write a code that sells a put option daily based on some parameters and with that order also submits a linked stop loss and take profit order. I have tried to incorporate Haakon's method for using Bracket Orders to do this, see below
https://www.quantconnect.com/forum/discussion/13928/let-us-create-functionality-for-bracket-orders-together-my-attempt-so-far/p1But I am still running into some issues where it looks like no take-profit orders are submitted and fill, stop and limit are all the same price.
Apologies if this seems trivial I am still quite new to the platform.
Any other feedback will be appreciated partially if there is a better way to structure the option chain checking and subscription, I am also running into the issue where it seems on each loop it doesn't re-check the nearest expiry available.
Thanks
Mia Alissi
Based on the provided code, it seems like the BracketOrder class is not fully implemented. The
__init__
method is defined, but there are no other methods in the class to handle the creation and management of the bracket orders.In the main.py file, a BracketOrder object is created with this line:
However, this object is not assigned to any variable, and no methods are called on it. This means that the bracket order is not actually being placed in the market.
To implement bracket orders, you need to create a class that initiates and handles all new bracket orders. This class should have methods for creating the initial order, as well as the stop loss and take profit orders. It should also have a method for handling order events, to ensure that when one part of the bracket order is filled, the other parts are cancelled.
Here is an example of how you might implement this, based on the information provided in the extracts:
In this example, the
placeOrder
method is used to place the initial order, as well as the stop loss and take profit orders. TheonOrderEvent
method is used to handle order events. When the initial order is filled, it places the stop loss and take profit orders. When either the stop loss or take profit order is filled, it cancels any remaining open orders for the symbol.Please note that this is just a basic example, and you may need to modify it to fit your specific needs. For example, you might want to add error checking to ensure that orders are only placed if there is sufficient buying power, or to handle cases where the initial order is not filled.
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.
James
Mia Alissi After making the changes the orders are being placed however, the opposing stop loss or take profits are not being cancelled when the other order is filled. In the class BracketOrder def onOrderEvent there is logic that should cancel the opposing order but this does not occur.
Mia Alissi
Based on the provided code, it seems like the issue might be related to the order in which the orders are being placed and the order events are being handled.
In the
placeOrder
method of theBracketOrder
class, the entry order, stop loss order, and take profit order are all placed at the same time:However, in the
onOrderEvent
method, the code only checks if the filled order is the entry order, and if it is, it returns without doing anything:This means that if the entry order is filled before the stop loss and take profit orders are placed, the
onOrderEvent
method will return before it has a chance to cancel the other orders.One possible solution to this issue is to only place the stop loss and take profit orders after the entry order has been filled. This can be done by moving the code to place these orders into the
onOrderEvent
method, like so:This way, the stop loss and take profit orders are only placed after the entry order is filled, and the
onOrderEvent
method can correctly handle the cancellation of the other orders when one of them is filled.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.
James
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!