Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is it possible for a strategy to obtain the number of open positions on an account?

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

    Is it possible for a strategy to obtain the number of open positions on an account?

    Hello,

    Is it possible to have a strategy only make a trade if there are currently zero open positions on an account? Or if the account's buying power is a certain amount? If so how?

    Can this be done via an indicator that the strategy utilizes?

    #2
    Hello burmaz,

    Thanks for your post.

    You could create a condition that checks if PositionAccount.MarketPosition == MarketPosition.Flat to detect if an account is in a Flat position in a strategy.

    The account Positions collection could be used to get the MarketPosition of an account in an indicator.

    Account.Get() could be used to get an AccountItem value for an account, such as the buying power (AccountItem.BuyingPower).

    See the help guide documentation below for more information.

    PositionAccount.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
    Account: https://ninjatrader.com/support/help...ount_class.htm
    Account.Positions: https://ninjatrader.com/support/help...ns_account.htm
    Account.Get(): https://ninjatrader.com/support/helpGuides/nt8/get.htm
    AccountItem: https://ninjatrader.com/support/help...ccountitem.htm
    <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
      Yes, using the account object you can obtain all of the open positions for an account. That would be true in an indicator or add-on as well, not just in a strategy.
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment


        #4
        Originally posted by burmaz View Post
        Hello,

        Is it possible to have a strategy only make a trade if there are currently zero open positions on an account? Or if the account's buying power is a certain amount? If so how?

        Can this be done via an indicator that the strategy utilizes?
        Yes. You could use something like Account Position == Flat.

        Comment


          #5
          Assuming your strategy code is in a section that is about to execute a trade, this will work avoid the execution if the account is not flat. Of course, it will return on the first position found, but that is fine.

          // Do not execute if not flat.
          string accountName = Account.Name;
          Account account = Account.All.FirstOrDefault(a => a.Name == accountName);
          lock (account.Positions)
          {
          foreach (Position position in account.Positions)
          {
          return;
          }
          }​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          47 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          23 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          33 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          50 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          42 views
          0 likes
          Last Post CarlTrading  
          Working...
          X