Thank you
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Sorting in Ninja script
Collapse
X
-
-
I come back to this thread, since I'd like to understand a couple of things in the very small script that I'm attaching here as an example:
1) this code can be compiled with no errors, but when I enable it from the Strategies tab through a checkmark in the related box, the checkmark immediately disappears, and the whole code can't be enabled because of an "Error on calling 'OnStartUp' method for strategy - Accessing an index with a value that is invalid since its out of range). Then, what must I do to either enlarge the range or have my list within the range, or anyway to enable it ?
2) the line if (Instrument.FullName == "AAPL") ......
is meant to have this code executed just once for any bar update, not once for ANY symbol of my instrument list (what would be redundant). Is there a more elegant way to do it ?
Thanks in advanceAttached FilesLast edited by Mauripasto; 06-16-2015, 05:05 PM.
Comment
-
As a start, call your list something other than List.. Beyond that, did you apply this indicator to an AAPL chart?Originally posted by Mauripasto View PostI come back to this thread, since I'd like to understand a couple of things in the very small script that I'm attaching here as an example:
1) this code can be compiled with no errors, but when I enable it from the Strategies tab through a checkmark in the related box, the checkmark immediately disappears, and the whole code can't be enabled because of an "Error on calling 'OnStartUp' method for strategy - Accessing an index with a value that is invalid since its out of range). Then, what must I do to either enlarge the range or have my list within the range, or anyway to enable it ?
2) the line if (Instrument.FullName == "AAPL") ......
is meant to have this code executed just once for any bar update, not once for ANY symbol of my instrument list (what would be redundant). Is there a more elegant way to do it ?
Thanks in advance
Comment
-
I just changed "List" to "Mine", but the problem persists.
I feel it is related to the array initialization of "Mine[n]", as if I previously had to set its length or maximum. If you run the small code, you should be able to see the error type.
Actually, this script is the faulty part of a larger code that I'm using not as an indicator, but as a strategy enabled from Control Center -> Strategies, not from a chart. Of course I do have a couple of charts (sometimes with AAPL) open in my workspace, but they shouldn't be affected by the code, at least as far as I know and see in the attached chart.Last edited by Mauripasto; 06-17-2015, 12:17 AM.
Comment
-
You are accessing this from somewhere other than OnBarUpdate().Originally posted by Mauripasto View PostI just changed "List" to "Mine", but the problem persists.
I feel it is related to the array initialization of "Mine[n]", as if I previously had to set its length or maximum. If you run the small code, you should be able to see the error type.
Actually, this script is the faulty part of a larger code that I'm using not as an indicator, but as a strategy enabled from Control Center -> Strategies, not from a chart. Of course I do have a couple of charts (sometimes with AAPL) open in my workspace, but they shouldn't be affected by the code, at least as far as I know and see in the attached chart.
Even then, unless applied to AAPL, this code will not run, so the list would be empty. In which case, Print would be accessing a non-existent index.
(additional query in blue)Code:if (Instrument.FullName == "AAPL") [COLOR="Blue"]//and if not applied to AAPL?[/COLOR] { List[0] = "15AAPL"; List[1] = "11GOOG"; List[2] = "20IBM"; List.Sort(); }Last edited by koganam; 06-17-2015, 07:10 AM.
Comment
-
First of all, thanks for your patience & consideration.
As previously stated, these few lines are the extreme semplification of a larger code, where all instruments I'm interested in, are singularly added. Please take for granted that the list to be sorted is never empty (I already passed through this with your and Patrick's help at the beginning of this thread), so we can consider the example-list "Mine" with those 3 members AAPL, GOOG and IBM as totally realistic, representative and meaningful.
Exactly because that list is populated from scratch at any new bar, processing + sorting + printing the list results ONCE per bar is absolutely enough to me. To limit this to one occurrence, I chose to do it when AAPL is examined, but of course either GOOG or $EURUSD would have been as good. This is why at post #32 I asked
Originally posted by Mauripasto View Post2) the line if (Instrument.FullName == "AAPL") ......
is meant to have this code executed just once for any bar update, not once for ANY symbol of my instrument list (what would be redundant). Is there a more elegant way to do it ?
As to the initial points of your answer, they are somehow unclear to me, but I know that the faulty lines are these ones:
foreach (string s in Mine)
{
Print(s);
}
since with these 4 lines, the small code can be compiled but not enabled as a strategy; while without them, the code faces no issue at all, its enabling is possible and it does work either. Unfortunately, displaying the results of the whole process in some way is necessary to me: so why are those 4 lines a mistake ?Last edited by Mauripasto; 06-17-2015, 04:10 PM.
Comment
-
About the only reason that you can get an index error from those lines is that the list is empty.Originally posted by Mauripasto View PostFirst of all, thanks for your patience & consideration.
As previously stated, these few lines are the extreme semplification of a larger code, where all instruments I'm interested in, are singularly added. Please take for granted that the list to be sorted is never empty (I already passed through this with your and Patrick's help at the beginning of this thread), so we can consider the example-list "Mine" with those 3 members AAPL, GOOG and IBM as totally realistic, representative and meaningful.
Exactly because that list is populated from scratch at any new bar, processing + sorting + printing the list results ONCE per bar is absolutely enough to me. To limit this to one occurrence, I chose to do it when AAPL is examined, but of course either GOOG or $EURUSD would have been as good. This is why at post #32 I asked
As to the initial points of your answer, they are somehow unclear to me, but I know that the faulty lines are these ones;
foreach (string s in Mine)
{
Print(s);
}
since with these 4 lines, the small code can be compiled but not enabled as a strategy; while without them, the code faces no issue at all, its enabling is possible and it does work either. Unfortunately, displaying the results of the whole process in some way is necessary to me: so why are those 4 lines a mistake ?
Try this:
Which will at the very least show us if the list is empty.Code:if (Mine.Count < 1) Print("The list is empty!"); else foreach (string s in Mine) { Print(s); }
You might also want to Print out the Instrument.Fullname so the you can see what it really is.Last edited by koganam; 06-17-2015, 04:19 PM.
Comment
-
I added your suggestion
but once again, because of these lines my PC can't enable the strategy from Control Center -> Strategies.Code:if (Mine.Count < 1) Print("The list is empty!"); else foreach (string s in Mine) { Print(s); }
Does the attached minimal script "D10" work on your terminal ?Attached Files
Comment
-
It enables here, but there is an error in the output window.Originally posted by Mauripasto View PostI added your suggestion
but once again, because of these lines my PC can't enable the strategy from Control Center -> Strategies.Code:if (Mine.Count < 1) Print("The list is empty!"); else foreach (string s in Mine) { Print(s); }
Does the attached minimal script "D10" work on your terminal ?
"Error on calling 'OnStartUp' method for strategy ..... Object reference not set to an instance of an object."
Comment
-
I don't think you are initializing storage properly.
Search for REVERSE
Comment
-
-
I think that I have already explained to you that your D10.cs will only work correctly to populate the list if the primary instrument is AAPL, which would happen if you applied the strategy to AAPL, whether on a chart or the Control Center.Originally posted by Mauripasto View Post...
Does the attached minimal script "D10" work on your terminal ?
If your D10.cs class is not applied to AAPL, the list WILL BE EMPTY, because that is the instruction that you have written. Instrument.FullName refers to only the primary instrument, and you have specifically coded that then and only then is the list to be created.
I also posted, directly from the documentation, that as you are not calling this from OnBarUpdate(), you need to make a null reference check.Last edited by koganam; 06-18-2015, 08:28 PM.
Comment
-
Thanks, in advance, for any info.
Very interesting thanks for sharing.

รับโปรโมชั่นพิเศษมากมายทันที ได้ที่นี่ royal1688 casino
Comment
-
Sorry for the delay, I was out of office, and thanks to everybody for your advice. As to your posts,
#39: I got that same error as you in the Output Window during the very little time when the strategy is enabled, since the "enabling checkmark" disappears 20-30 seconds after its activation (I'd be curious to know whether this automatic disabling happens to you too or not).
Unfortunaely I'm not able to tell in which line the error occurs, since I don't know where/how I may retrieve it.
#40: the examples were very useful. I also feel that this all depends upon general inizialization, but I already face the mentioned error(s) with the only five lines (!) of the attached "FurtherReduced" code:private List<string> Mine;and I wonder where the error is ....
Mine[0] = "15AAPL";
Mine[1] = "11GOOG";
Mine[2] = "20IBM";
Mine.Sort();
#41: you are right that - looking at my previous code - the list will be correctly populated only when the primary instrument is AAPL, however this is how it must work. In the other cases the routine will not be called, no list has to be created nor populated nor sorted nor printed, since I need this all just once for any bar update. Conversely, in the current reduced version I expressly cancelled the if-AAPL-statement, so that the list will be filled up by ANY instrument and never empty, just to see what happens, but the error occurs all the same. Similarly when making a null reference check with if (Instrument != null).Attached Files
Comment
-
OK. I overlooked the real error. You are trying to assign values into a list that has just been initialized, and so has a zero capacity. If you insist on your assignment method, you will have to explicitly increase the capacity of the list, Mine.Originally posted by Mauripasto View PostSorry for the delay, I was out of office, and thanks to everybody for your advice. As to your posts,
#39: I got that same error as you in the Output Window during the very little time when the strategy is enabled, since the "enabling checkmark" disappears 20-30 seconds after its activation (I'd be curious to know whether this automatic disabling happens to you too or not).
Unfortunaely I'm not able to tell in which line the error occurs, since I don't know where/how I may retrieve it.
#40: the examples were very useful. I also feel that this all depends upon general inizialization, but I already face the mentioned error(s) with the only five lines (!) of the attached "FurtherReduced" code:private List<string> Mine;and I wonder where the error is ....
Mine[0] = "15AAPL";
Mine[1] = "11GOOG";
Mine[2] = "20IBM";
Mine.Sort();
#41: you are right that - looking at my previous code - the list will be correctly populated only when the primary instrument is AAPL, however this is how it must work. In the other cases the routine will not be called, no list has to be created nor populated nor sorted nor printed, since I need this all just once for any bar update. Conversely, in the current reduced version I expressly cancelled the if-AAPL-statement, so that the list will be filled up by ANY instrument and never empty, just to see what happens, but the error occurs all the same. Similarly when making a null reference check with if (Instrument != null).
Much easier is to automatically allow capacity growth by Adding items to the list. Use the Mine.Add() method.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
88 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
48 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
31 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
34 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
69 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment