Help adding simple alert to indicator

 
Hi, I'm really frustrated trying to get this to work and figure I'd break down and ask for some help... I just want this indicator to alert.wav me when a new bar causes the graph to turn from Yellow to Green, or Yellow to Red or Green/Red turns Yellow. Green being bullish, Yellow is ranging, and Red is Bearish. Would be greatly appreciated. Thank you!



//+------------------------------------------------------------------+
//| |
//| Copyright © 2004, MetaQuotes Software Corp. |
//|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, FX Sniper "

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;

extern int Rperiod = 5;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum[];
int length;
double lengthvar;
double tmp ;
double wt[];
int c;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);

//---- drawing settings
SetIndexBuffer(2,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(0,ExtMapBuffer3);
SetIndexBuffer(3,sum);
SetIndexBuffer(4,wt);

SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
return(0);
}

int start()

{ Draw4HowLong = Bars-Rperiod - 5;
length = Rperiod;
loopbegin = Draw4HowLong - length - 1;

for(shift = loopbegin; shift >= 0; shift--)
{
sum[1] = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
tmp = ( i - lengthvar)*Close[length-i+shift];
sum[1]+=tmp;
}
wt[shift] = sum[1]*6/(length*(length+1));

//========== COLOR CODING ===========================================

ExtMapBuffer3[shift] = wt[shift]; //red
ExtMapBuffer2[shift] = wt[shift]; //green
ExtMapBuffer1[shift] = wt[shift]; //yellow

// for(c=loopbegin;c==shift;c++)
// {
if (wt[shift+1] > wt[shift])
{
ExtMapBuffer2[shift] = EMPTY_VALUE;



// ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
// Print("time= ",Time[shift]);
// ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
// ObjectSet("smiley_face", OBJPROP_COLOR, Red);
// ObjectSet("smiley_face", OBJPROP_WIDTH, 1);
// ObjectsRedraw();

//ExtMapBuffer3[shift+1] = EMPTY_VALUE;
//ExtMapBuffer3[shift+1] = EMPTY_VALUE;

}
else if (wt[shift+1] < wt[shift])
{
ExtMapBuffer1[shift] = EMPTY_VALUE; //-1 red/greem tight
//ExtMapBuffer3[shift+1] = EMPTY_VALUE;

}
else
{

ExtMapBuffer1[shift]=EMPTY_VALUE;//EMPTY_VALUE;
ExtMapBuffer2[shift]=EMPTY_VALUE;//EMPTY_VALUE;
}

}

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

use iCustom()


 

Have 2 global variables

int oldvalue, newvalue;
if (wt[shift+1] > wt[shift])
{
ExtMapBuffer2[shift] = EMPTY_VALUE;
newvalue=1;
// ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
// Print("time= ",Time[shift]);
// ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
// ObjectSet("smiley_face", OBJPROP_COLOR, Red);
// ObjectSet("smiley_face", OBJPROP_WIDTH, 1);
// ObjectsRedraw();
//ExtMapBuffer3[shift+1] = EMPTY_VALUE;
//ExtMapBuffer3[shift+1] = EMPTY_VALUE;
}
else if (wt[shift+1] < wt[shift]) 
{
ExtMapBuffer1[shift] = EMPTY_VALUE; //-1 red/greem tight
//ExtMapBuffer3[shift+1] = EMPTY_VALUE;
newvalue=2;
}
else 
{
newvalue=3;
ExtMapBuffer1[shift]=EMPTY_VALUE;//EMPTY_VALUE;
ExtMapBuffer2[shift]=EMPTY_VALUE;//EMPTY_VALUE;
} 

if(newvalue!=oldvalue)
{
if(newvalue==1){
Alert("The bar has become yellow");
}else if(newvalue==2){
Alert("The bar has become green");
}else if(newvalues==3){
Alert("The bar has become red");
}
oldvalue=newvalue;
}
 

It alerts now, but every tick, and it freezes up mt4 when I open it. I only want it to alert when a color changes on a specified timeframe (Current). Right now its changing on the 'tick' timeframe hundreds of times. here is what I have now... Thanks Heelflip!

//+------------------------------------------------------------------+
//|                                                        |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, FX Sniper "
#property  link      "http://www.metaquotes.net/"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Green
#property indicator_color3 Red

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;
extern int Rperiod = 5;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum[];
int length;
double lengthvar;
double tmp ;
double wt[];
int c;
int oldvalue;
int newvalue;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexBuffer(3,sum);
   SetIndexBuffer(4,wt);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
   return(0);
  }

int start()
{
      
      Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;

      for(shift = loopbegin; shift >= 0; shift--)
      { 
         sum[1] = 0;
         for(i = length; i >= 1  ; i--)
         {
         lengthvar = length + 1;
         lengthvar /= 3;
         tmp = 0;
         tmp = ( i - lengthvar)*Close[length-i+shift];
         sum[1]+=tmp;
         }
         wt[shift] = sum[1]*6/(length*(length+1));
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[shift] = wt[shift]; //red 
       ExtMapBuffer2[shift] = wt[shift]; //green
       ExtMapBuffer1[shift] = wt[shift]; //yellow
       
       //  for(c=loopbegin;c==shift;c++)
       // {
        if (wt[shift+1] > wt[shift])
        {
        ExtMapBuffer2[shift] = EMPTY_VALUE;
  
  
  
  // ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
  // Print("time=  ",Time[shift]);
  // ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
  // ObjectSet("smiley_face", OBJPROP_COLOR , Red);
  // ObjectSet("smiley_face", OBJPROP_WIDTH  , 1);
  // ObjectsRedraw();

        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
       else if (wt[shift+1] < wt[shift]) 
        {
        ExtMapBuffer1[shift] = EMPTY_VALUE; //-1 red/greem tight
       //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
       newvalue=2; 
        }
         else 
         {
         newvalue=3;
         ExtMapBuffer1[shift]=EMPTY_VALUE;//EMPTY_VALUE;
         ExtMapBuffer2[shift]=EMPTY_VALUE;//EMPTY_VALUE;
         }
        if(newvalue!=oldvalue)
        {
        if(newvalue==1){
        Alert("The bar has become yellow");
        }else if(newvalue==2){
        Alert("The bar has become green");
        }else if(newvalue==3){
        Alert("The bar has become red");
        }
        oldvalue=newvalue;
        }
      }
    
      return(0);
  }
//+------------------------------------------------------------------+


 
Jake8055:

It alerts now, but every tick, and it freezes up mt4 when I open it. I only want it to alert when a color changes on a specified timeframe (Current). Right now its changing on the 'tick' timeframe hundreds of times. here is what I have now... Thanks Heelflip!


Means you have to code it that way that it only runs the whole cycle at the first tick each bar....

Succes...

 
Jake8055:

It alerts now, but every tick, and it freezes up mt4 when I open it. I only want it to alert when a color changes on a specified timeframe (Current). Right now its changing on the 'tick' timeframe hundreds of times. here is what I have now... Thanks Heelflip!

Does this indicator re-paint ? if it does then you are going to have to build some kind of Hysteresis into it.
 
Sorry about that, I added the check in the wrong place. What was happening was that it was in the for loop and was alerting to the change in the Rperiod. It should be working now.
//+------------------------------------------------------------------+
//|                                                        |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, FX Sniper "
#property  link      "http://www.metaquotes.net/"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Green
#property indicator_color3 Red

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;
extern int Rperiod = 5;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum[];
int length;
double lengthvar;
double tmp ;
double wt[];
int c;
int oldvalue;
int newvalue;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexBuffer(3,sum);
   SetIndexBuffer(4,wt);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
   return(0);
  }

int start()
{
      
      Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;

      for(shift = loopbegin; shift >= 0; shift--)
      { 
         sum[1] = 0;
         for(i = length; i >= 1  ; i--)
         {
         lengthvar = length + 1;
         lengthvar /= 3;
         tmp = 0;
         tmp = ( i - lengthvar)*Close[length-i+shift];
         sum[1]+=tmp;
         }
         wt[shift] = sum[1]*6/(length*(length+1));
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[shift] = wt[shift]; //red 
       ExtMapBuffer2[shift] = wt[shift]; //green
       ExtMapBuffer1[shift] = wt[shift]; //yellow
       
       //  for(c=loopbegin;c==shift;c++)
       // {
        if (wt[shift+1] > wt[shift])
        {
        ExtMapBuffer2[shift] = EMPTY_VALUE;
  // ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
  // Print("time=  ",Time[shift]);
  // ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
  // ObjectSet("smiley_face", OBJPROP_COLOR , Red);
  // ObjectSet("smiley_face", OBJPROP_WIDTH  , 1);
  // ObjectsRedraw();

        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        newvalue=3;
        }
       else if (wt[shift+1] < wt[shift]) 
        {
        ExtMapBuffer1[shift] = EMPTY_VALUE; //-1 red/greem tight
       //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
       newvalue=2; 
        }         else 
         {
         newvalue=1;
         ExtMapBuffer1[shift]=EMPTY_VALUE;//EMPTY_VALUE;
         ExtMapBuffer2[shift]=EMPTY_VALUE;//EMPTY_VALUE;
         }
      }
            if(newvalue!=oldvalue)
        {
        if(newvalue==1){
        Alert("The bar has become yellow");
        }else if(newvalue==2){
        Alert("The bar has become green");
        }else if(newvalue==3){
        Alert("The bar has become red");
        }
        oldvalue=newvalue;
        }
      return(0);
  }
//+------------------------------------------------------------------+