Error indicador ADR

 

El indicador funciona pero no grafica bien. He intentado muchas formas diferentes de hacerlo pero he concluido que esta era la más eficiente. Se trata de un ADR calculado para velas diarias y graficado en el TF actual. 

//--- parameters
input int            adrPeriod = 10;          // Number of days for MA
input ENUM_MA_METHOD adrMethod = MODE_SMA;   // MA mode
input double         upPct = 0.8;            // Upper pct projection of ADR
input double         downPct = 0.8;          // Lower pct projection of ADR
input bool           alert = false;          // Alert ADR reached
//--- buffers
double         UpperBuffer[];
double         PctUpperBuffer[];
double         PctLowerBuffer[];
double         LowerBuffer[];
double         TRBuffer[];
double         ATRBuffer[];
//+------------------------------------------------------------------+
int OnInit(){
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,PctUpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,PctLowerBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,LowerBuffer,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(0,PLOT_SHIFT,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(1,PLOT_SHIFT,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(2,PLOT_SHIFT,adrPeriod*tfMultiplier());
   PlotIndexSetInteger(3,PLOT_SHIFT,adrPeriod*tfMultiplier());
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
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,copied;
   bool dayChange;
   if(prev_calculated == 0) limit = rates_total;
   else limit = rates_total-prev_calculated;

   datetime dayTime[];
   double dayHigh[],dayLow[];
   copied = CopyTime(_Symbol,PERIOD_D1,0,(rates_total)/tfMultiplier(),dayTime);
   if(copied <= 0) return -1;
   copied = CopyHigh(_Symbol,PERIOD_D1,0,(rates_total)/tfMultiplier(),dayHigh);
   if(copied <= 0) return -1;
   copied = CopyLow(_Symbol,PERIOD_D1,0,(rates_total)/tfMultiplier(),dayLow);
   if(copied <= 0) return -1;
   int pos = ArraySize(dayHigh)-1;
   ArrayResize(TRBuffer,pos+1,0);
   ArrayResize(ATRBuffer,copied+1,0);
   while(pos >= 0){
      TRBuffer[pos] = dayHigh[pos]-dayLow[pos];
      pos--;
     }

   int j = 0;
   for(int i = 0; i<limit-1; i++){
         if(NewDay(time[i],time[i+1],1)){
            j++;
            ATRBuffer[j] = iMAOnArrayMQL4(TRBuffer,0,adrPeriod,0,adrMethod,j);
            }
         if(j>=ArraySize(dayLow)) return(rates_total);
         UpperBuffer[i] = dayLow[j]+ATRBuffer[j];
         PctUpperBuffer[i] = dayLow[j]+upPct*ATRBuffer[j];
         PctLowerBuffer[i] = dayHigh[j]-downPct*ATRBuffer[j];
         LowerBuffer[i] = dayHigh[j]-ATRBuffer[j];
         }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

bool NewDay(datetime aTimeCur,datetime aTimePre){
   return((aTimeCur/86400)!=(aTimePre/86400));
  }

ENUM_TIMEFRAMES currentTf(){
   int period;
   if(_Period == PERIOD_CURRENT)  period = (ENUM_TIMEFRAMES) _Period;
   return(period);
   }

int tfMultiplier(){
   switch(currentTf()){
      case PERIOD_M1: return(1440);
      case PERIOD_M2: return(720);
      case PERIOD_M3: return(480);
      case PERIOD_M4: return(360);
      case PERIOD_M5: return(288);
      case PERIOD_M6: return(240);
      case PERIOD_M10: return(144);
      case PERIOD_M12: return(120);
      case PERIOD_M15: return(96);
      case PERIOD_M20: return(72);
      case PERIOD_M30: return(48);
      case PERIOD_H1: return(24);
      case PERIOD_H2: return(12);
      case PERIOD_H3: return(8);
      case PERIOD_H4: return(6);
      case PERIOD_H6: return(4);
      case PERIOD_H8: return(3);
      case PERIOD_H12: return(2);
      case PERIOD_D1: return(1);
      default: return(0);
      }
   }

Agradezco mucho toda la ayuda aportada, me está costando bastante el cambio a mq5.

 
dcaronm:

El indicador funciona pero no grafica bien. He intentado muchas formas diferentes de hacerlo pero he concluido que esta era la más eficiente. Se trata de un ADR calculado para velas diarias y graficado en el TF actual. 

Agradezco mucho toda la ayuda aportada, me está costando bastante el cambio a mq5.

Adjunto a continuación una captura de el único TF en el que por lo menos algo cuadra.

Muchas gracias

Archivos adjuntos:
Captura.PNG  57 kb