Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Sort a SortedList by Value

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

    Sort a SortedList by Value

    I have an indicator I'm trying to develop that records pivots and their bar numbers in a sortedlist. From those pivots I calculate the slopes that connect those pivots and store them in another sortedlist.

    I'm now trying to sort the sortedlist that contains the slopes but having problems on how to do that. I have found the following as an attempt, but this code doesn't even compile.(This method is found on line 261 in the attached code: AStrongTrendLineFinder)

    var orderByVal = highPvtPriceSlopesList.OrderBy(v => v.Value) .. this doesn't compile

    Does anyone know how to do this?

    thank you,
    taddypole...
    Attached Files

    #2
    Hello Taddypole,

    Thank you for your inquiry.

    It looks like you were missing the using declaration for LINQ expressions.

    Please add this using declaration inside of the "Using declarations" region:
    Code:
    using System.Linq;
    Once you have done this, the code will compile.

    More information about LINQ can be found over at this publicly available link: http://www.dotnetperls.com/linq

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thank you ZacharyG,


      Works perfectly....

      taddypole...

      Comment


        #4
        I can now sort the sortedlist by values. And I can print them. But how do I access the keys and values to use in my script?



        var orderByVal = highPvtPriceSlopesList.OrderBy(v => v.Value);

        var MNS = orderByVal.ElementAt(0);


        Print("");
        Print(" MNS = " + MNS);
        Print(" 2ndMNS = " + orderByVal.ElementAt(1) );
        Print(" 3ndMNS = " + orderByVal.ElementAt(2) );
        Print(" 4ndMNS = " + orderByderByVal.ElementAt(3)
        Print(" 5ndMNS = " + orderByVal.ElementAt(4) );
        Print("");




        I've tried several times but getting nowhere...

        regards,
        taddypole...
        Attached Files

        Comment


          #5
          Hello Taddypole,

          You can use a foreach statement to loop through orderByVal and access each element's Key and Value.

          Example:
          Code:
          foreach (var oBV in orderByVal)
          {
               Print(oBV.Key);
               Print(oBV.Value);
          }
          Rather than using Prints, you can assign a variable to the Key and Value of each element in orderByVal.

          The Key is of type int and Value is of type double.

          Please, let us know if we may be of further assistance.
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ZacharyG View Post
            Hello Taddypole,

            Rather than using Prints, you can assign a variable to the Key and Value of each element in orderByVal.
            The Key is of type int and Value is of type double.
            Zachary,

            That is the part I'm having problems with. Can you show me how to do that?

            regards,
            taddypole...

            Comment


              #7
              Hello Taddypole,

              Please note that orderByVal is already a collection.

              If you wish to store each orderByVal element in another collection, you can utilize a list. Or, if you would like to store the Key and Value seperately into their own lists, you can utilize two lists.

              More information about lists can be found on this publicly available link: http://www.dotnetperls.com/list
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by Taddypole View Post
                Zachary,

                That is the part I'm having problems with. Can you show me how to do that?

                regards,
                taddypole...
                ref: https://www.google.com/search?q=acce...+a+sorted+list

                Search engines work nicely when used.

                Comment


                  #9
                  thanks Koganam,

                  I had been looking all over but your link to me directly to a great place. Unfortunately when I tried that code I still can't get it to work. The error I get is:


                  'System.Linq.IOrderedEnumerable<System.Collections .Generic.KeyValuePair<int,double>>' does not contain a definition for 'GetKeyList' and no extension method 'GetKeyList'

                  and a similar one for 'GetKeyValue'...

                  Attached is the most recent code and a picture of the error.

                  regards,
                  taddypole...
                  Attached Files

                  Comment


                    #10
                    Thanks for all the assistance for I have a solution for my project. I will now be able to move on. Ended up with dictionary's and used the method:

                    var items = from pair in highPvtSlopesDictionary
                    orderby pair.Value ascending
                    select pair;;

                    foreach (KeyValuePair<int,double> pair in items)
                    {
                    count = count + 1;
                    if(count <= 3)
                    {
                    if(count == 1)
                    {
                    Print(" Pivots with greatest negative slopes for High Pivots ");
                    Print("");
                    pivot1 = pair.Key;
                    slope1 = pair.Value;
                    Print(" firstPivot= " + pivot1 + " slope= " + slope1 );
                    }
                    if(count == 2)
                    {
                    pivot2 = pair.Key;
                    slope2 = pair.Value;
                    Print(" secondPivot= " + pivot2 + " slope= " + slope2 );
                    }
                    if(count == 3)
                    {
                    pivot3 = pair.Key;
                    slope3 = pair.Value;
                    Print(" thirdPivot= " + pivot3 + " slope= " + slope3 );
                    }
                    }
                    }
                    count = 0;

                    so thanks again for the help...

                    regards,
                    taddypole...
                    Attached Files

                    Comment


                      #11
                      To continue on my project, I would like to add the code below that I have found on StackOverFlow.
                      It allows me to invert the parameters TKey and TValue of a dictionary. Doing this (if it works) would allow me better access to the TKey and TValues parameters. Would this extension class go in Userdefined Methods?

                      regards,
                      taddypole...




                      static class Extensions
                      {
                      public static Dictionary<TValue, TKey>
                      AsInverted<TKey, TValue>(this Dictionary<TKey, TValue> source)
                      {
                      var inverted = new Dictionary<TValue, TKey>();

                      foreach (KeyValuePair<TKey, TValue> key in source)
                      inverted.Add(key.Value, key.Key);

                      return inverted;
                      }
                      }

                      And your application code would look like this:

                      using System;
                      using System.Linq;
                      using System.Collections.Generic;

                      class Program
                      {
                      static void Main()
                      {
                      var dict = new Dictionary<String, Double>();
                      dict.Add("four", 4);
                      dict.Add("three", 3);
                      dict.Add("two", 2);
                      dict.Add("five", 5);
                      dict.Add("one", 1);

                      var sortedDict = new SortedDictionary<Double, String>(dict.AsInverted());
                      }
                      }

                      Comment


                        #12
                        Originally posted by Taddypole View Post
                        To continue on my project, I would like to add the code below that I have found on StackOverFlow.
                        It allows me to invert the parameters TKey and TValue of a dictionary. Doing this (if it works) would allow me better access to the TKey and TValues parameters. Would this extension class go in Userdefined Methods?

                        regards,
                        taddypole...




                        static class Extensions
                        {
                        public static Dictionary<TValue, TKey>
                        AsInverted<TKey, TValue>(this Dictionary<TKey, TValue> source)
                        {
                        var inverted = new Dictionary<TValue, TKey>();

                        foreach (KeyValuePair<TKey, TValue> key in source)
                        inverted.Add(key.Value, key.Key);

                        return inverted;
                        }
                        }

                        And your application code would look like this:

                        using System;
                        using System.Linq;
                        using System.Collections.Generic;

                        class Program
                        {
                        static void Main()
                        {
                        var dict = new Dictionary<String, Double>();
                        dict.Add("four", 4);
                        dict.Add("three", 3);
                        dict.Add("two", 2);
                        dict.Add("five", 5);
                        dict.Add("one", 1);

                        var sortedDict = new SortedDictionary<Double, String>(dict.AsInverted());
                        }
                        }
                        A bad idea. Look up the characteristics and requirements for a Dictionary.

                        There is no way that you can guarantee that the doubles will be unique. "A value clash is very unlikely to happen" does not cut it: the values of TKey must be unique.

                        One should not deliberately write code that may fail, and hope that the likelihood of failure is small enough. When you reverse the Dictionary, the doubles in the original Dictionary become the TKey values of the new Dictionary.

                        Comment


                          #13
                          koganam,

                          I had considered that condition and my plan was to verify that all the values(Pivot Prices) are unique before the inversion.

                          The logic is that if two Pivot prices are the same, I would only store or use the one with the shortest run or bar distance because the slope to the further away one would always be less anyway.

                          I certainly have been stumped enough times while developing this code when things didn't work just to find out it was a duplicate key issue.

                          regards,
                          taddypole...

                          Comment


                            #14
                            I now have code completed to verify no duplicate values are added to my dictionary and now trying to create the Inversion code in a UserDefined Method.

                            Per the attachment, I'm getting a compile error I don't understand.

                            ""Extension methods must be defined in a top level static class; Extensions is a nested class""

                            How do I correct this error?

                            regards,
                            taddypole...
                            Attached Files

                            Comment


                              #15
                              Hello Taddypole,

                              For that specific compile error, this is occurring because you are declaring a static class inside of a partial class. You will need to declare your static class outside of the Indicator partial class but within the NinjaTrader.Indicator namespace.
                              Zachary G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rocketman7, Today, 01:00 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post rocketman7  
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              27 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 04-23-2024, 09:53 PM
                              2 responses
                              74 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              193 views
                              0 likes
                              Last Post Hasadafa  
                              Working...
                              X