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

index a variable

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

    index a variable

    Hello,

    Is it possible to index a variable in OBU?

    Lets say my variable is called fib a private double.

    Code:
    for (double d = 1.61D; d < 4.24; d+= 0.01D)
                    {
                     fib[d];
                    }
    thanks

    #2
    Hello frankduc,

    Thanks for your post.

    No, a variable only has a singular value.

    fib, in this case, would need to either be an array or a data series in order to be indexed.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul,

      Thanks for the reply.

      In this case volumeValue is divided by numbers between 1.61 and 4.24

      Code:
      for (double d = 1.61D; d < 4.24; d+= 0.01D)
                      {
                        sumvolfibo = volumeValue / d;
                      }

      At the end of the pipeline my formulas dont return 263 answers. even if volumeValue is divided by those numbers.

      So i suppose i need indexing somewhere, hoping it will solve my problem.

      By data series you mean Volume[0] and by array i suppose you mean https://www.tutorialspoint.com/cshar...nal_arrays.htm

      thank you

      Comment


        #4
        Hello frankduc,

        Thanks for your reply.

        If that statement is run in OBU the end result would always be volumeValue / 4.23

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi,,

          I used a list to index and can now divide volumeValue by each double in the forloop. i call it sumvolfibo and it returns many numbers like 25000, 35000, 37000 etc
          The if statement in OBU or OnRender will only test the last value in the sumvolfibo 37000 in this exemple
          Do i have to list it again? So it returns all the barIndex for each returned numbers?

          Code:
          for(int barIndex = index; barIndex <= ChartBars.ToIndex; barIndex--)
                              {
                                  sumOfVolumes4 +=  Bars.GetVolume(barIndex);
                                  if (sumOfVolumes4 >= sumvolfibo)
                                  {
                                      foundIndex = barIndex;
          
                                  Print("sumvol" +sumvolfibo);
          
          
          
                                   break;
                                  }
                              }
          Thank you

          Comment


            #6
            Hello frankduc,

            Thanks for your reply.

            The code is looping through the bars based on the initial value of the variable called "index". As long as barIndex is less than or equal to the right edge bar, the barindex will be decremented.

            sumOfVolumes4 will contain the accumulation of bar values starting from the initial bar specified by "index" and the bars before that (as you are decrementing barIndex).

            If the accumulated volumes in sumOfVolumes4 is greater than or equal to sumvolfibo then you store the value of barIndex into foundIndex, print sumVol and break from the loop.

            If you print the foundIndex, does that provide what you need?
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              No it does not

              foundIndex should return or at least i want it to return every barIndex where sumvolfibo is greater or equal to sumOfVolumes4 . sumOfVolumes4 is just the accumulation of volume in each barIndex from variable index to left side of the chart.

              sumvolfibo returns more than one answer so logically foundIndex should too.

              my goal is that :

              sumvolfibo = 25 000 barIndex loop till 25 000 than give the first barIndex number 1240
              35 000 barIndex loop till 35 000 than give the first barIndex number 1350
              45 000 barIndex loop till 45 000 than give the first barIndex number 1358

              so foundIndex should return 1240, 1350, 1358

              Why the if statement only test the last value of sumvolfibo 45 000 and only return barIndex 1358 and not the rest?

              thanks

              Comment


                #8
                Hello frankduc,

                Thanks for your reply.

                You may want to review your logic of the for loop, "The code is looping through the bars based on the initial value of the variable called "index". As long as barIndex is less than or equal to the right edge bar, the barindex will be decremented." I think this is incorrect logic as there would be no end to that loop unless your conditions are met and then break.

                Would you not want to move the other way where you start at some value of index and move forward up to ChartBars.ToIndex?
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Paul,

                  Code:
                   
                   for(int barIndex = index; barIndex <= ChartBars.ToIndex; barIndex--)
                  Code:
                  for(int barIndex = index; barIndex >= ChartBars.FromIndex; barIndex--)
                  I do agree the second version of the for loop is more logical. As index starts in the middle of the chart barIndex 1856 and going left by -- it cumulate the bars from index to the left side of the chart till it reach 0.
                  But in the end both return the same result.

                  So its not the problem.

                  The conditions are met when the cumulation of the volume in barIndex (sumOfVolumes4 ) = about sumvolfibo and than break and it return the barIndex with the value it looks for.

                  When sumvolfibo as only one value like for say 25 000, there is no problem it works fine and the if statement return the right barIndex (foundIndex).

                  But what do we do if sumvolfibo as more than one value to test like 25 000, 35 000, 38000 etc.

                  How do we make the if statement to test each one of them? Thats my question.

                  thanks

                  Comment


                    #10
                    Hello Frank,

                    Thanks for your reply and clarification.

                    If I understand correctly you have multiple values to test and are looking to find the bar index for each when found.

                    You would need to use an outer loop to control the number of passes to obtain however many values you need to test for in the inner loop. As the inner loop breaks, the outer loop is increment and would need to load the next value to search for.

                    For an example of a nested loop, check the prime numbers example here: https://www.tutorialspoint.com/cshar...sted_loops.htm

                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Irukandji, Today, 09:34 AM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by RubenCazorla, Today, 09:07 AM
                    1 response
                    5 views
                    0 likes
                    Last Post RubenCazorla  
                    Started by TraderBCL, Today, 04:38 AM
                    3 responses
                    25 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by WeyldFalcon, 08-07-2020, 06:13 AM
                    11 responses
                    1,423 views
                    0 likes
                    Last Post jculp
                    by jculp
                     
                    Started by BarzTrading, Today, 07:25 AM
                    2 responses
                    29 views
                    1 like
                    Last Post BarzTrading  
                    Working...
                    X