Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Access a var from an indicator in a Strategy

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

    Access a var from an indicator in a Strategy

    I'm following the example "Exposing indicator values that are not plots" that's been provided but get compile errors.

    In the example they apply the line: Add(SampleBoolSeries()) in the "SampleBoolSeriesStrategy".

    In my application I do the same adding the line: Add(SwingOne()) in my strategy. When I do this I get a compile error:

    "No overload for method 'SwingOne' takes '0' arguments"

    If I add instead the line: Add(SampleBoolSeries()) I get no compile errors.

    So the sample indicator code compiles OK but mine does not. My question: Is my coding error in my strategy or in the referenced indicator?
    I'm not sure where to begin to track down this problem.

    Regards,
    taddypole...

    #2
    To add more detail to my previous post:

    I'm trying to access the pivots or swings from the swing indicator. So I created a copy of the swing indicator and called it SwingOne.

    Then I added variables for the SwingHighs and SwingLows so they could be referenced in my Strategy. I made these variables exposed as per the example since they are doubles and not dataseries values.

    Everything was going good until I added that line in the strategy. Add(SwingOne())
    I just can't figure out what the error message might be telling me.

    Comment


      #3
      Your custom indicator likely needs inputs like DataSeries and parameters to be specified, please use Intellisense to see what the compiler would expect.

      You can just use the per default exposed Swing indicator methods -

      Comment


        #4
        I've attached my SwingOne indicator.

        I've added in the variables section:

        private double exposedCurrentSwingHigh; // Exposed variables
        private double exposedCurrentSwingLow; // Exposed variables

        I've added in the Initialize section:

        double exposedCurrentSwingHigh = 0.0; // Exposed variables
        double exposedCurrentSwingLow = 0.0; // Exposed variables

        In the OnBarUpdate section, I've added:

        exposedCurrentSwingHigh = currentSwingHigh; // exposed Swing High
        exposedCurrentSwingLow = currentSwingLow; // exposed Swing Low

        and in the Properties section I've added:

        [Browsable(false)]
        [XmlIgnore()]
        public double ExposedCurrentSwingHigh
        {
        // We need to call the Update() method to ensure our exposed variable is in up-to-date.
        get { Update(); return exposedCurrentSwingHigh; }
        }

        [Browsable(false)]
        [XmlIgnore()]
        public double ExposedCurrentSwingLow
        {
        // We need to call the Update() method to ensure our exposed variable is in up-to-date.
        get { Update(); return exposedCurrentSwingLow; }
        }

        As far as I can tell, this follows the example closely.

        Any suggestions?

        Thanks,
        taddypole...
        Attached Files

        Comment


          #5
          The code compiles fine here, but why the work to expose those values when they are already available directly by accessing the respecive plots?

          double mySwingHigh = Swing(5).SwingHigh[0];

          Comment


            #6
            ok.. I think we are down to the point I don't understand.

            So are you saying: if I put:

            double mySwingHigh = Swing(5).SwingHigh[0];

            in my strategy, I will have access to those plots within my strategy?

            Does SwingOne indicator need to be applied to that chart also?

            And yes, that code compiled fine, it was the line in the strategy:

            Add(SwingOne());

            that produced the compile error when compiling my strategy.

            Thanks Bertrand...

            Comment


              #7
              Correct, that's what I meant - Plots are exposed from any indicator per default, so there's no custom exopse needed as for internal DataSeries and / or variables, so you were basically taking the hard route to get those values.

              The Add() did not work out, as it would expect parameters -

              Add(SwingOne(5)); >> this should compile as well, but not needed

              Comment


                #8
                ok... one more point of clarification:

                Am I correct in saying, that if I apply an indicator to a chart, it then becomes exposed to a strategy applied to that same chart?

                If this is true, is it also more efficient from a CPU loading point of view to use values from indicators rather than recomputing them in the strategy?

                Comment


                  #9
                  No, the indicator does not have to load on the chart to offer it's plots for value access - the strategy would start an indicator instance (if not already cached from another call) to you can call it's values for your calcs.

                  Comment


                    #10
                    ok... and the last point... is it more efficient?

                    If the indicator is already running on the chart, is accessing the indicator value from within a strategy more efficient than doing all the code for the indicator in the strategy?

                    Comment


                      #11
                      I've attached a chart that shows the difference of calculating the Swing indicator by either the COBC true or false. As you can see the swing lines turn out differently.

                      The problem i'm now having is, when i access the swing values from within my strategy which is calculated with COBC = false I don't get the same values as when calculated with COBC = true.

                      I"ve tried many many things but can't get those correct values. I can get close to those values but not exactly.

                      How do i access those values that are based on COBC = true when my strategy is running on COBC = false?

                      The most obvious was to limit just those calculations for the swing values in the strategy to only fire on the close of the bar but that still didn't yield the correct results.

                      How do I sync the calculation updates to only get the swing values which are calculated on COBC = true conditions?

                      Thanks,
                      taddypole...
                      Attached Files

                      Comment


                        #12
                        Yes, accessing the indicator instance would be more efficient than rerunning indicator cals in the strategy, since NT would attempt to cache instances with the same parameters used.

                        For realtime use you could look into this sample, showing how to work with FirstTickOfBar to use onbarclose and on tick calcs in the same script - http://www.ninjatrader.com/support/f...ad.php?t=19387

                        Comment


                          #13
                          thanks Bertrand,

                          I now have all that working.

                          Next I want to get the highest/lowest pivot or swing of the last 3

                          If SwingLow is a dataseries, I thought of using:

                          MIN(SwingLow,3)[0];

                          but this only gives me the lowest swing of the last 3 bars and not the last 3 swings.

                          How can I get the max/min of the last 3 dataseries objects?

                          Thank you for all your help.

                          taddypole...

                          Comment


                            #14
                            Never mind...

                            I've figured it out.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              The code compiles fine here, but why the work to expose those values when they are already available directly by accessing the respecive plots?

                              double mySwingHigh = Swing(5).SwingHigh[0];

                              Hi

                              I'm also using the Swing indicator and I want to integrate it into my strategy but when I compile with the line that you offered, I get the following error message:

                              " An object reference is required for the non-static field, ethod or property 'NinjaTrader.Strategy.Strategy.Swing(int)'

                              What am I missing?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              639 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              572 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X