Color Levels

Author: © 2022, Alexey Viktorov
0 Views
0 Downloads
0 Favorites
Color Levels
ÿþ/********************************************************************\

|                                                   Color Levels.mq5 |

|                                            © 2022, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

|            AE>4=8: =0 mql4 BCB https://www.mql5.com/ru/code/19088 |

\********************************************************************/

#property copyright "© 2022, Alexey Viktorov"

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

#property version   "1.20"

#property indicator_chart_window

//#property indicator_separate_window

#property indicator_buffers 0

#property indicator_plots   0

//---

enum ENUM_border_type

 {

  flat    = BORDER_FLAT,     //  ;>A:89 284

  raised  = BORDER_RAISED,   //  K?C:;K9 284

  sunken  = BORDER_SUNKEN,   //  >3=CBK9 284

 };

enum HIDDEN_PANEL

 {

  NoShow      = 0,  // !:@K20BL

  Show        = 1,  // >:070BL =5 ?5@5<5I0BL

  ShowAndMove = 2,  // >:070BL 8 ?5@5<5I0BL

 };



input   HIDDEN_PANEL      hiddenPanel     = NoShow;             //  0=5;L A:@K20BL?

input   ENUM_BASE_CORNER  InpCorner       = CORNER_LEFT_UPPER;  //  Graph corner for attachment

input   color             panl_0_cl       = clrGainsboro;       //  Color toolbar

input   ENUM_border_type  panl_0_st       = raised;             //  Type background

input   int               rectHeight      = 100;                //  Rectangle height in point

input   int               rectWidth       = 10;                 //  Rectangle width in bars

input   color             rect_1_cl       = clrPaleGreen;       //  1. Border color

input   ENUM_LINE_STYLE   rect_1_st       = STYLE_SOLID;        //  1. Border style

input   int               rect_1_wd       = 2;                  //  1. Border thickness

input   color             rect_2_cl       = clrCoral;           //  2. Border color

input   ENUM_LINE_STYLE   rect_2_st       = STYLE_SOLID;        //  2. Border style

input   int               rect_2_wd       = 2;                  //  2. Border thickness

input   color             rect_3_cl       = clrBlue;            //  3. Rectangle color

input   color             rect_4_cl       = clrRed;             //  4. Rectangle color

input   color             rect_5_cl       = clrDarkGray;        //  5. Rectangle color

input   color             line_1_cl       = clrDodgerBlue;      //  6. Line color

input   ENUM_LINE_STYLE   line_1_st       = STYLE_SOLID;        //  6. Line style

input   int               line_1_wd       = 2;                  //  6. Line thickness

input   color             line_2_cl       = clrFireBrick;       //  7. Line color

input   ENUM_LINE_STYLE   line_2_st       = STYLE_SOLID;        //  7. Line style

input   int               line_2_wd       = 2;                  //  7. Line thickness

input   int               x_coor          = 10;                 //  Coordinate X

input   int               y_coor          = 20;                 //  Coordinate Y



bool              chart_click   = false;

int               x_size        =  155,

                  y_size        =  30,

                  x_step        =  5,

                  y_panl        =  20,

                  x_rect        =  20,

                  y_rect        =  20,

                  y_line        =  6,

                  x, y,

                  hidden;

string            obj_name[8]   =  {"name_0","name_1","name_2","name_3","name_4","name_5","name_6","name_7"};

string            pr            =   "00_";

ENUM_BASE_CORNER  inpCorner;

long chart_ID = ChartID();

/****************indicator buffers****************/

/**************Custom indicator initialization function**************/

int OnInit()

 {

  ChartSetInteger(chart_ID, CHART_EVENT_OBJECT_CREATE, true);

  inpCorner = InpCorner;

  hidden = hiddenPanel == NoShow ? OBJ_NO_PERIODS : OBJ_ALL_PERIODS;

  XY_Distance();

  CreatePanel();

  return(INIT_SUCCEEDED);

 }/*******************************************************************/



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

 {

  return(rates_total);

 }/*******************************************************************/



/********************************************************************/

