NexusFi: Find Your Edge


Home Menu

 





How do you create a List to store variables?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one forrestang with 7 posts (0 thanks)
    2. looks_two drw112 with 3 posts (5 thanks)
    3. looks_3 Fat Tails with 2 posts (8 thanks)
    4. looks_4 cory with 1 posts (3 thanks)
      Best Posters
    1. looks_one gomi with 6 thanks per post
    2. looks_two Fat Tails with 4 thanks per post
    3. looks_3 NJAMC with 3 thanks per post
    4. looks_4 drw112 with 1.7 thanks per post
    1. trending_up 11,957 views
    2. thumb_up 25 thanks given
    3. group 6 followers
    1. forum 15 posts
    2. attach_file 2 attachments




 
Search this Thread

How do you create a List to store variables?

  #1 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047

I need to create a list, that will capture values as the session develops. Each day there will be a different number of values I need to store. I am mainly wanting to do this so that I can print the values out at the end of each session in a specific format.

I really have no idea what a list is, how it works, or anything like that, so I am needing a pretty basic, SIMPLE explanation or example of such a method in practice.

Any help would be appreciated.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
Futures True Range Report
The Elite Circle
Better Renko Gaps
The Elite Circle
Build trailing stop for micro index(s)
Psychology and Money Management
Are there any eval firms that allow you to sink to your …
Traders Hideout
 
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


You can use an ArrayList object. It is basically an array with a variable size, which allows you to add elements of various data types.

Step 1: create the array list:

 
Code
private ArrayList  myArray = new ArrayList();
You have now an empty ArrayList.


Step 2: Add elements to your array list:

 
Code
myArray.Add((double)(Input[0]));
It is important to specify the data type that you add.


Step 3: Retrieve the stored value.

 
Code
value =  (double) myArray[5];
This will retrieve the 6th value that you have added.

Tip
When adding or retrieving a value, you need to specify the data type which is stored in the array list. Without adding (double) in front of the array element, neither adding nor retrieving a value will work.


Of course you can use other data types, for example you can add (string) values.


Further information on array lists is here:

ArrayList Class (System.Collections)

C# ArrayList Tips

Reply With Quote
  #4 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090

here is code to computer average of 10 day true ranges but using Queue
 
