NexusFi: Find Your Edge


Home Menu

 





Bar Score Over N Bars; Array Question


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one aventeren with 4 posts (0 thanks)
    2. looks_two vegasfoster with 2 posts (2 thanks)
    3. looks_3 traderwerks with 1 posts (1 thanks)
    4. looks_4 shodson with 1 posts (4 thanks)
    1. trending_up 2,904 views
    2. thumb_up 7 thanks given
    3. group 5 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread

Bar Score Over N Bars; Array Question

  #1 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202

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

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
67 thanks
Battlestations: Show us your trading desks!
48 thanks
NexusFi site changelog and issues/problem reporting
47 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #3 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202


I've been thinking about this, and maybe I could also use a DataSeries to store the barvalue numbers, but if I were to do this, I'm not sure how I would sum the barvalues from N bars ago.

But before I get to the DataSeries summing part, would this be the correct way to implement and get the barvalues to the DataSeries?

//Variables
private DataSeries series; // Creates new DataSeries

//Initialize()
series = new DataSeries;

//OnBarUpdate()
series[0] = barvalue; // Populate the current series slot with the barvalue

Would this be a start?

Started this thread Reply With Quote
  #4 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844

Isn't there a sum function you can use?

Reply With Quote
The following user says Thank You to vegasfoster for this post:
  #5 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202


vegasfoster View Post
Isn't there a sum function you can use?

So I think shifting to a DataSeries approach might be best, because each bar will have a unique score attached to it.

The summing challenge is how to sum over a specified number of N bars. That's the piece I'm confused on.

Any thoughts?

Started this thread Reply With Quote
  #6 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465


aventeren View Post
So I think shifting to a DataSeries approach might be best, because each bar will have a unique score attached to it.

The summing challenge is how to sum over a specified number of N bars. That's the piece I'm confused on.

Any thoughts?


Summation (SUM)

Math. A gateway drug to reality.
Reply With Quote
The following user says Thank You to traderwerks for this post:
  #7 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844

Yes, you will need to create a dataseries then use the sum function to get the totals.

Just a thought, but you may look at analyzing negative and positive values separately and/or calculating a ratio of the two, then each dataseries would be something like,

dataseriespositive = Close[0]>Open[0] ? Calculation : null;
dataseriesnegative = Close[0]<Open[0] ? Calculation : null;

Now go make us rich!

Reply With Quote
The following user says Thank You to vegasfoster for this post:
  #8 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202


traderwerks View Post

So I got this figured out how to use a DataSeries. Here is the code I used:

//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!!!!

So with the above, I am able to sum my bar scores of N bars.

My next challenge is going to be plotting these values. Do make the seriessum values publicly available, in Properties I used a:

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

The above should make the Seriessum values available for use in another indicator.

So I created a new indicator, add referenced the IndicatorName (generic name) in the new indicator--and added in a new Plot.

//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

Started this thread Reply With Quote
  #9 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709

LINQ and generic collections make this easier.

in the "using" section up top, include these statements

 
Code
using System.Collections;
using System.Collections.Generic;
using System.Linq;
declare a class-level sorted list to store your scores

 
Code
SortedList<DateTime, int> _scores = null;
then in OnBarUpdate()

 
Code
if (_scores == null)
    _scores = new SortedList<DateTime, int>();
compute your score and add it to the sorted list, using the time of the bar as your index by which it will keep the list sorted

 
Code
int score = {0, 1 or 2, whatever logic you use to compute the score}
_scores.Add(Time[0], score);
To ensure the list has no more than the last n values, just remove the first one (the oldest one)

 
Code
if (_scores.Count > n)
    _scores.RemoveAt(0);
Then you can use LINQ to average all of the values in the list

 
Code
double average = _scores.Values.Average();

Here's a sample I wrote in Visual Studio which demonstrates this.

 
Code
SortedList<DateTime,int> list = new SortedList<DateTime,int>();
list.Add(new DateTime(2000,1,1), 1);
list.Add(new DateTime(2000,1,2), 0);
list.Add(new DateTime(2000,1,3), 2);

double avg = list.Values.Average();    // =1.0

list.RemoveAt(0);                      // removes the "1", list now has the "0" and "2"
list.Add(new DateTime(2000,1,4), 2);   // list is now 0,2,2

avg = list.Values.Average();           // = 1.333

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
The following 4 users say Thank You to shodson for this post:





Last Updated on June 21, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts