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

Containers

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

    Containers

    Hello!
    I have the following piece of code:
    HTML Code:
    if(IamHappy)
    {
      if(IaskforHelp)
     {
       Int IgetHelp = Bars.GetBar(Time[0]);
     }
    }
    I can have arrow on the chart when these conditions are fullfilled. My idea with this code is to collect the values of IgetHelp in a container and to be able to forloop in order to make some calculations. I have tried with the List container, but the problem here is that the length of this container is not adequate. Too high.

    I used afterwards the container Series<int> container as well as int[] val1 = new int[x] container. In these cases, when I try to get the value of val1[1] for example, I get the value of the bar with the index 1 from IgetHelp, what is not what I wanted. Let imagine that I want to have the values of IgetHelp when one moving average crosses over another. The arrows in my case would not be one direct next another, but could be separated by either two or three or more bars.

    How can I solve this problem so that:
    1) The values of IgetHelp can be found in a container where ONLY these values must be and not also those of bars with indexes 1 or 2 just near to my arrows?
    2) I can set the length of the container <List> to not higher than 5 for example before starting to save IgetHelp inside? Is that possible?
    Is there a way to solve this problem differently?


    Best regards
    Last edited by Stanfillirenfro; 01-17-2022, 08:19 AM.

    #2
    Hello Stanfillirenfro,

    From the given description it sounds like you would need to use two int type variables to store the CurrentBar at the time you draw each arrow. You could then reference those variables to know the specific bar indexes to get values from those bars or other bars between.

    A Series<int> would match the bar count for the series so that is really only useful if you wanted to store a constant value on each bar. If you store values only during the times you draw arrows that would leave gaps in the series where there are unset values. A series also expects you access the data using a BarsAgo from now.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Hallo Jesse and many thanks for your reply.


      On the output, I can clearly see all corret values of the currentBar when I apply Bars.GetBar(Time[0]); I just want to save each value each time in a container. I do not need bars between.
      The idea is to substract tow consecutives values of Bars.GetBar(Time[0]) to have the number of bars between two consecutive arrows. But When I save these values in a conatiner, while proceeding X[0] - X[1] for example, X{1] returns a value of the bar just next to the arrow and not the value of the previous bar arrow.

      How to proceed please?

      Thanks!
      Last edited by Stanfillirenfro; 01-17-2022, 09:35 AM.

      Comment


        #4
        Hello Stanfillirenfro,

        Unfortunately this description does not assist in describing what you are having trouble with.

        To store something in a container that would require using a Series<T>. Storing values only at the times when your event is true will leave a series with holes in it. Using X[0] - X[1] would only work if you stored values for 2 bars in a row and checked that value directly after doing that. That's checking 0 and 1 BarsAgo in the series. If your condition became true 10 bars ago you would need to use X[10] as the barsago to know what the value was at the time the condition was true.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse and many thanks for our reply and your help.

          I think we are getting closer to the solution.
          Have a look please.
          Let's imagine that at 9:10:35 I have an arrowdrawn on a bar. It is given to me through Bars.GetBar(Time[0]). I store this value in my Series<T>. At this time I have bar[0] in my Series<T>Other bars are printed afterwards, but not interesting for me.. At 9:15:23 for example I have another arrow. At this time I have AGAIN bar[0] which I store in my container again. So in the container I have bar[0] for the bar at 9:10:35 and bar[1] for the bar at 9:15:23. Between these two bars I have oder bars that I do not take into account. But when I make the calculation on my container, bar[1] ist not the bar at 9:15:23, but a bar just next to that at 9:10:35, although I have other bars which are not interesting for me per now.

          Any help would be appreciate.

          Thanks!
          Last edited by Stanfillirenfro; 01-17-2022, 10:09 AM.

          Comment


            #6
            Hello Stanfillirenfro,

            You have explained what happens with a series when you only set a few data points however I am not certain I understand what your question is. The series would consist of 2 valid data points and every other point would be invalid in your given example. You would need to know the specific bar indexes in the series to get that data moving forward in time. If you need to store 2 pieces of data from the two given times then two variables would be better suited for that use case so you would not need to know the specific bars to find the data in the series.

            You can alternative see the sample of IsValidDataPointAt to see how invalid points are used logically.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks Jesse for your reply.

              With the function IsValidDataPointAt(), I MUST need more bars on the chart, more that 256.. It is too much.
              I have attached a file from the output. Each line represents a value when an arrow is drawn. And each value is when I use the index 0 in Bars.GetBar(Time[0]); I have stored each of these values in my Series<T>, but the calculations in the Series do not work. If I take the five last lines of the output, I would have in my Series<T> indices from 0 to 4. But if I make the calculation X[0] - X[1] for example, X[1] in the calculation is the value of the bar bar[1] if the currentBar (bar[0]) is that with the last printed arrow.
              I just want a way to store the values from my output in a container and only these values. Is it possible? If yes, how to proceed?

              Any help?

              Thanks in advance.
              Attached Files

              Comment


                #8
                Hello Stanfillirenfro,

                Unfortunately I am not sure what you are asking for here. A series is how you can store data on a bar by bar basis. If you wanted to store just a few values that is not associated with the bars that would be a C# array or List<T>.

                A Series<T> works in a specific way and is associated with the bar series you are using. You don't have put data in every series location but if you don't it will be considered an invalid data point. Using a series in that way requires that you know what index your data is stored at because its not a consistent value for every bar. When you move forward in time the data stays in the same location but to access that using a BarsAgo you would have to keep increasing the [BarsAgo] used to find it.



                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jesse!
                  Great!
                  Exactly! I have used C# List<T>. Here is the real proble. I have this condition: if(CurrentBar < 50) return; In the List<T> in my case, I am everytime out of range in my loop. I do not want to use all the bars on the char, what would slow the proceed or whatever. Is there a way to set the lengh of the List<T> to 30 for example before starting storing values inside? How to set the above condition otherwise?

                  Thanks!

                  Comment


                    #10
                    Hello Stanfillirenfro,

                    If you are getting an out of range error when using a list that indicates you somehow used it incorrectly. A list has an unlimited length starting at 0 items, it counts each item you add. I couldn't really comment on why that error may be happening as I don't know how you overall had used the loop or the list. You can use Prints to better understand how your loop works and the values you are using within the loop. If you are trying to access a list index that doesn't exist you would get an out of range error.

                    If you are not clear on how lists or loops work I would suggest using some external C# guides that go over those concepts to get a better understanding of their uses/properties. That would help to understand if that is the right item to use for your goal in NinjaScript.


                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Jesse for your reply.
                      The problem this time is not the use of the List. To access items of a List one has to go through a for loop. When I compile the file, the output pups up the following error message; "Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart." I guess my condition if(CurrentBar < 50) return; is not appropriate since I should have more than 50 items in my List. I have also tried with "if(CurrentBars[0] < 0) return;" but the probem is not solve.

                      Any idea?
                      Thanks!

                      Comment


                        #12
                        Hello Stanfillirenfro,

                        The message you are seeing is a generic message that gives an example situation. You would need to check your logic to see what part is having an error. You can use prints to output parts of the logic to identify where the error is generated within that output. Once you know what line is having a problem you can print the values at that time to get a better idea of why that error is happening with the code having an error.


                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jesse!
                          I spent the major part of the night looking for solutions to my issue.
                          Now I can store items in my List<T> and I can even count them using a for loop. For example:
                          HTML Code:
                          for(int h = 0; h < list.Count; h++)
                          {
                             Print("list.Count" + " : " + h + ": " + list[h] + " : " + Time[h]);
                          }
                          I found that I have 247 values in my List.
                          The problem here is that when I try to make some calculations such as
                          HTML Code:
                          int sum = list[[h] - list[h+1]
                          , I am out of range although I have the condition:
                          HTML Code:
                          if(CurrentBar < 500) return;
                          I guess 247 is less that 500 right? Why am I in this case out of range?The error message is that I can not acess bar 513. I do not have more that 247 items in my container. Where is the problem?
                          Why the loop functions for the count of items and not for the calculations?
                          I am wondering if the operations addtion and subtaction are allowed in Ninjatrader while using List<T>.

                          Thanks!

                          Comment


                            #14
                            Hello Stanfillirenfro,

                            If the problem line is: int sum = list[[h] - list[h+1] then the error is because you use h + 1 which on the last index of the list's count won't exist. There won't be a 248th object to check. You could try using list.Count - 1 for the loop to avoid that, you otherwise need to add error checking in your code to make sure h and h + 1 make sense.

                            CurrentBar has nothing to do with your lists count so adding current bar checks in the loop wont be helpful. You should instead use Prints to identify the Count of your list and then what indexes h and h + 1 are to see if those values make sense when accessing the list.

                            I would suggest removing the following from your print: list[h] + " : " + Time[h]

                            If you get an index error then using list[h] will not be helpful to see all the values you are looping over so that should be removed so you stop getting the error. Time[h] does not make sense, your list h indexes are not BarsAgo. Time[h] would just be the most recent 247 bars from the CurrentBar or now in processing when used in your loop.

                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Many thanks Jesse for your valuable help.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ender_wiggum, Today, 09:50 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by rajendrasubedi2023, Today, 09:50 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by geotrades1, Today, 10:02 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post geotrades1  
                              Started by bmartz, Today, 09:30 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by geddyisodin, Today, 05:20 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X