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

SUM of Volumes with a condition by period

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

    SUM of Volumes with a condition by period

    hello,

    Does anyone know which script will present the Volumes Summuation of the inner bars for the cuurent Bar but with calculating condition?

    For excample, I want to present a Bar in chart 5 Min (customized in the properties by the user) that built from volumes samuuation of the five bars 1 Min, with the next condition:
    If the volume is possitive (Close > Open, 1 Min) add the volume to the Bar 5 Min
    And if the volume is negative (Close < Open, 1 Min) subtract the volume from the Bar 5 Min
    (Else sign volume [0] = sign volume [1])

    i.e, there are five Volume Bars in chart 1 Min with the values: 30, -35, 5, 15, 10 ,so I expect to see a Bar in chart 5 Min on 25.

    I have tried a lot of variations, like "Multi-Time Frame & Instruments", "Bars Type", and even looking at "VolumeBarsType" script, but unsuccessfully.

    I will be grateful to you for your support.

    Marom
    Last edited by marommos; 02-05-2023, 06:49 AM.

    #2
    Hello Marom,

    Thanks for your note.

    I am not certain that I understand exactly what you are wanting to accomplish.

    If the script is running on a 5-Minute chart, you could access the current volume of that 5-minute bar by using Volume[0]. If you use Calculate.OnBarClose, this would return the current volume at the close of the bar.

    Volume[int barsAgo]: https://ninjatrader.com/support/help...ies_volume.htm
    Calculate: https://ninjatrader.com/support/help.../calculate.htm

    A 1-Minute secondary bars object could be added to the script using AddDataSeries() and Volumes[1][0] could be used to get the current volume of the added 1-minute data series. To access previous volume values, a different barsAgo value would be passed in when calling Volumes. For example, to access the volume value of the added series 5 bars ago, you could use Volumes[1][5].

    Volumes[int barSeriesIndex][int barsAgo]​: https://ninjatrader.com/support/help...es_volumes.htm
    AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

    See this help guide page to gain an understanding of working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

    Let me know if I may assist further.
    Last edited by NinjaTrader_BrandonH; 02-06-2023, 10:03 AM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon,

      Thank you for your quick response.


      This is exactly what I'm looking for
      [A 1-Minute secondary bars object could be added to the script using AddDataSeries() and Volumes[1][0] could be used to get the current volume of the added 1-minute data series. To access previous volume values, a different barsAgo value would be passed in when calling Volumes. For example, to access the volume value of the added series 5 bars ago, you could use Volumes[1][5].

      Volumes[int barSeriesIndex][int barsAgo]​: https://ninjatrader.com/support/help...es_volumes.htm
      AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

      See this help guide page to gain an understanding of working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm]



      I have red all the above attached material again and again, but I didn't success to create the right script, this the reason I wrote here.

      Attached below one of many script that I wrote, for excample, The Period in the Script changes manually, but this isn't the problem,
      My problem that is the script present the SUM of bars but only in 1 Min Chart. and when I'm switch to other chart, 5 Min, 30 Min, etc. the Indicator present nothing (it disappear).
      I tried to write code with what you suggested above (Volumes[int barSeriesIndex][int barsAgo]​ and AddDataSeries()​), but unsuccessfully.

      I really will appreciate, if you will direct me to the right and complete script.

      Thank in advance

      Marom






      public class AMYNEWVOLMIN : Indicator
      {

      private Series<double> myDoubleSeries;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      AddPlot(new Stroke(Brushes.Orange, 1), PlotStyle.Bar, "Average Range");

      Name = "AMYNEWVOLMIN";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      }

      else if (State == State.DataLoaded)
      {
      myDoubleSeries = new Series<double>(this);
      }

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

      }
      }

      protected override void OnBarUpdate()
      {

      if (CurrentBar == 0)

      {
      myDoubleSeries[0] = 1;
      return;
      }

      if (BarsInProgress == 1)
      {

      if (Close[0] > Open[0])
      {
      myDoubleSeries[0] = Volume[0];
      }
      else if (Close[0] < Open[0])
      {
      myDoubleSeries[0] = (Volume[0])*-1;
      }

      Value[0] = SUM(myDoubleSeries, 5)[0];

      }

      }


      region Properties

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
      public int Period
      { get; set; }

      #endregion
      }
      }​
      Last edited by marommos; 02-05-2023, 06:32 PM.

      Comment


        #4
        Hello Marom,

        Thanks for your note.

        In the code you shared, you are checking if BarsInProgress == 1 and then assign Volume[0] to your myDoubleSeries variable.

        Since you are calling Volume[0] in a BarsInProgress == 1 condition, only the volume of the added 1-minute series will be saved to your myDoubleSeries variable.

        To get the current Volume of the primary series the script is running on, you must call Volume[0] when BarsInProgress == 0. Or, call Volumes[0][0] in a BarsInProgress == 1 condition.

        See this help guide page for more information: https://ninjatrader.com/support/help...arsninjascript

        I have attached a simple example script demonstrating how to print out the Volume of the primary series the script is running on and the Volume of an added secondary series. Prints will appear in a New > NinjaScript Editor window.

        Please let me know if I may assist further.
        Attached Files
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hello Brandon,

          Thank you very much for your support,

          In the Scrupt You have attached above, there is the same problem as my code. the code present only the data of Primary series, but when I'm moving to other chart, 5 MIN, 15 MIN, etc. appears a note with error.
          please find the attached Screenshot here.

          Thank you for your support continuation.

          Marom

          Attached Files

          Comment


            #6
            Hello Marom,

            Thanks for your note.​

            When adding the example script on a 5-minute chart and noting the Output window, I am seeing prints occur printing out the Volume of the primary series and secondary series at the close of the added secondary series as expected. Note that since we print the volume of the primary and secondary series inside the BarsInProgress == 1 condition, the print values will update at the close of each 1-minute bar.

            See the attached screenshot: https://brandonh-ninjatrader.tinytak...NF8yMDk0MTU4NQ

            The error you shared does not occur when I add the indicator to a 5-minute chart or 15-minute chart.

            This error will be displayed if you try to use a BarsAgo when it is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar. If you load more days on the chart, does the error persist?

            If you want to print out the primary series Volume at the close of the primary series, you could simply use Volume[0] in a condition that checks if BarsInProgress == 0.

            if (BarsInProgress == 0)
            Print("Primary series volume is " + Volume[0]);


            If you want to print out the secondary series Volume at the close of the secondary series (1-minute bar), you would check if BarsInProgress == 1 and call Volume[0].

            if (BarsInProgress == 1)
            Print("Secondary series volume is " + Volume[0]);


            If you want to print out the primary series and secondary series Volume at the close of each primary series bar, you wouldn't need a BarsInProgress condition and could simply use Volumes[0][0] and Volumes[1][0].

            //OnBarUpdate
            Print("Primary series volume is " + Volumes[0][0]);
            Print("Secondary series volume is " + Volumes[1][0]);


            If you want to print out the primary series Volume and secondary series Volume at the close of each 1-minute bar you would use the approach in the example script attached in my previous post.

            It is very important to review this help guide page to gain a full understanding of how to access values in a Multi-Timeframe NinjaScript: https://ninjatrader.com/support/help...nstruments.htm

            Let me know if I may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hello Brandon,

              Thank you for your response again

              1. Yes, I loaded more days and the error exist yet.
              2. I will be very happy to get result as image you attached above, and from there I will continue on self. now, with your script, I get the right result, but only chart 1 Min, in the other chart I get Error as I shown before.
              3. I have red a lot about "Multi-Time Frame & Instruments" before I wrote here, but, after when I created the script according this guide, I got Error as I'm get now.
              4. I have attached 2 screenshots. first is screenshot the script as opened in my station. and second is the result in the "Ninjascript output" in chart 1 Min.

              I will appriciate if you colud direct me to result as you got in your last Screenshot that attached above.
              A note: all the Indicator I created till now, except that we are try to create, opened well.

              Thanks in Advance,

              Marom

              Attached Files

              Comment


                #8
                Hello Marom,

                Thanks for your note.​​

                See this demonstration video showing the steps and settings I am using to test the MaromTest script shared in post # 4.

                Demonstration video: https://brandonh-ninjatrader.tinytak...NF8yMDk0NzI2Mg

                If you use the same exact steps and settings to test the MaromTest script, does the error appear?

                If the error appears, change the CurrentBars check to be the same as the code below and retest.

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

                Make Sure You Have Enough Bars: https://ninjatrader.com/support/help...nough_bars.htm

                Please let me know if I may further assist.
                Last edited by NinjaTrader_BrandonH; 02-20-2023, 10:17 AM.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Brandon,

                  Thank you for your response and support.

                  Actually, I have Removed the Script and imported again and it worked.
                  But, this Script you sent me is very simple, means, there is'nt any Object (int, double, etc.).
                  For excample, I added to your script, a very simple object (myDoubleSeries[0] = Volume[1][0]), and I got Error during the compilation. please find attached below the Script and Screenshot the error.
                  But if, I'm rmoving the [1] from Volume [1][0], the script is compile.

                  I have red a lot and I tried many variations, I red the part "Accessing the Price Data" under "Multi-Time Frame & Instruments" again and again, before I wrote here, write there about accessing through [0][0], but I did'nt success to create the right script.

                  So, what that actually I want to find is the SUM of the secondary Volume[1][0] for the current bar (the primary), with a condition that implemented on the secondary series.
                  In other words:

                  if (Close[1][0] > Open[1][0])
                  {
                  myDoubleSeries[1][0] = Volume[1][0];
                  }
                  else if (Close[1][0] < Open[1][0])
                  {
                  myDoubleSeries[1][0] = (Volume[1][0])*-1;
                  }

                  Value[0] = SUM(myDoubleSeries, Period)[1][0];​



                  So acttualy I want to see a current bar that contained a SUM of the inner bars volumes (secondary series), but with a condition. almost like a regular Volume. that in fact present the SUM of the inner bars volumes to the primary bar. but i'm loocking that with a condition.


                  I will very appriciate if you will direct me to the desired result or even to give me excample a script that showing an accessing to a secondry series with a condition.

                  Thank you in advanced,

                  Marom



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



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

                  else if (State == State.DataLoaded)
                  {
                  myDoubleSeries = new Series<double>(this);
                  }
                  else if (State == State.Configure)
                  {
                  AddDataSeries(BarsPeriodType.Minute, 1);
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if (CurrentBars[0] < 10 && CurrentBars[1] < 1)
                  return;

                  if (BarsInProgress == 1)
                  {
                  //Get the primary series Volume value when the added secondary 1-minute series is processing
                  Print("Prmiary series volume: " + Volumes[0][0]);
                  if (Close[0] > Open[0])
                  {
                  myDoubleSeries[0] = Volume[1][0];
                  }

                  //Value[0] = SUM(myDoubleSeries, 2)[0];
                  //Get the added secondary series Volume value
                  //Since we call Volume[0] in a BarsInProgress 1 condition, this returns the Volume of the secondary series
                  Print("Secondary series volume: " + Volume[0]);
                  }
                  }

                  }
                  }​
                  Attached Files
                  Last edited by marommos; 02-09-2023, 02:16 AM.

                  Comment


                    #10
                    Hello Marom,

                    Thanks for your note.

                    Yes, the example script is a simple script demonstrating how you could access Volume values from the primary series and added secondary series.

                    The error is caused by calling incorrect syntax when calling myDoubleSeries[0] = Volume[1][0];. Volume[] does not have a barSeriesIndex argument available to use. The plural should be used if you want to use a barsSeriesIndex argument (ex: Volumes[1][0]).

                    Volumes: https://ninjatrader.com/es/support/h...es_volumes.htm

                    Let me know if I may assist further.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Brandon,

                      Yes, I have corrected and now it compile. thank you, but I don't get the right result in my script.
                      I want to get a Bar 5 Min, for excample, that SUM the volumes of the bars 1 Min, with the next condition:
                      if (Closes[1][0] > Closes[1][1])
                      {
                      myDoubleSeries[0] = Volumes[1][0];
                      }
                      else if (Closes[1][0] < Closes[1][1])
                      {
                      myDoubleSeries[0] = (Volumes[1][0])*-1;​

                      And now I'm get other value.
                      I understand that I'm doing somthing wrong, so I will be glad if you could help me to resolve this issue. attached below my script.

                      Thank you in advanced,

                      Marom


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

                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                      public class AAMYNEWVOLMIN : Indicator
                      {
                      private Series<double> myDoubleSeries;

                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      AddPlot(new Stroke(Brushes.Orange, 1), PlotStyle.Bar, "MyTickVolumes");

                      //Period = 5;
                      Name = "AAMYNEWVOLMIN";
                      Calculate = Calculate.OnBarClose;
                      IsOverlay = false;

                      }

                      else if (State == State.DataLoaded)
                      {
                      myDoubleSeries = new Series<double>(this);
                      }

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


                      }
                      }


                      protected override void OnBarUpdate()
                      {


                      if (CurrentBar == 0)
                      {
                      myDoubleSeries[0] = 1;
                      return;
                      }


                      if (BarsInProgress == 1)

                      {


                      if (Closes[1][0] > Opens[1][0])
                      {
                      myDoubleSeries[0] = Volumes[1][0];
                      }
                      else if (Closes[1][0] < Opens[1][0])
                      {
                      myDoubleSeries[0] = (Volumes[1][0])*-1;
                      }

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

                      }


                      }


                      }
                      }​
                      Last edited by marommos; 02-11-2023, 05:02 PM.

                      Comment


                        #12
                        Hello Marom,

                        Thanks for your note.

                        Volumes[1][0] will get the current volume value of the added 1-Minute bars object in your script. This is because the barSeriesIndex value being passed into Volumes is 1 which references the first added series.

                        If you are running the strategy on a 5-Minute data series, you would use Volumes[0][0] to get the current volume value of the primary 5-Minute series. A barSeriesIndex value of 1 references the primary bar object the script is running on.

                        Volumes: https://ninjatrader.com/support/help...es_volumes.htm

                        It is important to thoroughly review the Multi-Timeframe/Multi-Instrument help guide to gain a full understanding about how multi-timeframe NinjaScripts work and how to access values.

                        Multi-Timeframe/Multi-Instrument: https://ninjatrader.com/support/help...gThePriceDataI nAMultibarsninjascript

                        If your script is not behaving as expected, debugging prints must be added to the script to understand your logic's behavior.
                        https://ninjatrader.com/support/foru...121#post791121

                        Let me know if I may assist further.
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello Brandon,

                          I have chanched to the Volume [0][0], but I get smae result. I have read a lot about "Volumes" and "Multi-Time Frame & Instruments", and yet I don't success to get the right result.
                          I think that without your support I will not get the right result. and I think that what I want is one step above what is in the guide.
                          So please, tell me if you want to help me, because all the time I directed to guide that I have read many times, and I invest a lot to write and read these posts.

                          I will return again what I'm looking for,
                          I try to create a script that will SUM the secondary series volumes, and will present on a bar in primary series.
                          For excample, SUM five bars of 1 Min, and presenting them on a bar of 5 Min.
                          For excample, I have five volumes bars in 1 Min with the values: 30, -35, 5, 15, 10, and there is the next condition:
                          if (Closes[0] > Opens[0])
                          {
                          mySeries[0] = Volumes[0];
                          }
                          else if (Closes[0] < Opens[0])
                          {
                          mySeries[0] = (Volumes[0])*-1;
                          }

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

                          So, I expect to see in the bar of 5 Min the value 25.


                          Thanks in advance,

                          Marom
                          Last edited by marommos; 02-14-2023, 06:35 PM.

                          Comment


                            #14
                            Hello Marom,

                            Thanks for your notes.

                            A simple way of adding together the last 5 volumes of the added 1-minute series would be to call SUM(Volumes[1], 5)[0]; in your script.

                            This will sum up the last 5 volumes of the added 1-minute series together. See the attached example script.

                            In the attached example script, we assign SUM(Volumes[1], 5)[0]; to a plot and we print out the last 5 volume values of the added 1-minute series to the Output window. We also add a print that adds the last 5 volume values of the added 1-minute series together.

                            If we add up the last 5 volume values of the added series (Volumes[1][0] + Volumes[1][1] + Volumes[1][2] + Volumes[1][3] + Volumes[1][4]) and compare it to the value of the plot ( SUM(Volumes[1], 5)[0]), we can see that the values match.

                            Let me know if I may assist further.​
                            Attached Files
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Brandon,

                              Thank you for your reply. it make sense.
                              But there is two problems:
                              1. I don't know where to enter the condition (if close>open add the volume, and if the close < open subtract the volume from the SUM).
                              2. The script don't work in Ticks Chart. I have changed in the script to: AddDataSeries(Data.BarsPeriodType.Tick, 1), and I didn't get any value on the indicator in chart 1 Min.

                              Please, I will be glad if you will help me to resolve these issues.
                              As I told you, I had trouble reaching a solution with the help of the guide for my requirement. and I tried a lot of variations to reach the desired solution. this is the reason I'm writing here.
                              A solution to this will save me precious time in the trading and will bring me extremely benefit from the way I summarize tick volumes Today. and in addition, it will allow me to follow after the historical datas.

                              Thank you for your suuport.

                              Marom
                              Last edited by marommos; 02-15-2023, 06:28 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by junkone, Today, 11:37 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post junkone
                              by junkone
                               
                              Started by frankthearm, Yesterday, 09:08 AM
                              12 responses
                              43 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              5 responses
                              35 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by proptrade13, Today, 11:06 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              35 views
                              0 likes
                              Last Post love2code2trade  
                              Working...
                              X