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

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 geddyisodin, Today, 05:20 AM
          5 responses
          32 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by bmartz, Today, 09:30 AM
          2 responses
          13 views
          0 likes
          Last Post bltdavid  
          Started by f.saeidi, Today, 11:02 AM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by geotrades1, Today, 10:02 AM
          4 responses
          12 views
          0 likes
          Last Post geotrades1  
          Started by rajendrasubedi2023, Today, 09:50 AM
          3 responses
          16 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X