Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing StrategyBase.Positions and Account.Executions in indicator

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

    Accessing StrategyBase.Positions and Account.Executions in indicator

    I am able to access NinjaTrader.Strategy.StrategyBase.Positions and NinjaTrader.Cbi.Account.Executions in my strategy's OnBarUpdate method. However, when I try to do this in any of the methods on my indicator, as in the example below, I get a compile error.

    Code:
    // using ...
    // using ...
    using NinjaTrader.Cbi;
    using NinjaTrader.Strategy;
    
    namespace NinjaTrader.Indicator
    {
        public class MyIndicatorName : Indicator
        {
            protected override void OnStartUp()
            {
                System.Collections.IEnumerator Trades = Account.Executions.GetEnumerator();
            }
        }
    }
    
    // ...
    // ...
    Is there a workaround where I can do this in my indicator??

    Thanks!

    #2
    Hello llstelle,

    Thank you for your post.

    What is the error you receive?

    If you wish to access positions from within an indicator you can use the following snippet for accessing account positions. I must note however that this is unsupported code and therefore there is no documentation on this:
    Code:
    			foreach (Account acct in Cbi.Globals.Accounts)
    			{
    				if (acct.Positions != null)
    				{
    					PositionCollection positions = acct.Positions;
    					foreach (Position pos in positions)
    					{
    						Print(pos.Account.Name + " " + pos.Instrument + " " + pos.MarketPosition + " " + pos.Quantity + " " + pos.AvgPrice);
    					}
    				}
    			}
    I look forward to assisting you further.

    Comment


      #3
      Awesome, the code segment that you suggested does exactly what I need it to. Last question, is there a NT API method call I can use to "get last price" of a specified instrument directly, say, ES 03-13 Globex. Or do I have put up with the overhead of adding the entire instrument series using Add() first? As you can tell, the next step I'm trying to do is to calculate the PnLs on each account position.

      The error code 'm getting is CS0120 "An object reference is required for the non-static field, method or property 'NinjaTrader.Cbi.Account.Executions.get'."

      Same goes with 'NinjaTrader.Cbi.Account.Positions.get' and 'NinjaTrader.Strategy.StrategyBase.Positions.get'.

      In Visual Studio, I get 'Cannot access non-static property '(StrategyBase.)Positions' in static context'. Same goes for 'Account.Executions'.

      Comment


        #4
        Hello llstelle,

        Thank you for your response.

        I am not aware of an item to pull last price without actually adding the instrument into the strategy or indicator.

        Please let me know if I may be of further assistance.

        Comment


          #5
          All right, no problem. Thanks a lot!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          577 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X