Interactive Broker python API Question

Discussion in 'Interactive Brokers' started by orangelam, Jan 30, 2019.

  1. for futures, do you know how to set a price delta for stoploss instead of inputting the absolute price?
    for example,
    contract=Future('HSI', '20190227', 'HKFE')
    ib.qualifyContracts(contract)
    order = Order()
    order.action = 'BUY'
    order.orderType = "MKT"
    order.totalQuantity = 1
    order.auxPrice =27900

    stop loss is set at 27900, however, sometimes we don't know the filled price, so is it possible to set a price delta like 200 (e.g. drop more than 200 ticks) to trigger stop loss? in Hong Kong Hang Seng index futures, there is a pre-opening session, i want to set a market order in this period so that IB system will help me to fill my order right after the market open. so for this case, i don't know the filled price.

    also, do you know how to mingle TimeCondition to above order, so that i can set a specific time entering a market price? actually i want to flatten my position right before market close, but i don't know what is the best way to do it. thanks!
     
    #11     Jan 31, 2019
  2. #12     Jan 31, 2019
  3. d08

    d08

    There's no delta stop as such AFAIK. You can query fill price and then send the stop based on that.
     
    #13     Jan 31, 2019
  4. How would a trailing stop work with a market order? I assume it would use execution price.
     
    #14     Jan 31, 2019
  5. thanks, i think goodAfterTime can set a specific time to send out the order, so that i can settle my position. i think this GAT order is completely independent of the initial order filled in the market open, so before sending out the GAT order, i need to check and make sure the first order is filled, right?
     
    #15     Feb 1, 2019
  6. yes, i need to decide the stoploss level based on the filled price, so i m thinking doing something like, after filling the order, find out the filled price then set my stoploss price. but do you know how to do that? can i use python to send out the stoploss level after filling the order?
     
    #16     Feb 1, 2019
  7. do you know how to send out the stoploss level after filling the order using python?
     
    #17     Feb 1, 2019
  8. d08

    d08

    There is no magic involved, you request executions (reqExecutions) and from that derive the execution price. Then send a stop order (placeOrder).
     
    #18     Feb 1, 2019
  9. yes, i will find out the filled price by looking ib.fills()
    i think it make more sense to look at the filled order.
    as i will send out my order in pre-opening session and wait for several minutes for the market to open for filling at opening price. do you think whether i should use below to let python to wait for the filled order?
    while not trade.isDone():
    ib.waitOnUpdate()
    as i haven't tried to do so, don't know whether it works in the pre-opening session...
    then i will look at ib.fills() to find out the filled price, however, i don't know how to revise the stoploss level on the filled order. i think Order() is for assigning a new order, how can i revise the stoploss level on the filled order?
     
    #19     Feb 1, 2019
  10. d08

    d08

    Modification is the same as cancel and submit new order. So might as well cancel and send a new one.

    Not sure you understand how the IB API works in principle. If you've registered and waiting for execution reports then no while loop is necessary. You wait until the whole order is complete or you wait for partial fill+some time and then send the stop. Marketable stop orders don't work outside RTH of course.
     
    #20     Feb 1, 2019