TicksGraphic

Author: Copyright 2022, www.mql5.com/ru/users/s22aa.
0 Views
0 Downloads
0 Favorites
TicksGraphic
ÿþ//+------------------------------------------------------------------+

//|                                                 TicksGraphic.mq5 |

//|                                  Copyright 2022, MetaQuotes Ltd. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2022, www.mql5.com/ru/users/s22aa."

#property link      "https://www.mql5.com/ru/users/s22aa"

#property version   "1.00"

#include <Graphics\Graphic.mqh>



input datetime _from = 0; // 40B0, =0G8=0O A :>B>@>9 70?@0H820NBAO B8:8

input datetime _to   = 0; // 40B0, ?> :>B>@CN 70?@0H820NBAO B8:8

input int _width = 1200;

input int _height = 400;

bool del_EA = false;

enum deals_time

  {

   Deals = 0,

   Time = 1

  };

deals_time   m_deals_time;

enum copy_ticks

  {

   TICKS_ALL = 0,

   TICKS_INFO = 1,

   TICKS_TRADE = 2

  };

copy_ticks   m_copy_ticks;

string Name;

ulong from_msc, to_msc;

MqlTick tick[];

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

int OnInit()

  {

   if(_from != 0 || _to != 0)

     {

      from_msc = _from * 1000;

      to_msc = _to * 1000;

      OnCalc();

     }

   return(INIT_SUCCEEDED);

  }

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

void OnChartEvent(const int id,

                  const long & lparam,

                  const double & dparam,

                  const string & sparam

                 )

  {

   if(TerminalInfoInteger(TERMINAL_KEYSTATE_CONTROL) < 0)

      if(id == CHARTEVENT_CLICK)

        {

         int sub_window = 0;

         datetime time = 0.0;

         double price = 0.0;

         datetime timeOpen[1];



         ChartXYToTimePrice(0, (int)lparam, (int)dparam, sub_window, time, price);

         CopyTime(_Symbol, PERIOD_CURRENT, time, 1, timeOpen);

         from_msc = timeOpen[0] * 1000;

         to_msc = from_msc + (PeriodSeconds() * 1000);



         Print(timeOpen[0]);

         OnCalc();

        }

  }

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

