![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
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]); }
Last edited by funk101; 05-17-2007 at 03:57 PM.
Reason: Add second part of question
|
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
You can't.
Information on arrays - http://msdn2.microsoft.com/en-us/lib...tem.array.aspx You can check out an ArrayList which is a dynamic array - http://msdn2.microsoft.com/en-us/lib...arraylist.aspx
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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. Code:
private ArrayList al = new ArrayList();
al.Add("NinjaTrader");
Print((string) al[0]);
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#4 |
|
Senior Member
|
I'll check it. Please see this new post about re-initing() below. I added another question.
|
|
|
|
|
|
#5 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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/H...iesObject.html
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Senior Member
|
if (Bars.PercentComplete*100 >= 98)
{ Print("First tick"); myHits = new ArrayList(); i = 0; } |
|
|
|
|
|
#7 |
|
Senior Member
|
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().
|
|
|
|
|
|
#8 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Then an array is not the right approach. All you need is a variable that increments.
Code:
private int hitTest = 0;
Within OnBarUpdate()
if (FirsTickOfBar)
hitTest = 0;
else
hitTest++; // This increments the value by 1
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Senior Member
|
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++; } } |
|
|
|
|
|
#10 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Understood. The MSDN reference I provided on arrays is your best bet to figure out what is going wrong.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Senior Member
|
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. |
|
|
|
|
|
#12 |
|
Senior Member
|
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.
|
|
|
|
|
|
#13 |
|
Senior Member
|
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)
|
|
|
|
|
|
#14 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Great.
Don't forget to dispose of the object by overriding the Dispose() method of the indicator re: Post #3.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Feb 2008
Location: www.integratedtradingtech.com
Posts: 270
Thanks: 1
Thanked 79 times in 64 posts
|
You might also try a Generic List, from what I've been reading it has better performance than ArrayList.
http://blogs.msdn.com/joshwil/archiv...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 |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NT 6 question | miked11 | Charting | 0 | 02-14-2007 10:05 AM |