Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to access Indicator property with arguments

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

    How to access Indicator property with arguments

    NT Forum,

    From a Strategy, how can I access an Indicator property (which requires an argument)?
    Technically, the Indicator property is not a "property", as the only type of parameterized property you can define in C# is indexers. The Indicator property is a public method. It is only required to return (get) a value, not accept (set) a value.

    When the Indicator is added to a Strategy using the NinjaScript Add(), the Indicator's public method can not be accessed. Using .net notation, it is possible to instantiate the Indicator and access the public method. However, I have concerns on how the Indicator would behave with each Stategy OnBarUpdate.

    Any guidance would be appreciated.

    Thanks and regards
    Shannon

    #2
    Originally posted by Shansen View Post
    NT Forum,

    From a Strategy, how can I access an Indicator property (which requires an argument)?
    Technically, the Indicator property is not a "property", as the only type of parameterized property you can define in C# is indexers. The Indicator property is a public method. It is only required to return (get) a value, not accept (set) a value.

    When the Indicator is added to a Strategy using the NinjaScript Add(), the Indicator's public method can not be accessed. Using .net notation, it is possible to instantiate the Indicator and access the public method. However, I have concerns on how the Indicator would behave with each Stategy OnBarUpdate.

    Any guidance would be appreciated.

    Thanks and regards
    Shannon
    You would use standard OOP dot notation to access public properties. Would you care to share a code example that you think does not correctly express that which you want?

    Indicator properties can be set or got.

    Properties are not methods. From the Microsoft literature: "Properties are an extension of fields and are accessed using the same syntax. Unlike fields, properties do not designate storage locations. Instead, properties have accessors that read, write, or compute their values."

    Comment


      #3
      Hello Shansen,

      The properties for an indicator are set by the overload parameters supplied to the indicator call.

      For example, if you are calling the SMA indicator, the SMA has a period property.

      If you call SMA(15)[0] this would return the SMA using 15 as the period property.
      If you call SMA(20)[0] one line later, this would return the SMA using 20 as the period property.

      If you want to call a method from the indicator, call the indicator and then access the method as a child.

      For example if you have an indicator called MyCustomIndicator that has a public method called CustomMethod.
      MyCustomIndicatorCall(parameter).CustomMethod().
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        koganam,

        Thank you for the response.

        Please find below a code outline (where Direction is an Enum accessible from the namespaces: Strategy and Indicator). The (non-functioning) ExampleStrategy uses the NinjaScript "Add()". I assume "Add()" appropriately calls OnStartUp, Initialize and OnBarUpdate methods in ExampleIndicator.

        In the event this is changed to instead instantiate the ExampleIndicator, would I have to litter ExampleStrategy with calls to ExampleIndicator.OnStartUp, ExampleIndicator.Initialize, and ExampleIndicator.OnBarUpdate?

        Code:
        namespace NinjaTrader.Strategy
        {
          public class ExampleStrategy: Strategy
          {
            protected override void Initialize()
            {
              Add(ExampleIndicator());
        
              // additional code      
              CalculateOnBarClose  = true;
            }
        
            protected override void OnBarUpdate()
            {
              if (ExampleIndicator.ExampleReturnMethod(Direction.Up)) 
              {
                // do something      
              }
            }
          }
        }

        Code:
        namespace NinjaTrader.Indicator
        {
          public class ExampleIndicator : Indicator
          {
        
            protected override void OnStartUp()
            {
             // additional code
            }
        
            protected override void Initialize()
            {
             // additional code
             Overlay = false;
            }
        
            protected override void OnBarUpdate()
            {
             // additional code
            }
        
            public bool ExampleReturnMethod (Direction direction))
            {
             // additional code to calculate and return value 
             return true;
            }
          }
        }
        Again, thanks for the guidance
        Regards
        Shannon

        Comment


          #5
          ExampleIndicator.ExampleReturnMethod(Direction.Up) should probably be ExampleIndicator().ExampleReturnMethod(Direction.U p)

          Other than that it looks pretty standard to me. You do not have to make any reference to the protected methods in an indicator: the Add() method takes care of creating the object correctly. Given your skills. a glance at the magic NinjaTrader wrapper code, with its dire warnings, will show you why and how. Remember, you can look but you cannot touch.
          Last edited by koganam; 04-30-2015, 04:18 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 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
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X