Spread Control 3

Author: Copyright © 2021-2022, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Spread Control 3
ÿþ//+------------------------------------------------------------------+

//|                                             Spread Control 3.mq5 |

//|                         Copyright © 2021-2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021-2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "3.001"

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   3

//--- plot SpreadMax

#property indicator_label1  "MqlTick Spread max"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrTomato

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SpreadMin

#property indicator_label2  "MqlTic Spread min"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrViolet

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot spread[]

#property indicator_label3  "OnCalculate spread[]"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrMediumTurquoise

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input int                  InpCode                 = 159;                  // Symbol code from the Wingdings font

input bool                 InpUseDateFromTo        = true;                 // Use Date From ... To

input datetime             InpDateFrom             = D'2021.12.31 00:00';  // --> Date From

input datetime             InpDateTo               = D'2021.12.31 23:59';  // --> Date To

input group             "Progress"

input bool                 InpUseProgress          = false;                // Use Progress

input ENUM_APPLIED_PRICE   InpLabelPrice           = PRICE_CLOSE;          // Label price

input string               InpName                 = "Progress";           // Label name

input int                  InpX                    = 50;                   // X-axis distance

input ENUM_BASE_CORNER     InpCorner               = CORNER_RIGHT_LOWER;   // Chart corner for anchoring

input string               InpFont                 = "Lucida Console";     // Font

input int                  InpFontSize             = 14;                   // Font size

input color                InpColor                = clrOrange;            // Color

input double               InpAngle                = 0.0;                  // Slope angle in degrees

input ENUM_ANCHOR_POINT    InpAnchor               = ANCHOR_RIGHT;         // Anchor type

input bool                 InpBack                 = false;                // Background object

input bool                 InpSelection            = false;                // Highlight to move

input bool                 InpHidden               = true;                 // Hidden in the object list

input long                 InpZOrder               = 0;                    // Priority for mouse click

//--- indicator buffers

double   SpreadMaxBuffer[];

double   SpreadMinBuffer[];

double   SpreadBuffer[];

//---

datetime m_prev_bars       = 0;        // "0" -> D'1970.01.01 00:00';

double   m_max             = DBL_MIN;

double   m_min             = DBL_MAX;

bool     m_first           = false;

int      m_event_id        = 5000;

//---

int      m_debug           = -1;

int      m_profiler        = -1;

int      m_tester          = -1;

int      m_forward         = -1;

int      m_optimization    = -1;

int      m_visual_mode     = -1;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   m_first     = true;

//--- forced initialization of variables

   m_prev_bars = 0;        // "0" -> D'1970.01.01 00:00';

   m_max       = DBL_MIN;

   m_min       = DBL_MAX;

//--- indicator buffers mapping

   SetIndexBuffer(0,SpreadMaxBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SpreadMinBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,SpreadBuffer,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpCode);

//--- an empty value for plotting, for which there is no drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- create a text label on the chart

   if(InpUseProgress)

      LabelCreate(0,InpName,ChartWindowFind(),InpX,0,InpCorner,"-//-",InpFont,InpFontSize,

                  InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder);

//--- create a timer with a 30 second period

   EventSetTimer(30);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   ObjectDelete(0,InpName);