void OnChartEvent(const int id,         // 845=B8D8:0B>@ A>1KB8O

                  const long& lparam,   // ?0@0<5B@ A>1KB8O B8?0 long

                  const double& dparam, // ?0@0<5B@ A>1KB8O B8?0 double

                  const string& sparam  // ?0@0<5B@ A>1KB8O B8?0 string

                 )

 {

  string rectName = StringSubstr(sparam, 3)+(string)(rand()%10000);

  if(id == CHARTEVENT_KEYDOWN)

   {

    if(lparam == 27)// =060B85 esc

     {

      inpCorner = InpCorner;

      hidden = hiddenPanel == NoShow ? OBJ_NO_PERIODS : OBJ_ALL_PERIODS;

      XY_Distance();

      CreatePanel();

      chart_click = false;

     }

   }

  if(id == CHARTEVENT_OBJECT_CREATE)

   {

    if(StringLen(sparam) > 6 && StringFind(sparam, "name_") == 0)

     {

      ObjectSetInteger(chart_ID, sparam, OBJPROP_SELECTABLE, true);

      ObjectSetInteger(chart_ID, sparam, OBJPROP_SELECTED, true);

     }

    ChartRedraw();

   }

  if(id == CHARTEVENT_OBJECT_CLICK)

   {

    int sub_window,

        numb = (int)StringSubstr(sparam, 8);

    datetime dt_1 = 0;

    double price_1 = 0.0;

    if(hiddenPanel == 1)

      ChartXYToTimePrice(chart_ID, x, y, sub_window, dt_1, price_1);

    else

      ChartXYToTimePrice(chart_ID, (int)lparam, (int)dparam, sub_window, dt_1, price_1);

    switch(numb)

     {

      case 1:

        RectangleCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, rect_1_cl, rect_1_wd, rect_1_st);

        break;

      case 2:

        RectangleCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, rect_2_cl, rect_2_wd, rect_2_st);

        break;

      case 3:

        RectangleCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, rect_3_cl, 1, true);

        break;

      case 4:

        RectangleCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, rect_4_cl, 1, true);

        break;

      case 5:

        RectangleCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, rect_5_cl, 1, true);

        break;

      case 6:

        TrendCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, line_1_cl, line_1_wd, line_1_st);

        break;

      case 7:

        TrendCreate(rectName, dt_1, price_1, dt_1+PeriodSeconds()*rectWidth, price_1-rectHeight*_Point, line_2_cl, line_2_wd, line_2_st);

        break;

      default:

        break;

     }

   }

  if(id == CHARTEVENT_CLICK)

   {

    if(chart_click)

     {

      inpCorner = InpCorner;

      hidden = hiddenPanel == NoShow ? OBJ_NO_PERIODS : OBJ_ALL_PERIODS;

      XY_Distance();

      CreatePanel();

      chart_click = false;

     }

    else

     {

      x = (int)lparam;

      y = (int)dparam;

      if(hiddenPanel == 1)

       {

        inpCorner = InpCorner;

        hidden = OBJ_ALL_PERIODS;

       }

      else

       {

        inpCorner = CORNER_LEFT_UPPER;

        hidden = OBJ_ALL_PERIODS;

        CreatePanel();

       }

     }

   }

 }/******************************************************************/



/********************************************************************/

void TrendCreate(string                 name,

                 datetime               time1,

                 double                 price1,

                 datetime               time2,

                 double                 price2,

                 const color            clr,

                 const int              width,

                 const ENUM_LINE_STYLE  style = STYLE_SOLID)

 {

  chart_click = true;

  if(ObjectFind(chart_ID, name) == - 1)

    ObjectCreate(chart_ID, name, OBJ_TREND, 0, time1, price1, time2, price2);

//---

  ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);

  ObjectSetInteger(chart_ID, name, OBJPROP_STYLE, style);

  ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, width);

//ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, true);

//ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, true);

  ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, false);

  ChartRedraw();

 }/******************************************************************/



/********************************************************************/

void RectangleCreate(string                 name,

                     datetime               time1,

                     double                 price1,

                     datetime               time2,

                     double                 price2,

                     const color            clr,

                     const int              width,

                     const bool             fill = false,

                     const ENUM_LINE_STYLE  style = STYLE_SOLID)

 {

  chart_click = true;

  if(ObjectFind(chart_ID, name) == - 1)

    ObjectCreate(chart_ID, name, OBJ_RECTANGLE, 0, time1, price1, time2, price2);

//---

  ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);

  ObjectSetInteger(chart_ID, name, OBJPROP_STYLE, style);

  ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, width);

  ObjectSetInteger(chart_ID, name, OBJPROP_FILL, fill);

//ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, true);

//ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, true);

  ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, false);

  ChartRedraw();

 }/******************************************************************/



/********************************************************************/

