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

Array.Sort not working as expected and no documentation about it?

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

    Array.Sort not working as expected and no documentation about it?

    Hi there,

    I have an array to hold different price data and I was trying to sort it, but the result is strange:

    1. Before sort, the array values are correctly assigned like this:

    MyArray[0] : 8783.75
    MyArray[1] : 8777
    MyArray[2] : 8790

    2. Then I did

    Array.Sort(MyArray) without any errors

    3 After sort, it prints out:

    MyArray[0] : 0
    MyArray[1] : 0
    MyArray[2] : 0

    Any ideas?

    Thanks!

    #2
    Hello PowerGrid

    If this is an array and not a Series<double>, this would fall under general C# that is not specific to NinjaTrader and may be better suited for a forum on C# programming like StackOverflow..

    My guess would be that you are assigning a new empty array to the variable.

    Below is a public link to the Microsoft documentation.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello PowerGrid

      If this is an array and not a Series<double>, this would fall under general C# that is not specific to NinjaTrader and may be better suited for a forum on C# programming like StackOverflow..

      My guess would be that you are assigning a new empty array to the variable.

      Below is a public link to the Microsoft documentation.
      https://docs.microsoft.com/en-us/dot...t?view=net-5.0
      Thanks ChelseaB, I did look into the MS doc but still no clue... no biggie, I will just write my own sort.

      Comment


        #4
        Hello PowerGrid,

        Are you willing to show the code you are needing assistance with?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello PowerGrid,

          Are you willing to show the code you are needing assistance with?
          Sure thing, here is the piece:

          if(aUpperFractal[2] > 0 ) //&& aLowerFractal[3] > 0)
          {
          Print("Before sort,aUpperFractal[0-2]: " + aUpperFractal[0].ToString() + "||" + aUpperFractal[1].ToString() + "||" + aUpperFractal[2].ToString());
          Array.Sort(aUpperFractal);
          Print("After sort, aUpperFractal[0-2]: " + aUpperFractal[0].ToString() + "||" + aUpperFractal[1].ToString() + "||" + aUpperFractal[2].ToString());
          }

          here is the output:

          Before sort,aUpperFractal[0-2]: 8783.25||8777||8790
          After sort, aUpperFractal[0-2]: 0||0||0

          maybe the call "Array.Sort(aUpperFractal)" overloaded aUpperFractal and reset it to default 0s?

          Thanks for your time.

          Comment


            #6
            Hello PowerGrid,

            I want to confirm that these are arrays and are not Series<double> objects or some other type of collection.

            May I see the declaration of aUpperFractal?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks ChelseaB.

              public double[] aUpperFractal = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //need to initiates

              Comment


                #8
                Hello PowerGrid,

                I'm assuming the array has values set some right?

                I've assigned values and tested and the C# array sort works without issue. The error must be with the logic in your script.

                Below is a link to a video of the test.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi ChelseaB,

                  Thank you so much for the vid! I watched 3 times to make sure the only difference between our code is, I assigned the value of that array under function of OnBarUpates(), could you go one step further to move your array.sort to OnBarUpates() instead of State.Dataloaded() to see what happens when you get a chance?

                  Comment


                    #10
                    Hello PowerGrid,

                    C# Arrays still work the same when assigned in OnBarUpdate or any other method. Assigning a value in OnBarUpdate does not change how C# works.

                    As you requested another video, I have created another video to demonstrate.

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi ChelseaB,

                      Thanks again for the video! I don't know what to say about this, I can't post the whole thing as this is the way I trade manually for over 3 year, so I guess I will just have to figure out myself what went wrong (or just scape the sort, write my own).

                      Again, thanks for your help!

                      Comment


                        #12
                        Hey,

                        He is sorting from low to high... so in a array that is initialised 21x 0 is in your case:

                        Before Sort --> 8783.25,8777,8790,0,0,0,0,0,....

                        and after sort:

                        0,0,0,0,0,0... ...0,0,0,0,0,0,8783.25,8777,8790

                        as you just print out the first positions the array looks empty but its not is my wild quess.

                        so try a "array.reverse()" after sort

                        Cheers

                        Comment


                          #13
                          Originally posted by nemnem View Post
                          Hey,

                          He is sorting from low to high... so in a array that is initialised 21x 0 is in your case:

                          Before Sort --> 8783.25,8777,8790,0,0,0,0,0,....

                          and after sort:

                          0,0,0,0,0,0... ...0,0,0,0,0,0,8783.25,8777,8790

                          as you just print out the first positions the array looks empty but its not is my wild quess.

                          so try a "array.reverse()" after sort

                          Cheers
                          WOW! THANK YOU SO MUCH nemnem!! IT WORKED!

                          After my post, I never come back till now and just saw your reply, what a logic error in my side... anyway, it totally worked!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by CortexZenUSA, Today, 12:53 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by CortexZenUSA, Today, 12:46 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by usazencortex, Today, 12:43 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post usazencortex  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          168 responses
                          2,265 views
                          0 likes
                          Last Post sidlercom80  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X