Statistics High and Low by Time 2

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

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

#property script_show_inputs

#include <Graphics\Graphic.mqh>

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

//| Enum Output                                                      |

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

enum ENUM_OUTPUT

  {

   count=0,    // count of series

   percent=1,  // percent from count of bars

  };

//--- input parameters

input int         InputCountBars    = 10000;    // Count of bars

input bool        InpSaveScreenShot = false;    // Save screenShot

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

input ENUM_OUTPUT InpOutput         = count;    // Output statistics

input bool        InpHighBullish    = true;     // High Bullish

input bool        InpLowBullish     = true;     // Low Bullish

input bool        InpHighBearish    = true;     // High Bearish

input bool        InpLowBearish     = true;     // Low Bearish

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

//| Script program start function                                    |

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

void OnStart()

  {

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

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

//|   OR                                                             |

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

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

   double arr_high_bullish[24],arr_high_bearish[24],arr_low_bullish[24],arr_low_bearish[24];

   ArrayInitialize(arr_high_bullish,0.0);

   ArrayInitialize(arr_high_bearish,0.0);

   ArrayInitialize(arr_low_bullish,0.0);

   ArrayInitialize(arr_low_bearish,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;

      double m_open=0.0;

      double m_close=0.0;

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

        {

         MqlDateTime STime;

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



         if(i==0)

           {

            m_open=rates[i].open;

            m_close=rates[i].close;

           }



         if(m_day_of_year!=STime.day_of_year)

           {

            if(m_hour_high>-1 && m_hour_low>-1)

              {

               m_close=rates[i-1].close;



               if(m_open<m_close)

                 {

                  arr_high_bullish[m_hour_high]=arr_high_bullish[m_hour_high]+1;

                  arr_low_bullish[m_hour_low]=arr_low_bullish[m_hour_low]+1;

                 }

               if(m_open>m_close)

                 {

                  arr_high_bearish[m_hour_high]=arr_high_bearish[m_hour_high]+1;

                  arr_low_bearish[m_hour_low]=arr_low_bearish[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;

            m_open=0.0;

            m_close=0.0;



            m_open=rates[i].open;

           }

         //---

         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 && m_hour_low>-1)

              {

               m_close=rates[i-1].close;



               if(m_open<m_close)

                 {

                  arr_high_bullish[m_hour_high]=arr_high_bullish[m_hour_high]+1;

                  arr_low_bullish[m_hour_low]=arr_low_bullish[m_hour_low]+1;

                 }

               if(m_open>m_close)

                 {

                  arr_high_bearish[m_hour_high]=arr_high_bearish[m_hour_high]+1;

                  arr_low_bearish[m_hour_low]=arr_low_bearish[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_XXXX[62]:    count of series                                 |

      //|   OR                                                             |

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

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

      double coefficient=100.0/(double)InputCountBars;

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

        {

         arr_high_bullish[i]=arr_high_bullish[i]*coefficient;

         arr_high_bearish[i]=arr_high_bearish[i]*coefficient;

         arr_low_bullish[i]=arr_low_bullish[i]*coefficient;

         arr_low_bearish[i]=arr_low_bearish[i]*coefficient;

        }

     }

//---

   CGraphic graphic;

   graphic.Create(0,"Graphic",0,30,30,750+30,398+30);

//---

   double x_high_bearish[],x_low_bearish[],x_high_bullish[],x_low_bullish[];

   int size=ArrayRange(arr_high_bearish,0);

   ArrayResize(x_high_bearish,size);

   ArrayResize(x_low_bearish,size);

   ArrayResize(x_high_bullish,size);

   ArrayResize(x_low_bullish,size);

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

     {

      x_high_bearish[i]=i;

      x_low_bearish[i]=i;

      x_high_bullish[i]=i;

      x_low_bullish[i]=i;

     }

//--- high bearish

   if(InpHighBearish)

     {

      double y_high_bearish[];

      ArrayResize(y_high_bearish,size);

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

         y_high_bearish[i]=arr_high_bearish[i];



      CCurve *curve_high_bearish=graphic.CurveAdd(x_high_bearish,y_high_bearish,CURVE_LINES);

      curve_high_bearish.Name("High bearish");

      curve_high_bearish.LinesSmooth(true);

      curve_high_bearish.LinesWidth(4);

     }

//--- low bearish

   if(InpLowBearish)

     {

      double y_low_bearish[];

      ArrayResize(y_low_bearish,size);

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

         y_low_bearish[i]=arr_low_bearish[i];



      CCurve *curve_low_bearish=graphic.CurveAdd(x_low_bearish,y_low_bearish,CURVE_LINES);

      curve_low_bearish.Name("Low bearish");

      curve_low_bearish.LinesSmooth(true);

      curve_low_bearish.LinesWidth(4);

     }

//--- high bullish

   if(InpHighBullish)

     {

      double y_high_bullish[];

      ArrayResize(y_high_bullish,size);

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

         y_high_bullish[i]=arr_high_bullish[i];



      CCurve *curve_high_bullish=graphic.CurveAdd(x_high_bullish,y_high_bullish,CURVE_LINES);

      curve_high_bullish.Name("High bullish");

      curve_high_bullish.LinesSmooth(true);

      curve_high_bullish.LinesWidth(4);

     }

//--- low bullish

   if(InpLowBullish)

     {

      double y_low_bullish[];

      ArrayResize(y_low_bullish,size);

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

         y_low_bullish[i]=arr_low_bullish[i];



      CCurve *curve_low_bullish=graphic.CurveAdd(x_low_bullish,y_low_bullish,CURVE_LINES);

      curve_low_bullish.Name("Low bullish");

      curve_low_bullish.LinesSmooth(true);

      curve_low_bullish.LinesWidth(4);

     }

//---

   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 High and Low by Time 2\

      string name="Statistics High and Low by Time 2\\"+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