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

AddDataSeries - How to debug

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

    AddDataSeries - How to debug

    Hi,

    I'm struggling a bit with an indicator I'm trying to build to get the average daily High of the last 5 days, and plot that value on my tick chart from the start of the current trading session to now.

    I use a tick chart but load daily data through AddDataSeries to perform the calculation.

    Is the below code correct considering I want the average of the last 5 daily Highs (Excluding today)?
    When I print the values, it seems that it's older days.
    It also seems that my numberOfBars is not working properly as the longer the script runs, the more in the past the lines is drawn (it goes prior to the current session).

    Code:
    else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Day, 1);
                }
    
    protected override void OnBarUpdate()
            {
                //Add your custom indicator logic here.
                if (CurrentBars[1] < 5) return;
    
    
                if (Bars.IsFirstBarOfSession)
                 {
                      // print the number of bars counted from the previous session
                     // Print("Previous session number of bars: " + numberOfBars);
    
                      // reset the counter as this is a new session
                      numberOfBars = 0;
                 }
    
                 // increment numberOfBars on every bar close
                 numberOfBars++;
    
                double sumHighs= 0;
                for (int i = 1; i <= 5; i++)
                {
                    double highs= Bars.GetHigh(CurrentBars[1] - i);
                    sumHighs+= highs;
                }
    
                double averageHighs = sumHighs/ 5;
    
                Draw.Line(this, "highs" + CurrentBar, false, numberOfBars, averageHighs, 0, averageHighs, new SolidColorBrush(Color.FromRgb(209, 212, 220)), DashStyleHelper.Dash, 2);
    
            }​
    Additionally, how do we control the number of days to load through the AddDataSeries?

    Many thanks.

    #2
    Hello datlayzard,

    The easiest way to do that would be to use the existing High series from the day series and pass that to an indicator that averages like an SMA with a 5 period. That would also exclude the current day because that day bar won't have closed yet, this assumes you are using OnBarClose processing. That would look like the following:

    Code:
    protected override void OnBarClose()
    {
         if (CurrentBars[0] < 1 || CurrentBars[1] < 5) return;
         if(BarsInProgress == 1) return;
    
         double avgHighPrice = SMA(Highs[1], 5)[0];​
    }
    That would let you use OnBarUpdate for the tick series updates but average the last 5 daily bars into a price.

    To adjust the days to load for the day series you can use the following overload:

    AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)

    Code:
    AddDataSeries(null, new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1}, 30, null, null);
    ​​
    Last edited by NinjaTrader_Jesse; 11-14-2023, 09:59 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you so much for the answer Jesse.

      This indeed looks simpler than my approach. However, if I want to check the values for Highs[1], it gives me the wrong information.
      I created the below loop to look at the value of the Highs of the last 5 days excluding today for ES and it gives me the following results:
      Highs[1][1] = 4339.75
      Highs[1][2] = 4264.75
      Highs[1][3] = 4215
      Highs[1][4] = 4197.5
      Highs[1][5] = 4185

      I know these numbers are not correct.

      If I wanted to do it in a loop like below, how should I do it, please? This would help me understand where I do it wrong.

      Code:
      double avgHighs = 0;
                  for (int i = 1; i < 6; i++)
                  {
                      avgHighs+= Highs[1][i];
                      Print("Highs[1][" + i.ToString() + "] = " + Highs[1][i].ToString());
                  }
                  avgHighs = avgHighs/5;​


      Many thanks.

      Comment


        #4
        Hello datlayzard,

        I would suggest printing Times[1][i] so you can see which bars those prints belong to. I tried the loop on my end and see it working as expected when comparing against a daily chart. The prints you need to look at would be the last set of prints in the output window so you would have to scroll to the bottom if you have many prints.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you Jesse.

          I did that and the results show old days (not the 5 most recent ones excluding today). I did scroll until the bottom of the output console.

          Click image for larger version

