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

Indicator value never updates when part of strategy...

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

    Indicator value never updates when part of strategy...

    I have a custom indicator that uses ADX, SMA, and TEMA indicators.

    It works fine when I apply it to a chart.

    However, when I include it in a strategy, the ADX value never updates. It stays set at 50 per the Print statements.

    I tried adding an Update() call into this indicator's getter.

    I even added a adx.Update() call before accessing the ads.Value[0].

    But to no avail. It still returns 50 all the time.

    What gives? Here's part of the code.

    Wayne

    Code:
        public class AvgTrend : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int avgLength = 90; // Default setting for AvgLength
                private int adxLength = 45; // Default setting for ADXLength
                private int adxMinimum = 10; // Default setting for ADXMinimum
                private ADX adx = null;
                private SMA sma = null;
                private int signal = 0;
                private TEMA tema = null;
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                adx = ADX(adxLength);
                sma = SMA(avgLength);
                tema = TEMA(14);
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Signal"));
                CalculateOnBarClose    = true;
                Overlay                = false;
                PriceTypeSupported    = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if( CurrentBar > 1) {
                    // Use this method for calculating your indicator values. Assign a value to each
                    // plot below by replacing 'Close[0]' with your own formula.
                    adx.Update();
                    if( adx.Value[0] > adxMinimum) {
                        Print(Time[0] + ": adx.Value[0] = " + adx.Value[0] + ", adxMinimum = " + adxMinimum +
                                    ", adxLength " + adxLength);
                        if( tema.Value[0] > sma.Value[0]) {
                            signal = 1;
                            if( signal != Signal[1]) {
                                Print( Time[0] + ": Changed to long");
                            }
                        }
                        if( tema.Value[0] < sma.Value[0]) {
                            signal = -1;
                            if( signal != Signal[1]) {
                                Print( Time[0] + ": Changed to short");
                            }
                        }
                    } else {
                        signal = 0;
                        if( signal != Signal[1]) {
                            Print( Time[0] + ": Changed to flat");
                        }
                    }
                    Signal.Set(signal);
                }
            }

    #2
    I did some debugging by adding Print() statements into the ADX indicator using Notepad since your editor disallows saving it.

    Anyway, I found that the OnBarUpdate() for ADX is getting called.

    However, I found that the High[0] and Low[0] are returning 0 (zero).

    It's called from inside another indicator which is in turn inside a strategy.

    Please help.

    Wayne

    Comment


      #3
      Help. It seems so simple.

      The DataSeries input for ADX isn't getting updated. High[0], Low[0], Close[0] and Open[0] inside the ADX are all zero.

      It gets updated fine when the adx is part of another indicator on a strategy on a chart.

      But when the indicator gets included into a strategy, the DataSeries for ADX isn't getting updated.

      Please help.

      Wayne

      Comment


        #4
        Another Print() message shows that the High and Low dataseries have a High.Count and Low.Count of ZERO.

        So they never get setup or initialized when ADX is instantiated inside an indicator that is itself instantiated inside a strategy.

        It seems that only the Input parameter gets passed on.

        Please help. Please.

        Wayne

        Comment


          #5
          I just tried setting High and Low explicitly to the same DataSeries as the customer indicators High and Low but the editor complains that those properties are read only.

          Why aren't they getting initialized?

          Wayne

          Comment


            #6
            Hi Wayne,

            The oddities in your code include your use of these:
            Code:
            private ADX adx = null;
            private SMA sma = null;
            private TEMA tema = null;
            and
            Code:
            protected override void Initialize()
            {
                 adx = ADX(adxLength);
                 sma = SMA(avgLength);
                 tema = TEMA(14);
            Please see this tip: http://www.ninjatrader-support.com/v...ead.php?t=3228
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh, those apply to strategies...

              Josh, the link you provide applies to strategies. I tried that same code in an indicator and it disallowed calling Add() with another indicator. It only allows plots to be Add()ed.

              So that's why I used this code to create the indicators inside this indicator.

              That work for SMA and TEMA. It's only the ADX that loses the High and Low.

              Anyway, I already copied the ADX to ADXNew and added the ability to pass the High and Low dataseries as properties.

              Now it all works.

              Just, FYI, loading the High and Low dataseries doesn't work automatically in this scenario as it does in others.

              Wayne

              Comment


                #8
                Wayne,

                Maybe I am not understanding what you are trying to do. You mentioned earlier you were trying to add the ADX onto a strategy to plot which is why I provided that link. If you wanted to plot the values of the ADX with a separate indicator you can simply create a new plot and just have that plot set to the value of the ADX.

                Something like
                Code:
                Plot0.Set(ADX(5)[0]);
                would do the trick.

                Also, I'm not sure what you mean by High and Low for the ADX. There is only one plot for the ADX and that is the ADX plot. There is no High or Low plots associated with this indicator.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Josh, you're not understanding. But with your questions, you will this time.

                  If you do what you just described to plot ADX inside an indicator, that will work fine.

                  Next take that indicator and plot it within a strategy. Like this:

                  Custom Strategy -> calls -> Custom Indicator -> calls -> ADX

                  When you nest it 2 levels like this the ADX only plots 50 constantly.

                  I debugged the reason why, the reason is that inside your built-in ADX indicator it uses High[0] and Low[0] to calculate the ADX.

                  Those DataSeries are both empty.

                  Understand now?

                  So how I solved this was by copying the ADX to ADXNew and adding two properties High and Low then pass in from the Custom indicator it's High and Low DataSeries so then ADXNew calculated correctly.

                  Wayne

                  Comment


                    #10
                    Hi Wayne,

                    Got it, but the issue is actually the way you are calling it in the indicator. If you do it the way outlined in my attachment you will have no issues calling it from the strategy. It is only when you do "private ADX adx = null" technique does it mess up. What you want to do is just create a Plot then .Set the plot to the ADX() value.

                    Please try my attachment. The indicator is named MyCustomIndicator12 and the strategy is named adxtest.
                    Attached Files
                    Josh P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post quantismo  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    8 responses
                    168 views
                    0 likes
                    Last Post jeronymite  
                    Started by cre8able, Today, 04:22 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post cre8able  
                    Working...
                    X