Different results from ArrayMaximum/ArrayMinimum on same time set of data!!!

 

Hi

Please help me out to debug problem/error in my code.

I am getting different values for Highest and Lowest prices, though the data set is for the same period.

The CurrOpen, PrevOpen and PrevClose are calculated correctly on each NewH01 bar, but Highest/Lowest prices are changed.

Relevant section of the code is highlighted where the error occurring.

void CiCPR::calculateCPR(int idxPTF) {

        //... STEP[A] Prepare data for idxPTF bar
                int              day = idxPTF;
                datetime aTimeM01[];                                                                            ArraySetAsSeries(aTimeM01,true);
                datetime copyStart = CPRSession[1+day].Begin;                                                   // Calculation on Previous Day's data
                datetime copyStop  = CPRSession[1+day].End;

                int count       = 0;
                while(count <= 5 && !IsStopped()) {                                                             // Maximum '5' iteration allowed to CopyTime()
                        if(CopyTime(mSymbol,PERIOD_M1,copyStart,copyStop,aTimeM01) == -1)                       { Sleep(50);    count++; }
                        else                                                                                    break;
                }
                int                      bars = ArraySize(aTimeM01);
                double   aHighM01[];     ArraySetAsSeries(aHighM01,true);
                double   aLowM01[];      ArraySetAsSeries(aLowM01,true);
                CopyHigh(mSymbol,PERIOD_M1,aTimeM01[0],aTimeM01[bars-1],aHighM01);
                CopyLow(mSymbol,PERIOD_M1,aTimeM01[0],aTimeM01[bars-1],aLowM01);

        //--- STEP[B] Calculate CPR/Support/Resistance levels
                mCurrOpen   = iOpen(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[0]));        // Open price  at current Bar
                mPrevOpen   = iOpen(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[bars-1]));   // Open price  at CPR Start Bar
                mPrevClose  = iClose(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[1]));       // Close price at previous Bar

                int idxFrom = iBarShift(mSymbol,PERIOD_M1,aTimeM01[bars-1]);
                int idxTo               = iBarShift(mSymbol,PERIOD_M1,aTimeM01[0]);

                //int idxHigh = ArrayMaximum(aHighM01,0,WHOLE_ARRAY);
                //int idxLow  = ArrayMinimum(aLowM01,0,WHOLE_ARRAY);

                int idxHigh = iHighest(mSymbol,PERIOD_M1,MODE_HIGH,idxFrom,idxTo);
                int idxLow  = iLowest(mSymbol,PERIOD_M1,MODE_LOW,idxFrom,idxTo);

                mPrevHigh   = iHigh(mSymbol,PERIOD_M1,idxHigh);                                               // Highest price in CPRSession
                mPrevLow    = iLow(mSymbol,PERIOD_M1,idxLow);                                                 // Lowest  price in CPRSession
                PrintFormat("day[%d] bars[%d] From[%s] to [%s] | CurrOpen[%.2f] PrevOpen[%.2f] PrevHigh[%5d][%.2f] PrevLow[%5d][%.2f] PrevClose[%.2f]",day,bars,
                                                                TimeToString(aTimeM01[bars-1]),TimeToString(aTimeM01[0]),mCurrOpen,mPrevOpen,idxHigh,mPrevHigh,idxLow,mPrevLow,mPrevClose);