Name:	Screenshot 2023-11-14 112208.png
Views:	79
Size:	7.9 KB
ID:	1277906

          I don't understand why the Times[1][1] is November 2 and not November 13.

          Any idea?

          Code:
              else if (State == State.Configure)
                      {
                          AddDataSeries(null, new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1}, 10, null, null);
                      }​
          Code:
          for (int i = 1; i < 6; i++)
                      {
                          Print(Times[1][i] + " " + i.ToString() + " - " + Highs[1][i]);
                      }​
          Many thanks.

          Comment


            #6
            Hello datlayzard,

            Have you opened a daily chart and made sure you have historical daily data loaded? You may need to use reload historical data from the chart to make sure you have current data.

            From my test I see the following output for the last prints, the date gap is due to the weekend and because we are looking back starting 1 bars ago. If you were looking at a daily chart the right most building bar is excluded and so is the next bar. The prints would correspond to the bars starting on the third bar from the right of the chart which would be the 10th.

            11/10/2023 3:00:00 PM 4435.5
            11/9/2023 3:00:00 PM 4413
            11/8/2023 3:00:00 PM 4407.75
            11/7/2023 3:00:00 PM 4403.25
            11/6/2023 3:00:00 PM 4389.5​
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you, Jesse.

              I apologize for all the back and forth. I reloaded the data by doing right click and 'Reload All Historical Data' on my chart and it gave the same results.
              I also closed and reopened NinjaTrader, but still have the same results.

              Should this reloading of historical data be done in a different place?

              Thank you.

              Comment


                #8
                Hello datlayzard,

                A chart would be where you can reload historical data. Have you physically opened a daily chart at this point to verify what data is shown? Do you see todays daily building bar which is the 14th, what would be considered [0] bars ago which is the 13th and then the [1] bars ago which is the 10th?

                If so where in your code are you executing the loop? Are you executing it from the tick series?


                Code:
                protected override void OnBarUpdate()
                {
                    if (CurrentBars[0] < 1 || CurrentBars[1] < 5) return;
                   if (BarsInProgress == 1) return;
                
                
                   for (int i = 1; i < 6; i++)
                   {
                      Print(Times[1][i] + " " + Highs[1][i].ToString());
                   }
                }​
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thank you Jesse.

                  I have my tick charts from which I run this indicator. I do have a daily chart open on the side as well to verify the data.

                  I run the code within the OnBarUpdate() which looks like this:
                  Code:
                          protected override void OnStateChange()
                          {
                              if (State == State.SetDefaults)
                              {
                                  Description                                    = @"Enter the description for your new custom Indicator here.";
                                  Name                                        = "stats";
                                  Calculate                                    = Calculate.OnBarClose;
                                  IsOverlay                                    = true;
                                  DisplayInDataBox                            = true;
                                  DrawOnPricePanel                            = true;
                                  DrawHorizontalGridLines                        = true;
                                  DrawVerticalGridLines                        = true;
                                  PaintPriceMarkers                            = true;
                                  ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                  //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                                  //See Help Guide for additional information.
                                  IsSuspendedWhileInactive                    = true;
                              }
                              else if (State == State.Configure)
                              {
                                  AddDataSeries(null, new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1}, 10, null, null);
                  
                              }
                  
                          }​
                  Code:
                          protected override void OnBarUpdate()
                          {
                              //Add your custom indicator logic here.
                  
                  
                              if (CurrentBars[0] < 1 || CurrentBars[1] < 5) return;
                              if(BarsInProgress == 1) return;
                              Print("HIGHS current bar: " + Highs[1][0]);
                              Print("HIGHS last bar: " + Highs[1][1]);
                              double avgRange = 0;
                              for (int i = 1; i < 6; i++)
                              {
                                  avgRange += Highs[1][i];
                                  Print(Times[1][i] + " " + i.ToString() + " - " + Highs[1][i]);
                              }
                  }​
                  And this is the output at the bottom of the output console.

                  Click image for larger version

Name:	Screenshot 2023-11-14 120457.png
Views:	89
Size:	8.1 KB
ID:	1277912
                  My daily chart looks fine:

                  Click image for larger version

Name:	Screenshot 2023-11-14 120545.png
Views:	66
Size:	21.8 KB
ID:	1277914

                  Thank you.
                  Attached Files

                  Comment


                    #10
                    Hello datlayzard,

                    The image of the daily bars looks like you are potentially missing some data, you would need to expand the chart so you can see the individual days like the image I have attached.

                    Attached Files
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Thank you, it's working! I'm not sure why, but by switching from ES to MES to ES again it's now showing all the good days on the daily chart and giving me the correct values in my tick chart.

                      I really appreciate your help on this.

                      Have a great day.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by JoMoon2024, Today, 06:56 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post JoMoon2024  
                      Started by Haiasi, 04-25-2024, 06:53 PM
                      2 responses
                      17 views
                      0 likes
                      Last Post Massinisa  
                      Started by Creamers, Today, 05:32 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Creamers  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      12 responses
                      1,786 views
                      0 likes
                      Last Post Leafcutter  
                      Started by poplagelu, Today, 05:00 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post poplagelu  
                      Working...
                      X