indicator does not refresh

 
Hi all.

please help me fix this bug, i converted an MT4 indicator to MT5 i didn't get the same result but it works; the only concern is that it does not refresh when a new candle opens.

If you have any suggestions give me.

thank you in advance

MT5 code :


#property version   "1.8" 

#property indicator_separate_window
#property  indicator_buffers 8
#property indicator_plots   5

#property  indicator_color1  clrLimeGreen

#property  indicator_color2  clrRed

#property  indicator_color3  clrBlue,clrGold
#property  indicator_color4  clrLimeGreen
#property  indicator_color5  clrRed

#property indicator_type2 DRAW_COLOR_HISTOGRAM
#property indicator_style2  STYLE_SOLID

#property indicator_type3 DRAW_COLOR_HISTOGRAM
#property indicator_style3  STYLE_SOLID

#property indicator_type4 DRAW_COLOR_HISTOGRAM
#property indicator_style4  STYLE_SOLID

#property indicator_type5 DRAW_COLOR_HISTOGRAM
#property indicator_style5  STYLE_SOLID

#property indicator_type6 DRAW_COLOR_HISTOGRAM
#property indicator_style6  STYLE_SOLID

#property indicator_type7 DRAW_COLOR_HISTOGRAM
#property indicator_style7  STYLE_SOLID

#property indicator_type8 DRAW_COLOR_HISTOGRAM
#property indicator_style8  STYLE_SOLID

#property  indicator_width3  2
#property  indicator_width4  2
#property  indicator_width5  2

input int    period = 35;
input int    smooth=10;
input bool DoAlert=true;
input bool alertMail=false;

/*
input bool   DoAlert=true;
input bool   alertMail=false;
*/
datetime lastAlertTime;
datetime Time=TimeCurrent();

/*/---- indicator buffers
double ExtBuffer0[]; //This buffer is for drawing
double ExtBuffer1[]; //This buffer is for color
*/
double         ExtBuffer0[];
double         ExtBuffer1[];
double         ExtBuffer2[];
double         ExtBuffer3[];
double         ExtBuffer4[];
double         ExtBuffer5[];
double         ExtBufferh1[];
double         ExtBufferh2[];

int counted_bars;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()

  {
   IndicatorSetString(INDICATOR_SHORTNAME, "Solar wind");
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits + 1);   
   
   //---- set the Buffer dynamic array as indicator buffer   
   SetIndexBuffer(0, ExtBufferh1, INDICATOR_DATA);   
   //---- shifting the start of drawing the indicator   
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);  
   //---- create label to display in DataWindow   
   PlotIndexSetString(0,PLOT_LABEL,"SW");
   //---- setting values of the indicator that won't be visible on the chart  
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);   
   
   //---- set the Buffer dynamic array as indicator buffer
   SetIndexBuffer(1, ExtBufferh2, INDICATOR_COLOR_INDEX);   
   //---- shifting the start of drawing the indicator   
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,1);   
   //---- create label to display in DataWindow   
   PlotIndexSetString(1,PLOT_LABEL,"SW");   
   //---- setting values of the indicator that won't be visible on the chart   
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   //---- set the Buffer dynamic array as indicator buffer   
   SetIndexBuffer(2, ExtBuffer0, INDICATOR_DATA);   
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,2);   
   //---- create label to display in DataWindow   
   PlotIndexSetString(2,PLOT_LABEL,"SW");  
   //---- setting values of the indicator that won't be visible on the chart   
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   //---- set the Buffer dynamic array as indicator buffer
   SetIndexBuffer(3, ExtBuffer1, INDICATOR_COLOR_INDEX);
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,3);
   PlotIndexSetString(3,PLOT_LABEL,"SW");
   //---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   //---- set the Buffer dynamic array as indicator buffer   
   SetIndexBuffer(4, ExtBuffer2, INDICATOR_DATA);   
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,4);   
   PlotIndexSetString(4,PLOT_LABEL,"SW");   
   //---- setting values of the indicator that won't be visible on the chart   
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   SetIndexBuffer(5, ExtBuffer3, INDICATOR_COLOR_INDEX);
   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,5);
   PlotIndexSetString(5,PLOT_LABEL,"SW");
   //---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   SetIndexBuffer(6, ExtBuffer4, INDICATOR_DATA);
   PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,6);
   PlotIndexSetString(6,PLOT_LABEL,"SW");
   //---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   SetIndexBuffer(7, ExtBuffer5, INDICATOR_COLOR_INDEX);
   PlotIndexSetInteger(7,PLOT_DRAW_BEGIN,7);
   PlotIndexSetString(6,PLOT_LABEL,"SW");
   //---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);

  }

