Last candle indicator problem (ERR_HISTORY_NOT_FOUND 440)

 

I am trying to make a very simple indicator. I just want to see if the last candle of each period is a bear or a bull.

I want to represent it with dot (red or blue). But this does not work. I don't see anything on the indicator.


IOpen and IClose does not work. I get this error (ERR_HISTORY_NOT_FOUND  440)


A little help please!!
Thank you!!



My code:


#property copyright ""
#property link      ""
#property version   "1.00"
#property indicator_separate_window
//#property indicator_chart_window

#property indicator_buffers 2
#property indicator_plots   2
//--- plot UP
#property indicator_label1  "UP"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot DOWN
#property indicator_label2  "DOWN"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
double         UP[];
double         DOWN[];
double _open;
double _close; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UP,INDICATOR_DATA);
   SetIndexBuffer(1,DOWN,INDICATOR_DATA);    


   IndicatorSetInteger(INDICATOR_DIGITS,1);
   IndicatorSetInteger(INDICATOR_HEIGHT,60);
   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,1);
   
    
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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;
         
         
         if(limit>1)
         {
            limit=rates_total-1;
            ArrayInitialize(UP,EMPTY_VALUE);
            ArrayInitialize(DOWN,EMPTY_VALUE);
         }
         
                 
         for(int i=limit; i>0 && !IsStopped(); i--)
         { 
 
             _open = iOpen(NULL,PERIOD_M1,i);
             _close = iClose(NULL,PERIOD_M1,i);   
             Compare( 0, 0);       
   
             _open = iOpen(NULL,PERIOD_M5,i);
             _close = iClose(NULL,PERIOD_M5,i);   
             Compare( 1, 0); 

            //(double) GetLastError();//->HISTORY_NOT_FOUNUD

       }
      
        
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void Compare(double level, int index)
{      

      if(_open<_close)
      {
            UP[index]=EMPTY_VALUE;
            DOWN[index]= level;
            
      }
      else
      {
            UP[index] = level;            
            DOWN[index]=EMPTY_VALUE;
      }
   
}
 

Why are you using iXXXX functions ??? You already have all the data in OnCalculate:

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[])

I'll give an example later ...

 

Code: 'Candle Type.mq5'

//+------------------------------------------------------------------+
//|                                                  Candle Type.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot UP
#property indicator_label1  "UP"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrDeepSkyBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot DOWN
#property indicator_label2  "DOWN"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrTomato
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      Input1=9;
//--- indicator buffers
double   UPBuffer[];
double   DOWNBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UPBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DOWNBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
//--- set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-5);
//--- set as an empty value 0
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   for(int i=limit; i<rates_total; i++)
     {
      UPBuffer[i]=0.0;
      DOWNBuffer[i]=0.0;
      if(open[i]<close[i])
         UPBuffer[i]=high[i];
      else
         DOWNBuffer[i]=high[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Result:


Files:
 
Vladimir Karputov:

Code: 'Candle Type.mq5'


Result:


Thanks for your reply Vladimir !!
I was using iXXX function because I wanted to see the color of the last candle of each timeframe (last month, last week, last day, last hour, last minute) ...

I want it to get better timing. I don't care if it's in in a "separate window" or in a "chart window" but I need to see the same thing on all charts. (something like a multimeter).

Thanks for your help, your time and your code. I'm going to study it !!