alert on new candle not working

 

The alert is supposed to wait for the candle to close and then compare the candle to the previous one to see if the colour has changed.

However, it is alerting on the current candle open.

ANy ideas what to change?


while(pos >= 0)
     {
       haOpen = (ExtMapBuffer3[pos+1] + ExtMapBuffer4[pos+1]) / 2;
       haClose = (Open[pos] + High[pos] + Low[pos] + Close[pos]) / 4;
       haHigh = MathMax(High[pos], MathMax(haOpen, haClose));
       haLow = MathMin(Low[pos], MathMin(haOpen, haClose));
       if(haOpen  <haClose) 
         {
           ExtMapBuffer1[pos] = haLow;
           ExtMapBuffer2[pos] = haHigh;
         } 
       else
         {
           ExtMapBuffer1[pos] = haHigh;
           ExtMapBuffer2[pos] = haLow;
         } 
       ExtMapBuffer3[pos] = haOpen;
       ExtMapBuffer4[pos] = haClose;
       
       //CHanged to bullish
       //draw on chart
       if (ExtMapBuffer3[pos] < ExtMapBuffer4[pos] &&
          ExtMapBuffer3[pos+1] > ExtMapBuffer4[pos+1]) //ie colour change
       {
         //ChangeUp[pos] = ExtMapBuffer3[pos] - 0.0020;
       }
       //Alert - check one extra candle back to ensure we are checking closes
       if (ExtMapBuffer3[pos+1] < ExtMapBuffer4[pos+1] &&
          ExtMapBuffer3[pos+2] > ExtMapBuffer4[pos+2]) //ie colour change
       {
         AlertOnce (Symbol()+" - HA colour change",1);
       }

       //changed to bearish
       //draw
       if (ExtMapBuffer3[pos] > ExtMapBuffer4[pos] &&
          ExtMapBuffer3[pos+1] < ExtMapBuffer4[pos+1]) //ie colour change
       {
         //ChangeDown[pos] = ExtMapBuffer3[pos] + 0.0020;
       }
       //alert
       if (ExtMapBuffer3[pos+1] > ExtMapBuffer4[pos+1] &&
          ExtMapBuffer3[pos+2] < ExtMapBuffer4[pos+2]) //ie colour change
       {
         AlertOnce (Symbol()+" - HA colour change",1);
       }
       
             pos--;
     }

   return(0);
}
 

you're currently checking from 'pos' to 0 index:

while(pos >= 0)

since you want to check on barclose try:

while(pos > 0) //---- or --- while(pos >= 1)

to disclude the bar being drawn

 
if the while is part of the normal indicator drawing, only alert on pos==0
 

This doesn;t seem to alert correctly:


//+------------------------------------------------------------------+
//|                                                  Heiken Ashi.mq4 |
//|                      Copyright c 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
//| For Heiken Ashi we recommend next chart settings ( press F8 or   |
//| select on menu 'Charts'->'Properties...'):                       |
//|  - On 'Color' Tab select 'Black' for 'Line Graph'                |
//|  - On 'Common' Tab disable 'Chart on Foreground' checkbox and    |
//|    select 'Line Chart' radiobutton                               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Green
#property indicator_color5 Green
#property indicator_color6 Red
//----
extern color color1 = Red;
extern color color2 = Green;
extern color color3 = Red;
extern color color4 = Green;
extern int alertsOn = true;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ChangeUp[];
double ChangeDown[];
double arrhaOpen[];
double arrhaClose[];
//----
int ExtCountedBars=0;

bool AlertOnce(string alert_msg, int ref)
{  
   static int LastAlert_1 = 0;
   
   switch(ref)
   {
      case 1:
         if( LastAlert_1 == 0 || LastAlert_1 < Bars )
         {
            Alert(alert_msg);
            LastAlert_1 = Bars;
            return (1);
         }
      break;
   }
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_HISTOGRAM, 0, 1, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM, 0, 1, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM, 0, 2, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3, DRAW_HISTOGRAM, 0, 2, color4);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0, 10);
   SetIndexDrawBegin(1, 10);
   SetIndexDrawBegin(2, 10);
   SetIndexDrawBegin(3, 10);
