Attention Weekends

Author: Copyright © 2018, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Attention Weekends
ÿþ//+------------------------------------------------------------------+

//|                                           Attention Weekends.mq5 |

//|                              Copyright © 2018, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2018, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.00"

//--- input parameters

input int      Input1=9;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create timer

   if(!EventSetTimer(60))

     {

      Print(__FUNCTION__,", ERROR: ","error create timer, attempt # 1");

      Sleep(1000*3);

      if(!EventSetTimer(60))

        {

         Print(__FUNCTION__,", ERROR: ","error create timer, attempt # 2");

         Sleep(1000*3);

         if(!EventSetTimer(60))

           {

            Print(__FUNCTION__,", ERROR: ","error create timer, attempt # 3");

            return(INIT_FAILED);

           }

        }

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(0,"Attention Weekends",0,OBJ_RECTANGLE);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---



  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   MqlDateTime STimeCurrent;

   TimeToStruct(TimeCurrent(),STimeCurrent);

   if(STimeCurrent.day_of_week!=5)

      return;



   datetime open_d1_0=iTime(Symbol(),PERIOD_D1,0);

   if(open_d1_0==D'1970.01.01 00:00')

      return;



   datetime open_0=iTime(Symbol(),Period(),0);

   if(open_0==D'1970.01.01 00:00')

      return;

   MqlRates rates[];

   ArraySetAsSeries(rates,true);

   int result=CopyRates(Symbol(),Period(),open_d1_0,open_0,rates);

   if(result<0)

      return;



   double max_price=DBL_MIN;

   double min_price=DBL_MAX;

   for(int i=0;i<result;i++)

     {

      if(rates[i].high>max_price)

         max_price=rates[i].high;

      if(rates[i].low<min_price)

         min_price=rates[i].low;

     }

   if(max_price==DBL_MIN || min_price==DBL_MAX)

      return;



   string   rectangle_name = "Attention Weekends "+TimeToString(open_d1_0,TIME_DATE|TIME_MINUTES);

   datetime time_left      = open_d1_0;   // left point time 

   double   price_left     = min_price;   // left point price 

   datetime time_right     = open_0;      // right point time 

   double   price_right    = max_price;   // right point price 



   if(ObjectFind(0,rectangle_name)<0)

      RectangleCreate(0,rectangle_name,0,time_left,price_left,time_right,price_right);

   else

     {

      RectanglePointChange(0,rectangle_name,0,time_left,price_left);

      RectanglePointChange(0,rectangle_name,1,time_right,price_right);

     }



//---



  }

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

//| Create rectangle by the given coordinates                        | 

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

bool RectangleCreate(const long            chart_ID=0,        // chart's ID 

                     const string          name="Rectangle",  // rectangle name 

                     const int             sub_window=0,      // subwindow index  

                     datetime              time1=0,           // first point time 

                     double                price1=0,          // first point price 

                     datetime              time2=0,           // second point time 

                     double                price2=0,          // second point price 

                     const color           clr=LavenderBlush, // rectangle color 

                     const ENUM_LINE_STYLE style=STYLE_DASHDOT,// style of rectangle lines 

                     const int             width=1,           // width of rectangle lines 

                     const bool            fill=true,         // filling rectangle with color 

                     const bool            back=false,        // in the background 

                     const bool            selection=false,   // highlight to move 

                     const bool            hidden=true,       // hidden in the object list 

                     const long            z_order=0)         // priority for mouse click 

  {

//--- set anchor points' coordinates if they are not set 

   ChangeRectangleEmptyPoints(time1,price1,time2,price2);

//--- reset the error value 

   ResetLastError();

//--- create a rectangle by the given coordinates 

   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- set rectangle color 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the style of rectangle lines 

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the rectangle lines 

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the rectangle 

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

//--- display in the foreground (false) or background (true) 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving 

//--- when creating a graphical object using ObjectCreate function, the object cannot be 

//--- highlighted and moved by default. Inside this method, selection parameter 

//--- is true by default making it possible to highlight and move the object 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution 

   return(true);

  }

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

//| Move the rectangle anchor point                                  | 

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

bool RectanglePointChange(const long   chart_ID=0,       // chart's ID 

                          const string name="Rectangle", // rectangle name 

                          const int    point_index=0,    // anchor point index 

                          datetime     time=0,           // anchor point time coordinate 

                          double       price=0)          // anchor point price coordinate 

  {

//--- if point position is not set, move it to the current bar having Bid price 

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value 

   ResetLastError();

//--- move the anchor point 

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Delete the rectangle                                             | 

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

bool RectangleDelete(const long   chart_ID=0,       // chart's ID 

                     const string name="Rectangle") // rectangle name 

  {

//--- reset the error value 

   ResetLastError();

//--- delete rectangle 

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Check the values of rectangle's anchor points and set default    | 

//| values for empty ones                                            | 

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

void ChangeRectangleEmptyPoints(datetime &time1,double &price1,

                                datetime &time2,double &price2)

  {

//--- if the first point's time is not set, it will be on the current bar 

   if(!time1)

      time1=TimeCurrent();

//--- if the first point's price is not set, it will have Bid value 

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- if the second point's time is not set, it is located 9 bars left from the second one 

   if(!time2)

     {

      //--- array for receiving the open time of the last 10 bars 

      datetime temp[10];

      CopyTime(Symbol(),Period(),time1,10,temp);

      //--- set the second point 9 bars left from the first one 

      time2=temp[0];

     }

//--- if the second point's price is not set, move it 300 points lower than the first one 

   if(!price2)

      price2=price1-300*SymbolInfoDouble(Symbol(),SYMBOL_POINT);

  }

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

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 ---