Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Updated values of indicator are not refreshed on the chart

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

    Updated values of indicator are not refreshed on the chart

    I am trying to smooth a centered Moving Average, but somehow the indicator never plots the "refreshed" values. I have searched through the forum and read all similar topics, but I am still unable to fix the indicator. Please give me some hints on what could go wrong. Thanks a lot in advance!


    I used the Output window and the chart "Show Data Box" to debug. The array CalValue holds the indicator values, but these values do not match those in the "Show Data Box". Is there something wrong with the Set() command? Or I should shift the bar back or something? I even removed the script from the indicator folder, used a new name for the script, and even re-installed NT 7 but the same problem still persisted.


    Please see the script and the Output window below.

    ************************************************** **************
    public class TCMA5 : Indicator
    {
    #region Variables
    private bool smooth = true; // Default setting is true
    private DataSeries mySMA;
    private double[] CalValue;
    private double[] Sum;
    private double[] Sumw;
    #endregion

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Middle"));
    mySMA = new DataSeries(this);
    CalValue = new double[50];
    Sum = new double[50]; // NT7 limits the number of available bars for calculations at 256
    Sumw = new double[50];
    Overlay = true;
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar <= 5) return;
    Print(" ");

    mySMA[0] = SMA(Input, 0)[0];

    for (int i = 20; i >= 0; i--)
    {
    Sum[i] = 9 * mySMA[i];
    Sumw[i] = 9;
    int k = 8;

    for (int j = 1; j <= 8; j++)
    {
    Sum[i] += k * mySMA[i+j];
    Sumw[i] += k;

    if (smooth==true & j <= i)
    {
    Sum[i] += k * mySMA[i-j];
    Sumw[i] += k;
    }

    k--;
    }

    CalValue[i] = Sum[i] / Sumw[i];
    }

    /// Plot the array CalValue
    for (int p = 0; p <= 20-1; p++)
    {
    Print("CalValue[" + p.ToString() + "] = " + CalValue[p].ToString());
    }
    Values[0].Set(CalValue[0]);
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Middle
    {
    get { return Values[0]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public bool Smooth
    {
    get { return smooth; }
    set { smooth = value; }
    }
    #endregion
    }

    ************************************************** ***************************



    These are the values from the Output Window (input: smooth = true). The chart is a daily chart of the stock XOM:

    *****************************************
    CalValue[0] = 88.6862222222222
    CalValue[1] = 88.5771698113207
    CalValue[2] = 88.4696666666667
    CalValue[3] = 88.3572727272727
    CalValue[4] = 88.2442253521127
    CalValue[5] = 88.1388
    CalValue[6] = 88.0338461538461
    CalValue[7] = 87.925625
    CalValue[8] = 87.8082716049383
    CalValue[9] = 87.7025925925926
    CalValue[10] = 87.6198765432098
    CalValue[11] = 87.5351851851851
    CalValue[12] = 87.4653086419753
    CalValue[13] = 87.4077777777778
    CalValue[14] = 87.3669135802469
    CalValue[15] = 87.3176543209876
    CalValue[16] = 87.307037037037
    CalValue[17] = 87.3366666666666
    CalValue[18] = 87.3922222222222
    CalValue[19] = 87.4985185185185

    ************************************************** *


    However, each time the chart plots the following values. Basically, the ninjascript works fine, but the chart only plots values as if the input "smooth" is set to zero. Why?

    **************************************
    CalValue[0] = 88.6862222222222
    CalValue[1] = 88.3988888888889
    CalValue[2] = 88.2133333333333
    CalValue[3] = 88.048
    CalValue[4] = 87.9091111111111
    CalValue[5] = 87.8384444444444
    CalValue[6] = 87.5957777777777
    CalValue[7] = 87.5348888888889
    CalValue[8] = 87.4088888888889
    CalValue[9] = 87.4468888888888
    CalValue[10] = 87.4566666666666
    CalValue[11] = 87.3546666666666
    CalValue[12] = 87.3675555555555
    CalValue[13] = 87.3731111111111
    CalValue[14] = 87.3708888888888
    CalValue[15] = 87.0395555555555
    CalValue[16] = 87.1595555555555
    CalValue[17] = 87.2768888888888
    CalValue[18] = 87.3608888888888
    CalValue[19] = 87.6359999999999
    ************************************************

    #2
    Hello Trader100,

    The Set() method is a very basic method that just sets the values of the plot so I do not think there is going to be anything wrong with it.

    I see that you are rebuilding your Array on each bar update which is the reason why you can see different values from the Data Box and the Output window. If you look are looking at the farther than the most current plot you may want to check the last list of values that your Output window is showing and the [0] index should be equal to the plot that you see on the chart.

    As for the reason why it may be 0 when not using the Smooth is because you are you are adding to your index so you are accessing different Array indexes. You may want to use the make sure your Array values are going to be the ones that you want.

    Happy to be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Thanks a lot for the hint, JC!

      Now, I think I understand what you say. I did an experiment and saw the following things:

      1) On a chart, I limited the number of candlebars to 43 bars. Because the calculations will not involve the "oldest" bar (the 42th bar) and the "newest" bar (the 0th bar), it is expected that there will be 41 groups of data.

      2) Next, I checked the Output window and counted the number of groups carefully. Sure enough, there are exactly 41 groups of data. If I am correct, this means that the indicator will show on the chart from the 41st bar to the 1st bar. (Please correct me if I am wrong here.)

      3) If I write Values[0].Set(CalValue[0]), this means that only the 0th element of each group will be plotted. Sure enough, each of the bar (the 41st to the 1st) shows the indicator value of the 0th element.


      Here is my need for help --- I only want to plot out the values of the "most recent" group. For example, I have the following results from the Output window:

      ==========================================
      :
      :
      :
      New: CalValue[0] = 88.37875
      New: CalValue[1] = 88.45875
      New: CalValue[2] = 88.3376923076923
      New: CalValue[3] = 88.2317948717948
      New: CalValue[4] = 88.0007692307692

      New: CalValue[0] = 88.6925
      New: CalValue[1] = 88.5190625
      New: CalValue[2] = 88.5451282051282
      New: CalValue[3] = 88.3376923076923
      New: CalValue[4] = 88.2317948717948

      New: CalValue[0] = 89.10125
      New: CalValue[1] = 88.914375
      New: CalValue[2] = 88.7094871794872
      New: CalValue[3] = 88.5451282051282
      New: CalValue[4] = 88.3376923076923
      ==============================================


      I only want the LAST group of data and have them plotted as follows:
      CalValue[0] = 89.10125 on the current bar (bar 0)
      CalValue[1] = 88.914375 on the previous bar (bar 1)
      CalValue[2] = 88.7094871794872 on the two-day-ago bar (bar 2)
      CalValue[3] = 88.5451282051282 on the three-day-ago bar (bar 3)
      CalValue[4] = 88.3376923076923 on the four-day-ago bar (bar 4)


      What codes should I write, or how do I structure my array and indexes in order to get my plot correct?

      Thanks a lot!

      Comment


        #4
        Trader100,

        I believe I have found a fix for your inquiry.

        This code adjusted for the end of your script should back adjust the values from the array -
        Code:
        /// Plot the array CalValue 
        for (int p = 0; p <= 20-1; p++)
        {
             Print("CalValue[" + p.ToString() + "] = " + CalValue[p].ToString());
             if (p == 0)
                  Values[0].Set(CalValue[p])
             else
                  Values[0].Set(p, CalValue[p]);
        }
        This will change the old values from the array and adjust the plot based of those new values.

        Let me know if this solution works for you
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Thanks a lot, Cal! Your fix works beautifully!

          Comment


            #6
            Please help me on another problem -

            As stated before, this indicator is a centered Moving Average whose values are stored in the array CalValue. Much thanks to NinjaTrader_Cal, I can now plot the final set of CalValue on the price chart.

            Since then, I have been trying to apply multiple colors on this indicator based on the following conditions:

            1) green color if the slope of the indicator is above 0.06
            2) yellowgreen color if the slope of the indicator is between -0.04 and 0.06, and is rising
            3) pink color if the slope of the indicator is between -0.04 and 0.06, and is falling
            4) red color if the slope of the indicator is below -0.04


            I find the functions PlotColors(), Slope(), Rising, Falling() from the NT7 Help Guide and learn about their format and use from other examples on this forum, especially:




            However, my codes still cannot function after I have tried over a dozen of formats, even though they can be compiled. Please tell me what's wrong. Thanks in advance!


            Please find my codes below:

            ================================================== =========
            /// Plot the array CalValue
            for (int p = 0; p <= 20-1; p++)
            {
            Print("CalValue[" + p.ToString() + "] = " + CalValue[p].ToString());

            if (p == 0) {
            Values[0].Set(CalValue[p]);

            if (Slope(Values[0], 2, 0) > 0.06)
            PlotColors[0][0] = Color.Green;
            else if (Slope(Values[0], 2, 0) <= 0.06 && Slope(Values[0], 2, 0) >= -0.04 && Rising(Values[0]))
            PlotColors[0][0] = Color.YellowGreen;
            else if (Slope(Values[0], 2, 0) <= 0.06 && Slope(Values[0], 2, 0) >= -0.04 && Falling(Values[0]))
            PlotColors[0][0] = Color.Pink;
            else
            PlotColors[0][0] = Color.Red;
            }

            else
            {
            Values[0].Set(p, CalValue[p]);

            if (Slope(Values[0], 2, 0) > 0.06)
            PlotColors[0][0] = Color.Green;
            else if (Slope(Values[0], 2, 0) <= 0.06 && Slope(Values[0], 2, 0) >= -0.04 && Rising(Values[0]))
            PlotColors[0][0] = Color.YellowGreen;
            else if (Slope(Values[0], 2, 0) <= 0.06 && Slope(Values[0], 2, 0) >= -0.04 && Falling(Values[0]))
            PlotColors[0][0] = Color.Pink;
            else
            PlotColors[0][0] = Color.Red;
            }
            }
            }

            Comment


              #7
              Trader100,

              You will want to take out the Values[0].Set and use just Value.Set();

              This will ensure that we are setting the correct value for the plot and referencing the correct plot.

              Let me know if this helps
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Thanks a lot for your quick response and help, Cal!

                I followed your advice and took out the Values[0].Set and used just Value.Set() However, I got many errors, and so I have to add back [0]. I guess I must have done something wrong. Anyway, I built two versions of my script. Both of them were compiled successfully, and the indicator was correct with the values.


                On the price chart (please see the attachment), you can see the indicator that changes color (green, pink, yellowgreen, red) based on my pre-set conditions. However, there are two problems:


                1) on the left side of the price chart, you will notice that the indicator shows a "tail" rising from zero to the candlestick areas. How can I "omit" this "rising tail"? I have tried to use the Reset() function, but I got many errors during compiling.


                2) on the right side of the price chart, the indicator somehow shows "branches" when it changes to another color. How can I "omit" these branches? I have also tried to use the Reset() function, but I got many errors during compiling.


                I had another version of the script (I wrote it after many frustrating hours of testing and debugging.) This 2nd version script shows the indicator in the bottom half of the chart. You will notice that this indicator has the same profile as the indicator shown on the price chart, but this indicator fails to pick up the correct color. I think my Slope() function and/or the Rising() and Failling() functions are wrong.


                Please advise and help me resolve the problems. Thanks in advance!
                Attached Files

                Comment


                  #9
                  Trader100,

                  1) on the left side of the price chart, you will notice that the indicator shows a "tail" rising from zero to the candlestick areas. How can I "omit" this "rising tail"? I have tried to use the Reset() function, but I got many errors during compiling.

                  You would need to change the way you are first setting the plot. From the code it looks like the Plot is being set at first and then you are adjusting it later when the bars required kicks in, in the script.

                  2) on the right side of the price chart, the indicator somehow shows "branches" when it changes to another color. How can I "omit" these branches? I have also tried to use the Reset() function, but I got many errors during compiling.

                  That is because of the back adjusting of the values for each of the different plots and only going back so far in the number of bars. I.E. the for loop look back.
                  Cal H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

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