Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A Series<T> does not save properly!

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

    A Series<T> does not save properly!

    Hello!
    I have two moving averages.
    When the fast MA crosses above the slower, I save the open of the bar on which the cross has happened in the Series<T> as followed:
    HTML Code:
    myOpen[0] = Open[0];
    Later on, as the uptrend is becoming stronger, a pullback occurs and the fast MA crosses below the slower one. After couple of bars,
    the faster MA crosses AGAIN above the slower one and I save the open of the bar on which the cross has occured again in:
    HTML Code:
    myOpen[0] = Open[0];
    On this point, I compare the two values in myOpen:
    HTML Code:
    If(myOpen[0] > myOpen[1])
    {
     DrawArrow();
    }​
    ​​

    Unfortunatelly, everytime the fast MA crosses above, I am seeing the arrow, and this is not what I expect.

    Finally, I have the following code:​
    HTML Code:
    private Series<double> myOpen;
    
    if(MAFast[1] < MASlow[1] && MAFast[0] > MASlow[0])
    {
      myOpen[0] = Open[0];
    }
    if(myOpen[0] > myOpen[1])
    {
      DrawArrow();
    }​
    Could someone points out where I am mistaking?

    Many thanks in advance!​

    #2
    Hello Stanfillirenfro,

    For this type of question it would be best to use prints to see what the values you are comparing are.

    If your condition which sets the series is not happening for every bar then using [0] and [1] bars ago doesn't make sense. A series increments for every bar on the chart so if you wanted to use [0] and [1] bars ago the current and previous bar values need to be set to two different values.

    The code you provided won't set a value for every bar so you would need to know when in time the last and most recent values were stored so you can use the correct BarsAgo to find those bars.

    If you will only use 2 datapoints for the myOpen at a time you could switch the series to two double variables and set those instead. That would look like the following:


    Code:
    private double myOpen0;
    private double myOpen1;
    
    if(MAFast[1] < MASlow[1] && MAFast[0] > MASlow[0])
    {
       myOpen1 = myOpen0; // shift the current value to the other variable 
       myOpen0 = Open[0];
    }
    if(myOpen0 > 0 && myOpen1 > 0 && myOpen0 > myOpen1)
    {
    DrawArrow();
    }​

    Comment


      #3
      Many thanks NinjaTrader_Jesse for your valuable help.

      Now I am taking the indicator to another level and instead of saving the Open[0] etc, I am saving the lowest value of the fast moving average corresponding to the lfive previous bars.
      I have proceeded as follow:

      HTML Code:
      myOpen0 = MIN(EMA(Low, 5))[0];
      Now I have:
      HTML Code:
      private double myOpen0;
      private double myOpen1;
      
      if(MAFast[1] < MASlow[1] && MAFast[0] > MASlow[0])  // (a)
      {
      myOpen1 = myOpen0; // shift the current value to the other variable
      myOpen0 = MIN(EMA(Low, 5))[0];
      Print("myOpen0" + " Size " + ":  " + myOpen0 + " : " + Time[0]);
      }
      if(myOpen0 > myOpen1)
      {
      DrawArrow();
      }​​
      The arrows are still appearing on every cross independently if myOpen0 is greather than myOpen1 or not.

      How to save properly the value of myOpen0 when the condition (a) is met?

      I have used the Print() function and I am having the values of myOpen0 exactly after the faster MA crosses above the slower one. I am not having the lowest value of the five previous bars.

      Many thanks in advance!
      Last edited by Stanfillirenfro; 07-28-2023, 07:09 AM.

      Comment


        #4
        Hello Stanfillirenfro,

        It looks like you are using MIN incorrectly, you have done a MIN with no period. You have supplied a EMA of the Low series which is an EMA of 5 period. For 5 previous bars you need to use 5 as the period on the MIN

        Code:
        myOpen0 = MIN(EMA(Low, 5), 5)[0];

        Comment


          #5
          Many Thnaks NinjaTrader_Jesse for your reply.

          I have corrected the mistake and there is NO improvment so far.

          Please have a look on the picture attached. The lime arrows represent the bars on which the faster MA has crossed above the slowest. The horizontal line represents the lowest value of the faster MA on app. 25 bars. As one can see, this value on bar 1 is lower than that on bar 2. So according to the my code, since I am expecting the value of the MA on bar#2 to be higher than that on bar#1 to have the arrow, something is going wrong here. I have used the Print() function to catch the different values of MA (myOpen0), but these values, instead of being the values of MA on bars 1 and 2, are the values of MA on the bars with arrow. I am suspecting the MIN() function to NOT be suitable for tis case, but I am failing to find an appropriate function.

          Again, my goal is to be able to save the lowest values of the MA on 5 bars prior to the cross of the faster MA above the slowest MA.

          I would appreciate any help!
          Attached Files

          Comment


            #6
            Hello Stanfillirenfro,

            If you wanted the value 5 bars before the cross you need to use 5 bars ago, you used 0 bars ago in your original code.

            myOpen0 = MIN(EMA(Low, 5), 5)[0];

            This reads as the lowest low series EMA 5 Period value with a period being checked of the previous 5 bars. If you wanted the lowest value of the MA 5 bars before the cross you need to use

            myOpen0 = MIN(EMA(Low, 5), 5)[5];

            This reads as the lowest low series EMA 5 Period value with a period being checked of the previous 5 bars, getting the MIN value of 5 bars ago.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            563 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            329 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            547 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X