Download MetaTrader 5

Help with Ichi Alert Indicator

To add comments, please log in or register
Richard Read
336
Richard Read  

Hi!

 

I have coded an alert for my Ichimoku trading, all it does is check for available conditions, and if met it send and Alert(). It is working well, but what I notice is that when I open up the terminal for the first time each day, it sends through yesterdays alerts as well as today's alerts. It's as if the script loads, doesn't realise its a new day, sends the alerts, updates the time and now realises its a new day and then sends the new alerts. Any idea how I can fix this?

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_chart_window

input string font="Arial";
input color clr=clrBlack;

datetime prevtime,curtime;
int BullishStrength,BearishStrength;
string price,tenkij,chik,kumo;
string pricec,tenkijc,chikc,kumoc;
color green=clrGreen;
color red=clrRed;
color orange=clrOrange;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   ObjectDelete(0,"price_arrow");
   ObjectDelete(0,"tenkij_arrow");
   ObjectDelete(0,"kumo_arrow");
   ObjectDelete(0,"chikou_arrow");

   ObjectDelete(0,"price_label");
   ObjectDelete(0,"tenkij_label");
   ObjectDelete(0,"kumo_label");
   ObjectDelete(0,"chikou_label");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete(0,"price_arrow");
   ObjectDelete(0,"tenkij_arrow");
   ObjectDelete(0,"kumo_arrow");
   ObjectDelete(0,"chikou_arrow");

   ObjectDelete(0,"price_label");
   ObjectDelete(0,"tenkij_label");
   ObjectDelete(0,"kumo_label");
   ObjectDelete(0,"chikou_label");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   curtime=iTime(Symbol(),PERIOD_D1,0);
   if(curtime>prevtime && 
      IsConnected()==true)
     {
      ObjectDelete(0,"price_arrow");
      ObjectDelete(0,"tenkij_arrow");
      ObjectDelete(0,"kumo_arrow");
      ObjectDelete(0,"chikou_arrow");

      BullishStrength=0;
      BearishStrength=0;

      //Price above Kumo?
      if(iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1))
        {
         BullishStrength++;
         price=CharToStr(233);
         pricec=green;
        }
      if(iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1))
        {
         BearishStrength++;
         price=CharToStr(234);
         pricec=red;
        }

      if(
         (iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1)) || 
         (iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1))
         )
        {
         price=CharToStr(232);
         pricec=orange;
        }

      //Tenkan-sen & Kijun-sen above Kumo?
      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1))
        {
         BullishStrength++;
         tenkij=CharToStr(233);
         tenkijc=green;
        }
      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1))
        {
         BearishStrength++;
         tenkij=CharToStr(234);
         tenkijc=red;
        }

      if((iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1)) || 
         (iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1)) || 

         (iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1)) || 
         (iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1))
         )

        {
         tenkij=CharToStr(232);
         tenkijc=orange;
        }

      //Kumo is bullish?
      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-25)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-25))
        {
         BullishStrength++;
         kumo=CharToStr(233);
         kumoc=green;
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-25)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-25))
        {
         BearishStrength++;
         kumo=CharToStr(234);
         kumoc=red;
        }

      //Chikou Span above price?
      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,26)>iHigh(Symbol(),PERIOD_D1,26))
        {
         BullishStrength++;
         chik=CharToStr(233);
         chikc=green;
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,26)<iLow(Symbol(),PERIOD_D1,26))
        {
         BearishStrength++;
         chik=CharToStr(234);
         chikc=red;
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,26)>iLow(Symbol(),PERIOD_D1,26) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,26)<iHigh(Symbol(),PERIOD_D1,26))
        {
         chik=CharToStr(232);
         chikc=orange;
        }

      //Create all the labels
      ObjectCreate("price_label",OBJ_LABEL,0,0,0);
      ObjectSet("price_label",OBJPROP_XDISTANCE,10);
      ObjectSet("price_label",OBJPROP_YDISTANCE,20);
      ObjectSetText("price_label","Price to Kumo = ",9,font,clr);

      ObjectCreate("tenkij_label",OBJ_LABEL,0,0,0);
      ObjectSet("tenkij_label",OBJPROP_XDISTANCE,10);
      ObjectSet("tenkij_label",OBJPROP_YDISTANCE,35);
      ObjectSetText("tenkij_label","Tenkan & Kijun to Kumo = ",9,font,clr);

      ObjectCreate("kumo_label",OBJ_LABEL,0,0,0);
      ObjectSet("kumo_label",OBJPROP_XDISTANCE,10);
      ObjectSet("kumo_label",OBJPROP_YDISTANCE,50);
      ObjectSetText("kumo_label","Kumo = ",9,font,clr);

      ObjectCreate("chikou_label",OBJ_LABEL,0,0,0);
      ObjectSet("chikou_label",OBJPROP_XDISTANCE,10);
      ObjectSet("chikou_label",OBJPROP_YDISTANCE,65);
      ObjectSetText("chikou_label","Chikou to Price = ",9,font,clr);

      ObjectCreate("price_arrow",OBJ_LABEL,0,0,0);
      ObjectSet("price_arrow",OBJPROP_XDISTANCE,160);
      ObjectSet("price_arrow",OBJPROP_YDISTANCE,20);
      ObjectSetText("price_arrow",price,9,"Wingdings",pricec);

      ObjectCreate("tenkij_arrow",OBJ_LABEL,0,0,0);
      ObjectSet("tenkij_arrow",OBJPROP_XDISTANCE,160);
      ObjectSet("tenkij_arrow",OBJPROP_YDISTANCE,35);
      ObjectSetText("tenkij_arrow",tenkij,9,"Wingdings",tenkijc);

      ObjectCreate("kumo_arrow",OBJ_LABEL,0,0,0);
      ObjectSet("kumo_arrow",OBJPROP_XDISTANCE,160);
      ObjectSet("kumo_arrow",OBJPROP_YDISTANCE,50);
      ObjectSetText("kumo_arrow",kumo,9,"Wingdings",kumoc);

      ObjectCreate("chikou_arrow",OBJ_LABEL,0,0,0);
      ObjectSet("chikou_arrow",OBJPROP_XDISTANCE,160);
      ObjectSet("chikou_arrow",OBJPROP_YDISTANCE,65);
      ObjectSetText("chikou_arrow",chik,9,"Wingdings",chikc);
      WindowRedraw();

      //BUY
      if(iClose(Symbol(),PERIOD_D1,2)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,2) && 
         iClose(Symbol(),PERIOD_D1,2)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,2) && 
         iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         BullishStrength>=2)
        {
         Alert(Symbol()," | BUY - Kumo Break");
        }

      if(iClose(Symbol(),PERIOD_D1,2)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,2) && 
         iClose(Symbol(),PERIOD_D1,2)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,2) && 
         iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         BullishStrength>=2)
        {
         Alert(Symbol()," | BUY - Kumo Break");
        }

      if(iClose(Symbol(),PERIOD_D1,2)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,2) && 
         iClose(Symbol(),PERIOD_D1,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1) && 
         BullishStrength>=2)
        {
         Alert(Symbol()," | BUY - Kijun Break");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,2)<=iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,2) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1) && 
         BullishStrength>=2)
        {
         Alert(Symbol()," | BUY - Tenkan & Kijun Cross");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-24)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-24) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-25)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-25) && 
         BullishStrength>=2)
        {
         Alert(Symbol()," | BUY - Senkou Cross");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,28)<iHigh(Symbol(),PERIOD_D1,28) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,27)>iHigh(Symbol(),PERIOD_D1,27) && 
         BullishStrength>=2)

        {
         Alert(Symbol()," | BUY - Chikou Break");
        }

      // SELL
      if(iClose(Symbol(),PERIOD_D1,2)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,2) && 
         iClose(Symbol(),PERIOD_D1,2)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,2) && 
         iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,1) && 
         BearishStrength>=2)
        {
         Alert(Symbol()," | SELL - Kumo Break");
        }

      if(iClose(Symbol(),PERIOD_D1,2)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,2) && 
         iClose(Symbol(),PERIOD_D1,2)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,2) && 
         iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,1) && 
         BearishStrength>=2)
        {
         Alert(Symbol()," | SELL - Kumo Break");
        }

      if(iClose(Symbol(),PERIOD_D1,2)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,2) && 
         iClose(Symbol(),PERIOD_D1,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1) && 
         BearishStrength>=2)
        {
         Alert(Symbol()," | SELL - Kijun Break");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,2)>=iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,2) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_TENKANSEN,1)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_KIJUNSEN,1) && 
         BearishStrength>=2)
        {
         Alert(Symbol()," | SELL - Tenkan & Kijun Cross");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-24)>iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-24) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-25)<iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANB,-25) && 
         BearishStrength>=2)
        {
         Alert(Symbol()," | SELL - Senkou Cross");
        }

      if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,28)>iLow(Symbol(),PERIOD_D1,28) && 
         iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_CHINKOUSPAN,27)<iLow(Symbol(),PERIOD_D1,27) && 
         BearishStrength>=2)

        {
         Alert(Symbol()," | SELL - Chikou Break");
        }
      prevtime=iTime(Symbol(),PERIOD_D1,0);
     }
   return(0);
  }
