NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 09-22-2009, 04:56 PM   #1
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default Sorting an Array

All,

I'm looking to sort an array with 100 items in it, each of the items are doubles. Will the Array.Sort method work in NT?

Thanks
Shannon
Shansen is offline  
Reply With Quote
Old 09-22-2009, 08:27 PM   #2
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 681
Default

Hey Shansen,

I would use an ArrayList type.

you will have to add the following reference to the top of your code
Code:
using System.Collections;
MSDN help is located here
__________________
"The notion of impossibility only exist in the world of those who had already been imprisoned by their own fears and limitations."

"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"


PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 09-22-2009, 10:15 PM   #3
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default

mrlogic,

Thanks for the reply.


If you would be so kind, I'd like to improve my knowledge in coding within NT. From a little research, there are several differences between Arrays and Arraylists:
  1. An array can be of any data type and contain one data type while array list can contain any data type in the form of the object.
  2. With array you can not dynamically increase or decrease the size of array dynamically. You must the define the size of the array. You can change the size of the array with redim statement but still you have to define type. While with array list you can make list of any sizes. When a element or object is added to array list it size is automatically increase the capacity of the array list.
  3. once you delete the item in array it kept that one as empty like a[2]="" but in case of arraylist a[3]index occupies the position of a[2] and also if you try to insert a[2] value array will throw error if any items reside inside but in case of arraylist a[2] is inserted in same position but at the same time position of a[2] become a[3] if there is already an item exists.
Given the size of the required array / arraylist is static and known at the outset, what are the benefits of using an arraylist over an array (e.g. efficiency of the sort method)?

Any insight on your knowledge and experience would be appreciated
Regards
Shannon
Shansen is offline  
Reply With Quote
Old 09-23-2009, 04:43 AM   #4
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 681
Default

In the case you present, the .Sort method is already coded for you. Additionally, future expansion is simpler since the ArrayList is a table of type object, which can hold any element type.
__________________
"The notion of impossibility only exist in the world of those who had already been imprisoned by their own fears and limitations."

"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"


PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 09-23-2009, 04:51 AM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Hamburg / Lueneburg - Germany
Posts: 10,867
Default

In addition to mrlogik's tips, you could check into this link here - http://dotnetperls.com/all-list
__________________
Bertrand, NinjaTrader Customer Service
NinjaTrader is a FREE application for advanced charting, market analytics, system development and trade simulation.

View schedule of upcoming online product training events.

NinjaTrader_Bertrand is offline  
Reply With Quote
Old 09-23-2009, 06:04 AM   #6
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default

mrlogik & Bertrand,

Thanks for your respective replies.

Showing my novice programing stature, if an array is coded in NT as:

Code:
#region Variables
	private double[] MyArray;
#endregion

protected override void Initialize()
{
	MyArray = new double[100];
}
What is the syntax to code an ArrayList?

Thanks again
Shannon

Last edited by Shansen; 09-23-2009 at 06:11 AM.
Shansen is offline  
Reply With Quote
Old 09-23-2009, 06:18 AM   #7
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Hamburg / Lueneburg - Germany
Posts: 10,867
Default

Shannon, don't have a snippet ready, but you can check the C# section of this link - http://msdn.microsoft.com/en-us/libr...arraylist.aspx
__________________
Bertrand, NinjaTrader Customer Service
NinjaTrader is a FREE application for advanced charting, market analytics, system development and trade simulation.

View schedule of upcoming online product training events.

NinjaTrader_Bertrand is offline  
Reply With Quote
Old 09-23-2009, 06:28 AM   #8
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 681
Default

Code:
#region Variables
private ArrayList() myArrayList;
#endregion

protected override void Initialize()
{
    myArrayList = new ArrayList();
}
__________________
"The notion of impossibility only exist in the world of those who had already been imprisoned by their own fears and limitations."

"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"


PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 09-26-2009, 01:28 AM   #9
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default

mrlogik & Bertrand,

After more research, would the "List" class be a better direction to head? From what I've read it is more efficient and "type safe".

As always any insight would be appreciated.
Regards
Shannon

Last edited by Shansen; 09-26-2009 at 05:17 AM.
Shansen is offline  
Reply With Quote
Old 09-26-2009, 06:07 AM   #10
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 681
Default

Shansen,

It really depends on the application, and what you're familiar with.

Do you have a specific design you need to fulfill, or is this more of a generic design?
__________________
"The notion of impossibility only exist in the world of those who had already been imprisoned by their own fears and limitations."

"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"


PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 09-26-2009, 06:39 AM   #11
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default

mrlogik,

Funny you should ask what I'm familiar with. Both ArrayList and List are new to me.

I'm looking to identify where the CurrentBar's range is significantly above average (relative to recent bars).

With your help I've constructed an ArrayList, used it to store on a rolling basis the ranges of the last 100 bars (inserting the latest bar range at index 0, and removing index 101 in the ArrayList when the ArrayCount equals 101). This ArrayList is cloned to a new ArrayList, this new ArrayList is then sorted. The range listed at the 90th index of the sorted ArrayList is used to evaluate if the CurrentBar's range is in the top 10% of the most recent 100 bars.

As always any suggestions are welcome.
Regards
Shannon
Shansen is offline  
Reply With Quote
Old 10-22-2009, 03:42 AM   #12
Shansen
Senior Member
 
Join Date: Jan 2009
Posts: 110
Default

mrlogik,

It would appear I've hit a snag...

The method arraylist.sort() does not appear to work as expected?
Please find below a sample of the NinjaScript.

As always, any insight would be appreciated
Regards
Shannon

Code:
#region Using declarations
using System.Collections;
#endregion

#region Variables
  private ArrayList HistoricalRanges = new ArrayList();
  private ArrayList SortedRanges = new ArrayList();	
  private double AboveAverageBarRange;
#endregion

protected override void OnBarUpdate()
{
  HistoricalRanges.Insert(0, Range()[1]);
  if (HistoricalRanges.Count > 100)
    HistoricalRanges.RemoveAt(100);
  SortedRanges = (ArrayList) HistoricalRanges.Clone();
  Print ("");
  Print (Time.ToString() + " Required range " + ((double) SortedRanges[Convert.ToInt32(Math.Min(SortedRanges.Count-1, 90))])); 
  SortedRanges.Sort();			
  Print (Time.ToString() + " Required range " + ((double) SortedRanges[Convert.ToInt32(Math.Min(SortedRanges.Count-1, 90))])); 	
}

Last edited by Shansen; 10-22-2009 at 03:52 AM.
Shansen 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
Sorting layers eswap0 Indicator Development 1 03-21-2009 11:02 PM
Sorting Columns in Market Analyzer designer Suggestions And Feedback 10 10-13-2008 04:10 AM
Sorting labels in parameters window cls71 Suggestions And Feedback 1 04-16-2008 05:30 AM
Multiple Market Analyzers vs. Sorting 1reason Market Analyzer 1 12-29-2007 05:01 PM
Parameter sorting/ordering rtrader Suggestions And Feedback 3 04-08-2007 05:27 PM


All times are GMT -6. The time now is 10:55 AM.