NinjaTrader Support Forum  
X

Attention!

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


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 05-17-2007, 03:39 PM   #1
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default Array question...

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
funk101 is offline  
Reply With Quote
Old 05-17-2007, 03:57 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

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
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 04:05 PM   #3
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

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]);
See the following for information on the Dispose() method - http://www.ninjatrader-support.com/H...6/Dispose.html
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 04:05 PM   #4
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default Hmm, that would be nice...

I'll check it. Please see this new post about re-initing() below. I added another question.
funk101 is offline  
Reply With Quote
Old 05-17-2007, 04:18 PM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

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
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 04:25 PM   #6
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default Can I just do this...

if (Bars.PercentComplete*100 >= 98)
{
Print("First tick");
myHits = new ArrayList();

i = 0;
}
funk101 is offline  
Reply With Quote
Old 05-17-2007, 04:30 PM   #7
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default Somethings not right, getting system out of memory errors...

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().
funk101 is offline  
Reply With Quote
Old 05-17-2007, 04:44 PM   #8
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

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
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 05:21 PM   #9
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default Um, well...

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++;
}
}
funk101 is offline  
Reply With Quote
Old 05-17-2007, 05:51 PM   #10
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Understood. The MSDN reference I provided on arrays is your best bet to figure out what is going wrong.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 06:10 PM   #11
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default uh...

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 is offline  
Reply With Quote
Old 05-17-2007, 06:21 PM   #12
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Default k, I figured it out...just for the record...

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 is offline  
Reply With Quote
Old 05-17-2007, 06:33 PM   #13
funk101
Senior Member
 
Join Date: Jan 2006
Location: Margate, Florida, USA
Posts: 426
Thanks: 0
Thanked 2 times in 2 posts
Send a message via AIM to funk101
Talking ArrayLists() rule!

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)
funk101 is offline  
Reply With Quote
Old 05-18-2007, 07:08 AM   #14
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Great.

Don't forget to dispose of the object by overriding the Dispose() method of the indicator re: Post #3.
NinjaTrader_Ray is offline  
Reply With Quote
Old 01-21-2009, 07:37 AM   #15
VTtrader
Senior Member
 
Join Date: Feb 2008
Location: www.integratedtradingtech.com
Posts: 270
Thanks: 1
Thanked 79 times in 64 posts
Default

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
VTtrader is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NT 6 question miked11 Charting 0 02-14-2007 10:05 AM


All times are GMT -6. The time now is 06:29 AM.