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


Sync account and strategy positions

  • Filter
  • Time
  • Show
Clear All
new posts

    Sync account and strategy positions

    How can I sync account and strategy positions without submitting any new orders?

    Example: Strategy enters long. Position closed manually outside the strategy. I want the strategy to become flat.

    Hello frslvr,

    Thanks for your post.

    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.)

    Note that strategies are only aware of trades made by that strategy in particular. Strategies are not aware of orders that are manually opened/closed.

    If you do not want the strategy to calculate a position from processing historical data, 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.

    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 canceled 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.

    See the help guide documentation below for more information.
    Strategy vs. Account Position —
    Start Behaviors —

    Let us know if we may assist further.
    Last edited by NinjaTrader_BrandonH; 12-06-2021, 09:53 AM.
    Brandon H.NinjaTrader Customer Service


      Brandon, thank you for your reply.

      My question is not related to the Historical state nor related to starting of the strategy.

      I'm asking about the real-time working strategy. If I close the account position manually, how can I make the strategy flat?


        Hello frslvr,

        Thanks for your note.

        As stated in my post #2, strategies are not able to read orders that are opened/closed manually. They are only aware of trades made by that strategy in particular and are intended to run without manual intervention.

        When the strategy places an order, say 1 buy order, the strategy position and account position would be 1 Long. If you manually close this position on the account, the strategy position would be 1 Long and the account position would be Flat since the strategy is unable to read manual orders. This would cause your strategy position and account position to be out of sync.

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service


          Let's abstract from the question about strategy knowing about the manual events.

          Is there a way for the strategy to flat the strategy position without touching the account? I just want to reset the strategy position to flat.


            Hello frslvr,

            Thanks for your post.

            There is no way to manually close an order that was placed by a strategy and force the strategy position to be Flat. Strategies will read the positions that the strategy itself places. It will not see orders that are manually placed or closed.

            You could consider detecting manual trades and managing them programmatically with a button, you can use the AddOn approach 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 -

            Account.Orders -

            Account.OrderUpdate -

            Account.Change -

            Account.Cancel -


            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by AlessioCianini12, Today, 01:55 PM
            0 responses
            Last Post AlessioCianini12  
            Started by rajneesh.sehgal, Today, 12:06 PM
            1 response
            Last Post NinjaTrader_ChristopherJ  
            Started by mo_fi, 01-21-2021, 10:08 PM
            22 responses
            Last Post Wessel
            by Wessel
            Started by ezrollin, 01-12-2022, 03:43 PM
            2 responses
            Last Post iq200
            by iq200
            Started by Red_70, Today, 11:07 AM
            3 responses
            Last Post Red_70
            by Red_70