Statistics High and Low by Time

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Statistics High and Low by Time
ÿþ//+------------------------------------------------------------------+

//|                              Statistics High and Low by Time.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.000"

#property script_show_inputs

#include <Graphics\Graphic.mqh>

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

//|                                                                  |

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

enum ENUM_OUTPUT

  {

   count=0,    // count of series

   percent=1,  // percent from count of bars

  };

//--- input parameters

input int         InputCountBars    = 100000;   // Count of bars

input bool        InpSaveScreenShot = false;    // Save screenShot

input int         InpSleep          = 12000;    // Sleep (milliseconds)

input ENUM_OUTPUT InpOutput         = count;    // Output statistics

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

//| Script program start function                                    |

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

void OnStart()

  {

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

//| arr_series[62]:    count of series                               |

//|   OR                                                             |

//| arr_series[62]:    percent from count of bars                    |

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

   double arr_high[24],arr_low[24];

   ArrayInitialize(arr_high,0.0);

   ArrayInitialize(arr_low,0.0);

   MqlRates rates[];

   int copied=CopyRates(Symbol(),Period(),0,InputCountBars,rates);

   if(copied==InputCountBars)

     {

      int m_day_of_year=-1;

      double m_high=DBL_MIN;

      double m_low=DBL_MAX;

      int m_hour_high=-1;

      int m_hour_low=-1;

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

        {

         MqlDateTime STime;

         TimeToStruct(rates[i].time,STime);



         if(m_day_of_year!=STime.day_of_year)

           {

            if(m_hour_high>-1)

               arr_high[m_hour_high]=arr_high[m_hour_high]+1;

            if(m_hour_low>-1)

               arr_low[m_hour_low]=arr_low[m_hour_low]+1;

            m_day_of_year=STime.day_of_year;

            m_high=DBL_MIN;

            m_low=DBL_MAX;

            m_hour_high=-1;

            m_hour_low=-1;

           }

         //---

         if(m_high<rates[i].high)

           {

            m_high=rates[i].high;

            m_hour_high=STime.hour;

           }

         if(m_low>rates[i].low)

           {

            m_low=rates[i].low;

            m_hour_low=STime.hour;

           }

         //--- last pass

         if(i==copied-1)

           {

            if(m_hour_high>-1)

               arr_high[m_hour_high]=arr_high[m_hour_high]+1;

            if(m_hour_low>-1)

               arr_low[m_hour_low]=arr_low[m_hour_low]+1;

            m_day_of_year=STime.day_of_year;

            m_high=DBL_MIN;

            m_low=DBL_MAX;

            m_hour_high=-1;

            m_hour_low=-1;

           }

        }

     }

   else

     {

      Print("Failed to get history data for the symbol ",Symbol(),". Bars copied ",copied," of ",InputCountBars);

      return;

     }

//---

   if(InpOutput==percent)

     {

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

      //| arr_highs[24]:     count of series                               |

      //|   OR                                                             |

      //| arr_highs[24]:     percent from count of bars                    |

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

      double coefficient=100.0/(double)InputCountBars;

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

        {

         arr_high[i]=arr_high[i]*coefficient;

         arr_low[i]=arr_low[i]*coefficient;

        }

     }

//---

   CGraphic graphic;

   graphic.Create(0,"Graphic",0,10,10,680,360);

//---

   double x_high[];

   double y_high[];

   int size=ArrayRange(arr_high,0);

   ArrayResize(x_high,size);

   ArrayResize(y_high,size);

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

     {

      x_high[i]=i;

      y_high[i]=arr_high[i];

     }

   CCurve *curve_high=graphic.CurveAdd(x_high,y_high,CURVE_LINES);

   curve_high.Name("High");

   curve_high.LinesSmooth(true);

//---

   double x_low[];

   double y_low[];

   size=ArrayRange(arr_low,0);

   ArrayResize(x_low,size);

   ArrayResize(y_low,size);

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

     {

      x_low[i]=i;

      y_low[i]=arr_low[i];

     }

   CCurve *curve_low=graphic.CurveAdd(x_low,y_low,CURVE_LINES);

   curve_low.Name("Low");

   curve_low.LinesSmooth(true);

//---

   graphic.XAxis().Name("Series type. "+Symbol()+","+StringSubstr(EnumToString(Period()),7));

   graphic.XAxis().NameSize(12);

   graphic.XAxis().AutoScale(false);

   graphic.XAxis().DefaultStep(1.0);

   if(InpOutput==percent)

      graphic.YAxis().Name("Percentage of series in "+IntegerToString(InputCountBars)+" bars");

   else

      graphic.YAxis().Name("Count of series in "+IntegerToString(InputCountBars)+" bars");

   graphic.YAxis().NameSize(12);

   graphic.CurvePlotAll();

   graphic.Update();

   if(InpSaveScreenShot)

     {

      //--- Save the chart screenshot in a file in the terminal_directory\MQL5\Files\Statistics of candles\

      string name="Statistics of candles Morse\\"+Symbol()+","+StringSubstr(EnumToString(Period()),7)+

                  ". "+IntegerToString(InputCountBars)+" bars"+".png";

      if(ChartScreenShot(0,name,750,394,ALIGN_RIGHT))

         Print("We've saved the screenshot ",name);

     }

//---

   Sleep(InpSleep);

   graphic.Destroy();

  }

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

Comments