Tick Counter Indicator

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Tick Counter Indicator
ÿþ//+------------------------------------------------------------------+

//|                                       Tick Counter Indicator.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.001"

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot Ticks

#property indicator_label1  "Ticks"

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_color1  clrFuchsia

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

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

//| Enum Copy Ticks                                                  |

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

enum ENUM_COPY_TICKS

  {

   info  = COPY_TICKS_INFO,   // ticks with Bid and/or Ask changes

   trade = COPY_TICKS_TRADE,  // ticks with changes in Last and Volume

   all   = COPY_TICKS_ALL,    // all ticks

  };

//--- input parameters

input ushort   InpDisplayBars = 300;   // Display Bars (in first start)

input ENUM_COPY_TICKS InpTicks= info;  // Flag that defines the type of the ticks that are received

input bool     InpComments    = true;  // Comment 'received'

//--- indicator buffers

double         TicksBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,TicksBuffer,INDICATOR_DATA);

//--- indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- name for indicator subwindow label

   string label="";

   switch(InpTicks)

     {

      case  info:

         label="Info";

         break;

      case  trade:

         label="Trade";

         break;

      default:

         label="All";

         break;

     }

   IndicatorSetString(INDICATOR_SHORTNAME,"Tick("+label+")");

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   if(InpComments)

      Comment("");

  }

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

//| 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(rates_total<InpDisplayBars+1)

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=rates_total-InpDisplayBars;

      ArrayInitialize(TicksBuffer,0.0);

     }

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

     {

      MqlTick tick_array[];   // Tick receiving array

      //--- Requesting the tick history

      ulong to_msc=(i+1==rates_total)?TimeCurrent()*1000:time[i+1]*1000;

      int received=CopyTicksRange(Symbol(),tick_array,InpTicks,time[i]*1000,to_msc);

      if(received<0)

        {

         if(InpComments)

            Comment("received ",received);

         return(0);

        }

      else

        {

         if(received>0)

           {

            if(InpComments)

               Comment("received ",received,", tick_array[0].time ",tick_array[0].time);

            TicksBuffer[i]=received;

           }

         else

           {

            if(received==0)

              {

               if(InpComments)

                  Comment("received ",0);

               TicksBuffer[i]=0.0;

              }

           }

        }

     }

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

   return(rates_total);

  }

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

Comments