Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar Score Over N Bars

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

  • NinjaTrader_PatrickH
    replied
    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.

    Leave a comment:


  • aventeren
    replied
    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?

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    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.

    Leave a comment:


  • aventeren
    replied
    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

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    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.

    Leave a comment:


  • eDanny
    replied
    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

    Leave a comment:


  • aventeren
    started a topic Bar Score Over N Bars

    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

Latest Posts

Collapse

Topics Statistics Last Post
Started by CarlTrading, 03-31-2026, 09:41 PM
1 response
152 views
1 like
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 04-01-2026, 02:41 AM
0 responses
89 views
1 like
Last Post CarlTrading  
Started by CaptainJack, 03-31-2026, 11:44 PM
0 responses
133 views
2 likes
Last Post CaptainJack  
Started by CarlTrading, 03-30-2026, 11:51 AM
0 responses
127 views
1 like
Last Post CarlTrading  
Started by CarlTrading, 03-30-2026, 11:48 AM
0 responses
107 views
0 likes
Last Post CarlTrading  
Working...
X