Download MetaTrader 5

iHighest error 4051

To add comments, please log in or register
fly7680
636
fly7680  

Using iHighest I get this error but I honestly do not know what you mean

//--- main loop
   for(int i = 0; i <= 300; i++)
     {      
    
         double val;

    
         bool Pattern = iOpen(Symbol(), PERIOD_CURRENT, 1+i)  > iClose(Symbol(), PERIOD_CURRENT, 1+i)  
                     && iOpen(Symbol(), PERIOD_CURRENT, 2+i)  > iClose(Symbol(), PERIOD_CURRENT, 2+i)
                     && iOpen(Symbol(), PERIOD_CURRENT, 3+i)  > iClose(Symbol(), PERIOD_CURRENT, 3+i)
                     && iOpen(Symbol(), PERIOD_CURRENT, 4+i)  < iClose(Symbol(), PERIOD_CURRENT, 4+i);
                    
         int N = iHighest(NULL,0,MODE_CLOSE,6+i,4+i);
        
         if(N!=-1) val = Close[N];
         else PrintFormat("Error in call iHighest. Error code=%d",GetLastError());
      
      //Indicator Buffer 1
        if( Pattern ) Buffer1[N] = val + 50*Point ;
honest_knave
Moderator
2326
honest_knave  

List of error codes

This value is most likely >= Bars, giving -1

int N = iHighest(NULL,0,MODE_CLOSE,6+i,4+i);

To check, modify your print statement:

   PrintFormat("Error in call iHighest. Error code=%d. Bars=%i start=%i",GetLastError(),Bars,4+i)
Error Codes - Appendixes - MQL4 Tutorial
Error Codes - Appendixes - MQL4 Tutorial
  • book.mql4.com
Error Codes - Appendixes - MQL4 Tutorial
fly7680
636
fly7680  
the problem is if pattern is true, It is not always performed...I do not understand why
fly7680
636
fly7680  
I always get the  Error in call iHighest. Error code=4051. Bars=101 start=143 144 145 146 147.....

honest_knave
Moderator
2326
honest_knave  
fly7680:
I always get the  Error in call iHighest. Error code=4051. Bars=101 start=143 144 145 146 147.....

You have your answer in the print statement. As I said:

honest_knave:

This value is most likely >= Bars, giving -1

int N = iHighest(NULL,0,MODE_CLOSE,6+i,4+i);
If you only have 101 bars, how can you start iHighest from 143?
fly7680
636
fly7680  
I can not understand you wonder really sorry. if I write 4+i think starting from the candle 4 to the Patern...No?
honest_knave
Moderator
2326
honest_knave  
fly7680:
I can not understand you wonder really sorry. if I write 4+i think starting from the candle 4 to the Patern...No?

You don't have enough bars.

fly7680:
I always get the  Error in call iHighest. Error code=4051. Bars=101 start=143 144 145 146 147.....

If you only have 101 bars available, you cannot use bar 143. It doesn't exist.

Why have you picked 300? You should pick something actually related to the bars on the chart. 

for(int i = 0; i <= 300; i++)

This post may help you:

Forum on trading, automated trading systems and testing trading strategies

Range not behaving as expected

whroeder1, 2016.05.11 18:55

First define your maximum lookback.
int lookback = ... // iMA(period) has look back of period.
                        // buffer[i+2] has look back of 2 (as TimeSeries)
                        // buffer[i-2] has look back of 2 (not TimeSeries)
                       // use maximum of all.
int lookback = ... // iMA(period) has look back of period.
                   // buffer[i+2] has look back of 2 (as TimeSeries)
                   // buffer[i-2] has look back of 2 (not TimeSeries)
                   // use maximum of all.
Old way, counting down as a TimeSeries
   int counted = IndicatorCounted();
   for(int iBar = Bars - 1 - MathMax(lookback, counted); iBar >= 0; --iBar){
      // Timeseries and Indicators Access uses index=iBar
   }
New way, counting down as a TimeSeries (Bars always equals rates_total so can be substituted.)
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar){
      // Timeseries and Indicators Access uses index=iBar
      https://docs.mql4.com/series
   }
   return rates_total-1; // Recalculate current bar next tick.

Returning rates_total-1 makes prev_calculated the same as IndicatorCounted().

Alternatively if you return rates_total then must decrement prev_calculated if non-zero at the beginning of On_Calculate to recalculate the current bar.

Always count down so you're not using future values.

New way, counting up, not a TimeSeries.
   set all accessed arrays and buffers to ArraySetAsSeries(false) Not the Predefined Variables

   for(int iBar = MathMax(lookback, prev_calculated; iBar < Bars; ++iBar){
      // To access a Timeseries use index=Bars -1- iBar
      // and convert any results back index=Bars -1- result
   }
   return rates_total-1; // Recalculate current bar next tick.
Always count up so you're not using future values.


 

whroeder1
15934
whroeder1  
fly7680
636
fly7680  

I fixed the code but I have the same problem ... this indicator has to read data to the historic but it does not properly.my problem is in this line, but I do not know why

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
         const int prev_calculated,
         const datetime& time[],
         const double& open[],
         const double& high[],
         const double& low[],
         const double& close[],
         const long& tick_volume[],
         const long& volume[],
         const int& spread[])
         {
         int limit = rates_total - prev_calculated;
        
         //--- initial zero
         if(prev_calculated < 1) limit=MathMin(5000-1, rates_total-1-50);
         {
        
        
         //--- main loop
         for(int i = limit-1; i >= 0; i--)
         {  
      
    
         double val;
         int N;
    
         bool Pattern = Open [1+i]  > Close[ 1+i]  
                     && Open [2+i]  > Close[ 2+i]
                     && Open [3+i]  > Close[ 3+i]
                     && Open [4+i]  < Close[ 4+i];            
        
      
      //Indicator Buffer 1
        if( Pattern == true )
        
        {
         N = iHighest(NULL,0,MODE_CLOSE,6+i,4+i);        
         if(N!=-1) val = High[N];
         else PrintFormat("Error in call iHighest. Error code=%d. Bars=%i start=%i",GetLastError(),Bars,4+i);
         Buffer1[N] = val + 30*Point;
         Print("Numero Candela: "+IntegerToString(N));
        }
       }
      }
        
    

   return(rates_total);
  }
honest_knave
Moderator
2326
honest_knave  
What did your print statements say?
fly7680
636
fly7680  
There are no errors but the number of the candle is always 4 or not concide with the 6 or 4. the candle count with the highest closing does not work.
12
To add comments, please log in or register