TrendLineAlert_V2

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

//|                                            TrendLineAlert_V2.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

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

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

 /*

  =48:0B>@ >B>1@0605B =0:;>==CN B@5=4>2CN ;8=8N, A ?><>ILN :>B>@>9 70405BAO C@>25=L A@010BK20=8O A83=0;0. 

  @8 ?5@2>< 70?CA:5 8=48:0B>@0 ;8=8O, 7040NI0O C@>25=L A@010BK20=8O, >:@0H5=0 2 A5@K9 F25B 8 =50:B82=0. 

  @8 87<5=5=88 ?>;>65=8O ;8=88 =0 3@0D8:5 B@5945@>< >=0 AB0=>28BAO 0:B82=>9 8 <5=O5B F25B =0 :@0A=K9.

  0: B>;L:> F5=0 4>945B 4> ;8=88 A@010BK20=8O 8 :>A=5BAO 55 8;8 70945B 70 3@0=8FC ;8=88, 8=48:0B>@ A@07C =0G=5B ?>4020BL 0;5@BK 8;8 72C:>2K5 A83=0;K. 

  0 :064>< B8:5 2 <><5=B 70E>40 F5=K 70 ;8=8N 8=48:0B>@ 1C45B ?>4020BL ?> A83=0;C 4> B5E ?>@, ?>:0 =5 8AG5@?05B ;8<8B A83=0;>2,

  >?@545;O5<K9 2 =0AB@>9:0E 8=48:0B>@0 2=5H=59 ?5@5<5==>9 AlertTotal (:>;8G5AB2> A83=0;>2).

  >A;5 MB>3> ;8=8O A@010BK20=8O >:@0A8BAO 2 A5@K9 F25B 8 AB0=5B =50:B8B2=>9 4> 55 A;54CI53> ?5@5<5I5=8O =0 B@51C5<K5 C@>2=8 B@5945@><.

*/

//---- 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.00"

#property description "=48:0B>@ ?>40QB A83=0;K  8 >B?@02;O5B push-A>>1I5=8O =0 A<0@BD>= ?@8 ?@>1>5 B@5=4>2>9 ;8=88, ?0@0<5B@K :>B>@>9 CAB0=02;820NBAO ?@8 AB0@B5 8=48:0B5@0 B@5945@><"

#property indicator_buffers 0

#property indicator_plots   0

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

#property indicator_chart_window 

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

//| 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="Trend_Level_1";                            //8<O C@>2=O A@010BK20=8O

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

input color active_level_color=clrRed;                              //F25B 0:B828@>20==>3> C@>2=O

input color inactive_level_color=clrGray;                           //F25B A@01>B02H53> C@>2=O

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

input ENUM_WIDTH level_width=w_3;                                   //B>;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 B@5=4>2>9 ;8=88                                        |

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

void CreateTline

(

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

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

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

 datetime time1,         // 2@5<O 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

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

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

 string   text           // B5:AB

 )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_TREND,nwin,time1,price1,time2,price2);

   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,false);

   ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true);

   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 B@5=4>2>9 ;8=88                                   |

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

void SetTline

(

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

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

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

 datetime time1,         // 2@5<O 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

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

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

 string   text           // B5:AB

 )

//---- 

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateTline(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text);

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

      ObjectMove(chart_id,name,0,time1,price1);

      ObjectMove(chart_id,name,1,time2,price2);

      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

     }

//----

  }

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

//|  @>25@:0 B@5=4>2>9 ;8=88 =0 ?5@5<5I5=85                         |

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

bool TlineMoveCheck

(

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

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

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

 datetime& time1,         // 2@5<O 1 F5=>2>3> C@>2=O

 double&   price1,        // 1 F5=>2>9 C@>25=L

 datetime& time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double&   price2         // 2 F5=>2>9 C@>25=L

 )

//---- 

  {

//----

   static double price1_=0,price2_=0;

   static datetime time1_=0,time2_=0;

//----

   if(ObjectFind(chart_id,name)!=-1)

     {

      time1=datetime(ObjectGetInteger(chart_id,name,OBJPROP_TIME,0));

      time2=datetime(ObjectGetInteger(chart_id,name,OBJPROP_TIME,1));

      price1=ObjectGetDouble(chart_id,name,OBJPROP_PRICE,0);

      price2=ObjectGetDouble(chart_id,name,OBJPROP_PRICE,1);



      if(time1!=time1_ || time2!=time2_ || price1!=price1_ || price2!=price2_)

        {

         time1_=time1;

         time2_=time2;

         price1_=price1;

         price2_=price2;

         return(true);

        }

     }

//----

   return(false);

  }

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