//---- indicator buffers mapping
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexBuffer(3, ExtMapBuffer4);
   
   SetIndexStyle(4, DRAW_ARROW, EMPTY);
   SetIndexArrow(4, 233);
   SetIndexBuffer(4, ChangeUp);
   SetIndexStyle(5, DRAW_ARROW, EMPTY);
   SetIndexArrow(5, 234);
   SetIndexBuffer(5, ChangeDown);
//---- initialization done

            //little aquare for alarms
            int scaleX=20,
                  scaleY=0,
                  offsetX=-17,
                  offsetY=0;

            ObjectCreate("alarmbox",OBJ_LABEL,0,0,0,0,0);
            ObjectSet("alarmbox",OBJPROP_CORNER,1);
            ObjectSet("alarmbox",OBJPROP_XDISTANCE,scaleX+offsetX);
            ObjectSet("alarmbox",OBJPROP_YDISTANCE,scaleY+offsetY);
            ObjectSetText("alarmbox",CharToStr(183),20,"Wingdings",Blue);

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
string newestArrow = "";
string name;
int j;
int i;
for(j = 0; j < Bars-1 ; j++){
   for(i = ObjectsTotal()-1; i >= 0; i--){
      name = ObjectName(i);
      if(ObjectType(name) == OBJ_ARROW && ObjectGet(name, OBJPROP_TIME1) >= Time[i]){
         newestArrow = name;
         break;
      }
   }
   if(newestArrow != "") break;
}

for(i = ObjectsTotal()-1; i >= 0; i--){
   name = ObjectName(i);
   if(name == newestArrow) continue;
   if(ObjectType(name) == OBJ_ARROW) ObjectDelete(name);
}

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   Comment("HA candle alerts ON");
   
   double haOpen, haHigh, haLow, haClose;
   if(Bars <= 10) 
       return(0);
   ExtCountedBars = IndicatorCounted();
//---- check for possible errors
   if(ExtCountedBars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(ExtCountedBars > 0) 
       ExtCountedBars--;
   int pos = Bars - ExtCountedBars - 1;
   //while(pos >= 0)
   while(pos > 0)
     {
       haOpen = (ExtMapBuffer3[pos+1] + ExtMapBuffer4[pos+1]) / 2;
       haClose = (Open[pos] + High[pos] + Low[pos] + Close[pos]) / 4;
       haHigh = MathMax(High[pos], MathMax(haOpen, haClose));
       haLow = MathMin(Low[pos], MathMin(haOpen, haClose));
       if(haOpen  <haClose) 
         {
           ExtMapBuffer1[pos] = haLow;
           ExtMapBuffer2[pos] = haHigh;
         } 
       else
         {
           ExtMapBuffer1[pos] = haHigh;
           ExtMapBuffer2[pos] = haLow;
         } 
       ExtMapBuffer3[pos] = haOpen;
       ExtMapBuffer4[pos] = haClose;
       
       //CHanged to bullish
       //draw on chart
       if (ExtMapBuffer3[pos] < ExtMapBuffer4[pos] &&
          ExtMapBuffer3[pos+1] > ExtMapBuffer4[pos+1]) //ie colour change
       {
         //ChangeUp[pos] = ExtMapBuffer3[pos] - 0.0020;
       }
       //Alert - check one extra candle back to ensure we are checking closes
       if (ExtMapBuffer3[pos+1] < ExtMapBuffer4[pos+1] &&
          ExtMapBuffer3[pos+2] > ExtMapBuffer4[pos+2]) //ie colour change
       {
         AlertOnce (Symbol()+" - HA colour change to green",1);
       }

       //changed to bearish
       //draw
       if (ExtMapBuffer3[pos] > ExtMapBuffer4[pos] &&
          ExtMapBuffer3[pos+1] < ExtMapBuffer4[pos+1]) //ie colour change
       {
         //ChangeDown[pos] = ExtMapBuffer3[pos] + 0.0020;
       }
       //alert
       if (ExtMapBuffer3[pos] > ExtMapBuffer4[pos] &&
          ExtMapBuffer3[pos+1] < ExtMapBuffer4[pos+1]) //ie colour change
       {
         AlertOnce (Symbol()+" - HA colour change to red",1);
       }
       
             pos--;
     }

   return(0);
}
//+------------------------------------------------------------------+
Reason: