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


Unable to get all the positions in NinjaTrader Script

  • Filter
  • Time
  • Show
Clear All
new posts

    Unable to get all the positions in NinjaTrader Script


    I am trying to access all the open positions on my simulator account but unable to get the correct count. I need to get 4 but my script output shows only 3. Attaching screenshots and my script

    Click image for larger version

Name:	image.png
Views:	94
Size:	54.4 KB
ID:	1238104
    Click image for larger version

Name:	image.png
Views:	87
Size:	70.0 KB
ID:	1238105
    Click image for larger version

Name:	image.png
Views:	72
Size:	332.1 KB
ID:	1238106


    Hello, thanks for writing in. The PositionAccount object will return the total account position quantity:

    If it is still returning an incorrect value try resetting the Sim101 account:

    Go to the Accounts tab on the Control Center>Right click the Sim101 account>Edit Account>Click Reset. This will set the Sim101 back to "Factory settings".
    Chris L.NinjaTrader Customer Service


      You may be confused.

      I don't think '3 L' or '4 L' mean 3 or 4 total positions.

      It means you are 'Long 3 contracts' and 'Long 4 contracts'.

      Saying '3 L' is just a shorthand way to state a position.

      A 'position' mainly consists of two things, a direction (Long/Short)
      and a quantity, and are managed uniquely per instrument and
      per account.

      If there is no position in a specific instrument for a specific
      account, that condition is called Flat, and obviously quantity
      is zero when the position is Flat.

      Each running strategy knows about it's own position, but no
      others -- so the 'strategy position' is unique to each strategy.

      Your overall account position takes into consideration everything
      happening in your account.

      If Strat A is '3 L' in ES and Strat B is '4 L' in ES -- then your
      account position at your broker is actually '7 L' in ES.

      Your account position is cumulative -- there is only ever one
      'account position' per instrument per account.

      As soon as Strat A exits its '3 L' position, your overall account
      position is adjusted to '4 L', which now happens to exactly match
      the Strat B position, which is exactly what you should expect.

      (Why? Because Strat A exits its '3 L' position by selling 3
      contracts, right? Well, that 'sell' order affects the account
      position, so if it was '7 L' before the 'sell 3' order, then after
      the 'sell 3' is filled, the account position is '4 L'. See that?)

      But suddenly,
      if Strat A goes 'short 3 contracts' (aka, '3 S') while Strat B is
      still '4 L' your account position becomes '1 L' -- because the
      broker sees the request to sell 3 contracts from Strat A and
      reduces the '4 L' account position to '1 L' -- because it sold 3

      It can become wildly confusing when running multiple strategies
      on the same account on the same instrument -- that's why it's
      highly discouraged.

      Make sense?

      Last edited by bltdavid; 03-06-2023, 11:06 AM. Reason: explain Flat position


        The corollary to this is that Account.Positions.Count usually flucuates
        between 0 and 1.

        Because most people, even if they trade multiple instruments, only
        ever take positions (aka, enter a trade) on 1 instrument at time.

        Thus '4 L' does not mean 4 positions, it means 1 position and the
        position direction is 'Long' and the position quantity is '4'.

        Btw, it is the same nomenclature across all of trading.

        In stocks, '1000 L' means you're Long 1000 shares -- obviously,
        the instrument you're long in is critical to know.

        That's why I said a 'position' always has these 4 things,

        and is managed uniquely on a per instrument per account basis.


          Thanks David for the insights and education.

          NinjaTrader_ChrisL - Your post helped me fix the exact issue I was facing.

          Appreciate the quick support !!!


            NinjaTrader_ChrisL - Hi Chris, I noticed that the PositionAccount does not work in Backtest mode but works in the live or simulation account in real time. Is there anyway to overcome this as it prevents me from backtesting my strategy..


              Hi, The only way to use an account is to make your own virtual account within the strategy that you manage through code. The Account information is not avaiable thorugh a backtest so you would need to set up an integer variable representing PositionAccount and manually update its values.
              Chris L.NinjaTrader Customer Service


              Latest Posts


              Topics Statistics Last Post
              Started by athexinehowley, Today, 02:39 AM
              0 responses
              Last Post athexinehowley  
              Started by dtaylor, Today, 02:24 AM
              0 responses
              Last Post dtaylor
              by dtaylor
              Started by RosaRichardow, Today, 12:21 AM
              1 response
              Last Post marcus2300  
              Started by ETFVoyageur, Today, 12:45 AM
              1 response
              Last Post ETFVoyageur  
              Started by TradeSaber, Yesterday, 10:33 PM
              1 response
              Last Post TradeSaber