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
-
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
-
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.
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
-
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:
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
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
-
Originally posted by Mauripasto View PostI added your suggestion
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
-
-
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
-
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 geddyisodin, 04-25-2024, 05:20 AM
|
8 responses
58 views
0 likes
|
Last Post Yesterday, 10:45 AM | ||
Started by jxs_xrj, 01-12-2020, 09:49 AM
|
4 responses
3,285 views
1 like
|
Last Post
by jgualdronc
Today, 10:02 AM
|
||
Can someone turn this thinkscript into a ninjascript. 5PM CST OHL and Previous Day
by Option Whisperer
Started by Option Whisperer, Today, 09:55 AM
|
0 responses
5 views
0 likes
|
Last Post Today, 09:55 AM | ||
Started by halgo_boulder, 04-20-2024, 08:44 AM
|
2 responses
22 views
0 likes
|
Last Post Today, 09:41 AM | ||
Started by mishhh, 05-25-2010, 08:54 AM
|
19 responses
6,189 views
0 likes
|
Last Post
by rene69851
Today, 09:29 AM
|
Comment