PriceAlert_V2

Author: Copyright © 2018, Nikolay Kositsin
Miscellaneous
It issuies visual alerts to the screenIt sends emails
0 Views
0 Downloads
0 Favorites
PriceAlert_V2
ÿþ//+------------------------------------------------------------------+

//|                                                PriceAlert_V2.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

//|                              Khabarovsk,   farria@mail.redcom.ru | 

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

//---- 02B>@AB2> 8=48:0B>@0

#property copyright "Copyright © 2018, Nikolay Kositsin"

//---- AAK;:0 =0 A09B 02B>@0

#property link "farria@mail.redcom.ru" 

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.10"

#property description "=48:0B>@ ?>405B A83=0;K ?@8 ?@>1>5 3>@87>=B0;L=>3> C@>2=O"

//---- >B@8A>2:0 8=48:0B>@0 2 3;02=>< >:=5

#property indicator_chart_window

//--- :>;8G5AB2> 8=48:0B>@=KE 1CD5@>2

#property indicator_buffers 0

//--- 8A?>;L7>20=> 2A53> 3@0D8G5A:8E ?>AB@>5=89

#property indicator_plots   0

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

//| 5@5G8A;5=85 4;O B>;I8=K C@>2=O                |

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

enum ENUM_WIDTH //"8? :>=AB0=BK

  {

   w_1 = 1,   //1

   w_2,       //2

   w_3,       //3

   w_4,       //4

   w_5        //5

  };

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

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                   |

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

input string level_name="Price_Level_1";           // <O C@>2=O A@010BK20=8O

input string level_comment="C@>25=L A@010BK20=8O"; // ><<5=B0@89 : C@>2=N A@010BK20=8O

input color active_level_color=Red;                // &25B 0:B828@>20==>3> C@>2=O

input color inactive_level_color=Gray;             // &25B A@01>B02H53> C@>2=O

input ENUM_LINE_STYLE level_style=STYLE_SOLID;     // !B8;L C@>2=O A@010BK20=8O

input ENUM_WIDTH level_width=w_3;                  // ">;I8=0 C@>2=O A@010BK20=8O

input bool AlertON=true;                           //  07@5H5=85 ?>40G8 0;5@B0

input uint AlertTotal=3;                           // :>;8G5AB2> A83=0;>2

input bool EMailON=false;                          //  07@5H5=85 ?>GB>2>9 >B?@02:8 A83=0;0

input bool PushON=false;                           //  07@5H5=85 >B?@02:8 A83=0;0 =0 <>18;L=K9

input bool Deletelevel=true;                       // C40;5=85 =0AB@>9:8 C@>2=O ?@8 A<5=5 B09<D@59<0 

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



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

//|  !>740=85 3>@87>=B0;L=>9 ;8=88                                   |

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

void CreateHline(long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

                 string   name,          // 8<O >1J5:B0

                 int      nwin,          // 8=45:A >:=0

                 double   price,         // F5=0 3>@87>=B0;L=>3> C@>2=O

                 color    Color,         // F25B ;8=88

                 int      style,         // AB8;L ;8=88

                 int      width,         // B>;I8=0 ;8=88

                 bool     background,    // D>=>2>5 >B>1@065=85 ;8=88

                 string   text)          // B5:AB

  {

//----

   ObjectCreate(chart_id,name,OBJ_HLINE,nwin,0,price);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);

   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);

   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,background);

   ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);

   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);

//----

  }

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

//|  5@5CAB0=>2:0 3>@87>=B0;L=>9 ;8=88                              |

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

void SetHline(long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

              string   name,          // 8<O >1J5:B0

              int      nwin,          // 8=45:A >:=0

              double   price,         // F5=0 3>@87>=B0;L=>3> C@>2=O

              color    Color,         // F25B ;8=88

              int      style,         // AB8;L ;8=88

              int      width,         // B>;I8=0 ;8=88

              bool     background,    // D>=>2>5 >B>1@065=85 ;8=88

              string   text)          // B5:AB

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateHline(chart_id,name,nwin,price,Color,style,width,background,text);

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

      ObjectMove(chart_id,name,0,0,price);

      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

     }

//----

  }

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//----

   if(ObjectFind(0,level_name)==-1) CreateHline(0,level_name,0,0,inactive_level_color,level_style,level_width,false,level_comment);

//----

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//----

   if(Deletelevel) ObjectDelete(0,level_name);

