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 nicthe, Today, 07:38 AM
          0 responses
          11 views
          0 likes
          Last Post nicthe
          by nicthe
           
          Started by burtoninlondon, 04-28-2024, 12:38 AM
          3 responses
          35 views
          0 likes
          Last Post burtoninlondon  
          Started by burtoninlondon, 05-01-2024, 10:13 AM
          2 responses
          25 views
          0 likes
          Last Post burtoninlondon  
          Started by LillyMarv, Today, 06:47 AM
          0 responses
          5 views
          0 likes
          Last Post LillyMarv  
          Started by Salahinho99, Today, 04:13 AM
          0 responses
          16 views
          0 likes
          Last Post Salahinho99  
          Working...
          X