Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unexpected behavior with entry orders

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

    Unexpected behavior with entry orders

    Hi there. I've been working on a strategy that enters both long and short. For months during development the code was behaving as expected, suddenly however when running backtests the strategy will enter long and then enter short and hold both long and short positions open until explicitly closed later on. As far as I can tell I am and have always been using the managed approach for order entries.

    So my entry instruction looks like this: EnterLong(tradedInstrument,1, @orderTag); and the short entry would be the same except with EnterShort. But both the running output and the log file are showing both long and short positions held concurrently and I'm confused as to why this might be happening.

    Here's a snapshot of the log. Notice entry and exit times and the order in which the trade numbers appear on the left. Order 1 opened Long @ 08:35:35 and closed at 15:59:46. However in between that order 2 opened short at 08:46:21 and closed @ 10:11:53.



    Click image for larger version  Name:	Screenshot 2023-08-08 at 12.23.27 PM.png Views:	0 Size:	181.4 KB ID:	1263651


    And here's output from instructing the program to TraceOrders which adds to the confusion. It appears to keep opening orders both long and short simultaneously then at some point it tries to close orders 7 and 1 and claims they don't exist and then winds up closing them both on a session close on the 4th along with other orders ...so in other words they did exist?


    1/3/2022 8:35:35 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 8:35:35 AM: BarsInProgress=2 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-1' FromEntrySignal=''
    1/3/2022 8:46:21 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 8:46:21 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-2' FromEntrySignal=''
    1/3/2022 10:11:53 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 10:11:53 AM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-2-close' FromEntrySignal='S-REG3-2'
    1/3/2022 5:02:50 PM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 5:02:50 PM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-3' FromEntrySignal=''
    1/3/2022 7:01:00 PM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 7:01:00 PM: BarsInProgress=2 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-4' FromEntrySignal=''
    1/3/2022 7:06:30 PM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 7:06:30 PM: BarsInProgress=2 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-4-close' FromEntrySignal='L-REG3-4'
    1/3/2022 7:06:58 PM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 7:06:58 PM: BarsInProgress=2 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-5' FromEntrySignal=''
    1/3/2022 7:16:43 PM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/3/2022 7:16:43 PM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-3-close' FromEntrySignal='S-REG3-3'
    1/3/2022 7:16:43 PM Strategy 'A1Current/-1': Ignored SubmitOrderManaged() method at 1/3/2022 7:16:43 PM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-3-close' FromEntrySignal='S-REG3-3' Reason='This was an exit order but no position exists to exit'
    1/4/2022 4:00:45 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/4/2022 4:00:45 AM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-6-close' FromEntrySignal='S-REG3-6'
    1/4/2022 6:04:10 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/4/2022 6:04:10 AM: BarsInProgress=2 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-7' FromEntrySignal=''
    1/4/2022 6:50:36 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/4/2022 6:50:36 AM: BarsInProgress=2 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-8' FromEntrySignal=
    1/4/2022 7:21:25 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/4/2022 7:21:25 AM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-7-close' FromEntrySignal='S-REG3-7'
    1/4/2022 7:21:25 AM Strategy 'A1Current/-1': Ignored SubmitOrderManaged() method at 1/4/2022 7:21:25 AM: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S-REG3-7-close' FromEntrySignal='S-REG3-7' Reason='This was an exit order but no position exists to exit'
    1/4/2022 10:02:54 AM Strategy 'A1Current/-1': Entered internal SubmitOrderManaged() method at 1/4/2022 10:02:54 AM: BarsInProgress=2 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-1-close' FromEntrySignal='L-REG3-1'
    1/4/2022 10:02:54 AM Strategy 'A1Current/-1': Ignored SubmitOrderManaged() method at 1/4/2022 10:02:54 AM: BarsInProgress=2 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L-REG3-1-close' FromEntrySignal='L-REG3-1' Reason='SignalName does not have a matching FromEntrySignal to exit'

    01/04/2022 03:59:46 PM - Closing L-REG3-1 on SESS condition at 63.15

    Net: 185 Cumulative Profit: -59

    01/04/2022 03:59:46 PM - Closing S-REG3-3 on SESS condition at 63.15

    Net: -127 Cumulative Profit: -59

    01/04/2022 03:59:46 PM - Closing L-REG3-5 on SESS condition at 63.15

    Net: 108 Cumulative Profit: -59

    01/04/2022 03:59:46 PM - Closing S-REG3-7 on SESS condition at 63.15

    Net: -96 Cumulative Profit: -59

    01/04/2022 03:59:46 PM - Closing L-REG3-8 on SESS condition at 63.15

    Net: 85 Cumulative Profit: -59



    Last edited by markdshark; 08-08-2023, 01:11 PM.

    #2
    Hello markdshark,

    Thanks for your post.

    When using the Managed Approach for order handling in a NinjaScript strategy, if you place a long entry order with a quantity of 1 on one account/instrument with EnterLong() and then call EnterShort() with a quantity of 1 on the same account and instrument, you will see 2 executions. One to close the prior long position and the other to get you into the desired 1 contract short position.

    From the Managed Approach help guide: "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

    So I may accurately assist, please answer all the questions below.
    • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.0.?.?)​
    • Are the EnterLong() and EnterShort() orders being submitted on the exact same account and same instrument?
    • What instrument are the orders being submitted on?
    • Are you able to reproduce the behavior using the Playback connection?
    If possible, please send me a reduced exported test strategy that demonstrates the behavior and the exact steps and settings you are using to reproduce the behavior so I may try to reproduce it on my end. Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

    To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.

    To export a strategy, go to Tools > Export > NinjaScript AddOn.​

    ​I look forward to assisting further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello markdshark,

      When using the Managed Approach for order handling in a NinjaScript strategy, if you place a long entry order with a quantity of 1 on one account/instrument with EnterLong() and then call EnterShort() with a quantity of 1 on the same account and instrument, you will see 2 executions. One to close the prior long position and the other to get you into the desired 1 contract short position.
      Yes that is VERY MUCH what I would like to see happen. Unfortunately... that does not appear to be happening. Both long and short orders are being held at the same time.


      Originally posted by NinjaTrader_BrandonH View Post
      So I may accurately assist, please answer all the questions below.
      • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.0.?.?)​
      • Are the EnterLong() and EnterShort() orders being submitted on the exact same account and same instrument?
      • What instrument are the orders being submitted on?
      • Are you able to reproduce the behavior using the Playback connection?
      If possible, please send me a reduced exported test strategy that demonstrates the behavior and the exact steps and settings you are using to reproduce the behavior so I may try to reproduce it on my end. Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.
      - 8.1.1.7 64-bit
      - Yes they are. You can also see this from the traceOrders log showing they are both on BarsArray2 which is a 1 tick data series for MCL 09-23
      ​- MCL 09-23
      - trying replay now
      - I have a somewhat sanitized file available. Where do I send it?

      Mark
      Last edited by markdshark; 08-08-2023, 01:56 PM.

      Comment


        #4
        Hello markdshark,

        Thanks for your notes.

        So that I may investigate this matter further, please send me a reduced exported test strategy that demonstrates the behavior and the exact steps and settings you are using to reproduce the behavior.

        Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue.

        I will use the test strategy, steps, and settings you provide to try to reproduce the behavior on my end and look further into what might be occurring.

        I look forward to assisting further
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Originally posted by NinjaTrader_BrandonH View Post
          Hello markdshark,

          So that I may investigate this matter further, please send me a reduced exported test strategy that demonstrates the behavior and the exact steps and settings you are using to reproduce the behavior.
          Yes, where do I send it?

          Comment


            #6
            Hello Mark,

            Thanks for your notes.

            You could send the reduced exported test strategy, steps you use, and settings you use to support[at]ninjatrader[dot]com so we may look into this matter further. In the subject of the email include ATTN: BrandonH and in the body of the email include a link to this forum thread.

            Note that the reduced strategy cannot use third-party addons that we do not have access to and should only contain the minimum amount of code used to reproduce the behavior you are reporting.

            I look forward to assisting further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Just FYI it doesn't happen in market replay using tick data. Only in Backtesting using tick replay.

              Ninjascript exported addon file and output log mailed to support attn you. Thanks!!

              Mark
              Last edited by markdshark; 08-08-2023, 03:55 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              44 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              124 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              65 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X