View Full Version : Array question...
funk101
05-17-2007, 03:39 PM
How do I do initialize an array dynamically? I don't know how many placeholders I'll need.
private double[] myArray;
myArray = new double[??];
Also:
I need to re-init the array on new bar:
I need a suggestion on best way to do this. Below is not working.
protected override void OnBarUpdate()
{
Print(Bars.PercentComplete*100);
if (Bars.PercentComplete*100 <= 0)
{
Print("First tick");
myHits = new double[20];
i = 0;
}
myHits[i] = Close[0];
i++;
for (int j=0;j<myHits.Length;j++)
{
Print("myHits["+j+"] = "+myHits[j]);
}
Plot0.Set(Close[0]);
}
NinjaTrader_Ray
05-17-2007, 03:57 PM
You can't.
Information on arrays - http://msdn2.microsoft.com/en-us/library/system.array.aspx
You can check out an ArrayList which is a dynamic array - http://msdn2.microsoft.com/en-us/library/system.collections.arraylist.aspx
NinjaTrader_Ray
05-17-2007, 04:05 PM
I should add. If you go down the path of using an ArrayList, here are some pointers -
- Make sure you override the Dispose() method and set the variable holding the ArrayList to null
- ArrayList hold objects of type object meaning, you can stuff anything you want in them, this means when you reference data being held by the ArrayList you need to cast it to the correct data type.
private ArrayList al = new ArrayList();
al.Add("NinjaTrader");
Print((string) al[0]);
See the following for information on the Dispose() method - http://www.ninjatrader-support.com/HelpGuideV6/Dispose.html
funk101
05-17-2007, 04:05 PM
I'll check it. Please see this new post about re-initing() below. I added another question.
NinjaTrader_Ray
05-17-2007, 04:18 PM
You can create a new array increase in size and copy the content of the old one into the new one, see the DOC reference I sent you.
If you need a way to store data per bar meaning, one array element for each bar on a chart I suggest using our DataSeries class.
http://www.ninjatrader-support.com/HelpGuideV6/DataSeriesObject.html
funk101
05-17-2007, 04:25 PM
if (Bars.PercentComplete*100 >= 98)
{
Print("First tick");
myHits = new ArrayList();
i = 0;
}
funk101
05-17-2007, 04:30 PM
Ok, what I want to do is when the market keeps testing a resistance, record how many times it hits resistance. Then on next bar, wipe clean the ArrayList(), and start again. So, I don't think I want to have arrays of arrays, just per bar, and reinit().
NinjaTrader_Ray
05-17-2007, 04:44 PM
Then an array is not the right approach. All you need is a variable that increments.
private int hitTest = 0;
Within OnBarUpdate()
if (FirsTickOfBar)
hitTest = 0;
else
hitTest++; // This increments the value by 1
funk101
05-17-2007, 05:21 PM
Yeah that's cool, I thought 'bout that too, and I have that in place, however, I could have some control over what I want to do with the info if I store it in an array, the storing is fine, however, I can't get it to reinit() when if (FirstTickOfBar) {} is called:
#region variables
private double[] myHits;
#endregion
protected override void OnBarUpdate()
{
if (FirstTickOfBar)
{
i = 0;
hitTest = 0;
Print("First bar");
myHits = new double[50];
}
else
{
hitTest++;
myHits[i] = Close[0];
Print("My hits["+i+"] = "+myHits[i]);
i++;
}
}
NinjaTrader_Ray
05-17-2007, 05:51 PM
Understood. The MSDN reference I provided on arrays is your best bet to figure out what is going wrong.
funk101
05-17-2007, 06:10 PM
Yeah, one would think :) However that MSDN site can be pretty unfriendly. For instance, I've been swimming in that array section for about an hour, and still can't figure out why that won't reinit;
Oh well.
funk101
05-17-2007, 06:21 PM
I was declaring an array of 50. Well, it needed to write passed that. So, when it did, on next bar app would fail. I upped the value to 5000 just to see and it works fine. So, I guess I should now figure out the ArrayList() thingy, since it truly needs a dynamic place holder.
funk101
05-17-2007, 06:33 PM
Yeah, that's the ticket. I like how it assigns 4 chunks at a time. Then I Dispose() with myList.Clear(); Thanks for the pointer.(no programming pun intended) ;)
NinjaTrader_Ray
05-18-2007, 07:08 AM
Great.
Don't forget to dispose of the object by overriding the Dispose() method of the indicator re: Post #3.
VTtrader
01-21-2009, 07:37 AM
You might also try a Generic List, from what I've been reading it has better performance than ArrayList.
http://blogs.msdn.com/joshwil/archive/2004/04/13/112598.aspx
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Just another consideration, I'm just now playing around with them, so I don't know from experience yet.
VT