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

Multi-timeframe chart/databox not in sync with indicator

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

    Multi-timeframe chart/databox not in sync with indicator

    I am trying to create an indicator that creates a summary aggregation of multiple data series which uses 5, 10, 15, 20 minute time frames. However, the values are not syncing with what is in the various data series on the chart as displayed in the data box. As indicated in the attached graphic, the bars at 11:40 have different values displayed in the indicator verses what is displayed in the data box when the mouse is hovering over the chart for that column. Below is the indicator code and a screenshot. What am I missing?

    protected override void OnStateChange() {

    if (State == State.SetDefaults) {

    AddPlot(new Stroke(Brushes.Black, 1), PlotStyle.Dot, "5 Minute");
    AddPlot(new Stroke(Brushes.Black, 1), PlotStyle.Dot, "10 Minute");
    AddPlot(new Stroke(Brushes.Black, 1), PlotStyle.Dot, "15 Minute");
    AddPlot(new Stroke(Brushes.Black, 1), PlotStyle.Dot, "20 Minute");
    }
    else if (State == State.Configure) {
    AddDataSeries(Data.BarsPeriodType.Minute, 10);
    AddDataSeries(Data.BarsPeriodType.Minute, 15);
    AddDataSeries(Data.BarsPeriodType.Minute, 20);
    }
    }

    protected override void OnBarUpdate() {
    if (CurrentBars[0] <= BarsRequiredToPlot ||
    CurrentBars[1] <= BarsRequiredToPlot ||
    CurrentBars[2] <= BarsRequiredToPlot ||
    CurrentBars[3] <= BarsRequiredToPlot)
    return;​

    // Fixed points for display
    Bars5Plot[0] = 0;
    Bars10Plot[0] = 5;
    Bars15Plot[0] = 10;
    Bars20Plot[0] = 15;

    var o = Opens[0][0];
    var c = Closes[0][0];
    var i = CurrentBars[0]; // bar index

    if (BarsInProgress == 0) {
    SetCurrentBrushColor(i, o, c, Bars5PlotBrush);
    My.Draw.Text(this, GetUniqueTag($"5BarText"), $"5 min i: {i} o: {o} c: {c}", Bars5Plot[0] + VerticalOffset, SmallFont);
    }

    if (BarsInProgress == 1) {
    o = Opens[1][0];
    c = Closes[1][0];
    i = CurrentBars[1];
    SetCurrentBrushColor(i, o, c, Bars10PlotBrush);
    My.Draw.Text(this, GetUniqueTag($"10BarText"), $"10 min i: {i} o: {o} c: {c}", Bars10Plot[0] + VerticalOffset, SmallFont);
    }

    if (BarsInProgress == 2) {
    o = Opens[2][0];
    c = Closes[2][0];
    i = CurrentBars[2];
    SetCurrentBrushColor(i, o, c, Bars15PlotBrush);
    My.Draw.Text(this, GetUniqueTag($"15BarText"), $"15 min i: {i} o: {o} c: {c}", Bars15Plot[0] + VerticalOffset, SmallFont);
    }

    if (BarsInProgress == 3) {
    o = Opens[3][0];
    c = Closes[3][0];
    i = CurrentBars[3];
    SetCurrentBrushColor(i, o, c, Bars20PlotBrush);
    My.Draw.Text(this, GetUniqueTag($"b20Text"), $"20 min i: {i} o: {o} c: {c}", Bars20Plot[0] + VerticalOffset, SmallFont);
    }
    }
    private void SetCurrentBrushColor(int index, double open, double close, BrushSeries plotBrush) {
    Print($"i: {index} o: {open}, c: {close}, c LT o: {close.LT(open)}, c GT o: {close.GT(open)}");
    if (close.LT(open))
    plotBrush[0] = Brushes.Red;
    else if (close.GT(open))
    plotBrush[0] = Brushes.LimeGreen;
    else
    plotBrush[0] = Brushes.Black;
    }​

    Click image for larger version

