08 February 2021

Scale Order with Adjustable Stop in TWS API - Interactive Brokers Part 6

Scenario:

I am using Python TWS API. For a Stock, I want to place a Limit Order with 2 Profit Takers and 1 Stop Loss. If Profit Taker 1 is hit, then the stop loss should decrease by the commensurate amount (so after Profit Taker 1 is hit, the stop loss quantity is now updated to be equal to Profit Taker 2 quantity). How to do this by packing this entire set of operation's into 1 order?

Solution:

Lets first experiment this scenario with the TWS GUI. For this we will use the Classic TWS. Let us make  the following assumptions.

  • Stock Symbol

    SBIN

    Total Quantity

    100

    Limit Price

    100

    Stop Loss

    95

    Price of Profit Taker 1

    105

    Quantity for Profit Taker 1

    40

    Price of Profit Taker 2

    110

    Quantity for Profit Taker 2

    60


  • STEP 1: Right Click on the Stock in the TWS Classic Watchlist. Click on Order Ticket. Choose the action as BUY,  Quantity as 100 , Order Type as LMT and Limit Price as 100. Click on Accept. An orderline is now created for the Parent Order. 


  • STEP 2: Now right click on the primary orderline, select Attach --> Limit  [under Target Order(Profit-Taker)] . This will create another orderline. Right click on that line and click Modify --> Order Ticket. Under the basic tab, select the order type as LMT and enter the Profit Taker 1. As per our example it is 105. Then click on the Scale tab on top of the order ticket. Put the initial component size as 40 and the subsequent component size as 60. Put the price increment as 5 (i.e; difference of primary target order and second target order as per the example)




  • STEP 3: Now right click on the primary orderline once again. Select Attach --> Adjustable Stop. Another orderline will be created for a stop loss order. Right click on the order line, select Modify--> Order ticket. Under the basic tab, select the Order Type as STP. On the right hand side of the screen, under adjust stop / trailing stop, select the order type (adjust to order type) as "STP". Enter the trigger price as 105 which is Profit Taker 1. In the adjusted stop price, enter 100. This will change the stop loss from 95 to 100 once the first target of 105 (Profit Taker 1) is touched. Click Accept button at the bottom of the window.

The Python Code in TWS API:

The parent order is the same as in any standard bracket order.

        #This will be our main or "parent" order
        parent = Order()
        parent.orderId = parentOrderId
        parent.action = action
        parent.orderType = "LMT"
        parent.totalQuantity = quantity
        parent.lmtPrice = limitPrice
        #The parent and children orders will need this attribute set to False to prevent accidental executions.
        #The LAST CHILD will have it set to True, 
        parent.transmit = False

Next comes the takeprofit order. Pay attention to the scaleInitLevelSize, scaleSubsLevelSize  and scalePriceIncrement. 

        takeProfit = Order()
        takeProfit.orderId = parent.orderId + 1
        takeProfit.action = "SELL" if action == "BUY" else "BUY"
        takeProfit.orderType = "LMT"
        takeProfit.totalQuantity = quantity
        takeProfit.lmtPrice = takeProfitLimitPrice1
        takeProfit.scaleInitLevelSize = 40 # Quantity for profit target 1
        takeProfit.scaleSubsLevelSize = 60 # Quantity for profit target 2
        takeProfit.scalePriceIncrement = (takeProfitLimitPrice2 - takeProfitLimitPrice1)
        takeProfit.parentId = parentOrderId
        takeProfit.transmit = False

Lastly in the stoploss order component , pay attention to the triggerPrice (which is set to the Profit Taker 1) and adjustedStopPrice. The latter can remain as the original stopLossPrice itself or be different.

        stopLoss = Order()
        stopLoss.orderId = parent.orderId + 2
        stopLoss.action = "SELL" if action == "BUY" else "BUY"
        stopLoss.orderType = "STP"
        #Stop trigger price
        stopLoss.auxPrice = stopLossPrice
        stopLoss.totalQuantity = quantity
        stopLoss.triggerPrice = triggerPrice
        stopLoss.adjustedStopPrice = adjustedStopPrice
        stopLoss.parentId = parentOrderId
        #In this case, the low side order will be the last child being sent. Therefore, it needs to set this attribute to True 
        #to activate all its predecessors
        stopLoss.transmit = True

No comments:

Post a Comment