Thanks
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Sorting in Ninja script
Collapse
X
-
Sorting in Ninja script
Can someone tell me how I may numerically sort a set of variables (e.g.: 3-7-1) in my code, in order to have 1-3-7 (or eventually 7-3-1) ?
ThanksTags: None
-
Actually, I'm dealing with % variations between the Dollar and other currencies in desired periods, in order to see which currency pair is stronger (let's say) right now as well as in the last hour.
Therefore I have a set of variables like this:Variable1 = Closes[5][0]/Closes[5][5];where the first value (Closes) is related to the US Dollar, the second to the Japanese Yen.
Other variables are comparisons of the US$ with other currencies.
Therefore, I would say, it's not really an already prepared list, but a series of values (although I could arrange it by myself as a list).
I hope I was able to clarify.Last edited by Mauripasto; 04-22-2015, 12:32 AM.
Comment
-
Originally posted by Mauripasto View PostActually, I'm dealing with % variations between the Dollar and other currencies in desired periods, in order to see which currency pair is stronger (let's say) right now as well as in the last hour.
Therefore I have a set of variables like this:Variable1 = Closes[5][0]/Closes[5][5];where the first value (Closes) is related to the US Dollar, the second to the Japanese Yen.
Other variables are comparisons of the US$ with other currencies.
Therefore, I would say, it's not really an already prepared list, but a series of values (although I could arrange it by myself as a list).
I hope I was able to clarify.Last edited by koganam; 04-22-2015, 12:49 AM.
Comment
-
-
Hello Mauripasto,
Thank you for your response.
Below is a basic example of collecting the Close and sorting it in a list:
Code:using System.Collections.Generic; namespace NinjaTrader.Indicator { [Description("")] public class Test1 : Indicator { #region Variables private List<double> myList; #endregion protected override void Initialize() { myList = new List<double>(); } protected override void OnBarUpdate() { if(CurrentBar <= BarsRequired) return; myList.Add(Close[0]); myList.Sort(); foreach (double aDouble in myList) { Print(aDouble); } } #region Properties #endregion } }
Comment
-
Thank for your help.
Now I'm facing new problems. I created my list of data (the ADX values of my preferred instruments), but I need to bind them to the related instruments. Therefore I wrote:
........Questions:
myList.Add(ADX(14)[0]);
myList2.Add(Instrument.FullName);
myList.Sort();
- how do I sort the list in the opposite order, i.e. from the higher to the lower values ?
- how do I identify the single sorted ADX elements: a(Double[1], a(Double[2], a(Double[3],..... ?
- same for the Instruments: how do I identify them (Instrument.FullName), and how do I bind them with their related ADX values ? In simple words, how do I combine AAPL with its own ADX (I suppose they have both the same [N] index number, am I correct ?)
- if the "foreach (double aDouble in myList)" instruction shows the single elements in the Output Window, may I display them also in an alert with something like: Alert(Mine, Priority.High, "Sorted:"+" a(Double[1]+" "+Instrument[1], @"C:\Programmi\NinjaTrader 7\sounds\Alert.wav", Color.White, Color.Black); ?
Comment
-
Originally posted by Mauripasto View PostThank for your help.
Now I'm facing new problems. I created my list of data (the ADX values of my preferred instruments), but I need to bind them to the related instruments. Therefore I wrote:
........Questions:
myList.Add(ADX(14)[0]);
myList2.Add(Instrument.FullName);
myList.Sort();
- how do I sort the list in the opposite order, i.e. from the higher to the lower values ?
- how do I identify the single sorted ADX elements: a(Double[1], a(Double[2], a(Double[3],..... ?
- same for the Instruments: how do I identify them (Instrument.FullName), and how do I bind them with their related ADX values ? In simple words, how do I combine AAPL with its own ADX (I suppose they have both the same [N] index number, am I correct ?)
- if the "foreach (double aDouble in myList)" instruction shows the single elements in the Output Window, may I display them also in an alert with something like: Alert(Mine, Priority.High, "Sorted:"+" a(Double[1]+" "+Instrument[1], @"C:\Programmi\NinjaTrader 7\sounds\Alert.wav", Color.White, Color.Black); ?
ref: https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx
Truth to tell, you can also use a Sorted List, also using TKey, TValue pairs.
ref: https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspxLast edited by koganam; 05-07-2015, 09:16 PM.
Comment
-
Thanks, but this is too complicated for my technical skills.
However, there often are easier ways (although inelegant), so I could sort my list simply using/adding this kind of string:myList2.Add(Math.Round(ADX(14)[0]) +" "+Instrument.FullName);this way getting all that I need, i.e. the series of preferred instruments sorted as desired upon their ADX values.
myList2.Sort();
I just need to know how to identify/call the single strings # 1, 2, 3......I got after the arrangement (probably with arrays, but which ones ?), as well as how to sort them in the reverse order (from the highest to the lowest).Last edited by Mauripasto; 05-08-2015, 03:24 AM.
Comment
-
Originally posted by Mauripasto View PostThanks, but this is too complicated for my technical skills.
However, there often are easier ways (although inelegant), so I could sort my list simply using/adding this kind of string:myList2.Add(Math.Round(ADX(14)[0]) +" "+Instrument.FullName);this way getting all that I need, i.e. the series of preferred instruments sorted as desired upon their ADX values.
myList2.Sort();
I just need to know how to identify/call the single strings # 1, 2, 3......I got after the arrangement (probably with arrays, but which ones ?), as well as how to sort them in the reverse order (from the highest to the lowest).
You reverse a List by using the Reverse() method, so sort and reverse, myList.Sort().Reverse().
However, you also asked this:same for the Instruments: how do I identify them (Instrument.FullName), and how do I bind them with their related ADX values ? In simple words, how do I combine AAPL with its own ADX (I suppose they have both the same [N] index number, am I correct ?)
Comment
-
-
Sorry but...
1) when I writemyList.Sort().Reverse();as mentioned, I got the error: Operator "." cannot be applied to operand of type "void".
Then I tried withmyList.Sort();getting no errors: is this as well ?
myList.Reverse();
2) I wanted to access the members of my List (created as before withmyList.Add(Math.Round(ADX(14)[0]) +" "+Instrument.FullName);and sorted). For this purpose, I used myList[0], as from the previous post, and could compile the code with no problem, but this time I got the error "You are accessing an index with a value that is invalid since its out of range" in the Output Window, and the strategy can't be enabled.
This happens not only with myList[0], but with whichever other index than [0],
3) Finally, when I writeforeach (string aDouble in myList)in order to display the 20 sorted members of my list in the Output Window, I don't get them as hoped, but 40,000 rows (!), i.e. approximately 2,000 rows for each of those 20 members (for istance, this is what Apple shows: "9 AAPL" 32 times, "10 AAPL" 39 times, ...... till "69 AAPL" with just two rows. The numbers are surely the ADX values of Apple, but why so many for ONE 2-minute-bar update (I use CalculateOnBarClose = true) while expecting to have created a list with as many strings as instruments (= just 20) ?
{
Print(aDouble);
}
It seems I'm pretty wrong at any step.....
Comment
-
Originally posted by Mauripasto View PostSorry but...
1) when I writemyList.Sort().Reverse();as mentioned, I got the error: Operator "." cannot be applied to operand of type "void".
Then I tried withmyList.Sort();getting no errors: is this as well ?
myList.Reverse();
What you have written is perfectly fine.
2) I wanted to access the members of my List (created as before withmyList.Add(Math.Round(ADX(14)[0]) +" "+Instrument.FullName);and sorted).
For this purpose, I used myList[0], as from the previous post, and could compile the code with no problem, but this time I got the error "You are accessing an index with a value that is invalid since its out of range" in the Output Window, and the strategy can't be enabled.
This happens not only with myList[0], but with whichever other index than [0],
Why and how it happens depends on the logic of your code. The snippets that you have provided are not enough to isolate your logic error.
3) Finally, when I writeforeach (string aDouble in myList)in order to display the 20 sorted members of my list in the Output Window, I don't get them as hoped, but 40,000 rows (!), i.e. approximately 2,000 rows for each of those 20 members (for istance, this is what Apple shows: "9 AAPL" 32 times, "10 AAPL" 39 times, ...... till "69 AAPL" with just two rows. The numbers are surely the ADX values of Apple, but why so many for ONE 2-minute-bar update (I use CalculateOnBarClose = true) while expecting to have created a list with as many strings as instruments (= just 20) ?
{
Print(aDouble);
}
It seems I'm pretty wrong at any step.....
Comment
-
1) Ok
2) Originally, I was thinking of a List of doubles, but strings give me better chances while also avoiding Dictionaries, TKeys and TValues. All I have to do is to initialize
myList = new List<string>();3) My purpose is to know which ones of 20 selected instruments have the highest ADX values at the moment; what I should be able to do by sorting those strings Math.Round(ADX(14)[0]) +" "+Instrument.FullName. Therefore it is correct to populate my list and to sort it on each bar update, this way getting (for istance):
45 $EURUSDand learning that at present EUR/USD is more volatile than USD/JPY, while GBP/AUD is flat.
............
30 $USDJPY
.............
10 $GBPAUD
The problem is that after any bar update, I must empty myList and populate it from scratch, otherwise the list keeps track of old data too.
Herewith I'm enucleating a few lines related to the sort routine, just to show that calling myList[x] results in an error in the Output Window, and that "foreach....print" at present returns thousands of strings, instead of 20 per bar as expected.
Attached Files
Comment
-
Originally posted by Mauripasto View Post1) Ok
2) Originally, I was thinking of a List of doubles, but strings give me better chances while also avoiding Dictionaries, TKeys and TValues. All I have to do is to initialize
myList = new List<string>();3) My purpose is to know which ones of 20 selected instruments have the highest ADX values at the moment; what I should be able to do by sorting those strings Math.Round(ADX(14)[0]) +" "+Instrument.FullName. Therefore it is correct to populate my list and to sort it on each bar update, this way getting (for istance):
45 $EURUSDand learning that at present EUR/USD is more volatile than USD/JPY, while GBP/AUD is flat.
............
30 $USDJPY
.............
10 $GBPAUD
The problem is that after any bar update, I must empty myList and populate it from scratch, otherwise the list keeps track of old data too.
Herewith I'm enucleating a few lines related to the sort routine, just to show that calling myList[x] results in an error in the Output Window, and that "foreach....print" at present returns thousands of strings, instead of 20 per bar as expected.
The errors I pointed out are also the ones that are occurring. Based on the code that you have posted, the number of members of your list will be the number of bars on your chart minus the barsRequired, for each instrument in the list of instruments that you are processing.
If you want one listing per instrument per bar, then you have to empty your list just before you repopulate it.Code:myList.Clear();
For the exact code that you have written, your myList<> has only one member, so of course, myList[1] has an invalid index. Moreover, the purpose of foreach is to remove the need to track the index anyway, so why are you trying to print the members by index?
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by SnailHorn, 03-27-2024, 09:54 AM
|
4 responses
16 views
0 likes
|
Last Post
by SnailHorn
Today, 09:19 AM
|
||
Started by ninza33, Today, 07:16 AM
|
3 responses
16 views
0 likes
|
Last Post
by ninza33
Today, 09:01 AM
|
||
Started by foxtrott21, Today, 08:27 AM
|
0 responses
4 views
0 likes
|
Last Post
by foxtrott21
Today, 08:27 AM
|
||
Started by jaypelle3, Today, 08:26 AM
|
0 responses
3 views
0 likes
|
Last Post
by jaypelle3
Today, 08:26 AM
|
||
Started by linkcou, Today, 07:01 AM
|
2 responses
24 views
0 likes
|
Last Post
by bltdavid
Today, 08:25 AM
|
Comment