//+------------------------------------------------------------------+
//| Data Calculation Function for Indicator                          |
//+------------------------------------------------------------------+
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;
   
   
   ArraySetAsSeries(ExtBuffer0, true);
   ArraySetAsSeries(ExtBuffer1, false);
   ArraySetAsSeries(ExtBuffer2, true);
   ArraySetAsSeries(ExtBuffer3, true);
   ArraySetAsSeries(ExtBuffer4, true);
   ArraySetAsSeries(ExtBuffer5, true);
   ArraySetAsSeries(ExtBufferh1,true);
   ArraySetAsSeries(ExtBufferh2,true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(ExtBuffer0, 0);
      ArrayInitialize(ExtBuffer1, 0);
      ArrayInitialize(ExtBuffer2, 0);
      ArrayInitialize(ExtBuffer3, 0);
      ArrayInitialize(ExtBuffer4, 0);
      ArrayInitialize(ExtBuffer5, 0);
      ArrayInitialize(ExtBufferh1, 0);
      ArrayInitialize(ExtBufferh2, 0);
      
     }
   else
      limit++;
   datetime Time[];
   
   /*double prev, current, old;*/
   double Value=0,Value1=0,Value2=0,Fish=0,Fish1=0,Fish2=0;
   double price;
   double MinL=0;
   double MaxH=0;

   /*int counted_bars=prev_calculated;*/
   if(prev_calculated<0)
      return(-1);
   if(prev_calculated>0)
      return(1);
   string sAlertMsg;


   ArraySetAsSeries(High, true);
   ArraySetAsSeries(Low, true);
   ArraySetAsSeries(close, true);
   ArraySetAsSeries(open, true);

   if(prev_calculated == rates_total)
      return(rates_total);
   

   for(int i = 0; i < limit; i++)
     {
      MaxH = High[ArrayMaximum(High, i, period)];
      MinL = Low[ArrayMinimum(Low, i, period)];
      price = (High[i] + Low[i]) / 2;
      Value = 0.33 * 2 * ((price - MinL) / (MaxH - MinL) - 0.5) + 0.67 * Value1;
      Value = MathMin(MathMax(Value, -0.999), 0.999);
      int indicator_index = rates_total - i - 1;
      ExtBuffer0[indicator_index] = 0.5 * MathLog((1 + Value) / (1 - Value)) + 0.5 * Fish1;
      Value1 = Value;
      Fish1=ExtBuffer0[i];
      if(ExtBuffer0[i]>0)
         ExtBuffer1[i]=10;
      else
         ExtBuffer1[i]=-10;
     }

   for(int i=limit; i>=0; i--)
     {
      double sum  = 0;
      double sumw = 0;
      int bars=Bars(_Symbol,_Period);
      for(int k=0; k<smooth && (i+k)<bars; k++)
        {
         double weight = smooth-k;
         sumw  += weight;
         sum   += weight*ExtBuffer1[i+k];
        }
      if(sumw!=0)
         ExtBuffer2[i] = sum/sumw;
      else
         ExtBuffer2[i] = 0;
     }


   for(int i=0; i<=limit; i++)
     {
      int sum  = 0;
      int sumw = 0;

      for(int k=0; k<smooth && (i-k)>=0; k++)
        {
         int weight = smooth-k;
         sumw  += weight;
         sum   += weight*ExtBuffer2[i-k];
        }
      if(sumw!=0)
         ExtBuffer3[i] = sum/sumw;
      else
         ExtBuffer3[i] = 0;
     }


   for(int i=limit; i>=0; i--)
   {
      ExtBuffer4[i]=EMPTY_VALUE;
      ExtBuffer5[i]=EMPTY_VALUE;
      ExtBufferh1[i]=EMPTY_VALUE;
      ExtBufferh2[i]=EMPTY_VALUE;
      if (ExtBuffer3[i]>0) { ExtBuffer4[i]=ExtBuffer3[i]; ExtBufferh1[i]=ExtBuffer3[i]; }
      if (ExtBuffer3[i]<0) { ExtBuffer5[i]=ExtBuffer3[i]; ExtBufferh2[i]=ExtBuffer3[i]; }
      
      if (ExtBuffer3[i+1] < 0 && ExtBuffer3[i] > 0)
      {
         if (DoAlert && i<5 && lastAlertTime!=Time[0])
         {
            sAlertMsg="Solar Wind - "+Symbol()+" "+TF2Str(Period())+": cross UP";
            if (DoAlert)     Alert(sAlertMsg);
            lastAlertTime = Time[0];  
            if (alertMail)   SendMail(sAlertMsg, "MT4 Alert!\n" + TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS )+"\n"+sAlertMsg);   
         }
      }
      else if(ExtBuffer3[i+1] > 0 && ExtBuffer3[i] < 0)
      {
         if (i<5 && lastAlertTime!=Time[0])
         {
            sAlertMsg="Solar Wind - "+Symbol()+" "+TF2Str(Period())+": cross DOWN";
            if (DoAlert)     Alert(sAlertMsg);
            lastAlertTime = Time[0];  
            if (alertMail)   SendMail(sAlertMsg, "MT4 Alert!\n" + TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS )+"\n"+sAlertMsg);   
         }
                     
      }
      
      
   }
   return(rates_total);
  }

// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod)
  {
   switch(iPeriod)
     {
      case PERIOD_M1:
         return("M1");
      case PERIOD_M5:
         return("M5");
      case PERIOD_M15:
         return("M15");
      case PERIOD_M30:
         return("M30");
      case PERIOD_H1:
         return("H1");
      case PERIOD_H4:
         return("H4");
      case PERIOD_D1:
         return("D1");
      case PERIOD_W1:
         return("W1");
      case PERIOD_MN1:
         return("MN1");
      default:
         return("M"+iPeriod);
     }
  }
//+------------------------------------------------------------------+
 
   if(prev_calculated<0)
      return(-1);
   if(prev_calculated>0)
      return(1);

This means that the indicator will do nothing unless prev_calculated equals 0.

 
Keith Watford:

This means that the indicator will do nothing unless prev_calculated equals 0.

So I have to add a third condition to it or change the current conditions, correct ?


Thanks for your help

 
Keith Watford:

This means that the indicator will do nothing unless prev_calculated equals 0.

I changed it like this and the indicator is no longer displayed


if(prev_calculated<=0)

      return(-1);

   if(prev_calculated>=0)

      return(1);
 
Tete Adate Adjete:

I changed it like this and the indicator is no longer displayed

   if(prev_calculated<=0)

      return(-1);

   if(prev_calculated>=0)

      return(1);

Are you kidding me??

 
Keith Watford:

Are you kidding me??

Please excuse me if I seem to disturb you unnecessarily.


but when I change the values, I get an error message showing that the value of the constant 

prev_calulated


cannot be changed that's why

 
Keith Watford:

Are you kidding me??

I just did it, now it's good; thank you for your help