//|  >8A: 10@0 ?> 2@5<5=8 >B:@KB8O                                  |

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

int FindBar

(

 int endtbar,//AB0@B>2K9 10@ (A0<K9 =>2K9)

 int startbar,//:>=5G=K9 10@ (A0<K9 AB0@K9)

 datetime bartime,// 2@5<O 10@0

 const datetime &time[]// B09<A5@8O time[]

 )

//---- 

  {

//----

   for(int bar=startbar; bar>=endtbar; bar--) if(time[bar]<=bartime) return(bar);

//----

   return(endtbar);

  }

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//---- A>740Q< C@>25=L, 5A;8 >= >BACBAB2C5B

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

//----

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//---- C40;O5< C@>25=L, 5A;8 MB> =5>1E>48<>

   if(Deletelevel) ObjectDelete(0,level_name);

//----

   ChartRedraw(NULL); 

  }

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

//| 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[]

                )

  {

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(rates_total<20) return(0);



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

   double level,price0;

   int bar0;

   static double price2,price1,K;

   static datetime time1,time2;

   static uint count;

   static int bar1,bar2,dbar,startpos;



//---- 2>AAB0=>2;5=85 C@>2=O 2 A;CG05 53> 8AG57=>25=8O

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

     {

      if(count) SetTline(0,level_name,0,time1,price1,time2,price2,active_level_color,level_style,level_width,level_comment);

      else SetTline(0,level_name,0,time1,price1,time2,price2,inactive_level_color,level_style,level_width,level_comment);

     }



   bar0=rates_total-1;

   price0=close[bar0];



//---- CAB0=>2:0 C@>2=O 2 =50:B82=CN, AB0@B>2CN ?>78F8N

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

     {

      TlineMoveCheck(0,level_name,0,time1,price1,time2,price2);



      if(!price1 || !price2 || !time1 || !time2)

        {

         bar1=rates_total-20;

         bar2=rates_total-1;

         time1=time[bar1];

         time2=time[bar2];

         price1=close[bar1];

         price2=close[bar2];

         SetTline(0,level_name,0,time1,price1,time2,price2,inactive_level_color,level_style,level_width,level_comment);

         TlineMoveCheck(0,level_name,0,time1,price1,time2,price2);

         count=0;

        }

     }



//---- ?@>25@:0 C@>2=O =0 ?5@5<5I5=85 8 53> 0:B82870F8O 

   if(TlineMoveCheck(0,level_name,0,time1,price1,time2,price2))

     {

      bar1=FindBar(0,bar0,time1,time);

      time1=time[bar1];



      bar2=FindBar(0,bar0,time2,time);

      time2=time[bar2];



      SetTline(0,level_name,0,time1,price1,time2,price2,active_level_color,level_style,level_width,level_comment);

      count=AlertTotal;



      dbar=bar2-bar1;

      if(!dbar) dbar=1;

      K=(price2-price1)/dbar;



      level=K*(bar0-bar1)+price1;

      if(price0>level) startpos=+1;

      else             startpos=-1;

     }



//---- 2KG8A;5=85 B5:CI53> 7=0G5=8O B@5=4>2>3>, =0:;>==>3> C@>2=O A@010BK20=8O

   level=K*(bar0-bar1)+price1;



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

   if(count) if(price0>=level && startpos<0)

        {

         BuySignal("TrendLineAlert_V2: @>87>HQ; ?@>1>9 B@5=4>2>9 ;8=88 =0 C@>2=5 "+DoubleToString(level,_Digits),rates_total,prev_calculated,close,spread);

         count--;

         if(!count) SetTline(0,level_name,0,time1,price1,time2,price2,inactive_level_color,level_style,level_width,level_comment);

        }



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

   if(count) if(price0<=level && startpos>0)

        {

         SellSignal("TrendLineAlert_V2: @>87>HQ; ?@>1>9 B@5=4>2>9 ;8=88 =0 C@>2=5 "+DoubleToString(level,_Digits),rates_total,prev_calculated,close,spread);

         count--;

         if(!count) SetTline(0,level_name,0,time1,price1,time2,price2,inactive_level_color,level_style,level_width,level_comment);

        }

//----  

   ChartRedraw(NULL); 

   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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---