Indicator that resets suddenly

 

Hi

I have programmed an indicator to mark the high and low of the previous day, the weekly and the ADR.

The indicator works, but suddenly it initializes and starts making the highs from the current candlestick.

I am not able to understand why it does it.

Capture indicator OK


Capture indicator KO


Source code

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[])
{
//---
   static datetime last_time=0;
   
   for (int i=(int)MathMax(prev_calculated-1,rates_total-numBarsHistory); i<rates_total; i++)
   {
      if (_Period == PERIOD_M5 || _Period == PERIOD_M15 || _Period == PERIOD_M10 || _Period == PERIOD_M30)
      {   
         getYesterdayHILO(time[i],YhighBuffer[i], YlowBuffer[i]);
         getPreviousWeekHILO(time[i],WhighBuffer[i], WlowBuffer[i]);
         getADR_HL(time[i], ADRHBuffer[i], ADRLBuffer[i]);
      }
      else
      {
         YhighBuffer[i] = NULL;
         YlowBuffer[i] = NULL;
         WhighBuffer[i] = NULL;
         WlowBuffer[i] = NULL;
         ADRHBuffer[i] = NULL;
         ADRLBuffer[i] = NULL;
      }
   }

//--- return value of prev_calculated for next call
   return(rates_total-1);  //Return the number of calculated bars, 
                           //Subtract 1 for the last bar recalculation
}

Any idea?


Thank you

 
nobody?
 
israelhernando #: nobody?

What do you expect? Your source code is incomplete and none of the important logic is visible or even explained, nor can anyone compile and test it either.

 
Fernando Carreiro #:

What do you expect? Your source code is incomplete and none of the important logic is visible or even explained, nor can anyone compile and test it either.

HI,


this is the source code for revision. I would be very grateful if you could check it


The problem is that the indicator does not always calculate the history correctly, I have to update the chart so that it does.

Maybe the problem is the onCalculate function, but I can't see.


//+------------------------------------------------------------------+
//|                                             isra_highlow_adr.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   6

//--- plot Yhigh
#property indicator_label1  "Yesterday High"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_DASHDOT
#property indicator_width1  1
//--- plot Ylow
#property indicator_label2  "Yesterday Low"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_DASHDOT
#property indicator_width2  1

//--- plot ADRH
#property indicator_label3  "ADR High"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_DASHDOT
#property indicator_width3  1
//--- plot ADRL
#property indicator_label4  "ADR Low"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrYellow
#property indicator_style4  STYLE_DASHDOT
#property indicator_width4  1
//--- plot Whigh
#property indicator_label5  "Last Week High"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrOrange
#property indicator_style5  STYLE_DASHDOT
#property indicator_width5  1
//--- plot Wlow
#property indicator_label6  "Last Week Low"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrOrange
#property indicator_style6  STYLE_DASHDOT
#property indicator_width6  1

//--- indicator buffers
double         YhighBuffer[];
double         YlowBuffer[];
double         WhighBuffer[];
double         WlowBuffer[];
double         ADRHBuffer[];
double         ADRLBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print ("INIT");

//--- indicator buffers mapping
   SetIndexBuffer(0,YhighBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,YlowBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ADRHBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ADRLBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,WhighBuffer,INDICATOR_DATA);
   SetIndexBuffer(5,WlowBuffer,INDICATOR_DATA);

   EventSetTimer(3); 

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason) 
  { 

  } 

//+------------------------------------------------------------------+
//| 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  i=0;
   if(i<prev_calculated)
      i=prev_calculated-1;
//--- start calculations


   while(i<rates_total && !IsStopped())
//   for (int i=(int)MathMax(prev_calculated-1,rates_total-numBarsHistory); i<rates_total; i++)
   {
      if (_Period == PERIOD_M5 || _Period == PERIOD_M15 || _Period == PERIOD_M10 || _Period == PERIOD_M30)
      {   
         getYesterdayHILO(time[i],YhighBuffer[i], YlowBuffer[i]);
         getPreviousWeekHILO(time[i],WhighBuffer[i], WlowBuffer[i]);
         getADR_HL(time[i], ADRHBuffer[i], ADRLBuffer[i]);
      }
      else
      {
         YhighBuffer[i] = NULL;
         YlowBuffer[i] = NULL;
         WhighBuffer[i] = NULL;
         WlowBuffer[i] = NULL;
         ADRHBuffer[i] = NULL;
         ADRLBuffer[i] = NULL;
      }
      i++;
   }

//--- return value of prev_calculated for next call
   return(rates_total-1);  //Return the number of calculated bars, 
                           //Subtract 1 for the last bar recalculation
}

int getDiaSemana(datetime fechaBar)
{
   MqlDateTime stm;
   TimeToStruct(fechaBar,stm);
   
   return stm.day_of_week;
}

void getYesterdayHILO(datetime fechaBar, double &highestY, double &lowestY)
{
   int dayIndex = iBarShift( _Symbol, PERIOD_D1, fechaBar, true);

   int diaSemana = getDiaSemana(fechaBar);

   int dif;
   if (diaSemana == 1)
      dif = 3;
   else if (diaSemana == 0) 
      dif = 2;
   else
      dif = 1;
   
   dayIndex = iBarShift( _Symbol, PERIOD_D1, fechaBar - dif*86400, true);
   
   highestY = iHigh(_Symbol, PERIOD_D1, dayIndex);
   lowestY  = iLow(_Symbol, PERIOD_D1, dayIndex);
}

void getPreviousWeekHILO(datetime fechaBar, double &highestY, double &lowestY)
{
   int dayIndex = iBarShift( _Symbol, PERIOD_W1, fechaBar - 604800, true);
   
   highestY = iHigh(_Symbol, PERIOD_W1, dayIndex);
   lowestY  = iLow(_Symbol, PERIOD_W1, dayIndex);
}
void getADR_HL(datetime fechaBar, double &adrH, double &adrL)
{
   //calcula ADR
   int dayIndex = iBarShift( _Symbol, PERIOD_D1, fechaBar, false);
   
   if (getDiaSemana(fechaBar) == 0)
      dayIndex++;
   
   if (dayIndex <0)
      int err = GetLastError();
   
   double highestY = iHigh(_Symbol, PERIOD_D1, dayIndex);
   double lowestY  = iLow(_Symbol, PERIOD_D1, dayIndex);
   double openY  = iOpen(_Symbol, PERIOD_D1, dayIndex);
   double closeY  = iClose(_Symbol, PERIOD_D1, dayIndex);

   double adr = getADR(fechaBar);

   //adrH
   if (highestY - lowestY < adr)
      adrH = lowestY + adr;
   else
   {
      if(closeY >= openY)
         adrH = lowestY + adr;
      else
         adrH = highestY;
   }
      
   //adrL
   if (highestY - lowestY < adr)
      adrL = highestY - adr;
   else
   {
      if(closeY >= openY)
         adrL = lowestY;
      else
         adrL = highestY - adr;
   }
}

double getADR(datetime fechaBar)
{
   //calcula ADR
   int dayIndex = iBarShift( _Symbol, PERIOD_D1, fechaBar, false);
   
   if (dayIndex <0)
      int err = GetLastError();
   
   double adr_acum = 0;
   int adr_count = 1;
   int periodsADRIni = 14;
   int periodsADR = periodsADRIni;
   
   while (adr_count <= periodsADR)
   {
      if (getDiaSemana(iTime(_Symbol, PERIOD_D1,dayIndex+adr_count)) ==0)
      {
         periodsADR++;
         adr_count++;
         continue;
      }

      double dif = iHigh(_Symbol, PERIOD_D1, dayIndex+adr_count) - 
                    iLow(_Symbol, PERIOD_D1, dayIndex+adr_count);
      
      adr_acum += dif;
      adr_count++;
   }
   double adr = adr_acum / periodsADRIni;
   
   return adr;
}


Thank you very much.

 

Another problem I see, is, in M5 the indicator sometimes works OK



But in M15, never



I hope you can help me.


Thank you !!

 
israelhernando #:

Another problem I see, is, in M5 the indicator sometimes works OK



But in M15, never



I hope you can help me.


Thank you !!

1) you are calculating for each individual bar, the Yesterday, Weekly and Adr , something that won't change for a certain region, you can get the values once, and store in the buffer.
2) with regards to it not working in a particular TF, check the iBarShift index, when it did not work.
 
Thank-god Avwerosuoghene Odukudu #:
1) you are calculating for each individual bar, the Yesterday, Weekly and Adr , something that won't change for a certain region, you can get the values once, and store in the buffer.
2) with regards to it not working in a particular TF, check the iBarShift index, when it did not work.

Thank you for your reply.

The first solution will optimize the algorithm, but, this is the cause of the problem (the indicator resets unexpectedly)?


Thank you again !!

 
I would make a guess and say, here is the issue:

   int  i=0;
   if(i<prev_calculated)
      i=prev_calculated-1;
//--- start calculations

You need to search the forum "do look backs properly".




Reason: