Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sorting in Ninja script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #31
    Ok, I've understood.
    Thank you

    Comment


      #32
      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 advance
      Attached Files
      Last edited by Mauripasto; 06-16-2015, 05:05 PM.

      Comment


        #33
        Originally posted by Mauripasto View Post
        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 advance
        As a start, call your list something other than List.. Beyond that, did you apply this indicator to an AAPL chart?

        Comment


          #34
          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.
          Attached Files
          Last edited by Mauripasto; 06-17-2015, 12:17 AM.

          Comment


            #35
            Originally posted by Mauripasto View Post
            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.
            You are accessing this from somewhere other than OnBarUpdate().



            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();
            			}
            (additional query in blue)
            Attached Files
            Last edited by koganam; 06-17-2015, 07:10 AM.

            Comment


              #36
              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 Post
              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 ?

              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


                #37
                Originally posted by Mauripasto View Post
                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




                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 ?
                About the only reason that you can get an index error from those lines is that the list is empty.

                Try this:
                Code:
                if (Mine.Count < 1) Print("The list is empty!");
                else foreach (string s in Mine)
                			{
                			Print(s);
                			}
                Which will at the very least show us if the list is empty.

                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


                  #38
                  I added your suggestion

                  Code:
                  if (Mine.Count < 1) Print("The list is empty!");
                  else foreach (string s in Mine)
                              {
                              Print(s);
                              }
                  but once again, because of these lines my PC can't enable the strategy from Control Center -> Strategies.

                  Does the attached minimal script "D10" work on your terminal ?
                  Attached Files

                  Comment


                    #39
                    Originally posted by Mauripasto View Post
                    I added your suggestion

                    Code:
                    if (Mine.Count < 1) Print("The list is empty!");
                    else foreach (string s in Mine)
                                {
                                Print(s);
                                }
                    but once again, because of these lines my PC can't enable the strategy from Control Center -> Strategies.

                    Does the attached minimal script "D10" work on your terminal ?
                    It enables here, but there is an error in the output window.

                    "Error on calling 'OnStartUp' method for strategy ..... Object reference not set to an instance of an object."

                    Comment


                      #40
                      I don't think you are initializing storage properly.

                      Create a new List, add elements to it, and loop over its elements with for and foreach.


                      Search for REVERSE

                      Comment


                        #41
                        Originally posted by sledge View Post
                        It enables here, but there is an error in the output window.

                        "Error on calling 'OnStartUp' method for strategy ..... Object reference not set to an instance of an object."
                        And what line exactly is the error referencing?

                        Comment


                          #42
                          Originally posted by Mauripasto View Post
                          ...
                          Does the attached minimal script "D10" work on your terminal ?
                          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.

                          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


                            #43
                            Thanks, in advance, for any info.
                            Very interesting thanks for sharing.






                            รับโปรโมชั่นพิเศษมากมายทันที ได้ที่นี่ royal1688 casino

                            Comment


                              #44
                              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;
                              Mine[0] = "15AAPL";
                              Mine[1] = "11GOOG";
                              Mine[2] = "20IBM";
                              Mine.Sort();
                              and I wonder where the error is ....

                              #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


                                #45
                                Originally posted by Mauripasto View Post
                                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;
                                Mine[0] = "15AAPL";
                                Mine[1] = "11GOOG";
                                Mine[2] = "20IBM";
                                Mine.Sort();
                                and I wonder where the error is ....

                                #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).
                                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.

                                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 NinjaTrader_Gaby  
                                Started by jxs_xrj, 01-12-2020, 09:49 AM
                                4 responses
                                3,285 views
                                1 like
                                Last Post jgualdronc  
                                Started by Option Whisperer, Today, 09:55 AM
                                0 responses
                                5 views
                                0 likes
                                Last Post Option Whisperer  
                                Started by halgo_boulder, 04-20-2024, 08:44 AM
                                2 responses
                                22 views
                                0 likes
                                Last Post halgo_boulder  
                                Started by mishhh, 05-25-2010, 08:54 AM
                                19 responses
                                6,189 views
                                0 likes
                                Last Post rene69851  
                                Working...
                                X