Half Trend working stop

 

Hi this is half trend indicator 

it stop working at Line 85,21

how to solve this error?

      trend[i]=trend[i+1];

//+------------------------------------------------------------------+
//|                                                   Half Trend.mq4 |
//|                                                   Copyright 2010 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 DeepSkyBlue  // up[]
#property indicator_width1 2
#property indicator_color2 Tomato       // down[]
#property indicator_width2 2
#property indicator_color3 DeepSkyBlue  // atrlo[]
#property indicator_width3 1
#property indicator_color4 Tomato       // atrhi[]
#property indicator_width4 1
#property indicator_color5 DeepSkyBlue  // arrup[]
#property indicator_width5 1
#property indicator_color6 Tomato      // arrdwn[]
#property indicator_width6 1
extern int     Amplitude=2;
extern bool    alertsOn         = true;
extern bool    alertsOnCurrent  = true;
extern bool    alertsMessage    = true;
extern bool    alertsSound      = true;
extern bool    alertsEmail      = false;

bool nexttrend;
double minhighprice,maxlowprice;
double up[],down[],atrlo[],atrhi[],trend[];
double arrup[],arrdwn[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(7); // +1 buffer - trend[]

   SetIndexBuffer(0,up);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,down);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,atrlo);
   SetIndexStyle(2,DRAW_HISTOGRAM, STYLE_SOLID);
   SetIndexBuffer(3,atrhi);
   SetIndexStyle(3,DRAW_HISTOGRAM, STYLE_SOLID);

   SetIndexBuffer(6,trend);

   SetIndexBuffer(4,arrup);
   SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID);
   SetIndexArrow(4,233);

   SetIndexBuffer(5,arrdwn);
   SetIndexStyle(5,DRAW_ARROW,STYLE_SOLID);
   SetIndexArrow(5,234);

   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(6,0.0);
   nexttrend=0;
   minhighprice= High[Bars-1];
   maxlowprice = Low[Bars-1];
   return (0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double atr,lowprice_i,highprice_i,lowma,highma;
   int workbar=0;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   int limit = MathMin(Bars-counted_bars,Bars-1);

   for(int i=Bars-1; i>=0; i--)
     {
      lowprice_i=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,Amplitude,i));
      highprice_i=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,Amplitude,i));
      lowma=NormalizeDouble(iMA(NULL,0,Amplitude,0,MODE_SMA,PRICE_LOW,i),Digits());
      highma=NormalizeDouble(iMA(NULL,0,Amplitude,0,MODE_SMA,PRICE_HIGH,i),Digits());
      trend[i]=trend[i+1];
      atr=iATR(Symbol(),0,100,i)/2;

      arrup[i]  = EMPTY_VALUE;
      arrdwn[i] = EMPTY_VALUE;
      if(nexttrend==1)
        {
         maxlowprice=MathMax(lowprice_i,maxlowprice);

         if(highma<maxlowprice && Close[i]<Low[i+1])
           {
            trend[i]=1.0;
            nexttrend=0;
            minhighprice=highprice_i;
           }
        }
      if(nexttrend==0)
        {
         minhighprice=MathMin(highprice_i,minhighprice);

         if(lowma>minhighprice && Close[i]>High[i+1])
           {
            trend[i]=0.0;
            nexttrend=1;
            maxlowprice=lowprice_i;
           }
        }
      if(trend[i]==0.0)
        {
         if(trend[i+1]!=0.0)
           {
            up[i]=down[i+1];
            up[i+1]=up[i];
            arrup[i] = up[i] - 2*atr;
           }
         else
           {
            up[i]=MathMax(maxlowprice,up[i+1]);
           }
         atrhi[i] = up[i] - atr;
         atrlo[i] = up[i];
         down[i]=0.0;
        }
      else
        {
         if(trend[i+1]!=1.0)
           {
            down[i]=up[i+1];
            down[i+1]=down[i];
            arrdwn[i] = down[i] + 2*atr;
           }
         else
           {
            down[i]=MathMin(minhighprice,down[i+1]);
           }
         atrhi[i] = down[i] + atr;
         atrlo[i] = down[i];
         up[i]=0.0;
        }
     }
   manageAlerts();
   return (0);
  }
//+------------------------------------------------------------------+
//| Manage Alerts                                                    |
//+------------------------------------------------------------------+
void manageAlerts()
  {
   if(alertsOn)
     {
      if(alertsOnCurrent)
         int whichBar = 0;
      else
         whichBar = 1;
      if(arrup[whichBar]  != EMPTY_VALUE)
         doAlert(whichBar,"up");
      if(arrdwn[whichBar] != EMPTY_VALUE)
         doAlert(whichBar,"down");
     }
  }
//+------------------------------------------------------------------+
//| Alert for Bars                                                   |
//+------------------------------------------------------------------+
void doAlert(int forBar, string doWhat)
  {
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;

   if(previousAlert != doWhat || previousTime != Time[forBar])
     {
      previousAlert  = doWhat;
      previousTime   = Time[forBar];
      message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," HalfTrend signal ",doWhat);
      if(alertsMessage)
         Alert(message);
      if(alertsEmail)
         SendMail(StringConcatenate(Symbol(),"HalfTrend "),message);
      if(alertsSound)
         PlaySound("alert2.wav");
     }
  }
//+------------------------------------------------------------------+
 
TBS:

Hi this is half trend indicator 

it stop working at Line 85,21

how to solve this error?


Might be a good idea to show which line that is.

 
TBS: Hi this is half trend indicator. it stop working at Line 85,21. how to solve this error?

At the very first iteration when "i = Bars - 1", then "i + 1" would give the value of "Bars" which is beyond the index of the array. You will then get "array out of range" error for "trend[i+1]".

You need to detect this initial condition and give it a seed value for "trend[i]" on the first iteration. The same problem will arise in all the places in your code where you reference "[i+1]", so you will need to detect this special case or condition.

Next time, please mention what error is given in the logs instead of just saying that it stopped working.

 
Fernando Carreiro #:

At the very first iteration when "i = Bars - 1", then "i + 1" would give the values of "Bars" which is beyond the index of the array. You will then get "array out of range" error for "trend[i+1]".

You need to detect this initial condition and give it a seed value for "trend[i]" on the first iteration. The same problem will arise in all the places in your code where you reference "[i+1]", so you will need to detect this special case or condition.

Next time, please mention what error is given in the logs instead of just saying that it stopped working.

It is an old indicator and doesn't use #property strict.

I may be wrong but if I remember correctly, I don't think that you get an array out of range error if #property strict is not used.

 
Keith Watford #: It is an old indicator and doesn't use #property strict. I may be wrong but if I remember correctly, I don't think that you get an array out of range error if #property strict is not used.

Thank you Keith for pointing that out. It has been quite long since I used the old non-strict code style, so I no longer remember what it reports.

Note to OP: Irrespective of the error reporting, it is still a case of the array index being out of the acceptable range, so you will need to change the code to adjust for the first iteration of the loop.

 
TBS:

Hi this is half trend indicator 

it stop working at Line 85,21

how to solve this error?



Try changing instances of   Bars-1   to  Bars-2

To keep the i+1 in series.

 
maximo #: Try changing instances of   Bars-1 to  Bars-2. To keep the i+1 in series.

That will not solve the problem, because the values of the buffers at the index [Bars-1] will then be undefined and throw the rest out of sync. They cannot just be ignored and have to be properly set.

 
Fernando Carreiro #:

That will not solve the problem, because the values of the buffers at the index [Bars-1] will then be undefined and throw the rest out of sync. They cannot just be ignored and have to be properly set.


The indicator simply calculates 1 less bar in history. 

There is no restriction on a lesser number of bars to calculate.

The indicator works fine.

Files:
Reason: