Indicator does not show historical values

 

Hello.

The custom indicator that i've created does not show historical values on chart.

When i try to attach the indicator on any chart i can only see the values beginning from current bar. If i change the broker for some other company it works normal but if i change the symbols GBPUSD-GBPJPY to GBPJPY-GBPUSD, the problem occurs again.

Please help me.

//+------------------------------------------------------------------+
//|                                                          au1.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "www.mql5.com"
#property link      "http:www.mql5.com"
#property indicator_chart_window
#property indicator_buffers 9
#property indicator_plots   1
#property indicator_applied_price PRICE_CLOSE

#property indicator_label1  "au1";
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#include <MovingAverages.mqh>
//--- input parameters

input string   S1_Symbol            =  "GBPJPY";   
input string   S2_Symbol            =  "GBPUSD";
double   Buffer[];   
double valx[1];
double valy[1];
double xyBuffer[];
double sqrxBuffer[];
double ortxBuffer[];
double ortyBuffer[];
double topxyBuffer[];
double topsqrxBuffer[];
datetime tm1[1];
datetime tm2[1];

int MAHand;
ENUM_TIMEFRAMES TF;
int TimeX=0;
int _handle1,_handle2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,valx,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,valy,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,xyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,sqrxBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ortxBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,ortyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,topxyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,topsqrxBuffer,INDICATOR_CALCULATIONS);
   _handle1 = iMA(S1_Symbol,0,200,0,MODE_SMA,PRICE_CLOSE); if(_handle1==INVALID_HANDLE) { IndicatorRelease(_handle1); return(INIT_FAILED); }
   _handle2 = iMA(S2_Symbol,0,200,0,MODE_SMA,PRICE_CLOSE); if(_handle2==INVALID_HANDLE) { IndicatorRelease(_handle2); return(INIT_FAILED); }
   IndicatorSetInteger(INDICATOR_DIGITS,5);   
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(_handle1!=INVALID_HANDLE) IndicatorRelease(_handle1);
   if(_handle2!=INVALID_HANDLE) IndicatorRelease(_handle2);
   
   Comment("comment1");
  }
//+------------------------------------------------------------------+
//| 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;
   static bool err=false;

   if(prev_calculated<=0 || err)
     {
      if(!SymbolSelect(S1_Symbol,true))
                  {
                     Alert("No symbol "+S1_Symbol);
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                  }
      if(!SymbolSelect(S2_Symbol,true))
                  {
                     Alert("No symbol "+S2_Symbol);
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                  }
        
      limit=1;
      
         int b1=Bars(S1_Symbol,PERIOD_CURRENT);
         int b2=Bars(S2_Symbol,PERIOD_CURRENT);
         if(b1==0 || b2==0)
                    {
                     Comment(MQL5InfoString(MQL5_PROGRAM_NAME)+": Please wait...");
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                   }
         int cr1=CopyTime(S1_Symbol,PERIOD_CURRENT,b1-1,1,tm1);
         int cr2=CopyTime(S2_Symbol,PERIOD_CURRENT,b2-1,1,tm2);
         if(cr1==-1 || cr2==-1)
                  {
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
//                   return(0);
                  }
         for(int i=0;i<rates_total;i++)
         {
            if(time[i]>=tm1[0] && time[i]>=tm2[0])
                     {
                        limit=i+1;
                        break;
                     }
         }
      if(err)
                     {
                     Comment("comment2");
                     err=false;
                     }
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,200);
     }
   else
     {
      limit=prev_calculated-1;
     }
     
     double   _ORTVALX[1];
     double   _ORTVALY[1];
     
   for(int i=limit;i<rates_total;i++)
{
    CopyClose(S1_Symbol,PERIOD_CURRENT,time[i],1,valx);
    CopyClose(S2_Symbol,PERIOD_CURRENT,time[i],1,valy);
    int _copied1=CopyBuffer(_handle1,0,time[i],1,_ORTVALX);  if(_copied1!=1) continue;
    int _copied2=CopyBuffer(_handle2,0,time[i],1,_ORTVALY);  if(_copied2!=1) continue;

    if(valx[0]==0 || valy[0]==0)
    {
        Buffer[i]=Buffer[i-1];
        continue;
    } 
    
    {        
        ortxBuffer[i]=_ORTVALX[0];
        ortyBuffer[i]=_ORTVALY[0];
        xyBuffer[i]=valx[0]*valy[0];
        sqrxBuffer[i]=valx[0]*valx[0];
    }     
}

for(int i=limit;i<rates_total && i >= 10;i++)
{
    topsqrxBuffer[i]=sqrxBuffer[i]+sqrxBuffer[i-1]+sqrxBuffer[i-2]+sqrxBuffer[i-3]+sqrxBuffer[i-4]+sqrxBuffer[i-5]+sqrxBuffer[i-6]+sqrxBuffer[i-7]+sqrxBuffer[i-8]+sqrxBuffer[i-9]+sqrxBuffer[i-10];
    topxyBuffer[i]=xyBuffer[i]+xyBuffer[i-1]+xyBuffer[i-2]+xyBuffer[i-3]+xyBuffer[i-4]+xyBuffer[i-5]+xyBuffer[i-6]+xyBuffer[i-7]+xyBuffer[i-8]+xyBuffer[i-9]+xyBuffer[i-10];  
    Buffer[i]=(topsqrxBuffer[i]+topxyBuffer[i]+ortxBuffer[i]+ortyBuffer[i]+xyBuffer[i]-sqrxBuffer[i]-valy[0])/250000;                                            
}
       
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: