Download MetaTrader 5

Alert when candle opens

To add comments, please log in or register
smika
131
smika  

Hi,

 

I am trying to create an indicator, which is working now, but I've got a lot of alerts by every thick. I want to change this, so that I have only an alert when a new candle is open. Can someone help me with my code and tell me what i am doing wrong?

 

Here is my code:

#define INDICATOR_NAME "TMS_Alert"

#property indicator_chart_window
input int RSI_Period = 10; // RSI_Period: 8-25
input ENUM_APPLIED_PRICE RSI_Price = PRICE_TYPICAL;
input int Volatility_Band = 34; // Volatility_Band: 20-40
input double StdDev = 1.6185; // Standard Deviations: 1-3
input int RSI_Price_Line = 2;      
input ENUM_MA_METHOD RSI_Price_Type = MODE_EMA;
input int Trade_Signal_Line = 7;  
input ENUM_MA_METHOD Trade_Signal_Type = MODE_SMA;
extern bool               Alert_Popup=true;           /* Alert: Enable Popup with Sound */
extern bool               Alert_Notification=false;    /* Alert: Enable Push Notification */
extern bool               Alert_Email=false;           /* Alert: Send email */
extern string             Alert_Subject="Default";     /* Email Subject. */ //If "Default" the subject is set by the indicator
double RSIBuf[], UpZone[], MdZone[], //yellow
       DnZone[], MaBuf[], //green
       MbBuf[]; //red
      
datetime now;
bool alerted = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{  
   IndicatorShortName("TDI_Alert(" + IntegerToString(RSI_Period) + "," + IntegerToString(Volatility_Band) + "," + IntegerToString(RSI_Price_Line) + "," + IntegerToString(Trade_Signal_Line) +  ")");
   return(0);
}
void OnDeinit(const int reason)
{
   Comment("");
}


///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
if (IsNewBar()) alerted = false;

{

  
   double curTdiGreen, curTdiRed, curTdiYellow;
   double prevTdiGreen, prevTdiRed, prevTdiYellow;
    
      
         {
             {
            
               curTdiGreen   = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,0);
               prevTdiGreen  = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,1);
               curTdiRed     = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,0);
               prevTdiRed    = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,1);
               curTdiYellow  = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,2,0);
               prevTdiYellow = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,2,1);
               if ((curTdiGreen>curTdiRed)&&(curTdiGreen>curTdiYellow)&&(prevTdiRed>prevTdiGreen))
               { //list all pairs with buy signal
                 //if ((curTdiGreen>curTdiRed)&&(curTdiRed>curTdiYellow)&&(prevTdiRed>prevTdiGreen))
                   DoAlert("LONG Signal!");
                 } else {  //list all pairs with sell signal
                 if ((curTdiYellow>curTdiRed)&&(curTdiRed>curTdiGreen)&&(prevTdiGreen>prevTdiRed))
                   DoAlert("Sell Signal!");
               }
              
             }
         }
         }
      
       alerted = true;
        
  return(0);
}

bool IsNewBar() {
   if (now != Time[0]) {
      now = Time[0];
      return (true);
   }

   return (false);
}

      
//-----------------------------------------------------------------------------
// function: DoAlert()
// Description: Alert on signal
//-----------------------------------------------------------------------------
      
void DoAlert(string sAlertMsg) {
  // Do Alerts        
  sAlertMsg=Symbol()+", "+TF2Str(Period())+": "+sAlertMsg;
  if (Alert_Popup)
    Alert(sAlertMsg, ", ", INDICATOR_NAME);
  if (Alert_Notification)
    SendNotification(sAlertMsg + ", "+ INDICATOR_NAME);
  if (Alert_Email) {
    if (Alert_Subject=="Default")
      Alert_Subject=sAlertMsg;
    SendMail( Alert_Subject, "MT4 Alert! "+INDICATOR_NAME+"\n" + TimeToStr(Time[0],TIME_DATE|TIME_SECONDS )+"\n"+sAlertMsg);              
  }
}      
      
//-----------------------------------------------------------------------------
// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod) {
  switch(iPeriod) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
    default: return("M"+(string)iPeriod);
  }
}
whroeder1
16076
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Where do you ever use your alerted variable?
smika
131
smika  
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it.

  2. Where do you ever use your alerted variable?

Thanks for your reply. I change the post. And thanks for the 2 question. This triggers something, and not it is working.

 

I forgot "if (!alerted)"

 

Thanks! 

To add comments, please log in or register