¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Python Tws Api Target order


 

I am trying to setup a bracket order that uses a mkt order as parent with a trail aux price stop as well as a target. I have built code that will execute a mkt order and then attach a trail with specified aux price but I can't seem to understand how to tell the limit order to place the target x amount of dollars above the entry price of the market order. I have some coding background but this API TWS stuff is so difficult for me to understand.?

here is some working code mentioned above. I am aware of the bracket order but how do you define the target price. Also does Interactive brokers even allow you to do a MKT bracket order with trail stop and target based on a hard number above entry??
thank you for any help.?
qty=2
def make_order(action, quantity):

        order = Order()
        order.m_orderType = 'MKT'
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_outsideRth = True
        return order 

def make_contract(symbol, sec_type, exch, curr, exp):
    Contract.m_symbol = symbol
    Contract.m_secType = sec_type
    Contract.m_exchange = exch
    Contract.m_currency = curr
    Contract.m_expiry= exp
    return Contract

def stop_lm(action,qty):
    stopLoss = Order()
    stopLoss.m_action = action
    stopLoss.m_orderType = "TRAIL"
    stopLoss.m_auxPrice = 4.00
    stopLoss.m_totalQuantity = qty
    stopLoss.m_outsideRth = True
    return stopLoss

 act="BUY"
 cont = make_contract ('ES', "FUT", 'GLOBEX', 'USD',"201909")
 offer = make_order(act, qty)
 tws.placeOrder(orderId, cont, offer)
 offers = stop_lm("SELL",qty)
 tws.placeOrder(orderId +1,cont,offers)


 

Hi Nikolaos,

Just wondering if you had any luck figuring this out? I'm running into the same problem currently.

Thank you in advance!


 

I know that yes you can create a bracket order with a "TRAIL LIMIT" buy order, (tip, simply set the buy price to a number much higher than the average price) when the buy order is submitted the trail automatically takes up the slack and starts trailing at the specefied trailing amount....?

As for the take profit and stop limit order, I am still struggling with myself, I would like to have the SELL take profit order automatically priced at say 20% above the fill order price, and stop limit SELL order priced 5% below the fill order price,?

If anyone can show the full code for a bracket order as described above, I would be super super grateful to.
Thank you
Adam