//----

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,    // :>;8G5AB2> 8AB>@88 2 10@0E =0 B5:CI5< B8:5

                const int prev_calculated,// :>;8G5AB2> 8AB>@88 2 10@0E =0 ?@54K4CI5< B8:5

                const datetime &time[],

                const double &open[],

                const double& high[],     // F5=>2>9 <0AA82 <0:A8<C<>2 F5=K 4;O @0AGQB0 8=48:0B>@0

                const double& low[],      // F5=>2>9 <0AA82 <8=8<C<>2 F5=K  4;O @0AGQB0 8=48:0B>@0

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE 

   double level,price0=close[rates_total-1];

   static double startprice,oldlevel;

   static uint count;



   if(ObjectFind(0,level_name)==-1)

     {

      if(count) CreateHline(0,level_name,0,oldlevel,active_level_color,level_style,level_width,false,level_comment);

      else CreateHline(0,level_name,0,oldlevel,inactive_level_color,level_style,level_width,false,level_comment);

     }



   if(prev_calculated>rates_total || prev_calculated<=0) // ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0

     {

      level=ObjectGetDouble(0,level_name,OBJPROP_PRICE);

      if(!level)

        {

         level=price0;

         SetHline(0,level_name,0,level,inactive_level_color,level_style,level_width,false,level_comment);

         oldlevel=level;

         count=0;

        }

     }



   level=ObjectGetDouble(0,level_name,OBJPROP_PRICE);



   if(level!=oldlevel)

     {

      SetHline(0,level_name,0,level,active_level_color,level_style,level_width,false,level_comment);

      oldlevel=level;

      count=AlertTotal;

      startprice=price0;

     }

        

//---- ?>40G0 0;5@B0 8 >B?@02:0 A>>1I5=89

   if(count) if(price0>=level && startprice<level)

        {

         BuySignal("PriceAlert_V2: @>87>H5; ?@>1>9 F5=>2>3> C@>2=O "+DoubleToString(level,_Digits),rates_total,prev_calculated,close,spread);

         count--;

         if(!count) SetHline(0,level_name,0,level,inactive_level_color,level_style,level_width,false,level_comment);

        }



//---- ?>40G0 0;5@B0 8 >B?@02:0 A>>1I5=89

   if(count) if(price0<=level && startprice>level)

        {

         SellSignal("PriceAlert_V2: @>87>H5; ?@>1>9 F5=>2>3> C@>2=O "+DoubleToString(level,_Digits),rates_total,prev_calculated,close,spread);

         count--;

         if(!count) SetHline(0,level_name,0,level,inactive_level_color,level_style,level_width,false,level_comment);

        }



//----   

   return(rates_total);

  }

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

//| Buy signal function                                              |

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

void BuySignal(string SignalSirname,      // B5:AB 8<5=8 8=48:0B>@0 4;O ?>GB>2KE 8 ?CH-A83=0;>2

               const int Rates_total,     // B5:CI55 :>;8G5AB2> 10@>2

               const int Prev_calculated, // :>;8G5AB2> 10@>2 =0 ?@54K4CI5< B8:5

               const double &Close[],     // F5=0 70:@KB8O

               const int &Spread[])       // A?@54

  {

//---

   static uint counter=0;

   if(Rates_total!=Prev_calculated) counter=0;

   bool BuySignal=true;

   bool SeriesTest=ArrayGetAsSeries(Close);

   int index;

   if(SeriesTest) index=0;

   else index=Rates_total-1;

   if(BuySignal && counter<=AlertTotal)

     {

      counter++;

      MqlDateTime tm;

      TimeToStruct(TimeCurrent(),tm);

      string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);

      double Ask=Close[index];

      double Bid=Close[index];

      Bid+=Spread[index]*_Point;

      string sAsk=DoubleToString(Ask,_Digits);

      string sBid=DoubleToString(Bid,_Digits);

      string sPeriod=GetStringTimeframe(ChartPeriod());

      if(AlertON)

        {

         Alert(SignalSirname);

         Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);

        }

      if(EMailON)

        {

         SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);

        }

      if(PushON)

        {

         SendNotification(SignalSirname);

         SendNotification("BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);

        }

     }



//---

  }

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

//| Sell signal function                                             |

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

void SellSignal(string SignalSirname,      // B5:AB 8<5=8 8=48:0B>@0 4;O ?>GB>2KE 8 ?CH-A83=0;>2

                const int Rates_total,     // B5:CI55 :>;8G5AB2> 10@>2

                const int Prev_calculated, // :>;8G5AB2> 10@>2 =0 ?@54K4CI5< B8:5

                const double &Close[],     // F5=0 70:@KB8O

                const int &Spread[])       // A?@54

  {

//---

   static uint counter=0;

   if(Rates_total!=Prev_calculated) counter=0;



   bool SellSignal=true;

   bool SeriesTest=ArrayGetAsSeries(Close);

   int index;

   if(SeriesTest) index=0;

   else index=Rates_total-1;

   if(SellSignal && counter<=AlertTotal)

     {

      counter++;

      MqlDateTime tm;

      TimeToStruct(TimeCurrent(),tm);

      string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);

      double Ask=Close[index];

      double Bid=Close[index];

      Bid+=Spread[index]*_Point;

      string sAsk=DoubleToString(Ask,_Digits);

      string sBid=DoubleToString(Bid,_Digits);

      string sPeriod=GetStringTimeframe(ChartPeriod());

      if(AlertON)

        {

         Alert(SignalSirname);

         Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);

        }

      if(EMailON)

        {

         SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);

        }

      if(PushON)

        {

         SendNotification(SignalSirname);

         SendNotification("SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);

        }

     }

//---

  }

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

//|  >;CG5=85 B09<D@59<0 2 2845 AB@>:8                              |

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

string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)

  {

//----

   return(StringSubstr(EnumToString(timeframe),7,-1));

//----

  }

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

Comments