Anyone could able to modify my code, so the alert can only be done right after the candle closed?

 

Hey guys,

I have written a alert indicator. I found out that, the indicator send out alert before the candle closed. Kindly let me know which part of my code need to be amended. Thanks!

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrGreen //CrossoverUp
#property indicator_color2 clrRed //CrossoverDown
#property indicator_width1 2 //CrossoverUp
#property indicator_width2 2 //CrossoverDown

//--------------------------Crossover Start--------------------------
extern string              Crossover_Alert="This is Crossover Column!";
extern bool                UseAlerts = true;
extern bool                UseEmailAlerts = false;
extern int                 MAPeriod1 =    6;
extern ENUM_MA_METHOD      MAMethod1=MODE_EMA;
extern int                 MAPeriod2 =    18;
extern ENUM_MA_METHOD      MAMethod2=MODE_EMA;
extern int                 MAPeriod3 =    50;
extern ENUM_MA_METHOD      MAMethod3=MODE_EMA;
extern int                 MAPeriod4 =    200;
extern ENUM_MA_METHOD      MAMethod4=MODE_SMA;
double                     CrossUp[];
double                     CrossDown[];
//--------------------------Crossover End--------------------------

datetime                   candletime=0;
datetime                   currenttime=0;

int init()
{
//--------------------------Indicator Start--------------------------
   SetIndexStyle  (0, DRAW_ARROW, EMPTY); //CrossoverUp
   SetIndexArrow  (0, 233);
   SetIndexBuffer (0, CrossUp);

   SetIndexStyle  (1, DRAW_ARROW, EMPTY); //CrossoverDown
   SetIndexArrow  (1, 234);
   SetIndexBuffer (1, CrossDown);
//--------------------------Indicator End--------------------------
   return(0);
}

int deinit()
{
      return(0);
}

int start() 
{
      int                  limit, i;
      int                  counted_bars = IndicatorCounted();
      double               Range, AvgRange, L, L1, H, H1,C;
             
      currenttime = Time[0];
      if(counted_bars < 0) return(-1); //---- check for possible errors
      if(counted_bars > 0) counted_bars--; //---- last counted bar will be recounted
      
      limit = Bars - counted_bars;
      Range = 0;
      AvgRange = 0;
      
      for (int counter = i; counter <= i+9; counter++) { 
         AvgRange = AvgRange + MathAbs(High[counter] - Low[counter]); 
      }

      Range = AvgRange / 10;      
      
      for(i=0; i <= limit; i++) {
         double ma1           =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+1); //6
         double ma1P          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+2);
         double ma1A          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i);
                             
         double ma2           =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+1); //18
         double ma2P          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+2);
         double ma2A          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i);

         H = High[i];
         L = Low[i];
         C = Close[i];
         O = Open[i];
         H1 = High[i+1];
         L1 = Low[i+1];

            if ( ma1 > ma2 && ma1P < ma2P && ma1A > ma2A  ) 
            {
                  if ( (H < H1 && L < L1 && C < H1) || (H<H1 && L>L1 && C<H1) ) 
                  {
                     CrossUp[i-1] = Low[i-1] - Range * 0.6;
                     if(currenttime != candletime && i == 1)
                     { 
                        Alert("Long");  
                     }
                     candletime=Time[0]; //Current Time is Time[0];
                  }
            }
         }
         else if( ma1<ma2 && ma1P>ma2P && ma1A<ma2A ) 
            {
                  if ( (H > H1 && L > L1 && C < L1) || (H>H1 && L<L1 && C>H1)) {
                     CrossDown[i-1] = High[i-1] + Range * 0.6;
                     if(currenttime != candletime && i == 1)
                     { 
                        Alert("Short");  
                     }
                     candletime=Time[0]; //Current Time is Time[0];
                  }
            }
         }
      }      
      return(0);
}
 
Oscar Sim:

Hey guys,

I have written a alert indicator. I found out that, the indicator send out alert before the candle closed. Kindly let me know which part of my code need to be amended. Thanks!

Hi dear,

The problem is here:

         double ma1           =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+1); //6
         double ma1P          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+2);
         double ma1A          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i);
                             
         double ma2           =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+1); //18
         double ma2P          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+2);
         double ma2A          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i);

         H = High[i];
         L = Low[i];
         C = Close[i];
         O = Open[i];
         H1 = High[i+1];
         L1 = Low[i+1];

Your Alert is sent at current price. You have to send it at previous candle close.

So, try this:

         double ma1           =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+2); //6
         double ma1P          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+3);
         double ma1A          =iMA(NULL,15,MAPeriod1,0,MAMethod1,PRICE_CLOSE,i+1);
                             
         double ma2           =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+2); //18
         double ma2P          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+3);
         double ma2A          =iMA(NULL,15,MAPeriod2,0,MAMethod2,PRICE_CLOSE,i+1);

         H = High[i+1];
         L = Low[i+1];
         C = Close[i+1];
         O = Open[i+1];
         H1 = High[i+2];
         L1 = Low[i+2];

By the way,

Why do you write this code?!!

CrossUp[i-1] = Low[i-1] - Range * 0.6;

and

CrossDown[i-1] = High[i-1] + Range * 0.6;

If you want to see the arrows on current candle, try this code:

CrossUp[i] = Low[i] - Range * 0.6;

and

CrossDown[i] = High[i] + Range * 0.6;

Regards,

Arash

 
 limit = Bars - counted_bars;
 /*
 Range = 0;
 AvgRange = 0;
      
 for (int counter = i; counter <= i+9; counter++) { 
    AvgRange = AvgRange + MathAbs(High[counter] - Low[counter]); 
 }

 Range = AvgRange / 10;      
 */
     
 for(i=0; i <= limit; i++) {
    Range = 0;
    AvgRange = 0;
      
    for (int counter = i; counter <= i+9; counter++) { 
       AvgRange = AvgRange + MathAbs(High[counter] - Low[counter]); 
    }

    Range = AvgRange / 10;      
Maybe you should put this part in the next "for loop".
 
Arash Lakzadeh:

Hi dear,

The problem is here:

Your Alert is sent at current price. You have to send it at previous candle close.

So, try this:

By the way,

Why do you write this code?!!

and

If you want to see the arrows on current candle, try this code:

and

Regards,

Arash

Thanks Arash! Will try the code out later!

 
Naguisa Unada:
Maybe you should put this part in the next "for loop".

This is just for the arrow icon to be set within certain range. Hence, with or without loop is ok.

 
Arash Lakzadeh:

Hi dear,

The problem is here:

Your Alert is sent at current price. You have to send it at previous candle close.

So, try this:

By the way,

Why do you write this code?!!

and

If you want to see the arrows on current candle, try this code:

and

Regards,

Arash

Hi Arash,

For the code below, the alert that send out did not meet my below condition, do you have any idea how I should change it?

if ( (H > H1 && L > L1 && C < L1) || (H>H1 && L<L1 && C>H1))
 
Oscar Sim:

Hi Arash,

For the code below, the alert that send out did not meet my below condition, do you have any idea how I should change it?

This condition depends on your strategy.

Did you test this condition on the chart?

Reason: