Very strange behavior of iCustom values on EA

 

Dears All,

I'm using a simple ATR trailng stop indicator:

// PROPRERTIES ********************************
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Red

// INPUTS ********************************
extern   int      niLENGTH                   = 14;
extern   double   riMULTI                    = 2;

// VARIABLES ********************************
double            rbLONG_STOP[];
double            rbSHORT_STOP[];
int               igINDEX                    = 0;
double            rgAVGTRUERANGE             = 0;
double            rgHPRICE                   = 0;
double            rgLPRICE                   = 0;
double            rgSTOP_HIGH                = 0;
double            rgSTOP_LOW                 = 0;
int               ngATRtrailingstopTrend     = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
   SetIndexStyle(0, DRAW_ARROW, 0, 2);
   SetIndexArrow(0, 159);
   SetIndexBuffer(0, rbLONG_STOP);
   
   SetIndexStyle(1, DRAW_ARROW, 0, 2);
   SetIndexArrow(1, 159);
   SetIndexBuffer(1, rbSHORT_STOP);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int         nlCOUNTED_BARS                   = IndicatorCounted();

   igINDEX                                      = Bars - nlCOUNTED_BARS - 1;
   
   while(igINDEX >= 0) {   
   
      rgAVGTRUERANGE                            = iATR(NULL, 0, niLENGTH, igINDEX);   
      rgLPRICE                                  = Close[igINDEX];
      rgHPRICE                                  = Close[igINDEX];             

   //----   
      if (rgHPRICE > rgSTOP_HIGH) {
         ngATRtrailingstopTrend                 = 1;      
      } 

      if (rgLPRICE < rgSTOP_LOW) {
         ngATRtrailingstopTrend                 = -1;      
      } 
   //-----
      if (ngATRtrailingstopTrend == 1) {
   
         if ((rgLPRICE - (riMULTI * rgAVGTRUERANGE)) > rgSTOP_LOW) {
            rgSTOP_LOW                          = rgLPRICE - (riMULTI * rgAVGTRUERANGE);       
         }

         rgSTOP_HIGH                            = rgHPRICE + (riMULTI * rgAVGTRUERANGE);        
      }

      if (ngATRtrailingstopTrend == -1) {
   
         if ((rgHPRICE + (riMULTI * rgAVGTRUERANGE)) < rgSTOP_HIGH) {
            rgSTOP_HIGH                         = rgHPRICE + (riMULTI * rgAVGTRUERANGE);       
         }

         rgSTOP_LOW                             = rgLPRICE - (riMULTI * rgAVGTRUERANGE);        
      }
   
   
      //----
      if (ngATRtrailingstopTrend == 1) {
            rbLONG_STOP[igINDEX]                = rgSTOP_LOW;
            rbSHORT_STOP[igINDEX]               = 0;
      }

      if (ngATRtrailingstopTrend == -1) {
            rbLONG_STOP[igINDEX]                = 0;
            rbSHORT_STOP[igINDEX]               = rgSTOP_HIGH;
      }
      
   igINDEX--;        
   }  
  
   
   return(0);
  }
//+------------------------------------------------------------------+

When I call it ont the EA by means of the function "iCustom" I have some unexplainable problems. When I start the EA and, for example (on a 5 minutes time-frame of EURUSD, with 14 and 1 as input values), I monitor the feedback value by an alert each minute, I discover that first values are wrong. After a while, that could be 1-2 ticks or 2-3-4 bars, values become righteous and so remain forever. If I restart the EA (or recompile it modifing a simple comment) also the problem restart. The strange is:

1) The problem

2) The fact that the problem has an unpreditcable duration (few seconds or some minutes, and after that all work ok up to next stop / recompile).

I'm testing the situation just with this simple code:

int      ngMINUTE_ST             = 0;
int      ngMINUTE_MEM            = 0;
double   rgTEST1_PV              = 0;
double   rgTEST2_PV              = 0;
double   rgTEST3_PV              = 0;
double   rgTEST4_PV              = 0;
bool     bg1ST_CYCLE             = True;

static   string   ssATRTRAILINGSTOP_FILE           = "QT_ATRtrailingstop"; 

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

   ngMINUTE_ST                      = Minute();
   
   rgTEST1_PV                       = iCustom(NULL, 0, ssATRTRAILINGSTOP_FILE, 14, 1, 0, 0);
   rgTEST2_PV                       = iCustom(NULL, 0, ssATRTRAILINGSTOP_FILE, 14, 1, 1, 0);
   rgTEST3_PV                       = iCustom(NULL, 0, ssATRTRAILINGSTOP_FILE, 14, 1, 0, 1);
   rgTEST4_PV                       = iCustom(NULL, 0, ssATRTRAILINGSTOP_FILE, 14, 1, 1, 1);
   
   if (ngMINUTE_ST != ngMINUTE_MEM) {

// Per non sparare fuori dati in contuazione, gli Alert vengo printati ad ogni cambio di minuto e comuque mai il primo ciclo
      if (!bg1ST_CYCLE) {
         Alert("rgATR_PV: ", DoubleToStr(rgTEST1_PV + rgTEST2_PV, 4));
         Alert("rgATR_PREV: ", DoubleToStr(rgTEST3_PV + rgTEST4_PV, 4));
      }
   }
   
   ngMINUTE_MEM                     = ngMINUTE_ST;

   bg1ST_CYCLE                      = False;
   
//----
   return(0);
  }
//+----------------------------------------------------------------

and I'm not able to understand the problem.

Thanks in advance for any suggestion.

 
   int         nlCOUNTED_BARS                   = IndicatorCounted();
   igINDEX                                      = Bars - nlCOUNTED_BARS - 1;
   while(igINDEX >= 0) {   
      rgAVGTRUERANGE                            = iATR(NULL, 0, niLENGTH, igINDEX);   
Initially the indicator starts computing with igINDEX = Bars-1, but iATR needs to have at least niLENGTH bars to work, so iATR returns zero and all your variables become bogus.
   int         nlCOUNTED_BARS                   = IndicatorCounted();
   if (nlCOUNTED_BARS < niLENGTH) nlCOUNTED_BARS = niLENGTH;
   igINDEX                                      = Bars - nlCOUNTED_BARS - 1;
   :
 
WHRoeder:
Initially the indicator starts computing with igINDEX = Bars-1, but iATR needs to have at least niLENGTH bars to work, so iATR returns zero and all your variables become bogus.

That is the point.

Thank you very much for your analysis and support.

Reason: