Order Automation Issue using python API

Discussion in 'IB Technical Issues' started by orangelam, Feb 8, 2019.

  1. Hi,
    I would like to do a simple order automation.

    When market open, enter at market price with a stoploss level everyday. Settle all positions right before the market everyday.

    What i am developing is,
    1) send out a market order several minutes before market open so that the system will help me to fill this order when market open

    2) after filling the order, i can got the filled price then i set my stoploss level based on this filled price, and send out a stoploss order

    However, i don't know how to ask system to help me to flat my position (i call this flat order) 1 minute before market close. Also, two problems will be arised

    1) if stoploss order is filled within the day, how to ask system to help me to cancel/disable this flat order?

    2)if stoploss order is not filled, then the flat order will be filled 1 minute before market close. for this case, how to ask system to cancel/disable the stoploss order?

    appreciate if anyone can tell me a simple way to do so! thanks!
  2. Your first step at 1 minute before market close is to ask for a position report. From this report can you conclude whether you still have an open position, or whether the stoploss has triggered meanwhile and your position got closed during the day. To be sure about the existence of the stoploss order can you also ask for an overview of all open orders.

    Now you have two possible routes to follow:
    Route 1: your stoploss triggered and your position got closed. No further action is required because you are flat and you have no remaining order lines.
    Route 2: your stoploss did not trigger during the day and you still have an open position. You now have two ways of closing this.
    Route 2a: you cancel the stoploss order and place a new order to close your open position.
    Route 2b: you modify the price of the existing stoploss order such that it triggers immediately. In this way are you using the existing stoploss order to flat your position, and in the process is the stoploss order also removed because it is being executed.
  3. yes you are right.

    why i think it is difficult is that i thought i can do all the stuff in the morning without any investigation after placing all the orders then wait for market close

    based on what you said, yes, i can write a script to check the position report 1 minute before the market close then i can perform what you suggested in 2a and 2b.

    however, is it possible to do everything in the beginning without making any checking 1 minute before market close? because i dont want to open IB again 1 min before close to send out orders.
  4. How would you be able to know in the morning what the situation will be at the end of the day? Are you able to predict the future?
    Probably you will need to find a way to keep your computer connected to IB throughout the day. Switch it on in the morning and switch it off in the evening. Or switch it on on Monday morning and switch it off on Friday evening.
  5. is it possible to send out something in the morning like, if the stop loss order is not filled right before afternoon market close, revise this stop loss order to a market order and fill the position.

    because i need to work and keep the computer working at home. some issues may cause problem like wifi connection, or computer shut down. i m using window, even i disable the window auto update, sometimes it still help me to update and then shutdown...
  6. There is a possibility to add conditions to an order. But I don't know whether it would be flexible enough to handle the case you want to achieve. IB's user manual about conditional orders: http://interactivebrokers.github.io/tws-api/order_conditions.html

    If your trading is valuable and profitable enough you could consider to buy a separate computer or laptop for your code to run.