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

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
    Brandon H.NinjaTrader Customer Service

    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 sidlercom80, 10-28-2023, 08:49 AM
          174 responses
          2,380 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by ETFVoyageur, Today, 02:08 AM
          2 responses
          18 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by PaulMohn, Yesterday, 02:06 AM
          6 responses
          18 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by NM_eFe, Yesterday, 06:14 AM
          3 responses
          16 views
          0 likes
          Last Post NM_eFe
          by NM_eFe
           
          Started by PaulMohn, Yesterday, 01:45 PM
          3 responses
          2 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X