Keep needing to compile indicator

 

Hi, I’m trying to write an indicator for the fozzy strategy, which briefly, buys when the RSI crosses the MA and is below the mid BB, and sell when RSI crosses the MA and is above the mid BB.

The issue I am having is when I change timeframes/currency all the arrows disappear and only reappear when I compile the code.

#property copyright "Brian307"
#property link      "B121ANS@Hotmail.com"
#include <WinUser32.mqh>
//--------------------------------------------------- 1 ---------------------------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 SkyBlue
#property indicator_color2 SkyBlue
#property indicator_color3 LimeGreen
#property indicator_color4 Red
//--------------------------------------------------- 2 ---------------------------------------------------------------------------------------
//---- indicator parameters
extern int    RSIPeriod = 8;
extern int    RSIMAPeriod = 8;
extern int    BandsPeriod=20;
extern int    BandsShift=0;
extern int    BandsDeviations=2.0;
//--------------------------------------------------- 3 ---------------------------------------------------------------------------------------
//---- buffers
double RSI[];
double RSIMA[];
double RSIBBU[];
double RSIBBL[];
double SignalUP[];
double SignalDN[];
//--------------------------------------------------- 4 ---------------------------------------------------------------------------------------
//---- variables
int i;
int RSIBBM;
//--------------------------------------------------- 5 ---------------------------------------------------------------------------------------
void init()
  {
  IndicatorBuffers(6);
 
//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexDrawBegin(0,i-1);
   SetIndexBuffer(0, RSI);

   SetIndexStyle(1,DRAW_NONE);
   SetIndexDrawBegin(1,i-1);
   SetIndexBuffer(1, RSIMA);
   
   SetIndexStyle(2,DRAW_ARROW, EMPTY, 2); 
   SetIndexDrawBegin(2,i-1);
   SetIndexBuffer(2, SignalUP);
   SetIndexArrow(2,233);

   SetIndexStyle(3,DRAW_ARROW, EMPTY, 2); 
   SetIndexDrawBegin(3,i-1);
   SetIndexBuffer(3, SignalDN);
   SetIndexArrow(3,234);

   SetIndexStyle(4,DRAW_NONE);
   SetIndexDrawBegin(4,i-1);
   SetIndexBuffer(4, RSIBBU);

   SetIndexStyle(5,DRAW_NONE);
   SetIndexDrawBegin(5,i-1);
   SetIndexBuffer(5, RSIBBL);
   
    return(0);
  }
//--------------------------------------------------- 6 ---------------------------------------------------------------------------------------
int start()
{                                                                                              // Special function start()
   int    counted_bars=IndicatorCounted();

   int limit, i;
   limit=(Bars-counted_bars)-1;
   for (i = limit; i >= 0; i--)
   {
      RSI[i]            = iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,i);
      RSIMA[i]          = iMAOnArray(RSI,0,RSIMAPeriod,0,MODE_SMA,i);
      RSIBBU[i]         = iBandsOnArray(RSI,0,BandsPeriod,BandsDeviations,BandsShift,MODE_UPPER,i);
      RSIBBL[i]         = iBandsOnArray(RSI,0,BandsPeriod,BandsDeviations,BandsShift,MODE_LOWER,i);
      RSIBBM            = (RSIBBL[i] + RSIBBU[i]) / 2;
      double iSAR_t1    = iSAR(NULL,0,0.02,0.2,i+1);
      double dHigh_t2   = iHigh(NULL,0,i+2);
      double dLow_t2    = iLow(NULL,0,i+2);
//--------------------------------------------------- 7 ---------------------------------------------------------------------------------------
                                                                                        // Initialize Latest Buffer Value
      SignalUP[i] = 0.0;
      SignalDN[i] = 0.0;
//--------------------------------------------------- 8 ---------------------------------------------------------------------------------------
                                                                                        // Long Signal
      if((RSI[i] > RSIMA[i]) && (RSI[i+1] < RSIMA[i+1]) && RSI[i+1] < RSIBBM)
      {
         SignalUP[i]    = Low[i] - Point * 40;
      } 
//--------------------------------------------------- 9 ---------------------------------------------------------------------------------------
                                                                                        // Short Signal
      if(RSI[i] < RSIMA[i] && RSI[i+1] > RSIMA[i+1] && RSI[i+1] > RSIBBM)
      {
         SignalDN[i]    = High[i] + Point * 40;
      }
   }
//--------------------------------------------------- 10 --------------------------------------------------------------------------------------
   return(0);
  }
//--------------------------------------------------- 11 ---------------------------------------------------------------------------------------

Could someone please help.

 

I haven't tried the code, but I don't like

SetIndexDrawBegin(0,i-1);

etc.

before i has been set (it is uninitialised)

 

I've just noticed that i is both a global and local variable (it's defined twice - once just above init() & again 2 lines into start()

This is a no-no in my view. The compile knows exactly what it's doing, but us lesser mortals get confused.

 
brewmanz:

I've just noticed that i is both a global and local variable (it's defined twice - once just above init() & again 2 lines into start()

This is a no-no in my view. The compile knows exactly what it's doing, but us lesser mortals get confused.



Hello brewmanz


Thank you for your comments.

As a novice programmer I will take notice at theses issues you commented on.


B

Reason: