Download MetaTrader 5

Help needed with time alert indicator

To add comments, please log in or register
ReactoFX
118
ReactoFX  

Hi everyone, I have created the attached indicator to alert me on some set moments in time, but being a newbie I cannot get it to work properly. Can anyone tell me what I have done wrong here end how it should be coded?


#property indicator_chart_window


extern int      Day_1 = 28;
extern int      Hour_1 = 14;
extern int      Minute_1 = 40;

extern int      Day_2 = 28;
extern int      Hour_2 = 14;
extern int      Minute_2 = 50;

extern string   __1 = "===== Alerts Definition =====";
extern bool     SoundAlert = true;
extern bool     EmailAlert = false;
extern bool     NotificationAlert = false;



//jjj
int LastAlert=0;

int NewAlert=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

     
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()

  {
       int i=0;
       int Seconds = 0;
       string TimeAlert_1 = Day_1 + " " + Hour_1 + ":" + Minute_1+ Seconds;
       datetime alertTime = StrToTime(TimeAlert_1);        
       static datetime alertTriggered = 0;


     if((TimeMinute(Time[i]) == Minute_1 && TimeHour(Time[i]) == Hour_1 && TimeDay(Time[i]) == Day_1) = TimeAlert_1)

     {
      Alert("Time alert");
      alertTriggered = alertTime;
     }

  }         


//+------------------------------------------------------------------+


Files:
Keith Watford
Moderator
10652
Keith Watford  
For small amounts of code please paste into your post using the SRC button. I have added it for you this time.
ReactoFX
118
ReactoFX  
Keith Watford:
For small amounts of code please paste into your post using the SRC button. I have added it for you this time.
Thanks Keith, I'll keep that in mind.
whroeder1
15902
whroeder1  
ReactoFX: . Can anyone tell me what I have done wrong here end how it should be coded?
  1. Not using SRC
  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  3.      if((TimeMinute(Time[i]) == Minute_1 && TimeHour(Time[i]) == Hour_1 && TimeDay(Time[i]) == Day_1) = TimeAlert_1)
    Not posting code that even compiles, (true = TimeAlert_1.)
    '&&' - l-value required    testscr.mq4    10    73
ReactoFX
118
ReactoFX  

Sorry, you're right I should have been more specific


I'm not sure  what you mean by (true = TimeAlert_1), can you clarify please


no need to clarify, apparently I needed some time to figure it out.


Thanks for pointing it out.

Kaleem Haider
131187
Kaleem Haider  
ReactoFX:

Sorry, you're right I should have been more specific


I'm not sure  what you mean by (true = TimeAlert_1), can you clearify please

you should be using == (double =) instead of single, that is how its done in C++.   You should also try to learn in the newer coding language where start are substituted by OnCalculate, deinit by OnDeinit, and many other changes.  Please refer to this guide for the newer language changes. https://docs.mql4.com/mql4changes  If you build a habit to use new language your transition to MQL5 language will be much smoother.  

I created an indicator a while ago.  I hope this helps.

 

//+------------------------------------------------------------------+
//|                                               Tipu Tme VLine.mq4 |
//|                                                    Kaleem Haider |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright © 2016, Kaleem Haider."
#property link        "https://www.mql5.com/en/users/kaleem.haider/seller"
#property description "Plots Vertical Lines for Time and Alert on Time."
#property version   "1.00"
#property strict
#property indicator_chart_window

static datetime prevTime;

input int iHour  = 23;           //Hour
input int iMin   = 0;            //Minute
input int iWidth = 2;            //Width
input color iColor= clrFireBrick;  //Color
input bool  bAlertM=true;          //Alert Mobile
input bool  bAlertS=true;           //Alert Onscreen
input bool  bAlertE= true;          //Alert Email
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit,shift;

   if(prev_calculated < 0)   return(0);
   if(prev_calculated>0) limit=rates_total-prev_calculated+1;
   else limit=rates_total-1;

   for(shift=limit-1; shift>=0 && !IsStopped(); shift--)
     {
      datetime dtTime=StrToTime(TimeToStr(time[shift],TIME_DATE)+" "+(string)iHour+":"+(string)iMin);
      //Plot Vertical Lines for Time
      if(time[shift]==dtTime)
        {
         string stempname="tiputline"+(string)dtTime;
         ObjectCreate(stempname,OBJ_VLINE,0,dtTime,0);
         ObjectSet(stempname,OBJPROP_WIDTH,iWidth);
         ObjectSet(stempname,OBJPROP_COLOR,iColor);
         ObjectSet(stempname,OBJPROP_BACK,true);
        }
     }
//Alert on time
   if(time[0]==StrToTime(TimeToStr(time[0],TIME_DATE)+" "+(string)iHour+":"+(string)iMin))
     {
      string sMsg = "Time Alert Hour: "+(string)iHour + " Minute: " + (string)iMin;
      string sSub = "Time Alert Hour: "+(string)iHour + " Minute: " + (string)iMin;
      SendAlert(bAlertM,bAlertS,bAlertE,sMsg,sSub);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Send Alert Function                                                                 |
//+------------------------------------------------------------------+
void SendAlert(bool bMobile,bool bScreen,bool bEmail,string sMsg,string sSub)
  {
   if(bMobile || bScreen || bEmail)
     {
      if(sMsg!="")
        {
        //prevTime stores last alert time, so alert only happens once a minute.
         if(prevTime<iTime(_Symbol,_Period,0))
           {
            prevTime=iTime(_Symbol,_Period,0);
            if(bMobile) SendNotification(sSub);
            if(bScreen) Alert(sSub);
            if(bEmail) SendMail(sSub,sMsg);
           }
        }
     }
  }

void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0,"tiputline");

   return;
  }
Files:
ReactoFX
118
ReactoFX  
Thanks Kaleem for your advice and for adding your solution to a similar problem, have a nice day.
To add comments, please log in or register