For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Cancelling Bracket order following closing of the main side



  • For while I've been trying to solve following problem: When I place bracket order with stop and limit, and if later the trade is closed not by stop or limit orders, but by the strategy logic, they (the children orders - Stop and Limit) remain active and one of them gets executed later. To avoid this I need to cancel it by using the order reference and call self.cancel(sop_order) or similar.

    My logic suggests that to call it I need to use something like if trade.isclosed: or maybe if self.position.size == 0: and then call the order by its id and this is where I get super confused.

    I wrote about this previously but couldn't find a solution since.

    Any ideas how to implement this part the code?

    Thanks,
    Rumen


  • administrators

    The fundamental problem is that a position open by a bracket should not have additional logic to close the position independently.

    But if you insist in shooting yourself in the foot, the only thing you have to do is to keep a reference to any of the bracketing orders (stop-loss side or take-profit side) and cancel it whenever you close the position with the bracket-foreign logic.

    if logic_orders_to_close_the_position:
       self.cancel(take_profit_order)
    

Log in to reply
 

});