//+------------------------------------------------------------------+          

 

Much appreciated!

 

Richard 

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
Make a check for the day. Yours don't have that check.
Keith Watford
Moderator
10645
Keith Watford  

The D1 values are updating. Use something like

            
  ResetLastError();
  curtime=iTime(Symbol(),PERIOD_D1,0);
  if(GetLastError()==4066)
     {
     Sleep(10000);
     curtime=iTime(Symbol(),PERIOD_D1,0);
     }
  if(curtime>prevtime && IsConnected()==true)
     {
     //your code
     //
     }

 or put the EA on the daily chart

whroeder1
15930
whroeder1  
if(iIchimoku(Symbol(),PERIOD_D1,9,26,52,MODE_SENKOUSPANA,-24) ...
The last parameter of the iIchimoku - MQL4 Documentation is the shift, the bar you want the value for. The current bar is zero, the previous bar is one. You are asking for the future.
Richard Read
336
Richard Read  
GumRai:

The D1 values are updating. Use something like

 or put the EA on the daily chart

Awesome, thanks I'll give this a try!
Richard Read
336
Richard Read  
WHRoeder:
The last parameter of the iIchimoku - MQL4 Documentation is the shift, the bar you want the value for. The current bar is zero, the previous bar is one. You are asking for the future.



Yes that's correct, I am looking at the future Kumo :)
whroeder1
15930
whroeder1  
The future does not exist!
Richard Read
336
Richard Read  
GumRai:

The D1 values are updating. Use something like

 or put the EA on the daily chart

Ok I tried this, but the problem is I am running the alert from a custom indicator - and the Sleep() function can't be called from an indicator. Any work around?

 

Thanks!

 

Richard 

Keith Watford
Moderator
10645
Keith Watford  
read1985:

Ok I tried this, but the problem is I am running the alert from a custom indicator - and the Sleep() function can't be called from an indicator. Any work around?

 

Thanks!

 

Richard 

Sorry, you did say it is an indicator. I should pay more attention :)

You could try

 ResetLastError();
  curtime=iTime(Symbol(),PERIOD_D1,0);
  if(GetLastError()==4066)
     {
     return(0);
     }

 That way it will return and try again the next tick.

There is a problem with checking for error 4066 in a loop, because the error is only returned once. I've never tested it to see if the error is returned in subsequent calls on start()

whroeder1
15930
whroeder1  

GumRai: 

That way it will return and try again the next tick.

There is a problem with checking for error 4066 in a loop, because the error is only returned once. I've never tested it to see if the error is returned in subsequent calls on start()

I believe it won't be. If you can't sleep 15s and retry (because it is an indicator) you can check if curtime is still zero
Richard Read
336
Richard Read  

Awesome thanks for the replies! I'll give that a try and let you know. Will only be able to test it properly on Monday morning but I think this should work.

 

Thanks!

 

Richard 

123
To add comments, please log in or register