void XY_Distance()

 {

  switch(inpCorner)

   {

    case CORNER_LEFT_LOWER:

      x = x_coor;

      y = y_coor+y_size;

      break;

    case CORNER_RIGHT_LOWER:

      x = x_coor+x_size;

      y = y_coor+y_size;

      break;

    case CORNER_RIGHT_UPPER:

      x = x_coor+x_size;

      y = y_coor;

      break;

    default:

      x = x_coor;

      y = y_coor;

      break;

   }

 }/******************************************************************/



/********************************************************************/

void CreatePanel()

 {

  ENUM_BORDER_TYPE border_type = (ENUM_BORDER_TYPE)panl_0_st;

  RectLabelCreate(pr+obj_name[0], (ENUM_BORDER_TYPE)panl_0_st,  x_size, y_size, true, panl_0_cl, panl_0_cl);

  int xx = (int)ObjectGetInteger(chart_ID, pr+obj_name[0], OBJPROP_XDISTANCE),

      yy = (int)ObjectGetInteger(chart_ID, pr+obj_name[0], OBJPROP_YDISTANCE),

      ysize = y_rect, kx, ky;

  kx = inpCorner == CORNER_LEFT_UPPER || inpCorner == CORNER_LEFT_LOWER ? 1 : -1;

  ky = inpCorner == CORNER_LEFT_UPPER || inpCorner == CORNER_RIGHT_UPPER ? 1 : -1;

//---

  for(int i = 1; i < 8; i++)

   {

    color fill = clrNONE,

          border = clrNONE;

    x = xx+(i*x_step+(i-1)*x_rect)*kx;

    y = yy+x_step*ky;

    switch(i)

     {

      case 1 :

        fill = rect_1_cl;

        border = panl_0_cl;

        border_type = BORDER_FLAT;

        break;

      case 2 :

        fill = rect_2_cl;

        border = panl_0_cl;

        border_type = BORDER_FLAT;

        break;

      case 3 :

        fill = rect_3_cl;

        border = rect_3_cl;

        border_type = BORDER_FLAT;

        break;

      case 4 :

        fill = rect_4_cl;

        border = rect_4_cl;

        border_type = BORDER_FLAT;

        break;

      case 5 :

        fill = rect_5_cl;

        border = rect_5_cl;

        border_type = BORDER_FLAT;

        break;

      case 6 :

        fill = line_1_cl;

        border = line_1_cl;

        border_type = BORDER_FLAT;

        ysize = y_line;

        break;

      case 7 :

        fill = line_2_cl;

        border = line_2_cl;

        border_type = BORDER_FLAT;

        ysize = y_line;

        x = xx+(6*x_step+5*x_rect)*kx;

        y = yy+(x_step+15)*ky;

        break;

     }

    RectLabelCreate(pr+obj_name[i], border_type, x_rect, ysize, true, fill, border);

   }

  ChartRedraw();

 }/******************************************************************/



/********************************************************************/

void RectLabelCreate(const string             name,

                     const ENUM_BORDER_TYPE   style,

                     const int                width,

                     const int                height,

                     const bool               fill,

                     const color              clr,

                     const color              bg_clr)

 {

  if(ObjectFind(chart_ID, name) == - 1)

    ObjectCreate(chart_ID, name, OBJ_RECTANGLE_LABEL, 0, 0, 0);



  ObjectSetInteger(chart_ID, name, OBJPROP_CORNER, inpCorner);

  ObjectSetInteger(chart_ID, name, OBJPROP_XDISTANCE, x);

  ObjectSetInteger(chart_ID, name, OBJPROP_YDISTANCE, y);

  ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);

  ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, 2);

  ObjectSetInteger(chart_ID, name, OBJPROP_XSIZE, width);

  ObjectSetInteger(chart_ID, name, OBJPROP_YSIZE, height);

  ObjectSetInteger(chart_ID, name, OBJPROP_BORDER_TYPE, style);

  ObjectSetInteger(chart_ID, name, OBJPROP_FILL, fill);

  ObjectSetInteger(chart_ID, name, OBJPROP_BGCOLOR, bg_clr);

  ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, false);

  ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, false);

  ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, 7);

  ObjectSetInteger(chart_ID, name, OBJPROP_TIMEFRAMES, hidden);

 }/******************************************************************/



/********************************************************************/

void OnDeinit(const int reason)

 {

  ObjectsDeleteAll(chart_ID, pr, 0, OBJ_RECTANGLE_LABEL);

  ChartRedraw();

  Comment("");

 }/*******************************************************************/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---