How to have a variable look back?

 

Hi

If you want a constant look back like MA it's writing is simple by:

limit= rates_total - 1 - MathMax(prev_calculated, lookback)

But i need a variable look back that is calulated in the last OnCalulate run.

I tried two strategies:

1. Defining a variable look back using "While" instead of "For" like this:

   if(prev>0)
     {
      if(peaksBuffer[0]!=EMPTY_VALUE)
        {
         DrawLine(2, clrAliceBlue, "", (int)peaksBuffer[0]);
         int i= 0;
         while(peaksBuffer[i]!=EMPTY_VALUE)
           {
            i++;
           }
         DrawLine(2, clrYellow, "", (int)peaksBuffer[i]);
        }
     }   

just got wrong results.


2. Tried to save last position I needed using "Bars-i-1" like this:

   if(prev>0)
   {
   for( int i=limit-1; i>=0 ;i--)
   { 
   
      //---Bullish
      if( peaksBuffer[i]!=EMPTY_VALUE )
         {
          DrawLine(5,clrBlue,"", rates-i-1-peakOld);
          peakOld= rates-i-1-(int)peaksBuffer[i]; 
          DrawLine(1,clrYellow,"", rates-i-1-peakOld);
         }
     }
    }

... and again nothing but wrong results and wrong lines.


I'm wondering how it can draw a line in new index before updating with new data. 

Anyways I've searched a lot but there was no answer for my request. Atteched my complete code and if you see it will be your kidness. 

Thanks

Files:
test.mq4  10 kb
 
Ehsan Pr: But i need a variable look back that is calulated in the last OnCalulate run.

You don't. Lookback handles reading past earliest bar. After that, you just recalculate bar zero per tick (and bar one on the start of the new bar).

See How to do your lookbacks correctly #9#14 & #19.

 
William Roeder #:

You don't. Lookback handles reading past earliest bar. After that, you just recalculate bar zero per tick (and bar one on the start of the new bar).

See How to do your lookbacks correctly #9#14 & #19.

Yes I know that after first run rates_total - prev_calculated = 1 but if you look at my code what I need is the value of MACD for last two peaks on MACD to compare them. So how can I have those values? In other words my problem is not out of range error but is getting value of last two peaks. 

If I'm misunderstanding please explain more (I'm mostly new) or link a reference. I've red "How to do your lookbacks correctly" but didn't find my answer.

Thanks

 
Ehsan Pr #: for last two peaks on MACD to compare them. 

Move up hill until you find a peak, move down hill until you find a trough, move up hill until you find a second peak. Don't go past the end of history. Has nothing to do with lookback.

/** Finds the INDEX of the maximum local extream in an _array_, i.e.\. moving
 * up hill until you reach the top.                                        */
template<typename T>
INDEX      max_extreama(const T&   inp[],      ///<[in]Array to search
                               COUNT      size,       ///<[in]Size of a local
                               INDEX   iEnd=INV_INDEX,///<[in]One past last.
                               INDEX      iBeg=0,     ///<[in]Starting index.
                               bool    isAscend=true) /**<[in]Direction of
                                                       * the search. */{
   if(iEnd == INV_INDEX)   iEnd  = ArraySize(inp);
   if(iEnd == iBeg)        return iEnd;
   if(isAscend){
      INDEX    iExtreme = iBeg++;   T     extreme  = inp[iExtreme];
      INDEX    iLimit   = MathMin(iEnd, iExtreme + size);
      for(; iBeg < iLimit; ++iBeg){
         T     value = inp[iBeg];
         if(extreme <= value){
            iExtreme = iBeg;  extreme = value;
            iLimit   = MathMin(iEnd, iExtreme + size);
      }  }
      return iExtreme;
   }
   INDEX    iExtreme = --iEnd;   T     extreme  = inp[iExtreme];
   // INDEX is uint, can't use MathMax(iBeg, iEnd - size) as that might try
   // to go negative. Instead (iEnd - size < iBeg) -> (iEnd < iBeg + size)
   --size;  // iEnd decremented first so size is one smaller.
   INDEX    iLast    = (iExtreme <= iBeg + size) ? iBeg : iExtreme - size;
   while(iEnd > iLast){
      T     value = inp[--iEnd];
      if(extreme <= value){
         iExtreme = iEnd;  extreme = value;
         iLast    = (iExtreme <= iBeg + size) ? iBeg : iExtreme - size;
   }  }
   return iExtreme;
}
/** Finds the INDEX of the minimum local extream in an _array_, i.e.\. moving
 * down hill until you reach the bottom.                                   */
template<typename T>
INDEX      min_extreama(const T&   inp[],      ///<[in]Array to search
                               COUNT      size,       ///<[in]Size of a local
                               INDEX   iEnd=INV_INDEX,///<[in]One past last.
                               INDEX   iBeg=0,        ///<[in]Starting index.
                               bool    isAscend=true) /**<[in]Direction of
                                                       * the search */{
   if(iEnd == INV_INDEX)   iEnd  = ArraySize(inp);
   if(iEnd == iBeg)        return iEnd;
   if(isAscend){
      INDEX    iExtreme = iBeg++;   T     extreme  = inp[iExtreme];
      INDEX    iLimit   = MathMin(iEnd, iExtreme + size);
      for(; iBeg < iLimit; ++iBeg){
         T     value = inp[iBeg];
         if(value <= extreme){
            iExtreme = iBeg;  extreme = value;
            iLimit   = MathMin(iEnd, iExtreme + size);
         }
      }
      return iExtreme;
   }
   INDEX    iExtreme = --iEnd;   T     extreme  = inp[iExtreme];
   // INDEX is uint, can't use MathMax(iBeg, iEnd - size) as that might try
   // to go negative. Instead (iEnd - size < iBeg) == (iEnd < iBeg + size)
   --size;  // iEnd decremented first so size is one smaller.
   INDEX    iLast    = (iExtreme <= iBeg + size) ? iBeg : iExtreme - size;
   while(iEnd > iBeg){
      T     value = inp[--iEnd];
      if(value <= extreme){
         iExtreme = iEnd;  extreme = value;
         iLast    = (iExtreme <= iBeg + size) ? iBeg : iExtreme - size;
   }  }
   return iExtreme;
}
 
William Roeder #:

Move up hill until you find a peak, move down hill until you find a trough, move up hill until you find a second peak. Don't go past the end of history. Has nothing to do with lookback.

I have a function named MACD_PV that calculated hills and troughs with a particular algorithm and saved in an array. in this array [i] is cross position of MACD main and signal and value of array[i] is shift to reach last peak. I want next function (MACD_Trend) to read this array and draw line on last peak by using value of array as shift from index 0 and function do it well. but the problem is drawing line on peak before last peak with another color. In fact in tester, oddly two different lines will create on last earliest peak with one candle delay!

You are suggesting me a new way to calculate peaks and troughs but I want to use my function.

How can I have value of last two peaks using array. Other cells of array are empty (EMPTY_VALUE).