/*
... results using 'ArrayMaximum' and 'ArrayMinimum' functions | Seach max/min bar within aHighM01[] and aLowM01[]
2024.05.23 17:33:48.077 2024.05.01 01:01:20   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[ 1193][2335.07] PrevLow[  525][2311.19] PrevClose[2328.72]
2024.05.23 17:33:48.077 2024.05.01 01:01:20   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[  167][2296.43] PrevLow[ 1300][2333.82] PrevClose[2344.69]
2024.05.23 17:33:48.078 2024.05.01 01:01:20   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 1333][2334.38] PrevLow[  624][2316.93] PrevClose[2336.76]
2024.05.23 17:33:48.078 2024.05.01 01:01:20   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1114][2331.32] PrevLow[   10][2285.25] PrevClose[2313.11]

2024.05.23 17:33:48.994 2024.05.01 02:00:00   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[ 1193][2334.22] PrevLow[  525][2305.63] PrevClose[2328.72]
2024.05.23 17:33:48.994 2024.05.01 02:00:00   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[  167][2292.28] PrevLow[ 1300][2332.83] PrevClose[2344.69]
2024.05.23 17:33:48.994 2024.05.01 02:00:00   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 1333][2333.21] PrevLow[  624][2312.88] PrevClose[2336.76]
2024.05.23 17:33:48.994 2024.05.01 02:00:00   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1114][2329.94] PrevLow[   10][2290.88] PrevClose[2313.11]

2024.05.23 17:33:49.887 2024.05.01 03:00:00   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[ 1193][2330.10] PrevLow[  525][2298.71] PrevClose[2328.72]
2024.05.23 17:33:49.887 2024.05.01 03:00:00   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[  167][2292.13] PrevLow[ 1300][2334.67] PrevClose[2344.69]
2024.05.23 17:33:49.887 2024.05.01 03:00:00   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 1333][2334.61] PrevLow[  624][2304.96] PrevClose[2336.76]
2024.05.23 17:33:49.887 2024.05.01 03:00:00   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1114][2328.55] PrevLow[   10][2291.05] PrevClose[2313.11]

... result using 'iHighest' and 'iLowest' functions | With given time and finding barIndex to search for Highest/Lowest value Index bar
2024.05.23 17:50:05.948 2024.05.01 01:01:20   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[10713][2402.46] PrevLow[7585][2291.31] PrevClose[2328.72]
2024.05.23 17:50:05.948 2024.05.01 01:01:20   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[ 3458][2352.59] PrevLow[7585][2291.31] PrevClose[2344.69]
2024.05.23 17:50:05.948 2024.05.01 01:01:20   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 3458][2352.59] PrevLow[5194][2304.97] PrevClose[2336.76]
2024.05.23 17:50:05.949 2024.05.01 01:01:20   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1654][2346.72] PrevLow[ 550][2309.53] PrevClose[2313.11]

2024.05.23 17:50:20.227 2024.05.01 02:00:00   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[10821][2413.50] PrevLow[7644][2291.31] PrevClose[2328.72]
2024.05.23 17:50:20.227 2024.05.01 02:00:00   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[ 3517][2352.59] PrevLow[7644][2291.31] PrevClose[2344.69]
2024.05.23 17:50:20.228 2024.05.01 02:00:00   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 3517][2352.59] PrevLow[5253][2304.97] PrevClose[2336.76]
2024.05.23 17:50:20.228 2024.05.01 02:00:00   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1713][2346.72] PrevLow[ 609][2309.53] PrevClose[2313.11]

2024.05.23 17:50:27.266 2024.05.01 03:00:00   day[3] bars[1378] From[2024.04.24 15:00] to [2024.04.25 14:59] | CurrOpen[2328.68] PrevOpen[2316.18] PrevHigh[10909][2417.81] PrevLow[7704][2291.31] PrevClose[2328.72]
2024.05.23 17:50:27.266 2024.05.01 03:00:00   day[2] bars[1378] From[2024.04.25 15:00] to [2024.04.26 14:59] | CurrOpen[2344.69] PrevOpen[2328.57] PrevHigh[ 3577][2352.59] PrevLow[7704][2291.31] PrevClose[2344.69]
2024.05.23 17:50:27.266 2024.05.01 03:00:00   day[1] bars[1374] From[2024.04.26 15:00] to [2024.04.29 14:59] | CurrOpen[2336.78] PrevOpen[2344.93] PrevHigh[ 3577][2352.59] PrevLow[5313][2304.97] PrevClose[2336.76]
2024.05.23 17:50:27.266 2024.05.01 03:00:00   day[0] bars[1377] From[2024.04.29 15:00] to [2024.04.30 14:59] | CurrOpen[2313.05] PrevOpen[2337.05] PrevHigh[ 1773][2346.72] PrevLow[ 669][2309.53] PrevClose[2313.11]

*/
} // End of method calculateCPR()

Your help to identify cause of error will be highly appreciated.

Regards.

 

Hi @Sardion Maranatha 

I am trying to get you right.

I have copied High / Low prices in array for bars with specified time start and stop. So the copyXXXX() should always return the same bars within the specified time range on each call at different times.

Within this array highest and lowest bar index should remain same.

I will highly appreciate, if you can show me alternate way to get these data.

Regards.

Sardion Maranatha
Sardion Maranatha
  • 2024.05.09
  • www.mql5.com
Trader's profile
 

Hi @Sardion Maranatha

I think I have got your point and have tried using the following change:

                CopyHigh(mSymbol,PERIOD_M1,TimeCurrent(),aTimeM01[bars-1],aHighM01);
                CopyLow(mSymbol,PERIOD_M1,TimeCurrent(),aTimeM01[bars-1],aLowM01);
        //+---------------------------------------------------------------------------------------------------------------------------+
        //| STEP[B] Calculate CPR/Support/Resistance levels
        //+---------------------------------------------------------------------------------------------------------------------------+
                mCurrOpen   = iOpen(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[0]));                        // Open price  at current Bar
                mPrevOpen   = iOpen(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[bars-1]));   // Open price  at CPR Start Bar
                mPrevClose  = iClose(mSymbol,PERIOD_M1,iBarShift(mSymbol,PERIOD_M1,aTimeM01[1]));                       // Close price at previous Bar

                int idxStart = iBarShift(mSymbol,PERIOD_M1,aTimeM01[0]);
                int barCount = iBarShift(mSymbol,PERIOD_M1,aTimeM01[bars-1]) - idxStart;

                int idxHigh = ArrayMaximum(aHighM01,idxStart,barCount);
                int idxLow  = ArrayMinimum(aLowM01,idxStart,barCount);

However prices are still changing.

2024.05.24 14:06:49.591 2024.03.08 01:01:20   day[3] bars[1374] From[2024.03.01 15:00] to [2024.03.04 14:59] | CurrOpen[2082.64] PrevOpen[2052.42] PrevHigh[ 5514][2083.98] PrevLow[ 4811][2083.45] PrevClose[2082.64]
2024.05.24 14:06:49.591 2024.03.08 01:01:20   day[2] bars[1378] From[2024.03.04 15:00] to [2024.03.05 14:59] | CurrOpen[2124.33] PrevOpen[2082.55] PrevHigh[ 4140][2115.23] PrevLow[ 3437][2124.06] PrevClose[2124.33]
2024.05.24 14:06:49.591 2024.03.08 01:01:20   day[1] bars[1378] From[2024.03.05 15:00] to [2024.03.06 14:59] | CurrOpen[2134.06] PrevOpen[2124.45] PrevHigh[ 2762][2127.93] PrevLow[ 2059][2126.48] PrevClose[2134.09]
2024.05.24 14:06:49.591 2024.03.08 01:01:20   day[0] bars[1378] From[2024.03.06 15:00] to [2024.03.07 14:59] | CurrOpen[2158.58] PrevOpen[2134.15] PrevHigh[ 1384][2148.81] PrevLow[  681][2156.36] PrevClose[2158.60]

2024.05.24 14:07:00.733 2024.03.08 02:00:00   day[3] bars[1374] From[2024.03.01 15:00] to [2024.03.04 14:59] | CurrOpen[2082.64] PrevOpen[2052.42] PrevHigh[ 5514][2083.42] PrevLow[ 4811][2082.69] PrevClose[2082.64]
2024.05.24 14:07:00.733 2024.03.08 02:00:00   day[2] bars[1378] From[2024.03.04 15:00] to [2024.03.05 14:59] | CurrOpen[2124.33] PrevOpen[2082.55] PrevHigh[ 4140][2115.30] PrevLow[ 3437][2125.58] PrevClose[2124.33]
2024.05.24 14:07:00.733 2024.03.08 02:00:00   day[1] bars[1378] From[2024.03.05 15:00] to [2024.03.06 14:59] | CurrOpen[2134.06] PrevOpen[2124.45] PrevHigh[ 2762][2127.98] PrevLow[ 2059][2128.51] PrevClose[2134.09]
2024.05.24 14:07:00.733 2024.03.08 02:00:00   day[0] bars[1378] From[2024.03.06 15:00] to [2024.03.07 14:59] | CurrOpen[2158.58] PrevOpen[2134.15] PrevHigh[ 1384][2147.32] PrevLow[  681][2155.29] PrevClose[2158.60]

2024.05.24 14:07:01.632 2024.03.08 03:00:00   day[3] bars[1374] From[2024.03.01 15:00] to [2024.03.04 14:59] | CurrOpen[2082.64] PrevOpen[2052.42] PrevHigh[ 5514][2082.58] PrevLow[ 4811][2082.60] PrevClose[2082.64]
2024.05.24 14:07:01.632 2024.03.08 03:00:00   day[2] bars[1378] From[2024.03.04 15:00] to [2024.03.05 14:59] | CurrOpen[2124.33] PrevOpen[2082.55] PrevHigh[ 4140][2115.60] PrevLow[ 3437][2122.38] PrevClose[2124.33]
2024.05.24 14:07:01.632 2024.03.08 03:00:00   day[1] bars[1378] From[2024.03.05 15:00] to [2024.03.06 14:59] | CurrOpen[2134.06] PrevOpen[2124.45] PrevHigh[ 2762][2126.82] PrevLow[ 2059][2132.97] PrevClose[2134.09]
2024.05.24 14:07:01.632 2024.03.08 03:00:00   day[0] bars[1378] From[2024.03.06 15:00] to [2024.03.07 14:59] | CurrOpen[2158.58] PrevOpen[2134.15] PrevHigh[ 1384][2148.68] PrevLow[  681][2154.38] PrevClose[2158.60]

2024.05.24 14:08:35.773 2024.03.08 04:00:00   day[3] bars[1374] From[2024.03.01 15:00] to [2024.03.04 14:59] | CurrOpen[2082.64] PrevOpen[2052.42] PrevHigh[ 5514][2079.77] PrevLow[ 4860][2082.70] PrevClose[2082.64]
2024.05.24 14:08:35.773 2024.03.08 04:00:00   day[2] bars[1378] From[2024.03.04 15:00] to [2024.03.05 14:59] | CurrOpen[2124.33] PrevOpen[2082.55] PrevHigh[ 4140][2112.19] PrevLow[ 3486][2122.99] PrevClose[2124.33]
2024.05.24 14:08:35.773 2024.03.08 04:00:00   day[1] bars[1378] From[2024.03.05 15:00] to [2024.03.06 14:59] | CurrOpen[2134.06] PrevOpen[2124.45] PrevHigh[ 2762][2125.68] PrevLow[ 2108][2132.86] PrevClose[2134.09]
2024.05.24 14:08:35.773 2024.03.08 04:00:00   day[0] bars[1378] From[2024.03.06 15:00] to [2024.03.07 14:59] | CurrOpen[2158.58] PrevOpen[2134.15] PrevHigh[ 1384][2145.79] PrevLow[  730][2155.98] PrevClose[2158.60]
 

Hi

I think iHighest andiLowest functions are not compatible for mt5 (just like iBarShift), you need to use your own version of this function – which would work for mt5, something like this:

int iHighestMQL4(string symbol,
                 int tf,
                 int type,
                 int count=WHOLE_ARRAY,
                 int start=0)
  {
   if(start<0) return(-1);
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   if(count<=0) count=Bars(symbol,timeframe);
   if(type<=MODE_OPEN)
     {
      double Open[];
      ArraySetAsSeries(Open,true);
      CopyOpen(symbol,timeframe,start,count,Open);
      return(ArrayMaximum(Open,0,count)+start);
     }
   if(type==MODE_LOW)
     {
      double Low[];
      ArraySetAsSeries(Low,true);
      CopyLow(symbol,timeframe,start,count,Low);
      return(ArrayMaximum(Low,0,count)+start);
     }
   if(type==MODE_HIGH)
     {
      double High[];
      ArraySetAsSeries(High,true);
      CopyHigh(symbol,timeframe,start,count,High);
      return(ArrayMaximum(High,0,count)+start);
     }
   if(type==MODE_CLOSE)
     {
      double Close[];
      ArraySetAsSeries(Close,true);
      CopyClose(symbol,timeframe,start,count,Close);
      return(ArrayMaximum(Close,0,count)+start);
     }
   if(type==MODE_VOLUME)
     {
      long Volume[];
      ArraySetAsSeries(Volume,true);
      CopyTickVolume(symbol,timeframe,start,count,Volume);
      return(ArrayMaximum(Volume,0,count)+start);
     }
   if(type>=MODE_TIME)
     {
      datetime Time[];
      ArraySetAsSeries(Time,true);
      CopyTime(symbol,timeframe,start,count,Time);
      return(ArrayMaximum(Time,0,count)+start);
      //---
     }
   return(0);
  }

Have a nice weekend

 
Marzena Maria Szmit #:

Hi

I think iHighest andiLowest functions are not compatible for mt5 (just like iBarShift), you need to use your own version of this function – which would work for mt5, something like this:

Have a nice weekend

Hi

This is a new piece of information for me :)

Happy weekend to you too.

 

UPDATE :

I have been able to resolve this with the following code.

CopyHigh(mSymbol,PERIOD_M1,TimeCurrent(),aTimeM01[bars-1],aHighM01);
CopyLow(mSymbol,PERIOD_M1,TimeCurrent(),aTimeM01[bars-1],aLowM01);

int idxStart = iBarShift(mSymbol,PERIOD_M1,aTimeM01[0]);
int idxLast  = iBarShift(mSymbol,PERIOD_M1,aTimeM01[bars-1]);

int idxHigh = ArrayMaximum(aHighM01,idxStart,idxLast-idxStart+1);
int idxLow  = ArrayMinimum(aLowM01,idxStart,idxLast-idxStart+1);

mPrevHigh   = iHigh(mSymbol,PERIOD_M1,idxHigh);
mPrevLow    = iLow(mSymbol,PERIOD_M1,idxLow);

Thanks and wish all happy weekend.