Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Indicators by reference within a Strategy

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

    Accessing Indicators by reference within a Strategy

    Hi,
    I am newish to C# and new to NT and so forgive the question...
    From within a strategy I would like to access an Indicator by reference. The reason for doing this is so that I do not have to search and replace the name of the indicator in multiple spots within the strategy when I want to use a new indicator (with a different name but same input and output parameters).

    When using the indicator within the strategy this works fine and passes me back the value of "NewStop1"....

    exitLongStopT1Price = MyIndicator(orderPrice, BarsSinceEntry(), exitLongStopT1Price, "Long").NewStop1[0];

    this does not work....

    Indicator.MyIndicator refindicator= new Indicator.MyIndicator();

    exitLongStopT1Price = refindicator(orderPrice, BarsSinceEntry(), exitLongStopT1Price, "Long").NewStop1[0];


    I get an error saying "refindicator is a Variable but is used like a Method". I have tried several combinations of () and [] to no avail. In the end, I'm stumped.

    Also, in the MyIndicator, I have the parameters defined as follows.
    [Description("")]
    [GridCategory("Parameters")]
    public double AveragePrice1
    {
    get { return averagePrice1; }
    set { averagePrice1 = value; }
    }


    The Indicator works fine and, again, if I call it directly from within the strategy it works fine. I have not made any out of the ordinary customizations to the Indicator. Any help or sample code would be greatly appreciated.

    #2
    joesandyego,

    You are accessing a singleton double value from your indicator like that. If you want to do that you need to store it into a double variable. That double value will be the value whatever value the indicator was at the point in time you assigned it its value. If the indicator changes, the variable will not change along with it.

    If you are trying to just simplify the naming you need to create it as an indicator.

    private MACD someInd;
    someInd = MACD(12, 26, 9);
    Print(someInd.Avg[0]);
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      OK. Thanks for the response. So if I understand you correctly then using your example of MACD, if I need to call MACD multiple times within my strategy using different Fast,Slow and Signal parameters each time it is called then I will need to call MACD directly with each new set of parameters each time. Because even in your example of ' someInd = MACD(12, 26, 9); ' MACD is effectively used directly when assigning someInd.

      Users must get around this somehow. Because I think that, for example, many Stop Loss and Exit strategies may need real time parameters related to a specific position (if multiple positions are active) such as AvgPrice. So the Stop Loss/Exit indicator may get called multiple times within a strategy (OnBarUpdate) with different parameters passed each time. If I wanted to change to a new Stop Loss/Exit indicator then I would need to find and replace the name of the Indicator in my strategy which could be multiple instances and then recompile. If I could call the Indicator via reference then I could just have a switch/control that switches easily between Indicators.

      Again, thanks for your response and I hope that you could just confirm my understanding.

      Comment


        #4
        If you are trying to have dynamic parameters then you have no other option but to call it with the parameters you want. This is not really an issue.

        Print(MACD(variable1, variable2, variable3).Avg[0]);

        Just change the variables to whatever you need them to be and it will be dynamic. There really is not much benefit to creating "references". The only reason people do it is so they can type less in code. If you have to have it dynamic, then you need to do it the way shown above.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your responses and time. I understand.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          666 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          377 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          110 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          575 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          580 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X