Spread Control

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

//|                                               Spread Control.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.003"

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot SpreadMax

#property indicator_label1  "MqlTick Spread m_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 kSpread m_min"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrViolet

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

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

//--- indicator buffers

double   SpreadMaxBuffer[];

double   SpreadMinBuffer[];

//---

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

double   m_max       = DBL_MIN;

double   m_min       = DBL_MAX;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- 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);

//--- 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);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//---

   return(INIT_SUCCEEDED);

  }

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

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

  {

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=(rates_total-1-3600<0)?0:rates_total-1-3600;

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

     {

      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)

         continue;

      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;

     }

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

   return(rates_total);

  }

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

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