void OnCalc(void)

  {

   double x[], y1[], y2[], y3[];

   int size = -1;



   do

     {

      size = CopyTicksRange(_Symbol, tick, COPY_TICKS_ALL, from_msc, to_msc);

      if(size < 0)

         Sleep(100);

     }

   while(size < 0 && !IsStopped());



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

     {

      for(int l = 0; l < 3; l++)

        {

         int count = 0, coeff = 1;

         double volume = 0;



         ArrayResize(x, size);

         ArrayResize(y1, size);

         ArrayResize(y2, size);

         ArrayResize(y3, size);

         CCurve *curve1;

         CCurve *curve2;

         CCurve *curve3;

         m_copy_ticks = (copy_ticks)l;

         m_deals_time = (deals_time)j;



         CGraphic graphic;

         CGraphic graphic2;



         if(ObjectFind(0, "Graphic") >= 0)

            graphic.Attach(0, "Graphic");

         else

            graphic.Create(0, "Graphic", 0, 0, 0, _width, _height);



         if(l == 2)

           {

            if(ObjectFind(0, "Graphic2") >= 0)

               graphic2.Attach(0, "Graphic2");

            else

               graphic2.Create(0, "Graphic2", 0, 0, _height, _width, _height * 2);

            coeff = 2;

           }



         switch(m_copy_ticks)

           {

            case  TICKS_ALL:

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

                 {

                  y1[i] = (double)tick[i].ask;

                  y2[i] = (double)tick[i].bid;

                  y3[i] = (double)tick[i].last;



                  switch(m_deals_time)

                    {

                     case Deals:

                        x[i] = (double)i;

                        break;

                     case Time:

                        x[i] = (double)(tick[i].time_msc - from_msc);

                        break;

                    }

                 }

               curve1 = graphic.CurveAdd(x, y1, ColorToARGB(clrBlue), CURVE_LINES, "ask");

               curve2 = graphic.CurveAdd(x, y2, ColorToARGB(clrRed), CURVE_LINES, "bid");

               curve3 = graphic.CurveAdd(x, y3, ColorToARGB(clrGreen), CURVE_LINES, "last");

               break;



            case  TICKS_INFO:

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

                 {

                  if((tick[i].flags & TICK_FLAG_ASK) == TICK_FLAG_ASK || (tick[i].flags & TICK_FLAG_BID) == TICK_FLAG_BID)

                    {

                     y1[count] = (double)tick[i].ask;

                     y2[count] = (double)tick[i].bid;



                     switch(m_deals_time)

                       {

                        case Deals:

                           x[count] = (double)count;

                           break;

                        case Time:

                           x[count] = (double)(tick[i].time_msc - from_msc);

                           break;

                       }

                     count++;

                    }

                 }

               ArrayResize(x, count);

               ArrayResize(y1, count);

               ArrayResize(y2, count);



               curve1 = graphic.CurveAdd(x, y1, ColorToARGB(clrBlue), CURVE_LINES, "ask");

               curve2 = graphic.CurveAdd(x, y2, ColorToARGB(clrRed), CURVE_LINES, "bid");

               break;



            case  TICKS_TRADE:

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

                 {

                  if((tick[i].flags & TICK_FLAG_LAST) == TICK_FLAG_LAST || (tick[i].flags & TICK_FLAG_VOLUME) == TICK_FLAG_VOLUME)

                    {

                     y1[count] = (double)tick[i].last;

                     switch(m_deals_time)

                       {

                        case Deals:

                           x[count] = (double)count;

                           y2[count] = (double)tick[i].volume;

                           volume += (double)tick[i].volume;

                           break;

                        case Time:

                           x[count] = (double)(tick[i].time_msc - from_msc);



                           if(count != 0 && tick[i].time_msc - from_msc == x[count - 1])

                             {

                              y2[count] = (double)(tick[i].volume + y2[count - 1]);

                              volume += (double)tick[i].volume;

                             }

                           else

                             {

                              y2[count] = (double)tick[i].volume;

                              volume += (double)tick[i].volume;

                             }



                           break;

                       }

                     count++;

                    }

                 }

               ArrayResize(x, count);

               ArrayResize(y1, count);

               ArrayResize(y2, count);



               curve1 = graphic.CurveAdd(x, y1, ColorToARGB(clrGreen), CURVE_LINES, "last");

               curve2 = graphic2.CurveAdd(x, y2, ColorToARGB(clrBlue), CURVE_HISTOGRAM, "Volume");

               break;

           }



         graphic.BackgroundMain("Ticks count = " + (string)count);

         graphic.BackgroundMainSize(14);



         graphic.XAxis().Name(_Symbol + " " + TimeToString(from_msc / 1000, TIME_DATE | TIME_MINUTES) + " - " + TimeToString(to_msc / 1000, TIME_MINUTES));

         graphic.XAxis().NameSize(14);

         //graphic.YAxis().ValuesSize(10);



         graphic.CurvePlotAll();

         graphic.Update();



         if(l == 2)

           {

            graphic2.XAxis().Name("volume = " + DoubleToString(volume, 0));

            graphic2.XAxis().NameSize(14);

            graphic2.CurvePlotAll();

            graphic2.Update();

           }



         Name = _Symbol + "\\" + EnumToString((copy_ticks)l) + "\\" + EnumToString((deals_time)j);

         ScreenShot(Name, coeff);

         Name = _Symbol + "\\ " + EnumToString((deals_time)j) + "\\" + EnumToString((copy_ticks)l);

         ScreenShot(Name, coeff);

        }

     }



   ObjectsDeleteAll(0, "Graphic");





   if(del_EA)

      ExpertRemove();

  }

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

void ScreenShot(string name = "Graphic", int coeff = 1)

  {

   long clr =  ChartGetInteger(0, CHART_COLOR_BACKGROUND);

   ChartSetInteger(0, CHART_SHOW, false);

   ChartSetInteger(0, CHART_COLOR_BACKGROUND, clrWhite);

   ChartScreenShot(0, name + ".jpg", _width, _height * coeff);

   ChartSetInteger(0, CHART_SHOW, true);

   ChartSetInteger(0, CHART_COLOR_BACKGROUND, clr);

  }

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

Comments