Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Syncing Stop Loss orders quantity and current position

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Syncing Stop Loss orders quantity and current position

    I am working on custom addon which does add Stop loss and take profit orders for the filled positions...

    Depending on the price action the stoploss order is moved to breakeven.

    Sometimes the stop order quantities are out of sync with the existing positions , and these stop orders are filled, its opening a positions in opposite directions...

    This happens when the Stoploss orders are updated and TP1 are filled simultaneously, below is the summary of the situation..

    Please advice on handling such situations...

    2024-07-25 07:47:43:100 --> SLTP4=3 Accepted Stop price=19173.5 Quantity=3 -- all good till this point

    2024-07-25 07:47:58:297 --> Stoploss order price update change triggered (New state='Change submitted')

    2024-07-25 07:47:58:315 --> TP2 Filled Fill price=19151.75 ---Remaining positions Short 2

    2024-07-25 07:47:58:405 --> SLBE Accepted Stop price=19158.75 Quantity=3 (New state='Accepted')

    2024-07-25 07:47:59:197 --> TP3 Filled (Name='TP3' New state='Filled' Fill price=19146.75) -- Remaining Positions Sort 1

    2024-07-25 07:47:59:303 --> SLTP1 Stop price=19158.75 Quantity=2 (New state='Accepted' ) while there is only one position

    2024-07-25 07:50:03:960 --> SLTP1 Filled 2 Longs while there is only 1 Short position

    One additional position filled in opposite direction which has been closed manually​


    Below is complete order log when this situation occurred,

    Code:
    2024-07-25 07:47:43:100|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP4' New state='Accepted' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19173.5 Quantity=3 Type='Stop Market' Time in force=DAY Oco='TP4fc7ebe2bcff74f36b68d86be2e25a4d7' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:47:43:106|1|64|Instrument='MNQ SEP24' Account='Sim101' Average price=19159 Quantity=3 Market position=Short Operation=Update
    
    
    POSITION-UPDATE  3 SELL SLTP3= + TP2 + Tp3   Stop Orders in sync with positions...
    
    2024-07-25 07:47:58:297|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP4' New state='Change submitted' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=3 Type='Stop Market' Time in force=DAY Oco='TP4fc7ebe2bcff74f36b68d86be2e25a4d7' Filled=0 Fill price=0 Error='No error' Native error=''
    
    StopLoss updated to Breakeven price triggered
    
    2024-07-25 07:47:58:315|1|32|Order='715035565dd74708ac1d94bacd9a8607/Sim101' Name='TP2' New state='Filled' Instrument='MNQ SEP24' Action='Buy' Limit price=19151.75 Stop price=19151.75 Quantity=1 Type='Limit' Time in force=DAY Oco='TP233dbcb5a96ea424593cea83c68a18c9dTP' Filled=1 Fill price=19151.75 Error='No error' Native error=''
    FILLED
    2024-07-25 07:47:58:316|1|8|Execution='893405840b704590ba73ed207afd9444' Instrument='MNQ SEP24' Account='Sim101' Exchange=Default Price=19151.75 Quantity=1 Market position=Long Operation=Operation_Add Order='715035565dd74708ac1d94bacd9a8607' Time='07/25/2024 07:47:58'
    2024-07-25 07:47:58:405|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP4' New state='Accepted' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=3 Type='Stop Market' Time in force=DAY Oco='TP4fc7ebe2bcff74f36b68d86be2e25a4d7' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:47:58:424|1|64|Instrument='MNQ SEP24' Account='Sim101' Average price=19158.625 Quantity=2 Market position=Short Operation=Update
    POSITION-UPDATE 2 SELL SLTP3=3  + Tp3
    
    2024-07-25 07:47:59:197|1|32|Order='f8535b2a9f49469e9ef63b0537e81086/Sim101' Name='TP3' New state='Filled' Instrument='MNQ SEP24' Action='Buy' Limit price=19146.75 Stop price=19146.75 Quantity=1 Type='Limit' Time in force=DAY Oco='TP3e4968e80051f4328b50cd76820f1d185TP' Filled=1 Fill price=19146.75 Error='No error' Native error=''
    FILLED
    2024-07-25 07:47:59:198|1|8|Execution='a99774843dbb4f0fb6692e3fa7ad5b1d' Instrument='MNQ SEP24' Account='Sim101' Exchange=Default Price=19146.75 Quantity=1 Market position=Long Operation=Operation_Add Order='f8535b2a9f49469e9ef63b0537e81086' Time='07/25/2024 07:47:59'
    2024-07-25 07:47:59:199|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP1' New state='Change submitted' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=2 Type='Stop Market' Time in force=DAY Oco='TP1f8535b2a9f49469e9ef63b0537e81086' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:47:59:303|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP1' New state='Accepted' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=2 Type='Stop Market' Time in force=DAY Oco='TP1f8535b2a9f49469e9ef63b0537e81086' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:47:59:306|1|64|Instrument='MNQ SEP24' Account='Sim101' Average price=19158.5 Quantity=1 Market position=Short Operation=Update
    POSITION-UPDATE
    2024-07-25 07:50:03:946|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP1' New state='Working' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=2 Type='Stop Market' Time in force=DAY Oco='TP1f8535b2a9f49469e9ef63b0537e81086' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:50:03:960|1|32|Order='60d67be2bd4946e4b73638f3e3a68844/Sim101' Name='SLTP1' New state='Filled' Instrument='MNQ SEP24' Action='Buy to cover' Limit price=0 Stop price=19158.75 Quantity=2 Type='Stop Market' Time in force=DAY Oco='TP1f8535b2a9f49469e9ef63b0537e81086' Filled=2 Fill price=19158.75 Error='No error' Native error=''
    FILLED
    2024-07-25 07:50:03:960|1|8|Execution='04d7871c55e24e96a1d45d427763db7c' Instrument='MNQ SEP24' Account='Sim101' Exchange=Default Price=19158.75 Quantity=2 Market position=Long Operation=Operation_Add Order='60d67be2bd4946e4b73638f3e3a68844' Time='07/25/2024 07:50:03'
    2024-07-25 07:50:04:066|1|64|Instrument='MNQ SEP24' Account='Sim101' Average price=19158.75 Quantity=1 Market position=Long Operation=Update
    POSITION-UPDATE
    2024-07-25 07:52:24:592|1|32|Order='5a54cb6c253b4642a20dfcb080b9ed69/Sim101' Name='Close' New state='Submitted' Instrument='MNQ SEP24' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:52:24:704|1|32|Order='5a54cb6c253b4642a20dfcb080b9ed69/Sim101' Name='Close' New state='Accepted' Instrument='MNQ SEP24' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:52:24:704|1|32|Order='5a54cb6c253b4642a20dfcb080b9ed69/Sim101' Name='Close' New state='Working' Instrument='MNQ SEP24' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2024-07-25 07:52:24:717|1|32|Order='5a54cb6c253b4642a20dfcb080b9ed69/Sim101' Name='Close' New state='Filled' Instrument='MNQ SEP24' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=19136 Error='No error' Native error=''
    FILLED
    2024-07-25 07:52:24:718|1|8|Execution='30da46415734419eb50132da11763532' Instrument='MNQ SEP24' Account='Sim101' Exchange=Default Price=19136 Quantity=1 Market position=Short Operation=Operation_Add Order='5a54cb6c253b4642a20dfcb080b9ed69' Time='07/25/2024 07:52:24'
    2024-07-25 07:52:24:830|1|64|Instrument='MNQ SEP24' Account='Sim101' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    FLAT
    POSITION-UPDATE
    
    
    2024-07-25 07:47:43:100 --> SLTP4=3 Accepted Stop price=19173.5 Quantity=3 -- all good till this point
    2024-07-25 07:47:58:297 --> SLBE Triggered  
    2024-07-25 07:47:58:315 --> TP2 Filled Fill price=19151.75 ---Remaning positions Short 2
    2024-07-25 07:47:58:405 --> SLBE Accepted  Stop price=19158.75 Quantity=3
    2024-07-25 07:47:59:197 --> TP3 Filled Fill price=19146.75-- Remaining Positions Sort 1
    2024-07-25 07:47:59:303 --> SLTP1 Stop price=19158.75 Quantity=2
    2024-07-25 07:50:03:960 --> SLTP1 Filled 2 Longs while there is only 1 Short position
    
    One additional position filled in opposite direction which has been closed manually
    ​

    #2
    Hello Kdevineni,

    Based on the information provided this sounds like what is considered an in flight execution, that is where you complete two contradictory actions which results in an unwanted position. With the example you provided the only way to avoid that would be to make sure that the change order is not happening at the same price which the target will fill to avoid submitting a change at the same time. If this is for a breakeven you may need to increase the profit targets price so it is not near the positions average price when the breakeven move is to happen.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    559 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    546 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X