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

Arrays in NT

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

    Arrays in NT

    Sorry for such a basic question but how do I assign values to an an array?

    Array[7] = {1,2,3,4,5,6,7}; // ?

    #2
    GKonheiser,

    Array.IList.Add()

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      That always throws an exception.

      Comment


        #4
        Originally posted by GKonheiser View Post
        Sorry for such a basic question but how do I assign values to an an array?

        Array[7] = {1,2,3,4,5,6,7}; // ?
        Your question, given the example that you show is unclear. Are you asking how to declare an Array?
        Last edited by koganam; 09-02-2016, 11:05 AM. Reason: Corrected spelling.

        Comment


          #5
          I have an array,

          double[] levels = new double[7] {w1,w2,w3,w4,w5,w6,w7};

          What I'm trying to do is search the array and find the next highest and lowest number to Close[0] but I cant get the syntax correct, I have:-

          hLevel = Array.Find(levels, x > Close[0]);
          lLevel = Array.Find(levels, x < Close[0]);

          could you tell me what the correct syntax is for this?

          Comment


            #6
            GKonheiser,

            Take a look at the link below, it should shed more light on the subject -
            http://www.dotnetperls.com/array-find
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by GKonheiser View Post
              I have an array,

              double[] levels = new double[7] {w1,w2,w3,w4,w5,w6,w7};

              What I'm trying to do is search the array and find the next highest and lowest number to Close[0] but I cant get the syntax correct, I have:-

              hLevel = Array.Find(levels, x > Close[0]);
              lLevel = Array.Find(levels, x < Close[0]);

              could you tell me what the correct syntax is for this?
              One way would be to sort the array, then loop through it to find the values that span Close[0] value where you seek.

              Comment


                #8
                Ive now got the array declared but when i try to get the value in the array they come back as null even thou I have populated the array,

                this is the code i have so far:-

                if(BarsInProgress == dBIP) // Day Bars
                {
                // Calculate Pivot Points(for order Entery)

                pp = Math.Round((Highs[dBIP][0] + Lows[dBIP][0] + Closes[dBIP][0])/3,2);
                r1 = Math.Round((2 * pp) - Lows[dBIP][0],2);
                s1 = Math.Round((2 * pp) - Highs[dBIP][0],2);
                r2 = Math.Round(pp + (Highs[dBIP][0] - Lows[dBIP][0]),2);
                s2 = Math.Round(pp - (Highs[dBIP][0] - Lows[dBIP][0]),2);
                r3 = Math.Round(r1 + (Highs[dBIP][0] - Lows[dBIP][0]),2);
                s3 = Math.Round(s1 - (Highs[dBIP][0] - Lows[dBIP][0]),2);

                double[] pivots = new double[7] {r3, r2, r1, pp, s1, s2, s3};

                Array.Sort (pivots);


                }

                if(BarsInProgress == mBIP)
                {
                int h = Array.BinarySearch(pivots, Close[mBIP]);

                if( pivots[0] != 0 || pivots[1] != 0 || pivots[2] != 0 || pivots[3] != 0 || pivots[4] != 0 || pivots[5] != 0 || pivots[6] != 0 )
                {
                hPiv = pivots[h+1];
                lPiv = pivots[h-1];
                }

                Comment


                  #9
                  it looks like pivots is not defined in the same scope when you reference it.

                  Consider defining it outside of the if statement or outside of OnBarUpdate()
                  LanceNinjaTrader Customer Service

                  Comment


                    #10
                    I need to define the array in OnBarUpdate in Daily bars as I am calculating levels on a daily basis and then using the array on a minute time frame?

                    Comment


                      #11
                      As there are parts of your code missing I can't say for sure but at first glance I saw

                      double[] pivots = new double[7] {r3, r2, r1, pp, s1, s2, s3};

                      was defined inside of one if statement

                      In the later if statement you check pivots[0] != 0

                      These won't be referring to each other if that is what you're trying to do. Sorry if I misunderstand.
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by GKonheiser View Post
                        I need to define the array in OnBarUpdate in Daily bars as I am calculating levels on a daily basis and then using the array on a minute time frame?
                        No you do not. Define it as a class variable and populate it in OBU. Your problem is coming from a scope limitation.

                        Also, by the very way that you have defined the pivots array, it is already sorted from high to low, so you do not need to run a sort routine in this particular case.

                        Comment


                          #13
                          Got it, understand what i was doing wrong, Thanks

                          Comment


                            #14
                            Error comparing doubles

                            Hi

                            So I have defined and populated my array with doubles (pivot points). I am then using:

                            i = Array.BinarySearch(pivots, Closes[mBIP]);

                            to compare Closes to the array but I get an error "**NT** Error on calling 'OnBarUpdate' method for strategy 'PivShortCode/b11fc6d627cb43aaab280f46194ae269': Failed to compare two elements in the array."

                            When i debug this in VStudio, and look at the error its telling me {"Object must be of type Double."}??

                            Both Closes[mBIP] and all elements in pivots are doubles.

                            can anyone shed light on why this is, do I need to convert Closes in some way?

                            Comment


                              #15
                              Originally posted by GKonheiser View Post
                              Hi

                              So I have defined and populated my array with doubles (pivot points). I am then using:

                              i = Array.BinarySearch(pivots, Closes[mBIP]);

                              to compare Closes to the array but I get an error "**NT** Error on calling 'OnBarUpdate' method for strategy 'PivShortCode/b11fc6d627cb43aaab280f46194ae269': Failed to compare two elements in the array."

                              When i debug this in VStudio, and look at the error its telling me {"Object must be of type Double."}??

                              Both Closes[mBIP] and all elements in pivots are doubles.

                              can anyone shed light on why this is, do I need to convert Closes in some way?
                              What type is "i" ?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cre8able, 02-11-2023, 05:43 PM
                              3 responses
                              236 views
                              0 likes
                              Last Post rhubear
                              by rhubear
                               
                              Started by frslvr, 04-11-2024, 07:26 AM
                              8 responses
                              113 views
                              1 like
                              Last Post NinjaTrader_BrandonH  
                              Started by stafe, 04-15-2024, 08:34 PM
                              10 responses
                              45 views
                              0 likes
                              Last Post stafe
                              by stafe
                               
                              Started by rocketman7, Today, 09:41 AM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by traderqz, Today, 09:44 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X