Author: Copyright 2023, MetaQuotes Ltd.
Price Data Components
Series array that contains close prices for each bar
0 Views
0 Downloads
0 Favorites
ScalePrice
ÿþ//+------------------------------------------------------------------+

//|                                                   ScalePrice.mq5 |

//|                                  Copyright 2023, MetaQuotes Ltd. |

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

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

#property copyright "Copyright 2023, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.02"

#property indicator_chart_window

#property strict

#include <Canvas\iCanvas_CB.mqh> // https://www.mql5.com/ru/code/22164



input int font_size = 20; // Font size

input color _clrText = clrNONE;// clrNONE - color automatically

input bool background = false;  // Object in the background

double shift = 0;

struct Scale

  {

   double            shift;

   int               width;

   int               font_size;

   int               step;

  };

color clrText, clrLine;

Scale S = {0.0, 80, 0, 0};

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

int OnInit()

  {

   clrText = _clrText;

   Canvas.SetBack(background);

   S.font_size = font_size;

   S.step = font_size * 2;

   ChartSetInteger(0, CHART_EVENT_MOUSE_WHEEL, true);

   ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);

   int w, h;

   Canvas.CurentFont("Tahoma", S.font_size, S.step, 0xFFFFFFFF);

   Canvas.TextSize(DoubleToString(iClose(_Symbol, PERIOD_CURRENT, 0), _Digits), w, h);

   S.width = w + 15;

   S.shift = ChartGetDouble(0, CHART_SHIFT_SIZE) + (100.0 * S.width) / W.Width;

   ChartSetInteger(0, CHART_SHOW_PRICE_SCALE, 0, false);

   if(!ChartGetInteger(0, CHART_SHIFT))

     {

      ChartSetInteger(0, CHART_SHIFT, true);

      ChartSetDouble(0, CHART_SHIFT_SIZE, S.shift);

     }

   DrawScale();

   return(INIT_SUCCEEDED);

  }

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

void OnDeinit(const int reason)

  {

   ChartSetInteger(0, CHART_SHOW_PRICE_SCALE, 0, true);

   ChartSetInteger(0, CHART_SCALEFIX, false);

   ChartSetInteger(0, CHART_MOUSE_SCROLL, true);

   ChartRedraw();

  }

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

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

  {

   DrawScale();

   return(rates_total);

  }

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

   static double lastY = 0;

   static uint time_click = 0;



   if(id == CHARTEVENT_CLICK)

     {

      if(lparam > W.Width - S.width)

         if(GetTickCount() <= time_click + 500)

           {

            ChartSetInteger(0, CHART_SCALEFIX, false);

            ChartSetInteger(0, CHART_MOUSE_SCROLL, true);

            DrawScale();

           }

         else

            time_click = GetTickCount();

     }



   if(id == CHARTEVENT_MOUSE_MOVE)

     {

      if(((uint)sparam & 1) == 1) //  ""

        {

         if(lparam > W.Width - S.width)

           {

            double deltaY = dparam - lastY;

            if(deltaY != 0)

              {

               double coef = W.Height / (W.Y_max - W.Y_min);

               double Y_min = W.Y_min - deltaY / coef;

               double Y_max = W.Y_max + deltaY / coef;

               ChartSetInteger(0, CHART_SCALEFIX, true);

               ChartSetInteger(0, CHART_MOUSE_SCROLL, false);

               ChartSetDouble(0, CHART_FIXED_MIN, Y_min);

               ChartSetDouble(0, CHART_FIXED_MAX, Y_max);

               ChartChanged();  // ?5@570?8AK205B ?@8=C48B5;L=> AB@C:BC@C W, B.:. ?0@0<5B@K '0@B0 87<5=8;8AL.

              }

           }

         else

            ChartSetInteger(0, CHART_MOUSE_SCROLL, true);

        }

      lastY = dparam;

     }



   if(id == CHARTEVENT_MOUSE_MOVE || id == CHARTEVENT_CHART_CHANGE)

      DrawScale();

  }

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

void DrawScale()

  {

   static uint last = 0;

   uint cur = GetTickCount();

   if(cur - last < 30)

      return;

   last = cur;

   Canvas.Erase(0x00FFFFFF);

   if(_clrText == clrNONE)

      clrText = (color)Canvas.AnotherColor(W.Color);

   double cur_price = Canvas.Price(S.step / 2);

   int cur_y = Round(Canvas.Y(cur_price));

   Canvas.CurentFont("Tahoma", S.font_size, S.step, clrText);

   int left = W.Width - S.width;

   Canvas.FillRectangle(left, 0, W.Width - 1, W.Height - 1, ColorToARGB(W.Color, 255));

   while(true)

     {

      Canvas.LineHorizontal(left, left + 6, cur_y, ColorToARGB(clrText, 255));

      _CommXY(left + 9, cur_y - S.font_size / 2, DoubleToString(cur_price, _Digits));

      cur_y += S.step;

      if(cur_y > (W.Height - 8))

         break;

      cur_price = Canvas.Price(cur_y);

     }

   cur_price = iClose(_Symbol, PERIOD_CURRENT, 0);

   cur_y = Round(_Y(cur_price));

   Canvas.FillRectangle(left, cur_y - S.font_size / 2, W.Width - 1, cur_y + S.font_size / 2, ColorToARGB(clrText, 255));

   Canvas.CurentFont("Tahoma", S.font_size, S.step, W.Color);

   _CommXY(left + 9, cur_y - S.font_size / 2, DoubleToString(cur_price, _Digits));

   Canvas.LineHorizontal(0, left, W.MouseY, ColorToARGB(clrText, 150));

   Canvas.LineVertical(W.MouseX, 0, W.Height - 1, ColorToARGB(clrText, 150));

   Canvas.FillRectangle(left, W.MouseY - S.font_size / 2, W.Width - 1, W.MouseY + S.font_size / 2, ColorToARGB(clrText, 255));

   Canvas.CurentFont("Tahoma", S.font_size, S.step, W.Color);

   _CommXY(left + 9, W.MouseY - S.font_size / 2, DoubleToString(W.MousePrice, _Digits));

   Canvas.LineVertical(left, 0, W.Height - 1, ColorToARGB(clrText, 255));

   Canvas.Update();

  }

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

Comments