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

Bar Score Over N Bars

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

    Bar Score Over N Bars

    Howdy--

    I have created an indicator that assigns each bar a unique 0, 1 or 2 "score" based on a series of logic conditions. This piece is working fine.

    I then wanted to create a running "score" of the unique bar "scores" over the previous n bars. To do this, I figured that creating an array to hold each bar's score would be best, as the size of the array could be limited with a parameter (ie, 10 bars, 50 bars, 100 bars, etc). I then wanted to sum this array values to create the score. But before I could get to summing the array values, I ran into a challenge associated with how to add and keep array values. Here is what I have so far:

    //Variables
    private int[] array; // creates int array of undefined size and without values
    private int arraysize = 50; // variable to define how many values the array will hold (50 bars in this case)

    //Initialize()
    array = new int[arraysize]; // I initialized my int type array with a size of arraysize (50 in this case)

    //OnBarUpdate()
    array[0] = barvalue; // barvalue is the generic name I'm using to populate the array's current [0] value with either a 0, 1 or 2

    I then used DrawText printed above the current bar to confirm that the barvalue and array[0] values were correct (they were). So what that meant to me is that I was correctly assigning the barvalue into the array's top position.

    I then used DrawText to confirm that the array was correctly storing the array[1] values, too--but the array[1] value is always 0.

    What am I missing about arrays that will not allow my array to keep values that I assigned to it on the previous bar? Why can't I access the array[1] values that had previously been assigned? Why is the array[1] value being reset to 0?

    After I get the array to properly hold values, I'll then need to figure out how to sum the values in the array.

    Thanks for your help!

    Aventeren

    #2
    Array value

    It seems to me you are constantly changing the value in the [0] array element only and the [1] element and so on never have anything added into them. Two ways to address this is:
    1. Check for FirstTickOfBar and then use a loop to move all the values down, [50] = [49], [49] = [48], etc. Then assign [0] the correct value once it is calculated. Sum with loop described in 2.
    2. More efficiently would be to use a DataSeries which has one element per bar and are always in sync. Just simply poke the right value into DataSeriesName[0] and it will stick with that bar forever. Then use a loop to sum up the values you want by using a counter equal to your number of bars parameter.

    Dan
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello Aventeren,

      Thank you for your post.

      Why not use a custom DataSeries to store the values for each bar? This will allow you to access the values just as you would the bar data ([0], [1], [2], etc.).

      For information on the DataSeries Class in NinjaScript please visit the following link: http://www.ninjatrader.com/support/h...ries_class.htm

      And an IntSeries may be more desirable for this scenario: http://www.ninjatrader.com/support/h...ries_class.htm

      For a reference sample on storing information in a DataSeries please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=7299

      Please let me know if I may be of further assistance.

      Comment


        #4
        Originally posted by eDanny View Post
        It seems to me you are constantly changing the value in the [0] array element only and the [1] element and so on never have anything added into them. Two ways to address this is:
        1. Check for FirstTickOfBar and then use a loop to move all the values down, [50] = [49], [49] = [48], etc. Then assign [0] the correct value once it is calculated. Sum with loop described in 2.
        2. More efficiently would be to use a DataSeries which has one element per bar and are always in sync. Just simply poke the right value into DataSeriesName[0] and it will stick with that bar forever. Then use a loop to sum up the values you want by using a counter equal to your number of bars parameter.

        Dan
        Thanks, Danny; I had looked at a DataSeries, but I wasn't sure about what the best way would be to sum N bar scores. But before I get to the DataSeries sum piece, if I were to do a DataSeries instead of an Array approach, here is what I would do:

        //Variables
        private DataSeries series; // Define a new DataSeries with name "series"

        //Initialize()
        series = new DataSeries(this); // Is this the correct way to initialize a DataSeries?

        //OnBarUpdate()
        series[0] = barvalue; // Would this be the correct way to assign the barvalue to the DataSeries?

        So before I move to the summing part, can you please confirm that the above would be the correct way to shift to a DataSeries approach?

        Thanks!

        Aventeren

        Comment


          #5
          Hello Aventeren,

          Thank you for your response.

          The correct way to assign the DataSeries in the OnBarUpdate() method is to use the following:
          Code:
          series.Set(barvalue);
          Please let me know if you have any questions.

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello Aventeren,

            Thank you for your response.

            The correct way to assign the DataSeries in the OnBarUpdate() method is to use the following:
            Code:
            series.Set(barvalue);
            Please let me know if you have any questions.
            Right on; so series.Set(barvalue) would populate the series[0] position?

            Comment


              #7
              Hello Aventeren,

              Correct, on each OnBarUpdate() the Set() method will set the current value based on what is detailed in the Set() method, in this case barvalue.

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello Aventeren,

                Correct, on each OnBarUpdate() the Set() method will set the current value based on what is detailed in the Set() method, in this case barvalue.
                Okay, so let's assume that I do this. Now I have a DataSeries's value for each bar--and I should be able to reference these values using the [0], [1], [2], ..., [100] BarsAgo framework.

                Now, how would I sum the previous N DataSeries values to create the running score--and how would I do that in such a manner as I could change N from 5 to 50 to 100 and get the correct score?

                Thanks!

                Comment


                  #9
                  One Small Step...

                  Okay, so I've correctly implemented the DataSeries, and I am able to correct assign the bar score to the DataSeries, and I have confirmed that the DataSeries is displaying the correct bar score for the [0] and [1] bars.

                  Now, how can I sum N bar scores using the values stored in the DataSeries?

                  Could I use like a For loop or something?

                  Hmmmmm.....

                  Comment


                    #10
                    Originally posted by aventeren View Post
                    Okay, so I've correctly implemented the DataSeries, and I am able to correct assign the bar score to the DataSeries, and I have confirmed that the DataSeries is displaying the correct bar score for the [0] and [1] bars.

                    Now, how can I sum N bar scores using the values stored in the DataSeries?

                    Could I use like a For loop or something?

                    Hmmmmm.....
                    So I found this: http://www.ninjatrader.com/support/f...ad.php?t=55693.

                    This may be the way...

                    I'm going to try this and report back.

                    Comment


                      #11
                      Originally posted by aventeren View Post
                      So I found this: http://www.ninjatrader.com/support/f...ad.php?t=55693.

                      This may be the way...

                      I'm going to try this and report back.
                      So I was successful on the DataSeries route. THANKS!

                      I did this by:

                      //Variables
                      private DataSeries series;
                      private int sumlength = 10; // Defines how many bars I want in my sum (10 in this case)
                      private double seriessum; // This is the variable that will hold the sum IT HAS TO BE TYPE DOUBLE BC IT WILL BE HOLDING A DATASERIES!!!

                      //Initialize()
                      series = new DataSeries(this); // Need to initialize the DataSeries

                      // OnBarUpDate()
                      series.Set(barscore); // Use .Set(variable) to populate the DataSeries (barscore is the score value assigned to each bar)
                      seriessum = SUM(series, sumlength)[0]; // This is how you sum the score over sumlength bars (10 in this case) IT IS IMPORTANT TO ADD ON THE [0] AFTER THE SUM()...So Don't forget!!!!

                      Okay, that was it.

                      Thanks to everyone for their help. Hopefully this will help others.

                      Comment


                        #12
                        Next Question: How Do I Plot the Score?

                        Okay, I now have a Bar Score Over N Bars. Now I want to plot these values in a separate panel. Can someone help me figure this one out?

                        I revealed the seriessum values in Properties by:

                        [Browsable(false)]
                        [XmlIgnore()]
                        public double Seriessum
                        {
                        get { return seriessum; }
                        }

                        So I should be able to reference this indicator and grab these values. From my understanding, I would do that in the new indicator by:

                        //Initialize()
                        Add(IndicatorName(parameter1, parameter2, etc)); // Adds the IndicatorName to the new indicator so that I can grab the public values.

                        // OnBarUpdate()
                        Add(new Plot(Color.Green, "seriessum"); // From what I understand, this defines the plot for Values[0]

                        Okay, so I think the above is right. Next, how to I actually plot the scores in this indicator?

                        Thanks! I REALLY appreciate your help.

                        Aventeren

                        Comment


                          #13
                          Originally posted by aventeren View Post
                          Okay, I now have a Bar Score Over N Bars. Now I want to plot these values in a separate panel. Can someone help me figure this one out?

                          I revealed the seriessum values in Properties by:

                          [Browsable(false)]
                          [XmlIgnore()]
                          public double Seriessum
                          {
                          get { return seriessum; }
                          }

                          So I should be able to reference this indicator and grab these values. From my understanding, I would do that in the new indicator by:

                          //Initialize()
                          Add(IndicatorName(parameter1, parameter2, etc)); // Adds the IndicatorName to the new indicator so that I can grab the public values.

                          // OnBarUpdate()
                          Add(new Plot(Color.Green, "seriessum"); // From what I understand, this defines the plot for Values[0]

                          Okay, so I think the above is right. Next, how to I actually plot the scores in this indicator?

                          Thanks! I REALLY appreciate your help.

                          Aventeren
                          You always plot the indicator by simply assigning values to the Plot/Values[0]/Value. In this case, not knowing the indicator or plot names, here is the generic code that you will use in the calling indicator.

                          Code:
                           
                          Values[0].Set(CalledIndicator([] parameters).PlotIdentifierInCalledIndicator[0]);
                          Instead of "Values[0]", you can use "Plots[0]", or just "Value".
                          Last edited by koganam; 06-20-2013, 04:21 PM.

                          Comment


                            #14
                            Originally posted by koganam View Post
                            You always plot the indicator by simply assigning values to the Plot/Values[0]/Value. In this case, not knowing the indicator or plot names, here is the generic code that you will use in the calling indicator.

                            Code:
                             
                            Values[0].Set(CalledIndicator([] paramaeters).PlotIdentifierInCalledIndicator[0]);
                            Instead of "Values[0]", you can use "Plots[0]", or just "Value".
                            Thanks for your help, Koganam.

                            Let's assume that the indicator that I am going to call is called IndicatorSource. Therefore, to add IndicatorSource to the new indicator, under Initialize I would do:

                            //Iniitalize()
                            Add(IndicatorSource()); // Adds the IndicatorSource

                            But I have a question on this, as the IndicatorSource does not have any parameters that need to be spelled out (ie, like a EMA length or moving average type, etc.). So to add the IndicatorSource to the new indicator, do I just use:

                            Add(IndicatorName());

                            or do I have to:

                            Add(IndicatorName(something)); // Where "something" is something that you can help me with?

                            Also, given in the IndicatorSource Properties that I have publicly revealed the seriessum by:

                            [Browsable(false)]
                            [XmlIgnore()]
                            public double Seriessum
                            {
                            get { return seriessum; }
                            }

                            That means that I now want to call the Seriessum values from the IndicatorSource...and then plot those in the new indicator.

                            So from what I understand, I had to add a new plot to the new indicator (also under Initialize()):

                            //Initialize()
                            Add(new Plot(Color.Green, "score")); // From what I understand, the Values[0] are assigned to this plot.

                            But this is where I run into a block, as I don't know how to place the Seriessum values from the IndicatorSource into the new "score" plot.

                            Would I use something like:

                            Value.Set(IndicatorSource.Seriessum[0]);

                            ???

                            Comment


                              #15
                              So I think I need to work on this step wise.

                              1st: Can someone please confirm that if I place this code:

                              [Browsable(false)]
                              [XmlIgnore()]
                              public double Seriessum
                              {
                              get { return seriessum; }
                              }

                              In the Properties section of the IndicatorSource code, that other indicators will be able to access the IndicatorSource's Seriessum values?

                              Before I get into adding an indicator to another indicator, I first want to make sure that I have properly revealed the Seriessum values for use.

                              Thanks,

                              Aventeren

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Stanfillirenfro, Yesterday, 09:19 AM
                              7 responses
                              51 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by TraderCro, 04-12-2024, 11:36 AM
                              4 responses
                              69 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Mindset, Yesterday, 02:04 AM
                              1 response
                              15 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by agclub, 04-21-2024, 08:57 PM
                              4 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Irukandji, Today, 04:58 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Irukandji  
                              Working...
                              X