Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SUM of Volumes with a condition by period

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

    #16
    Hello Marom,

    Thanks for your note.

    In the code you shared in post # 13 and post #11, you are using mySeries[0] = (Volumes[0])*-1;. This would not be the correct syntax for subtracting a value from another value. You would need to use the -= operator instead to subtract the volume from your variable.

    For example, x -= y would read as x = x - y.

    To add one value to another value, the += operator should be used.

    For example, x += y would read as x = x + y.

    SUM() will add up all values of the Series<double> variable for the period provided. If you pass in a period of 5, it will add up the past 5 Series<double> values together and return the total of those 5 added variables.

    You would need to add debugging prints to the script that print out each value you are accessing in your script's calculations to see exactly what value you are accessing in your script and how your logic is evaluating based on those values.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121

    Ultimately, it will be up to you to come up with the exact logic and debug the script to accomplish your specific goal.

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #17
      Hello Brandon,

      Thank you for your reply.

      I modified by your suggestions the script, and it works a little better, but I have noticed that there is a error one step ahead.
      I'm trying to SUM volumes of five Bars of 1- Minute and present on a Bar 5- Minute, without any condition, and I get incorrect values. (Please find the attached Screenshot and Script below)
      In the Output window appears the "Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array"

      So I have read a lot about the reason that could be cause to this error, "GetValueAt", "IsValidDataPoint", "Reset", "MaximumBarsLookBack", "Array" etc.
      But I didn't success to resolve it.

      Please your advise.

      Thank you in advanced.

      Marom


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


      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class aaab : Indicator
      {
      private Series<double> mySeries;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Indicator here.";
      Name = "aaab";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      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;
      AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Bar, "aaab");
      }


      else if (State == State.DataLoaded)
      {
      mySeries = new Series<double>(this, MaximumBarsLookBack.Infinite);
      }

      else if (State == State.Configure)
      {
      AddDataSeries(BarsPeriodType.Minute, 1);
      }
      }

      protected override void OnBarUpdate()
      {



      if (CurrentBars[0] < 10 && CurrentBars[1] < 10)
      return;


      mySeries[0] = Volumes[1][0];

      if(CurrentBar % 5 == 0)
      mySeries.Reset();


      if(mySeries.IsValidDataPoint(0))

      Values[0][0] = SUM(mySeries, 5)[0];

      Print("Vol values of added series: " + Volumes[1][0] + " | " + Volumes[1][1] + " | " + Volumes[1][2] + " | " + Volumes[1][3] + " | " + Volumes[1][4]);
      Print(" Last 5 volumes added together: " +(Values[0][0]) + " | "+ Time[0]);
      //Print("values series: " + Values[1][0] + " | " + Values[1][1] + " | " + Values[1][2] + " | " + Values[1][3] + " | " + Values[1][4]);
      }
      }
      }​
      Attached Files
      Last edited by marommos; 02-19-2023, 01:49 PM.

      Comment


        #18
        Hello Marom,

        Thanks for your note.

        "Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array"

        This error message indicates that you are accessing an invalid index or BarsAgo value somewhere in your script.

        To find the exact line of code causing the error, debugging prints would need to be added to the script that prints out the indexes you are referencing throughout your script, the CurrentBar, and the current Time. You must debug the script to determine the exact line of code causing the error.

        Note the index you are referencing must be less than the CurrentBar.

        Below is a link to the help guide on this specific example of an indexing error.
        https://ninjatrader.com/support/help...nough_bars.htm

        Also, below are links to the help guide on CurrentBar and CurrentBars.
        http://ninjatrader.com/support/helpG...currentbar.htm
        https://ninjatrader.com/support/help...urrentbars.htm

        Indexing errors can also occur when using an invalid index with arrays or collections.
        Below are public links to 3rd party educational sites on arrays and index out of range errors.


        What was the exact index you tried to use that was invalid?
        Did you ensure the Count of that object is equal to or greater than the index used?

        Let me know if I may assist further.​
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #19
          Hello Brandon,

          Thank you for your response.

          I have read all the links before, and all the things are already implemnted in the script.

          About your question, I want to use all the index for SUM the volumes for the bars in the chart 5 Minue (for a bar [0][0]: [1][0],[1][1],[1][2],[1][3],[1][4] after that, for a bar [0][1]: [1][0],[1][1],[1][2],[1][3],[1][4] etc.)
          In a simply way, for now, I want to SUM the volume bars in 1- Minute, and present on bars in 5- Minute. as wrriting in the script.

          I think we are spending precious time. I return on the things again and again in the previous posts, we are in post #19 and I think I have stated all the problems clearly. this is very important for me to fix it. I will be very happy if this post will move to other team member.

          Thank you,

          Marom
          Last edited by marommos; 02-20-2023, 09:58 AM.

          Comment


            #20
            Hello Marom,

            This is Chelsea the lead of the NinjaScript Engineering Support team stepping in for Brandon.

            Brandon is correct here that you want to use prints to understand the behavior and the invalid index.

            The issue is an invalid index.

            Adding a print of CurrentBars[0] and CurrentBars[1] and BarsInProgress will tell us what series is processing, and how many bars have been processed for each series.

            Print(string.Format("{0} | BarsInProgress: {1}, CurrentBars[0]: {2}, CurrentBars[1]: {3}", Time[0], BarsInProgress, CurrentBars[0], CurrentBars[1]));

            Place this line above the line causing the error, and the output will likely tell us that CurrentBars[1] is greater than 10 and CurrentBars[0] is less than 10.

            With the condition:
            if (CurrentBars[0] < 10 && CurrentBars[1] < 10)

            This will be true only if both CurrentBars[1] is less than 10 AND CurrentBars[0] is less than 10.

            With the condition:
            if (CurrentBars[0] < 10 || CurrentBars[1] < 10)

            This will be true if either CurrentBars[1] is less than 10 OR CurrentBars[0] is less than 10.

            As we need both to be above 10, we need to return if EITHER is less than 10.


            To confirm this is the issue, please add the print and provide the output so that we may confirm.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              Hello Chelsea,

              Thank you about your explanation. I hope that I understood your instructions.
              Please find the txt Output and script export.
              Please pay attention, I have modified in the condition the value to 5 (if (CurrentBars[0] < 5 && CurrentBars[1] < 5))
              Right now, the value in bars 5- Minutes doesn't make sense.

              I would like to hear from you how to forward from here


              Thank you very much,

              Marom
              Attached Files
              Last edited by marommos; 02-20-2023, 01:43 PM.

              Comment


                #22
                Hello marommos,

                if (CurrentBars[0] < 5 && CurrentBars[1] < 5)

                You are still using && meaning CurrentBars[0] must be less than 5 AND CurrentBars[1] must be less than 5.

                In post # 20 I am suggesting you use || which means OR. CurrentBars[0] is less than 5 OR CurrentBars[1] is less than 5, then return.


                From your output:
                14/02/2023 11:07:00 | BarsInProgress: 1, CurrentBars[0]: 0, CurrentBars[1]: 6

                CurrentBars[0] is 0, CurrentBars[1] is 6. If you are requiring both to be less than 5 to return, then this will not return as CurrentBars[1] is not less than 5. Using an index of Closes[0][1] will result in an indexing error as CurrentBars[0] is 0.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Hello Chelsea,

                  You right, this is my mistake.
                  I have modified it to: if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
                  The values remained as it is.

                  Please find the updated attached txt Output and script export.

                  Thank you,

                  Marom
                  Attached Files

                  Comment


                    #24
                    Hello Marom,

                    The index error appears to be resolved.

                    I am seeing on every BarsInProgress 0 update and every BarsInProgress 1 update, you are saving Volumes[1][0] to mySeries[0], which is synchronized with the primary series only. (Meaning mySeries will have 1 slot for every primary bar.)

                    Are you having a further issue?

                    Are you wanting mySeries to be synchronized to BarsArray[1]?

                    new Series<double>(BarsArray[1], MaximumBarsLookBack.Infinite);
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Hello Chelsea,

                      Thank you for your quickly response.

                      Right now I want only to resolve this issue, after that I will add other objects.
                      And I think, yes. I want that mySeries will be synchronized to BarArray[1]. I don't know if it is the right way for my purpose.

                      Finally, for this step, I want to SUM volume bars of 1- Minute and present at bars 5- Minute, actually, the values of the volume bars should be the same as the values of the indicator bars in 5- Minute chart.
                      After that, I want to add a condition, and switch the BarsPeriodType to Tick.1
                      But these will add later on. now I want to resolve the current issue.

                      Just a note, the solution to write: Values[0][0] = SUM(Volumes[1], 5)[0]; will not help, becuase I will want to add a condition for the SUM.


                      Thank you,

                      Marom
                      Last edited by marommos; 02-20-2023, 02:51 PM.

                      Comment


                        #26
                        Hello Marom,

                        Thanks for your note.

                        To synchronize mySeries to BarsArray[1], you would call BarsArray[1] when instantiating mySeries in State.DataLoaded. For example, the code might look something like this:

                        mySeries = new Series<double>(BarsArray[1]);

                        An example of this could be seen on the Series<T> help guide page linked below.

                        Series<T>: https://ninjatrader.com/support/help...t8/seriest.htm

                        SUM(Volumes[1], 5)[0]; would be the simple way to add up the last 5 Volumes of the added 1-minute data series in the script so that those 5 1-minute volume values added together equal to the volume value of the 5-minute bar. That said, please elaborate on the condition you are wanting to incorporate.

                        Please let me know if I may assist you further.
                        Last edited by NinjaTrader_BrandonH; 02-20-2023, 03:10 PM.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #27
                          Hello Brandon,

                          Yes Yes, it works!!

                          I have read a lot from your guides and I tried to add before the line: mySeries = new Series&lt;double&gt;(BarsArray[1]); , but I didn't success to create a right script.

                          Now I can to proceed with the script and I will be very happy for your advice when there will be.

                          Thank you very much,

                          Marom​

                          Comment


                            #28
                            Hello Brandon,

                            I finished to write the final script, and it looks good. (attached below)
                            But I have question about a synchronization between a number of the Ticks that create (each 1- Minute, 5 Minute, etc.) to the bar period that choosed by the user in the chart window (actually the primary series, usually Minutes charts).

                            For excample, assume that I want to SUM the ticks volumes each 5- Minutes, and at 21:00-21:05 there are 1500 ticks, and 21:05-21:10 there are 2000 ticks, so the Period in SUM syntax (Values[0][0] = SUM(mySeries, Period)[0]; ) should be change accordingly.
                            Once the SUM shall contain in the calculate 1500 ticks, and once 2000 ticks.
                            In other words, how could I create a variable period for SUM?
                            I tried create Arr that count the number of ticks each current bar, but it's impposible to put a index value (from the Arr) in the SUM syntax (Values[0][0] = SUM(mySeries, Period)[0]; )​ (please find in the attached script)


                            I will be happy if you will direct me for a solution.

                            Thank you,

                            Marom
                            Attached Files
                            Last edited by marommos; 02-21-2023, 06:59 AM.

                            Comment


                              #29
                              Hello Marom,

                              Thanks for your note.

                              In the script you shared, I see that you are assigning mySeries the current Volume value of the added 1-Tick series (Volumes[1][0]) multiplied by the current volValue (volValue[0]).

                              Then, you are using Values[0][0] = SUM(mySeries, 10)[0]; where 10 is being used for the SUM() Period value.

                              This means that SUM() will add together the last 10 values from mySeries and assign the result to the plot.

                              What you are wanting to do is accumulate the number of Ticks during the 5-minute bar and use that value for the Period parameter when calling SUM(). Is that correct?

                              You could consider implementing a counter in your script that accumulates the number of ticks that occur on the primary series. To start the counter over at 0 on a new bar, you could use IsFirstTickOfBar. See the attached example script demonstrating how a tick counter could be made.

                              Note that the way the example works is that once the currently forming bar closes, the number of ticks for that closed bar will be drawn on the bottom-right corner of the chart. It also prints out the number of ticks to a New > NinjaScript Output window.

                              The commented-out Print statement and Draw.TextFixed() method could be uncommented to see the tick counter count the number of ticks on the currently forming bar. Note that if you uncomment these lines of code, you may want to comment out the Print statements and Draw.TextFixed() method within the IsFirstTickOfBar condition.

                              IsFirstTickOfBar: https://ninjatrader.com/support/help...ttickofbar.htm

                              Please let me know if I may assist further.​​
                              Attached Files
                              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                              Comment


                                #30
                                Hello Brandon,

                                Thank you very much for your support.

                                I think that I didn't understand. the values of ticks in Output window are 0 and 1 throughout the result (Please find the txt file attached), meaning I didn't see any counting, the same result was recieved in the text on the bottom- right corner.

                                But let's assume we reached the desired values, where I put that in the SUM line?
                                If I'm put int or double object in the Period parameter rubric the script isn't compile.

                                Please your advise.

                                Thanks in advanced,

                                Marom
                                Attached Files
                                Last edited by marommos; 02-21-2023, 05:28 PM.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                633 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                364 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                105 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                567 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                568 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X