Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Series<T> not saving values properly

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

    Series<T> not saving values properly

    Hello!

    I am struggling since months already with a piece of code related to Series<T>.Allow me to start with pieces of codes:
    HTML Code:
      myRSI[0]        = RSI()[0];
      myFirstBuf[0]   = WMA(myRSI, Period1)[0]
      mySecondBuf[0]  = SMA(myRSI, Period2)[0];
    The Print() function pups up expected values.
    From here, I add a condition as followed:
    HTML Code:
     if(myFirstBuf[1] < mySecondBuf[1] && myFirstBuf[0] > mySecondBuf[0])
     {
       mySwingLow[0] = MIN(myFirstBuf, 5)[0];
       Print(mySwingLow[0] + " >>>>> " + mySwingLow[1] + "--------" + " : " + Time[0]);
     }
    Here, I am realizing that the values for mySwingLow[1] are zero!!! That means, the Series<T> does not save values at Bar[1].
    What could be the problem? What could be wrong in the code?

    I would appreciate any help.

    #2
    Hello Stanfillirenfro,

    Is the series value only assigned if the condition is true?

    Is this not assigned outside of the condition as well?

    Was the condition true one bar ago?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_ChelseaB for your reply.
      Is the series value only assigned if the condition is true?
      Yes!
      Is this not assigned outside of the condition as well?
      After failing to have expected results, I have assigned the Series<T> outside of the condition and I do have the values for Series[0] and Series[1] as shwon on the attached pictures.

      In the condition, Series[1] turns to zero all the time!
      Attached Files

      Comment


        #4
        Hello Stanfillirenfro,

        The output file you provided has no labels. This is to me just a file of random numbers.

        If the [1] previous bar is 0, then 1 bar ago you either did not set a value or set the value to 0.

        Try setting the value on every bar outside of any condition logic block above the code that is using the value.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Many thanks NinjaTrader_ChelseaB for youtr reply.

          I have printed the Series<T> both outise and inside the condition (see attached pictures)

          Inside the condition, the values of Series[1] are zero.
          Attached Files

          Comment


            #6
            Hello Stanfillirenfro,

            Further debugging will be needed.

            Try the following:

            Code:
            mySwingLow[0] = MIN(myFirstBuf, 5)[0];
            
            Print(string.Format( "{0} | CurrentBar: {1}, value assigned, mySwingLow[0]: {2}", Time[0], CurrentBar, mySwingLow[0] ));
            
            if (CurrentBar < 1)
            return;
            
            Print(string.Format( "{0} | before condition, mySwingLow[0]: {1}, mySwingLow[1]: {2}", Time[0], mySwingLow[0], mySwingLow[1] ));
            
            if(myFirstBuf[1] < mySecondBuf[1] && myFirstBuf[0] > mySecondBuf[0])
            {
            Print(string.Format( "{0} | condition logic block, mySwingLow[0]: {1}, mySwingLow[1]: {2}", Time[0], mySwingLow[0], mySwingLow[1] ));
            }
            Temporarily comment out all other prints.

            Provide the output saved to a text file​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Many thanks NinjaTrader_ChelseaB for your help.

              The print of the code you sent me has given the values on the attached files.
              The values of Series[0] and Series[1] in the condition logic bloc are the same. See attached file please!
              Weird!!!
              Attached Files

              Comment


                #8
                Hello Stanfillirenfro,

                This should give you a starting point to work with.

                The output shows the previous bar value is available when the series is assigned a value outside of the condition.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Many thanks NinjaTrader_ChelseaB for your reply.

                  Yes, the output provides the good starting point to work with.
                  Outside of the condition, every single bar is concerned and therfore is it easy to have the value of each bar outside of the bar. In the condition however, only the value of the lowest bar among the five previous bars is saved. The bars 1, 2, 3 and four could not be concerned if they are not the lowest bars. And there is the big problem! I have even used Queue and converted it to Array to save the values in the condition, but I have the same result that the value of the Bar[1] inside of the condition is zero. I would appreciate if you could give a tip to overcome this problem.

                  Thanks!

                  Comment


                    #10
                    Hello Stanfillirenfro,

                    I would say its something in your logic.

                    The sample code I had you test was to show the values are available.

                    You should debug your logic with prints to understand why your logic does not work and the logic I provided does.

                    As demonstrated, you can access the previous bar values when the values are being set for every bar.

                    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Have you setup sufficient guard code to make sure you have enough bars?

                      More info about Series<T> and guard code is here, here, here, and here.
                      Last edited by bltdavid; 02-07-2025, 03:43 PM.

                      Comment


                        #12
                        Many thanks NinjaTrader_ChelseaB and bltdavid for your help and advices.

                        HTML Code:
                        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script.
                        NinjaTrader_ChelseaB, no, I want to achieve this indicator myself. Whatever the time to complete it will be, I will just keep trying. thanks anyway for your support.

                        bltdavid, this code is just a part of the whole indicator. I have and indicator with the same variables and condition which draws arrows with the same condition.
                        HTML Code:
                        if(myFirstBuf[1] < mySecondBuf[1] && myFirstBuf[0] > mySecondBuf[0])
                        {
                          DrawArrow();  
                        }
                        I have arrows with this condition. What is causing me problem is that I am trying to save giving values of some swings when this condition is met.
                        Drawing the arrows is no problem. But saving values with the same condition should also not be a problem! Weird!!!

                        Thanks for your help!

                        Comment


                          #13
                          Originally posted by Stanfillirenfro View Post
                          Here, I am realizing that the values for mySwingLow[1] are zero!!! That means, the Series<T> does not save values at Bar[1].
                          What could be the problem? What could be wrong in the code?​
                          The reason I felt motivated to chime in was this:

                          You're using SMA/WMA and specifying Period1/Period2, which says
                          to me that you should have MAX(Period1, Period2) bars present to
                          have accurate values from those indicators -- thus my suggestion
                          that you confirm your guard code to make sure you have enough
                          bars.

                          Also, your use of the MIN indicator shows you're wanting to look
                          at the past 5 bars -- well, have you made sure that 5 bars actually
                          exist before that code is executed?

                          Btw, do realize MIN(myFirstBuf, 5)[0] will most likely always
                          return zero if any of the most recent 5 values in myFirstBuf are zero,
                          since that's exactly what you're asking MIN to do -- ya know, return the
                          lowest value of the last 5 values, well, zero will always be lower than
                          any real MA values you've accumulated from an MA indicator, esp if
                          you're assigning zero to myFirstBuf[0] as a default value(*).

                          Ergo, the guard code can be critical to make sure things work correctly
                          and should not be overlooked.

                          -=o=-

                          Your code snippets didn't provide enough detail (in the way of guard
                          code), so I thought I'd make a point to add my 2˘ to the discussion.

                          Just food for thought.



                          (*) Make sure you are assigning a reasonable default value to every
                          Series<T> slot, just in case your boolean conditions prevent new values
                          from being assigned later inside your if statements. Since you're using
                          MIN to find the lowest of the last 5 values, avoid using 0 and use some
                          relatively high value (like double.MaxValue) as your default value.
                          Last edited by bltdavid; 02-07-2025, 10:26 PM.

                          Comment


                            #14
                            Many thanks bltdavid for your reply and explanation.

                            I have made some researches on
                            Code:
                            double.MinValue
                            and printed it out.
                            I have the following number:
                            Code:
                            -1,79769313486232E+308
                            As far as I understand, double.MinValue is already a given number. In my case, I want to save the number out of my condition in an array. I am not seeing how double.MinValue can be considered here.

                            The idea in my case is to save numbers ONLY after a condition is met. And only the number everytime the condition is met. After printing the expected numbers, the output window shows values even after the condition is met and also before!!! Let's say I have six consecutives last bars. The condition is met on the fifth bar from the left, that means Bar[1]. Let's say after a certain time, we have 10 MORE bars on the char and the condition is still in place. The output window shows that the numbers are still printed even for the sixth, seventh eigth... bars. But these bars are not concerned by the condition. Why is the numbers saved for them?

                            I thought Series<T> saves values for every single bars. I am looking for an array able to save the values ONLY when the condition is met. So I used a
                            Code:
                            private ArrayList arlistLow = new ArrayList();
                            and saved values inside. The problem is that every single bar is concerned.

                            Is there an array to save only the values when the condition is met and not for every singe bars?

                            Thanks!

                            Comment


                              #15
                              First, my suggestion was to use double.MaxValue, not double.MinValue.

                              The idea is that if the last 10 bars had all been initialized to double.MaxValue,
                              but only one time in those last 10 bars was your condition met, then that bar's
                              value would be returned by MIN, because that value would the lowest of the
                              last 10 values, because double.MaxValue is such a large number.

                              If you're using MIN, then double.MaxValue is a good initializer value. Think
                              about it, if later MIN actually returns double.MaxValue, that means no MA
                              values were saved into your Series, which means your 'save condition' was
                              never met -- because none of the last 10 double.MaxValue initial values
                              in the Series were overwritten, so MIN was forced to return the lowest of
                              the last 10 values, but all 10 values were still double.MaxValue.

                              Make sense?

                              Conversely, if you're using MAX, then use double.MinValue instead.

                              It just depends on your use of MIN/MAX whether or not using initial
                              values of Min/MaxValue makes any sense. Your logic dictates
                              the code you need.

                              -=o=-

                              As to your second point, if you don't care about one value per bar, then
                              use a List<double>, which allows you to store any number of values
                              'per bar' (including no values at all).

                              private List<double> myList = new List<double>;

                              But keep in mind ...
                              The difficulty that you'll have is trying to relate the last, say, 10 values
                              stored in your List<double> to how many bars it took to accumulate
                              those 10 values. In other words, use a List<double> only if knowing
                              how many bars it took to populate your List<double> is irrelevant.

                              Just my 2˘.

                              Last edited by bltdavid; 02-13-2025, 08:38 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kalalex, Yesterday, 01:31 AM
                              2 responses
                              24 views
                              0 likes
                              Last Post AndyM
                              by AndyM
                               
                              Started by bxl2019, 03-16-2025, 12:34 PM
                              3 responses
                              19 views
                              0 likes
                              Last Post Leeroy_Jenkins  
                              Started by Hilltree, Yesterday, 09:00 AM
                              2 responses
                              16 views
                              0 likes
                              Last Post Hilltree  
                              Started by samish18, Yesterday, 06:46 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post samish18  
                              Started by HappyTrader76, Yesterday, 04:14 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post HappyTrader76  
                              Working...
                              X