Code
	#region Variables
		private int			period		= 10;
	
		private double SumOfDaily = 0; // Average by day
		private double[] Dailyarray;
		private Queue Dailyqueue;

		
		#endregion
	
		/// <summary>
		/// This method is used to configure the indicator and is called once before any bar data is loaded.
		/// </summary>
		protected override void Initialize()
		{
	
			Dailyqueue = new Queue(Period);
			Dailyarray = new double[100];
			Overlay				= true;
		}

		/// <summary>
		/// Called on each bar update event (incoming tick)
		/// </summary>

		protected override void OnBarUpdate()
		{
			if (CurrentBar < Period+1) 
        	return;
			if (Bars.SessionBreak)
        	// Reset or store some values here 
			{
if ((PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorOpen[0]) < 
	Math.Abs(PriorDayOHLC().PriorOpen[0] - PriorDayOHLC().PriorLow[0]))
	Dailyqueue.Enqueue(PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorOpen[0]);
else
	Dailyqueue.Enqueue(Math.Abs(PriorDayOHLC().PriorOpen[0] - PriorDayOHLC().PriorLow[0]));

				if (Dailyqueue.Count > Period + 1)
				{
					Dailyqueue.Dequeue();
				}
				
				Dailyqueue.CopyTo(Dailyarray,0);
				SumOfDaily = 0;
				for (int x = 1; x < Period + 1; x++) 
					{ 
						SumOfDaily = SumOfDaily + Dailyarray[x];
					}
					
			}

Reply With Quote
Thanked by:
  #5 (permalink)
 drw112 
Chicago IL
 
Experience: Beginner
Platform: Sierra Chart
Trading: Futures
Posts: 117 since Feb 2012
Thanks Given: 22
Thanks Received: 120

Going off what Fat Tails said, there is also the List<T> type. It allows you to give a type to your List so if you only want doubles you can do List<double> myDoubleList = new List<double>()

I like the list if you are planning on looping through and want a strong statically typed object. If you need to access it by index then ArrayList is probably better.

Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


drw112 View Post
Going off what Fat Tails said, there is also the List<T> type. It allows you to give a type to your List so if you only want doubles you can do List<double> myDoubleList = new List<double>()

I like the list if you are planning on looping through and want a strong statically typed object. If you need to access it by index then ArrayList is probably better.

Do you know of an example of this implemented in NT somewhere?

Started this thread Reply With Quote
  #7 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


Fat Tails View Post
You can use an ArrayList object. It is basically an array with a variable size, which allows you to add elements of various data types.

Step 1: create the array list:

 
Code
private ArrayList  myArray = new ArrayList();
You have now an empty ArrayList.


Step 2: Add elements to your array list:

 
Code
myArray.Add((double)(Input[0]));
It is important to specify the data type that you add.


Step 3: Retrieve the stored value.

 
Code
value =  (double) myArray[5];
This will retrieve the 6th value that you have added.

Tip
When adding or retrieving a value, you need to specify the data type which is stored in the array list. Without adding (double) in front of the array element, neither adding nor retrieving a value will work.


Of course you can use other data types, for example you can add (string) values.


Further information on array lists is here:

ArrayList Class (System.Collections)

C# ArrayList Tips

I will give this a try. But thanks,as this was pretty much the level of explanation I was looking for.

Started this thread Reply With Quote
  #8 (permalink)
 drw112 
Chicago IL
 
Experience: Beginner
Platform: Sierra Chart
Trading: Futures
Posts: 117 since Feb 2012
Thanks Given: 22
Thanks Received: 120


forrestang View Post
Do you know of an example of this implemented in NT somewhere?

Generic Multi-dimensional List - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum

This thread pretty much explains it. This user is building a List within a List but the idea is the same just slightly more complicated.

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #9 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505


drw112 View Post
Going off what Fat Tails said, there is also the List<T> type. It allows you to give a type to your List so if you only want doubles you can do List<double> myDoubleList = new List<double>()

I like the list if you are planning on looping through and want a strong statically typed object. If you need to access it by index then ArrayList is probably better.

You can access a List<T> by index.

List<double> will have a better performance than ArrayList because doubles won't be casted to objects when inserting, and objects won't be casted to doubles when accessing. List<double> will also be safer because you will always be sure your list only contains valid doubles, not random objects.

So IMO you'd better use List<double>. ArrayLists are legacy objects used before generic collections appeared in .NET 2.0

Usage is easy, here is an example I found on C# List Examples

 
Code
    
List<int> list = new List<int>();

list.Add(2);     
list.Add(3);     
list.Add(7);     

foreach (int prime in list) // Loop through List with foreach     
{         
Console.WriteLine(prime);     
}

Reply With Quote
  #10 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102



gomi View Post
You can access a List<T> by index.

List<double> will have a better performance than ArrayList because doubles won't be casted to objects when inserting, and objects won't be casted to doubles when accessing. List<double> will also be safer because you will always be sure your list only contains valid doubles, not random objects.

So IMO you'd better use List<double>. ArrayLists are legacy objects used before generic collections appeared in .NET 2.0

Usage is easy, here is an example I found on C# List Examples

 
Code
    
List<int> list = new List<int>();

list.Add(2);     
list.Add(3);     
list.Add(7);     

foreach (int prime in list) // Loop through List with foreach     
{         
Console.WriteLine(prime);     
}

@ gomi: Thank you for the hint.

I suppose that you are right, so I will make use of it in future.

Reply With Quote
Thanked by:




Last Updated on March 27, 2012


© 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