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

Unable to get all the positions in NinjaTrader Script

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

    Unable to get all the positions in NinjaTrader Script

    Hello,

    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:	90
Size:	54.4 KB
ID:	1238104
    Click image for larger version

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

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

    #2

    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

    Comment


      #3
      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
      contracts.

      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

      Comment


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

        Why?
        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,
        Direction
        Quantity
        Instrument
        Account

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

        Comment


          #5
          Thanks David for the insights and education.

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

          Appreciate the quick support !!!

          Comment


            #6
            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..

            Comment


              #7
              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

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Rapine Heihei, 04-23-2024, 07:51 PM
              2 responses
              30 views
              0 likes
              Last Post Max238
              by Max238
               
              Started by Shansen, 08-30-2019, 10:18 PM
              24 responses
              943 views
              0 likes
              Last Post spwizard  
              Started by Max238, Today, 01:28 AM
              0 responses
              9 views
              0 likes
              Last Post Max238
              by Max238
               
              Started by rocketman7, Today, 01:00 AM
              0 responses
              4 views
              0 likes
              Last Post rocketman7  
              Started by wzgy0920, 04-20-2024, 06:09 PM
              2 responses
              28 views
              0 likes
              Last Post wzgy0920  
              Working...
              X