//--- destroy the timer after completing the work

   EventKillTimer();

  }

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

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

  {

   if(m_first)

     {

      m_first=false;

      //--- form custom event ID

      ushort custom_event_id=(ushort)(m_event_id-CHARTEVENT_CUSTOM);

      //---  send a custom event with parameters filling

      EventChartCustom(ChartID(),custom_event_id,0,0.0,__FUNCTION__);

      //---

      int height=ChartHeightInPixelsGet(ChartID(),ChartWindowFind());

      if(height!=-1)

        {

         LabelMove(ChartID(),InpName,InpX,height/2);

         ChartRedraw();

        }

     }

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      //---

      if(InpUseDateFromTo)

        {

         if(time[i]<InpDateFrom || time[i]>InpDateTo)

           {

            SpreadMaxBuffer[i]=0.0;

            SpreadMinBuffer[i]=0.0;

            SpreadBuffer[i]=EMPTY_VALUE;

            continue;

           }

        }

      //---

      MqlTick  ticks_array[];                            // tick receiving array

      uint     flags=COPY_TICKS_ALL;                     // flag that defines the type of the ticks that are received

      ulong    from_msc=(ulong)time[i]*1000;             // date, starting from which ticks are requested

      ulong    to_msc=from_msc+(ulong)((PeriodSeconds(Period())+1)*1000); // date, up to which ticks are requested

      int copy_ticks_range=CopyTicksRange(Symbol(),ticks_array,COPY_TICKS_ALL,from_msc,to_msc);

      SpreadMaxBuffer[i]=0.0;

      SpreadMinBuffer[i]=0.0;

      if(copy_ticks_range<1)

         return(0);

      if(time[i]!=m_prev_bars)

        {

         m_prev_bars=time[i];

         if(m_max==DBL_MIN && m_min==DBL_MAX)

           {

            SpreadMaxBuffer[i]=0.0;

            SpreadMinBuffer[i]=0.0;

           }

         m_max=DBL_MIN;

         m_min=DBL_MAX;

        }

      for(int j=0; j<copy_ticks_range; j++)

        {

         double count_spread=(ticks_array[j].ask-ticks_array[j].bid)/Point();

         if(count_spread>m_max)

            m_max=count_spread;

         if(count_spread<m_min)

            m_min=count_spread;

        }

      SpreadMaxBuffer[i]=m_max;

      SpreadMinBuffer[i]=m_min;

      SpreadBuffer[i]=spread[i];

      //---

      if(InpUseProgress)

        {

         LabelTextChange(0,InpName,TimeToString(time[i],TIME_DATE|TIME_MINUTES));

         ChartRedraw();

        }

      //---

      if(InpUseDateFromTo)

        {

         if(time[i]<InpDateFrom || time[i]>InpDateTo)

           {

            SpreadMaxBuffer[i]=0.0;

            SpreadMinBuffer[i]=0.0;

            SpreadBuffer[i]=spread[i];

            continue;

           }

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//--- create a text label on the chart

   if(ObjectFind(0,InpName)<0)

      LabelCreate(0,InpName,ChartWindowFind(),InpX,0,InpCorner,"-//-",InpFont,InpFontSize,

                  InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_CHART_CHANGE || id==m_event_id)

     {

      int height=ChartHeightInPixelsGet(ChartID(),ChartWindowFind());

      if(height==-1)

         return;

      LabelMove(ChartID(),InpName,InpX,height/2);

      ChartRedraw();

     }

  }

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

//| Create a text label                                              |

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

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

                 const string            name="Label",             // label name

                 const int               sub_window=0,             // subwindow index

                 const int               x=0,                      // X coordinate

                 const int               y=0,                      // Y coordinate

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring

                 const string            text="Label",             // text

                 const string            font="Arial",             // font

                 const int               font_size=10,             // font size

                 const color             clr=clrRed,               // color

                 const double            angle=0.0,                // text slope

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type

                 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

  {

//--- reset the error value

   ResetLastError();

//--- create a text label

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create text label! Error code = ",GetLastError());

      return(false);

     }

//--- set label coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the label by mouse

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

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

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

               const string name="Label", // label name

               const int    x=0,          // X coordinate

               const int    y=0)          // Y coordinate

  {

//--- reset the error value

   ResetLastError();

//--- move the text label

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change the label text                                            |

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

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

                     const string name="Label", // object name

                     const string text="Text")  // text

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Gets the height of chart (in pixels)                             |

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

int ChartHeightInPixelsGet(const long chart_ID=0,const int sub_window=0)

  {

//--- prepare the variable to get the property value

   long result=-1;

//--- reset the error value

   ResetLastError();

//--- receive the property value

   if(!ChartGetInteger(chart_ID,CHART_HEIGHT_IN_PIXELS,sub_window,result))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

     }

//--- return the value of the chart property

   return((int)result);

  }

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

Comments