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

Cancel Individual Order in IB



  • Hi everyone,

    I have a strategy up and running well on IB paper trader.

    In the strategy I have many orders working across a number of FX currency pairs. During the strategy there are times I need to cancels orders on one currency pair with out affecting the others. I have the live orders stored and managed in a CSV file locally that updates regularly to stay updated with fills etc.

    So when I need to cancel a particular order I can call it from my CSV and obtain the OrderId or any other information required. I tried the below line but got the attribution error can anyone please help me troubleshoot this?

    myOrder.OrderId.values returns the OrderId in this case 6281 as an integer

    self.cancel(myOrder.OrderId.values)

    Traceback (most recent call last):
    File "/Users/~~~~~~~/opt/anaconda3/lib/python3.7/site-packages/backtrader/brokers/ibbroker.py", line 310, in cancel
    o = self.orderbyid[order.m_orderId]
    AttributeError: 'numpy.int64' object has no attribute 'm_orderId'

    Thanks



  • You need the actual Backtrader order object to be able to cancel it. Usually this is not a problem since the strategy may cache the orders it get from buy/sell calls or the ones got from notify_order. In your case you try to "re-create" the Order objects from the information in the CSV file (which may potentially contain the orders created outside the current Backtracer session - like previous trading session - which are not known to Backtrader).

    Unfortunately creating the order is solely prerogative of the particular Broker ( IBBroker, OandaBroker and so on ) and should not be attempted by the strategy or any other user component. AFAIK no broker provides the way to re-create the order according to ref or any other id.

    On another note: even if all the pending orders are stored in CSV - how they are synced in case of crash or internet connection disruption or power outage? Will you also track the orders which were executed, rejected or expired during the time the system was down ?

    I remember this topic was already discussed few times in this forum, but no working solution was presented so far.



  • Thanks for your reply Vladsisld, yes recreating the old order to cancel it sounds like a bit of a step too far for me.

    Yes I have had to build out the position and order management completely separately. I am fortunate that the strategy is fairly simple and runs once a day, this may not be possible for more active traders, so I place orders in to enter positions good for the day that way if they are filled it is in my position and if not filled the strategy won't double up the entry order the following day. I therefore am able to utilise a separate script outside of backtrader to check my positions and orders and update them as needed. I have that script on a schedule running every few hours during trading days.

    There are other changes to the orders I make, backtrader allows me to place only certain types of orders, so my order management script checks all my working orders each time it runs and updates the bracket orders to allow trailing stops and eventually to utilise the IB algos etc, it also checks the positions and makes sure I have the right amount of orders for each position. its not ideal to run that order management separately but it seems to be working for me for now.

    I do like using backtrader, it has been an excellent tool and a great way to learn python over the last year or so, I am sure my code is terrible but the simplicity of the platform has allowed me to build a working Algo so thank you to everyone who contributed to it.


Log in to reply
 

});