Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sorting in Ninja script

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

    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) ?
    Thanks

    #2
    Originally posted by Mauripasto View Post
    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) ?
    Thanks
    How is the set organized? A List, independent standalone, how?

    Comment


      #3
      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


        #4
        Originally posted by Mauripasto View Post
        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.
        Instead of writing a sort routine, put the values in a List and sort the List.
        Last edited by koganam; 04-22-2015, 12:49 AM.

        Comment


          #5
          Fine, thank you. How do I do both things ?

          Comment


            #6
            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
                }
            }
            For information on the List Class, and Add and Sort methods please visit the following links:

            Comment


              #7
              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:
              ........
              myList.Add(ADX(14)[0]);
              myList2.Add(Instrument.FullName);

              myList.Sort();

              Questions:
              1. how do I sort the list in the opposite order, i.e. from the higher to the lower values ?
              2. how do I identify the single sorted ADX elements: a(Double[1], a(Double[2], a(Double[3],..... ?
              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 ?)
              4. 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


                #8
                Originally posted by Mauripasto View Post
                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:
                ........
                myList.Add(ADX(14)[0]);
                myList2.Add(Instrument.FullName);

                myList.Sort();

                Questions:
                1. how do I sort the list in the opposite order, i.e. from the higher to the lower values ?
                2. how do I identify the single sorted ADX elements: a(Double[1], a(Double[2], a(Double[3],..... ?
                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 ?)
                4. 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); ?
                For a grouped list of variables that are to have a unique correspondence you will need a Dictionary of some kind, not a simple list. If they are to be automatically sorted, you can use a Sorted Dictionary. In a dictionary, the key must be composed of unique values, so I would use the Instrument for the index, TKey, and the indicator values for the TValue.

                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).aspx
                Last edited by koganam; 05-07-2015, 09:16 PM.

                Comment


                  #9
                  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);

                  myList2.Sort();
                  this way getting all that I need, i.e. the series of preferred instruments sorted as desired upon their ADX values.

                  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


                    #10
                    Originally posted by Mauripasto View Post
                    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);

                    myList2.Sort();
                    this way getting all that I need, i.e. the series of preferred instruments sorted as desired upon their ADX values.

                    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 access members of a List by using an index for what you want. The first member is at index zero, so myList[0] is the first member, myList[1] is the second et.c.

                    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 ?)
                    If those items are not linked, then your sort of either of your single list will break the correspondence of the items. I answered the totality of your concern.

                    Comment


                      #11
                      Perfect.
                      Thank you very much.

                      Comment


                        #12
                        Sorry but...

                        1) when I write
                        myList.Sort().Reverse();
                        as mentioned, I got the error: Operator "." cannot be applied to operand of type "void".
                        Then I tried with
                        myList.Sort();
                        myList.Reverse();
                        getting no errors: is this as well ?

                        2) I wanted to access the members of my List (created as before with
                        myList.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 write
                        foreach (string aDouble in myList)
                        {
                        Print(aDouble);
                        }
                        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) ?

                        It seems I'm pretty wrong at any step.....

                        Comment


                          #13
                          Originally posted by Mauripasto View Post
                          Sorry but...

                          1) when I write
                          myList.Sort().Reverse();
                          as mentioned, I got the error: Operator "." cannot be applied to operand of type "void".
                          Then I tried with
                          myList.Sort();
                          myList.Reverse();
                          getting no errors: is this as well ?
                          My mistake. I left out the isolator. It should have been (myList.Sort()).Reverse().

                          What you have written is perfectly fine.
                          2) I wanted to access the members of my List (created as before with
                          myList.Add(Math.Round(ADX(14)[0]) +" "+Instrument.FullName);
                          and sorted).
                          I thought that you created a List of doubles. I doubt that you can populate a List<double> with strings. There must be something here that I do not understand.
                          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],
                          That just means that you are accessing an index that does not exist at the time that you are trying to access it. That message can come from anything that is being indexed, not necessarily your List. It could come from a DataSeries() for example.

                          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 write
                          foreach (string aDouble in myList)
                          {
                          Print(aDouble);
                          }
                          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) ?

                          It seems I'm pretty wrong at any step.....
                          That also sounds like a logic error. At first glance it would appear that you may be adding to the List on each bar update, which would mean that you have added to the List on each bar. Do you have 2,000 bars on the chart?

                          Comment


                            #14
                            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 $EURUSD
                            ............
                            30 $USDJPY
                            .............
                            10 $GBPAUD
                            and learning that at present EUR/USD is more volatile than USD/JPY, while GBP/AUD is flat.
                            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


                              #15
                              Originally posted by Mauripasto View Post
                              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 $EURUSD
                              ............
                              30 $USDJPY
                              .............
                              10 $GBPAUD
                              and learning that at present EUR/USD is more volatile than USD/JPY, while GBP/AUD is flat.
                              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.
                              Actually, your scheme is a neat scheme for what you want to do.

                              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();
                              empties a list.

                              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 Jonker, Today, 01:19 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jonker
                              by Jonker
                               
                              Started by futtrader, Today, 01:16 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post futtrader  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              14 responses
                              1,791 views
                              0 likes
                              Last Post aligator  
                              Started by Jimmyk, 01-26-2018, 05:19 AM
                              6 responses
                              844 views
                              0 likes
                              Last Post emuns
                              by emuns
                               
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,295 views
                              1 like
                              Last Post jgualdronc  
                              Working...
                              X