Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit on Session End Close As Entry

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

    Exit on Session End Close As Entry

    Hello,

    I found today the strange NT8 behavior:

    Click image for larger version

Name:	NVIDIA_Share_CcOMuHmRnY.png
Views:	458
Size:	243.3 KB
ID:	1167185

    For some reason it executed automatically an entry order in place of a Exit on session close.
    For some reason it executed automatically 4 contracts.
    It does not make sense for it to execute an Entry instead of an exit on Exit on session Close.

    I used an unmanaged automated exit strategy on Manual Long only entry, so it does not make sense it executed a long entry as Exit (if any order was still active it would have been a long order necessitating a short exit order).
    And even if there was still an active 4 lots short order, the Exit on session Close should have closed the order, but for some reason it entered an order instead that was anew and active today.
    I have no automated entry script so I don't understand why it did execute this order.

    How can I find out why it did execute this Entry order instead of exit order?
    How can I find out why it executed a Long order instead of a Short?
    How can I check the orders of yesterday prior to this order (I only see today's orders on the Executions tab)?

    How can I prevent it from executing this random order again?

    Attached Files

    #2
    Hello Cormick,

    Thanks for your post.

    To analyze how a strategy is placing trades you could use the Playback connection to reproduce the behavior of the script. Running the strategy on the Playback connection would allow you to mimic the strategy running realtime. You could test a specified date range to see how the strategy performs during that time.

    Playback: https://ninjatrader.com/support/help...connection.htm

    It could be possible that the strategy was started out of sync.

    When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

    Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will wait until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

    Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

    Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)

    Adopt Account Position would be used if you want the strategy to inherit the Account Position upon enablement. This requires additional programming.

    If you do not want the strategy to calculate a position from processing historical data. Simple add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

    Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm
    Start Behaviors — https://ninjatrader.com/support/help..._positions.htm

    Let us know if we may assist 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
      Hi Brandon,

      Thanks for the answer and detailed pointers.

      Here's my current State.StateDefaults

      StartBehavior = StartBehavior.WaitUntilFlat;
      RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      IsInstantiatedOnEachOptimizationIteration = true;
      IsAdoptAccountPositionAware = true;
      StartBehavior = StartBehavior.AdoptAccountPosition;
      IsUnmanaged = true;

      Do you mean to change it to Immediately submit, synchronize account

      My use will be:

      Manual entry on Chart trader, automated exit from the strategy.

      Is it correct to use Immediately submit, synchronize account ?

      If yes, what's the syntax for Immediately submit, synchronize account to hardcode in the State.SetDefaults?

      If no, what else would you suggest?


      Additional checked sources:

      https://ninjatrader.com/support/foru...19#post1067219

      https://ninjatrader.com/support/foru...541#post811541

      https://ninjatrader.com/support/help..._positions.htm




      Comment


        #4
        Hello Cormick,

        Thanks for your note.

        I see that you are trying to use a start behavior of 'Wait until flat' and 'AdoptAccountPosition'. You would need to set your strategy to use a single start behavior instead of multiple start behaviors.

        Here's my current State.StateDefaults

        StartBehavior = StartBehavior.WaitUntilFlat;
        RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        IsInstantiatedOnEachOptimizationIteration = true;
        IsAdoptAccountPositionAware = true;
        StartBehavior = StartBehavior.AdoptAccountPosition;
        IsUnmanaged = true;


        If you would like to wait until you are in a flat position before the strategy places trades, you would set the start behavior to 'Wait until flat'. If you want the strategy to inherit the Account Position upon enablement, you would use a start behavior of 'Adopt Account Position'.

        If you use Adopt Account Position, you will need to first set IsAdoptAccountPositionAware to true in your script. Then, you would set a start behavior of 'Adopt Account Position' in the strategy's properties section when enabling the strategy.

        See the help guide pages below for more information.
        IsAdoptAccountPositionAware: https://ninjatrader.com/support/help...itionaware.htm
        Start Behavior: https://ninjatrader.com/support/help...ccountposition

        Let us know if we may assist 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
          Hi Brandon,

          Thanks for the observation.

          I'll use:

          RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          IsInstantiatedOnEachOptimizationIteration = true;
          IsAdoptAccountPositionAware = true;
          StartBehavior = StartBehavior.AdoptAccountPosition;
          IsUnmanaged = true;

          How Can I prevent the sync issue while using StartBehavior = StartBehavior.AdoptAccountPosition; ?


          Is it correct to use Immediately submit, synchronize account ?

          If yes, what's the syntax for Immediately submit, synchronize account to hardcode in the State.SetDefaults?

          StartBehavior = StartBehavior.Immediatelysubmis.Synchronized.Accou nt ?

          If no, what else would you suggest?

          Comment


            #6
            Hello Cormick,

            Thanks for your note.

            It would be up to you regarding which start behavior you would like the strategy to use. You could use either AdoptAccountPosition or Immediately Submit, Synchronize Account for your start behavior but not both at the same time.

            AdoptAccountPosition will sync the strategy position to the account position upon enablement of the strategy. This is typically used if there is already an account position placed and you would like to sync the strategy position to the existing account position.

            Immediately Submit, Synchronize Account will submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.) If you decide to use Immediately Submit, Synchronize Account, you could hardcode this into your strategy using the following code within State.SetDefaults.

            StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;

            Let us know if we may assist 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
              Hi Brandon,

              Thanks for the detailed answer.
              I'll test both later and see which works best for my use.

              Thanks again!
              Be well!

              Comment


                #8
                Hi Brandon,

                I've tested the 2 ways but the position column on the Strategies Tab on the Control Center window still shows after the order exit:

                With
                IsAdoptAccountPositionAware = true;
                StartBehavior = StartBehavior.AdoptAccountPosition;

                After Closure:
                Click image for larger version  Name:	NVIDIA_Share_XpAfswOmSj.png Views:	0 Size:	22.7 KB ID:	1167334


                when I click the ChartTrader close button, it does not close the position.
                Only when I disable the strategy (untick the enable checkbox on the Strategies Tab) does it reset the position column to blank (-).
                Click image for larger version  Name:	vmplayer_P330UdbtD6.png Views:	0 Size:	26.2 KB ID:	1167336



                With
                StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;

                Before entry:

                Click image for larger version  Name:	vmplayer_hwe1OFteM6.png Views:	0 Size:	23.8 KB ID:	1167338

                While live order:

                Click image for larger version  Name:	vmplayer_NodbTMii5t.png Views:	0 Size:	24.1 KB ID:	1167339

                After closure:


                when I click the ChartTrader close button, it does not close the position.
                Only when I disable the strategy (untick the enable checkbox on the Strategies Tab) does it reset the position column to blank (-). I need it not to open new positions when the strategy auto-closes the Account position.
                How can I prevent it from opening the position in the Position column when the strategy auto-closes the Acct.Position in the Acct.position column?
                Can you please test it on your end or retrieve the final working solution from a colleague and share the definitive solution?
                Attached Files
                Last edited by Cormick; 08-11-2021, 03:39 AM.

                Comment


                  #9
                  Hello Cormick,

                  Thanks for your note.

                  Is the manual order being placed before or after the strategy is enabled?

                  Strategies could manage a position on the start-up of a strategy to sync the account position and strategy position. However, once the strategy is enabled it will not be able to recognize any manual orders that are placed. This is because strategies are not able to see any manual orders that are placed once the strategy is enabled. The strategy would not be able to cancel any manually placed orders or change any manually placed orders. Strategies would only be able to see the Account position when it comes to manually placed orders.

                  If you would like to interact with manually placed orders, you would need to use the Account AddOn approach to check for the current Account position and manage orders accordingly using CreateOrder() and Submit().

                  See this help guide documentation for more information about the AddOn approach: https://ninjatrader.com/support/help...ount_class.htm

                  Let us know if we may assist 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


                    #10
                    Hi Brandon,

                    Thank you for your reply.

                    The manual order is placed after the strategy is enabled.
                    The strategy is ready before I enter the manual trades.
                    Then the strategy auto-closes the trades with no manual intervention.
                    I tested it and the strategy works as intended—it auto-closes the manually placed orders.

                    But the issue is that in addition to auto-closing the manually entered orders/Acct.Position it adds the Position new entry order at the same time of autoclosure of the manually entered order (as a sort of reversing mechanism).
                    Clearly I don't need that.

                    Thank you for the Add-On reference.
                    However I don't see the solution I need, or I don't understand it from your reply.

                    All I need is for the strategy not to open new positions when the strategy auto-closes the Account position.

                    How can I achieve that?

                    Why does it add a new Position (what is the cause so we can stop it)?


                    Can you give me the exact steps I simply can replicate for it to achieve what I need?

                    Else can you please get the solution form a colleague?

                    Comment


                      #11
                      Hello Cormick,

                      Thanks for your note.

                      I understand that an order is being placed by the strategy at the same time that the strategy auto-closes the manual order that was placed.

                      This may be due to the strategy's logic and debugging steps should be taken to explore how the unexpected order is being placed by the strategy.

                      To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                      In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                      https://ninjatrader.com/support/foru...121#post791121

                      Note that strategies are not intended to be used with manual orders after the strategy has been enabled. AdoptAccountPosition is a start behavior meaning that it only takes effect when the strategy is enabled to sync the strategy position to an existing account position.

                      In regards to the AddOn approach, to detect manual trades and manage them programmatically, you can use the AddOn Framework and create a NinjaScript that:
                      1. Loops through the Account.Orders collection for the account to find active orders
                      2. Subscribes to Account OrderUpdate events to look for any newly opened orders.
                      3. Tracks these Orders in the script so they can be changed or cancelled with Account.Change or Account.Cancel
                      I have included NinjaTrader 8 documentation on these items below if you wish to investigate further.

                      Order object - https://ninjatrader.com/support/help...n-us/order.htm

                      Account.Orders - https://ninjatrader.com/support/help...rs_account.htm

                      Account.OrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

                      Account.Change - https://ninjatrader.com/support/help...-us/change.htm

                      Account.Cancel - https://ninjatrader.com/support/help...-us/cancel.htm

                      Let us know if we may assist 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


                        #12
                        Hi Brandon,

                        Thank you for your answer.

                        Prints won't help as nowhere in the code there is any condition nor action to execute any Buy Entry order— only exit sell orders to the long entry orders.
                        So the Strategy code cannot be responsible for the Entry Buy order that is added.
                        Prints won't reveal anything since no condition not action are present that could reveal the Buy Entry order.
                        Prints could only reveal if at all new Short Exit Orders, but they are not presents in context.

                        I don't see either how the AddOn approach could help in context.
                        I don't need to control the manually entered orders. The strategy as is already controls them.
                        What I need is only to find the cause of the Buy Entry order added to The Position.
                        Since nowhere in the strategy code there is any action nor condition to place any Buy Entry Order—
                        there is only Conditions and Actions regarding Exiting Long order, the AddOn approach cannot be the needed solution.

                        Thank you for the shared documentation, I checked it, however here also no answer in context.


                        Why does it add a new Position (other than Prints, and AddOn not suited in context suggestions, what is the cause so we can stop it)?


                        Can you give me the exact steps I simply can replicate for it to achieve what I need?

                        Else can you please get the solution form a colleague?


                        Comment


                          #13
                          Hello Cormick,

                          Thanks for your note.

                          Prints are essential for understanding how a strategy is behaving. By adding prints to a strategy, we are able to see the values being used for the strategy's calculations. We can then compare those values to understand how a strategy is working, such as how orders are being placed or closed.

                          If you are not familiar with prints it is recommended to learn this tool first, as this is the main part of understanding the behavior of scripts.

                          "I don't need to control the manually entered orders. The strategy as is already controls them."

                          This is incorrect. As previously stated, a NinjaScript strategy cannot manage orders that are manually placed. A strategy can only manage orders that were placed by the strategy.

                          "What I need is only to find the cause of the Buy Entry order added to The Position"

                          Is this referring to an order that is submitted by the strategy?

                          Are you referring to a strategy position or an account position?

                          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


                            #14
                            Hi Brandon,

                            Thanks for the reply.

                            Thanks for the Prints suggestion.
                            I've already used Prints before.
                            But since no condition nor action nor variable is present in the strategy script for Buy Entry orders, I rule that suggestion out.

                            I begin to see why you did not understand. I should have explicited this before.
                            I use SubmitOrderUnmanaged() method in my strategy. It is the only way to 'detect' manually entered orders.
                            That's why the strategy can and does control/manages (unmanagedwise) the manually entered orders.
                            So the strategy isn't bound by the restrictions of the managed approach that indeed restrict the managed orders strategies to automated only orders.

                            Yes and no or maybe/maybe not for the question: Is this referring to an order that is submitted by the strategy?

                            Yes as the strategy seems to be responsible for auto-submitting the Buy Entry order added to The Position.
                            No as nothing in the strategy code formulates anywhere any Buy Entry order added to The Position.

                            Maybe/Maybe not as I suspect Ninjatrader as acting in the background with the Unmanaged approach and it being responsible for the addition of the Buy Entry order not needed.

                            For you question:
                            Are you referring to a strategy position or an account position?

                            In post #8 above, the 2 After Closure screenshots (2nd is on Imgur as the limit upload was reached):
                            Hello, I found today the strange NT8 behavior: NVIDIA_Share_CcOMuHmRnY.png For some reason it executed automatically an entry order in place of a Exit on session close. For some reason it executed automatically 4 contracts. It does not make sense for it to execute an Entry instead of an exit on Exit on session Close. I used


                            There is a Position 1 lot added after closure. That is the issue.

                            (I must have miss placed the screenshots above, they are interchangeable)

                            1S for

                            StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;

                            1 L for

                            IsAdoptAccountPositionAware = true;
                            StartBehavior = StartBehavior.AdoptAccountPosition;


                            I checked on the Executions tab on the Control Center Window, and only the correct orders appear (on added Buy Entry Order on the Execution Tab, it only appears on the Strategy Tab).

                            Do you know if the added order on the Strategy tab is executed in real trading?
                            I mean if only the Execution Tab reflects the real behavior and the Strategy Tab is only an internal 'accounting thing' in Ninjatrader I can accommodate that.
                            The important thing is for the added But Entry order not to be added in real trading environment.





                            Comment


                              #15
                              Hello Cormick,

                              Thanks for your note.

                              I use SubmitOrderUnmanaged() method in my strategy. It is the only way to 'detect' manually entered orders.

                              SubmitOrderUnmanaged() does not detect manually entered orders. This method is used to generate an unmanaged approach order. This may be why you are seeing the strategy submit an order that you are not expecting to see submitted. See the help guide below.

                              SubmitOrderUnmanaged: https://ninjatrader.com/support/help...runmanaged.htm

                              To detect manually entered orders, you would have to use the AddOn approach mentioned in post #11, seen below, to track the manually placed orders in your script. Once you track those orders, you could use Account.Cancel to cancel the orders.
                              1. Loops through the Account.Orders collection for the account to find active orders
                              2. Subscribes to Account OrderUpdate events to look for any newly opened orders.
                              3. Tracks these Orders in the script so they can be changed or cancelled with Account.Change or Account.Cancel
                              In post #8 above, the 2 After Closure screenshots (2nd is on Imgur as the limit upload was reached):

                              I see that the position being referred to is a strategy position. This means that some logic in your strategy is causing the order to be placed. Prints would need to be added to the strategy to see how the strategy is calculating values and placing orders with SubmitOrderUnmanaged().

                              The start behavior will only synchronize the account and strategy positions at the start of a strategy. If you manually place orders after the strategy is enabled, the strategy will be out of sync since strategies cannot see manual orders placed.

                              Let us know if we may assist 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

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              85 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              47 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              29 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              32 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              67 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X