Name:	ntchart.png
Views:	65
Size:	105.7 KB
ID:	1299937

    #2
    Hello love2code2trade,

    Thanks for your post.

    It seems like when multiple bar series are processing at the same time it could be causing the values assigned to the variables to be incorrect.

    The variables should be defined as class-level variables in the script. The "o" variable and "c" variable should be defined as class-level double variables. The "i" variable should be defined as a class-level int variable.

    You should create a separate variable for each series and assign a value to the variable in the BarsInProgress check, such as say "o5" to use for the 5 minute series, "o10" to use for the 10 minute series, "o15" to use for the 15 minute series, and "o20" to use for the 20 minute series.

    For example:
    Code:
    //class level variables
    private double o5, o10, o15, o20;
    
    
    //OnBarUpdate()
    if (BarsInProgress == 0)
    {
        o5 = Opens[0][0];
    }
    
    if (BarsInProgress == 1)
    {
        o10 = Opens[1][0];
    }
    
    if (BarsInProgress == 2)
    {
        o15 = Opens[2][0];
    }
    
    if (BarsInProgress == 3)
    {
        o20 = Opens[3][0];
    }
    Then you would use those variables in your Draw.Text() method to draw the values as text on the chart window.

    Please let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,
      Thanks for your prompt reply. I implemented your changes as indicated below. However, the issue still persists.

      private double _o5, _o10, _o15, _o20;
      private double _c5, _c10, _c15, _c20;
      private int _i5, _i10, _i15, _i20;
      private DateTime _t5, _t10, _t15, _t20;

      protected override void OnBarUpdate() {
      if (CurrentBars[0] <= BarsRequiredToPlot ||
      CurrentBars[1] <= BarsRequiredToPlot ||
      CurrentBars[2] <= BarsRequiredToPlot ||
      CurrentBars[3] <= BarsRequiredToPlot)
      return;

      // Fixed points for display
      Bars5Plot[0] = 0;
      Bars10Plot[0] = 5;
      Bars15Plot[0] = 10;
      Bars20Plot[0] = 15;

      if (BarsInProgress == 0) {
      _o5 = Opens[0][0];
      _c5 = Closes[0][0];
      _i5 = CurrentBars[0];
      _t5 = Times[0][0];
      SetCurrentBrushColor(_i5, _o5, _c5, Bars5PlotBrush);
      My.Draw.Text(this, GetUniqueTag($"5MinText"), $"5 min {_t5.Hour}:{_t5.Minute} i: {_i5} o: {_o5} c: {_c5}", Bars5Plot[0] + VerticalOffset, SmallFont);
      }

      if (BarsInProgress == 1) {
      _o10 = Opens[1][0];
      _c10 = Closes[1][0];
      _i10 = CurrentBars[1];
      _t10 = Times[1][0];
      SetCurrentBrushColor(_i10, _o10, _c10, Bars10PlotBrush);
      My.Draw.Text(this, GetUniqueTag($"10MinText"), $"10 min {_t10.Hour}:{_t10.Minute} i: {_i10} o: {_o10} c: {_c10}", Bars10Plot[0] + VerticalOffset, SmallFont);
      }

      if (BarsInProgress == 2) {
      _o15 = Opens[2][0];
      _c15 = Closes[2][0];
      _i15 = CurrentBars[2];
      _t15 = Times[2][0];
      SetCurrentBrushColor(_i15, _o15, _c15, Bars15PlotBrush);
      My.Draw.Text(this, GetUniqueTag($"15MinText"), $"15 min {_t15.Hour}:{_t15.Minute} i: {_i15} o: {_o15} c: {_c15}", Bars15Plot[0] + VerticalOffset, SmallFont);
      }

      if (BarsInProgress == 3) {
      _o20 = Opens[3][0];
      _c20 = Closes[3][0];
      _i20 = CurrentBars[3];
      _t20 = Times[3][0];
      SetCurrentBrushColor(_i20, _o20, _c20, Bars20PlotBrush);
      My.Draw.Text(this, GetUniqueTag($"20MinText"), $"20 min {_t20.Hour}:{_t20.Minute} i: {_i20} o: {_o20} c: {_c20}", Bars20Plot[0] + VerticalOffset, SmallFont);
      }
      }

      private void SetCurrentBrushColor(int index, double open, double close, BrushSeries plotBrush) {
      Print($"i: {index} o: {open}, c: {close}, c LT o: {close.LT(open)}, c GT o: {close.GT(open)}");
      if (close.LT(open))
      plotBrush[0] = Brushes.Red;
      else if (close.GT(open))
      plotBrush[0] = Brushes.LimeGreen;
      else
      plotBrush[0] = Brushes.Black;
      }​

      Comment


        #4
        Hello,

        Thanks for your notes.

        In your initial post, the code you shared shows 4 plots added to the script with the names "5 Minute", "10 Minute", "15 Minute", and "20 Minute".

        However, I noticed you are using "Bars5Plot[0]", "Bars10Plot[0]", "Bars15Plot[0]", and "Bars20Plot[0]" in the script. Are these additional plots added in the script or are these custom Series?

        Please add debugging prints to the script (one line above the Draw method) that prints out all the variable values being used for the Draw method.

        Compare the prints to the chart window and to the Data Box window to see how the logic is evaluating.

        Do the prints match the Data Box window or the text drawn chart window?

        Then, send us an exported copy of the script (with prints added) along with the exact steps you are taking to reproduce the behavior so we may try to reproduce the behavior on our end and investigate further.

        To export the script, go to Tools > Export > NinjaScript AddOn.

        Exporting: https://ninjatrader.com/support/help...tAsSourceFiles

        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon, thankyou for your reply. I have attached the requested resources.

          Configure a chart to have 4 data series of 5, 10, 15, 20 minute time frames using heiken-ashi bars for the ES using Days to load of 5 and End date of 4/19/2024 for all data series on the chart then add indicator with Input series as the 5 minute data series.

          MyMultiFrameIndicator_20240418.zip

          Click image for larger version  Name:	chartconfig.png Views:	0 Size:	346.0 KB ID:	1300143
          Last edited by love2code2trade; 04-18-2024, 10:34 PM.

          Comment


            #6
            Hello love2code2trade,

            The code in the script you have posted in post # 5 does not match the code you are suggesting you changed in post # 3.
            This code is calling SetFrame() from BarsInProgress 0 only.

            May we have an export of the updated script?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,
              Thankyou for your reply. My apologies, I took out the code that didnt work per post #3 with the class level variables and just made them direct calls. Additionally, I just did some cleanup and i had taken out some custom helpers that made it harder to export as well as to have nothing else interfering with the base functionality we are troubleshooting. the problem is still the same with the values not syncing with the data box.​ I have attached the updated script from which i am working with the missing BarsInProgress conditional statements added back. Thanks for your help!

              MyMultiFrameIndicator_20240419.zip

              Comment


                #8
                Hello love2code2trade,

                Thanks for your notes.

                I see in the script you shared that you are adding a regular data series to the script but you are testing on Heiken Ashi bars.

                You would need to call AddHeikenAshi() in the script to add a secondary Heiken Ashi bars object to the script to use for calculations.

                When modifying your script to call AddHeikenAshi() instead of AddDataSeries() I am seeing the indicator values match the Data Box window.

                See this help guide page for more information about AddHeikenAshi(): https://ninjatrader.com/support/help...heikenashi.htm
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Taddypole, 04-26-2024, 02:47 PM
                5 responses
                35 views
                0 likes
                Last Post eDanny
                by eDanny
                 
                Started by kujista, 04-23-2024, 06:23 AM
                6 responses
                48 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by giulyko00, 04-24-2024, 12:03 PM
                7 responses
                36 views
                0 likes
                Last Post eDanny
                by eDanny
                 
                Started by NM_eFe, Today, 10:13 AM
                0 responses
                11 views
                0 likes
                Last Post NM_eFe
                by NM_eFe
                 
                Started by hdge4u, Yesterday, 12:23 PM
                1 response
                11 views
                0 likes
                Last Post hdge4u
                by hdge4u
                 
                Working...
                X