Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Changing ATM Stop within Strategy on Live account failed.

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

    Changing ATM Stop within Strategy on Live account failed.

    I have been testing an automated strategy that uses an ATM for trade management. As part of the process the Strategy sumits the entry, waits for it to be filled and then modifies the two ATM stops so that they are different for each entry.

    I have done extensive testing on this code on the SIM account which has worked fine. However, the first time I have moved it to a live account the stop movement code is just ignored.

    I know this is my debug but this is what it is meant to look like:


    NQ,30/01/20 07:00:10,Attempting "2x16T,60T,25S,16B-ET,-5B-ES" Entry LONG - Current state Flat
    NQ,30/01/20 07:00:10,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: Requested
    NQ,30/01/20 07:00:10,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: Created
    NQ,30/01/20 07:00:10,The entry order status is: Submitted
    NQ,30/01/20 07:00:10,The entry order status is: Submitted
    NQ,30/01/20 07:00:10,The entry order status is: Submitted
    NQ,30/01/20 07:00:10,The entry order status is: Submitted
    NQ,30/01/20 07:00:10,The entry order status is: Filled
    NQ,30/01/20 07:00:10,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: Filled
    NQ,30/01/20 07:00:10,ATM Strategy: "2x16T,60T,25S,16B-ET,-5B-ES" with stop at 9044.75
    NQ,30/01/20 07:00:10,The entry order average fill price is: 9053.63 -- order requested at 9053.50
    NQ,30/01/20 07:00:10,The entry order filled amount is: 2
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) updating stops for 2 contracts to 9044.75
    NQ,30/01/20 07:00:10,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: AdjustStops
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Initialized
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop1 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop1 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Accepted
    NQ,30/01/20 07:00:10,AtmStrategyChangeStopTarget - looks ok
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop2 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Submitted
    NQ,30/01/20 07:00:10,Stop2 not yet Accepted
    NQ,30/01/20 07:00:10,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (1a5e2b5e257143558764478d8f0b1cd9) attempting update Stop2 to 9044.75
    NQ,30/01/20 07:00:10,Current state is Accepted
    NQ,30/01/20 07:00:10,AtmStrategyChangeStopTarget - looks ok
    NQ,30/01/20 07:00:10,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: Working
    NQ,30/01/20 07:00:25,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES ended for -355.00 loss
    NQ,30/01/20 07:00:25,Strategy "1a5e2b5e257143558764478d8f0b1cd9" --> New Order process state: Flat

    You'll note from above that when adjusting the stops the STATE quickly moves from 'Submitted' to 'Accepted'.

    On the live account the STATE never goes to 'Accepted' and my code simply keeps waiting for it to change:


    NQ,30/01/20 13:54:31,Attempting "2x16T,60T,25S,16B-ET,-5B-ES" Entry SHORT - Current state Flat
    NQ,30/01/20 13:54:31,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: Requested
    NQ,30/01/20 13:54:31,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: Created
    NQ,30/01/20 13:54:31,The entry order status is: Initialized
    NQ,30/01/20 13:54:32,The entry order status is: Submitted
    NQ,30/01/20 13:54:32,The entry order status is: Submitted
    NQ,30/01/20 13:54:32,The entry order status is: Submitted
    NQ,30/01/20 13:54:32,The entry order status is: Submitted
    NQ,30/01/20 13:54:32,The entry order status is: Filled
    NQ,30/01/20 13:54:34,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: Filled
    NQ,30/01/20 13:54:34,ATM Strategy: "2x16T,60T,25S,16B-ET,-5B-ES" with stop at 9063.50
    NQ,30/01/20 13:54:34,The entry order average fill price is: 9053.25 -- order requested at 9053.00
    NQ,30/01/20 13:54:34,The entry order filled amount is: 2
    NQ,30/01/20 13:54:34,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) updating stops for 2 contracts to 9063.50
    NQ,30/01/20 13:54:34,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: AdjustStops
    NQ,30/01/20 13:54:34,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:54:34,Current state is Working
    NQ,30/01/20 13:54:34,Stop1 not yet Accepted
    NQ,30/01/20 13:54:34,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:54:34,Current state is Working
    NQ,30/01/20 13:54:34,Stop1 not yet Accepted
    NQ,30/01/20 13:54:34,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:54:34,Current state is Working
    NQ,30/01/20 13:54:34,Stop1 not yet Accepted



    ************************** LOTS OF LINES REMOVED


    NQ,30/01/20 13:58:55,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:58:55,Current state is Working
    NQ,30/01/20 13:58:55,Stop1 not yet Accepted
    NQ,30/01/20 13:58:55,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:58:55,Current state is Working
    NQ,30/01/20 13:58:55,Stop1 not yet Accepted
    NQ,30/01/20 13:58:55,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES (0c3abf99b3d1404d95d7df6a40495b7e) attempting update Stop1 to 9063.50
    NQ,30/01/20 13:58:55,Current state is Working
    NQ,30/01/20 13:58:55,Stop1 not yet Accepted
    NQ,30/01/20 13:58:55,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: Closing
    NQ,30/01/20 13:58:55,ATM Strategy: 2x16T,60T,25S,16B-ET,-5B-ES ended for -250.00 loss
    NQ,30/01/20 13:58:55,Strategy "0c3abf99b3d1404d95d7df6a40495b7e" --> New Order process state: Flat

    Note the stuck at STATE 'Working'. These states are retrieved using GetAtmStrategyStopTargetOrderStatus() as instructed.

    Am I missing something here? Is the STATE sequence different for SIM and LIVE accounts?

    Also, I've looked at the logs and it seems like the request to change the Stops isn't even submitted. Relevant section:


    2020-01-30 13:54:32:116|1|32|Order='99b63a6bab074db3a5d92e346 8ad3863/J8659' Name='Entry' New state='Submitted' Instrument='NQ 03-20' Action='Sell' Limit price=0 Stop price=0 Quantity=2 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:385|1|32|Order='723458417/J8659' Name='Entry' New state='Accepted' Instrument='NQ 03-20' Action='Sell' Limit price=0 Stop price=0 Quantity=2 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:385|1|32|Order='723458417/J8659' Name='Entry' New state='Working' Instrument='NQ 03-20' Action='Sell' Limit price=0 Stop price=0 Quantity=2 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:457|1|32|Order='723458417/J8659' Name='Entry' New state='Filled' Instrument='NQ 03-20' Action='Sell' Limit price=0 Stop price=0 Quantity=2 Type='Market' Time in force=GTC Oco='' Filled=2 Fill price=9053.25 Error='No error' Native error=''
    2020-01-30 13:54:32:461|1|8|Execution='291544827392' Instrument='NQ 03-20' Account='J8659' Exchange=Globex Price=9053.25 Quantity=2 Market position=Short Operation=Operation_Add Order='723458417' Time='01/30/2020 13:54:32'
    2020-01-30 13:54:32:463|1|64|Instrument='NQ 03-20' Account='J8659' Average price=9053.25 Quantity=2 Market position=Short Operation=Operation_Add
    2020-01-30 13:54:32:465|1|32|Order='ca5dced986df42e9bfbea97af 2569501/J8659' Name='Stop1' New state='Submitted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:467|1|32|Order='fe9463c45df94b5eb6673b5e6 4f5e138/J8659' Name='Target1' New state='Submitted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9049.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:468|1|32|Order='84dae512a5b44effa2e3238f1 b7ce2f6/J8659' Name='Stop2' New state='Submitted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:468|1|32|Order='5dbb93a5154c493f8e0b3748e 6f7920f/J8659' Name='Target2' New state='Submitted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9038.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:758|1|32|Order='723615329/J8659' Name='Stop1' New state='Accepted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:758|1|32|Order='723615329/J8659' Name='Stop1' New state='Working' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:758|1|32|Order='723610284/J8659' Name='Stop2' New state='Accepted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:758|1|32|Order='723610284/J8659' Name='Stop2' New state='Working' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:759|1|32|Order='723507478/J8659' Name='Target1' New state='Accepted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9049.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:759|1|32|Order='723507478/J8659' Name='Target1' New state='Working' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9049.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:759|1|32|Order='723507479/J8659' Name='Target2' New state='Accepted' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9038.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:54:32:759|1|32|Order='723507479/J8659' Name='Target2' New state='Working' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9038.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:58:55:350|1|32|Order='723615329/J8659' Name='Stop1' New state='Filled' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=1 Fill price=9059.5 Error='No error' Native error=''
    2020-01-30 13:58:55:354|1|8|Execution='291549029632' Instrument='NQ 03-20' Account='J8659' Exchange=Globex Price=9059.5 Quantity=1 Market position=Long Operation=Operation_Add Order='723615329' Time='01/30/2020 13:58:55'
    2020-01-30 13:58:55:354|1|32|Order='723610284/J8659' Name='Stop2' New state='Filled' Instrument='NQ 03-20' Action='Buy to cover' Limit price=0 Stop price=9059.5 Quantity=1 Type='Stop Market' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=1 Fill price=9059.5 Error='No error' Native error=''
    2020-01-30 13:58:55:354|1|32|Order='723507478/J8659' Name='Target1' New state='Cancelled' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9049.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='e48d1ce4616047d5a68cad59c70ee752' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:58:55:354|1|32|Order='723507479/J8659' Name='Target2' New state='Cancelled' Instrument='NQ 03-20' Action='Buy to cover' Limit price=9038.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='5c8990eb48184c57b47d3f81351e6e43' Filled=0 Fill price=0 Error='No error' Native error=''
    2020-01-30 13:58:55:354|1|8|Execution='291549030144' Instrument='NQ 03-20' Account='J8659' Exchange=Globex Price=9059.5 Quantity=1 Market position=Long Operation=Operation_Add Order='723610284' Time='01/30/2020 13:58:55'
    2020-01-30 13:58:55:459|1|64|Instrument='NQ 03-20' Account='J8659' Average price=0 Quantity=0 Market position=Short Operation=Remove

    Help please.

    Regards,

    Steve

    P.S. In writing and checking this long post I now believe I can see what is wrong. It would appear that the STATE sequence for Stops is different for SIM and LIVE. I see now that LIVE is returning 'Working' as opposed to 'Accepted' and therefore my code does NOT submit the request to change the Stop. So of course it never happens. Apologies for this long post, but it has helped me understand. I would appreciate you confirming that the STATE sequences are slightly different. Can you clarify what state I should wait for BEFORE submitting the Stop change request.

    Thanks.

    #2
    Hi Steve, thanks for your post.

    The order state will go through the same sequence no matter if the order is live or simulated:

    Submitted
    Accepted
    Working
    Filled

    This can be tested using the OnOrderUpdate method. You should wait for the Working state before modifying an order.

    To get more info on ignored orders you can turn on Trace Orders and observe the NS output window, documentation is linked here:



    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chris ....

      .... but then for the Stops why don't I ever see the 'Filled' state on the debug I showed you (failed example above - and in the Ninjatrader log it doesn't show 'Filled' for a Stop)? It stops at 'Working' for several seconds until the (default) Stop is hit and the order closes. I do see 'Filled' for the main order for sure - that's what I wait for before I start trying to change the Stops.

      In fact - I remember now, when I first wrote the code - I started by issuing the Stop change requests as soon as I detected the main Order 'Filled'. These failed as they didn't seem to have reached a ready State. That's when I added further code to wait for a ready state on the Stops BEFORE issuing the change request on them.

      For whatever reason it appears the Stops get to 'Working' (but no further), but I don't see the 'Accepted' - this is probably a code timing issue as I can see it in the Ninjatrader log.

      I've now modded my code to issue the change as soon as it detects EITHER 'Accepted' or 'Working'. I'll see what happens and report back. I'm pretty sure it will work.

      Cheers,

      Steve

      Comment


        #4
        Hi Steve, thanks for your reply.

        I am getting a Filled state for the "Stop loss" order when the order is acctually filled. This can be confirmed by printing the order out in OnOrderupdate:

        Code:
        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                {
                    if(order.Name == "Stop loss")
                    {
                        Print(order);
                    }
                }
        It is recommended that you wait for either accepted or working as this state transition can vary.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pechtri, 06-22-2023, 02:31 AM
        9 responses
        122 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by frankthearm, 04-18-2024, 09:08 AM
        16 responses
        66 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by habeebft, Today, 01:18 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by benmarkal, Today, 12:52 PM
        2 responses
        18 views
        0 likes
        Last Post benmarkal  
        Started by f.saeidi, Today, 01:38 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X