Bars Inside 2

Author: Copyright © 2020-2021, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Bars Inside 2
ÿþ//+------------------------------------------------------------------+

//|                                                Bars Inside 2.mq5 |

//|                         Copyright © 2020-2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2020-2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "2.000"

#property indicator_separate_window

#property indicator_buffers 0

#property indicator_plots   0

//---

#include <Graphics\Graphic.mqh>

//---

CGraphic m_graphic;

//--- input parameters

input string   InpVLineName   = "Bars Inside";  // VLine Name

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

   m_graphic.Create(0,"Graphic",ChartWindowFind(),5,5,685,190);

   m_graphic.HistoryNameWidth(90);

   m_graphic.Update();

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   m_graphic.Destroy();

  }

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

//| 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 value of prev_calculated for next call

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//--- left-clicking on a chart

   if(id==CHARTEVENT_CLICK)

     {

      int      x           = (int)lparam;

      int      y           = (int)dparam;

      datetime dt          = 0;

      double   price       = 0;

      int      window      = 0;

      int      bar         = 0;

      if(ChartXYToTimePrice(0,x,y,window,dt,price))

        {

         //---

         VLineMove(ChartID(),InpVLineName,dt);

         //---

         bar=iBarShift(Symbol(),Period(),dt,false);

         if(bar>-1)

           {

            datetime dt_bar=iTime(Symbol(),Period(),bar);

            Print("Mouse click coordinates on a chart: x = ",lparam,"  y = ",dparam," ",

                  TimeToString(dt,TIME_DATE|TIME_SECONDS)," ",DoubleToString(price,Digits())," bar ",IntegerToString(bar)," ",

                  TimeToString(dt_bar,TIME_DATE|TIME_SECONDS));

            datetime stop_time;

            if(bar==0)

               stop_time=TimeCurrent();

            else

               stop_time=iTime(Symbol(),Period(),bar-1);

            double arr_close[];

            ArraySetAsSeries(arr_close,true);

            if(CopyClose(Symbol(),PERIOD_M1,dt_bar,stop_time,arr_close)>0)

              {

               int max_index=ArrayMaximum(arr_close,0,WHOLE_ARRAY);

               int min_index=ArrayMinimum(arr_close,0,WHOLE_ARRAY);

               double max=arr_close[max_index];

               double min=arr_close[min_index];

               //---

               m_graphic.CurveRemoveByIndex(0);

               CCurve *curve=m_graphic.CurveAdd(arr_close,CURVE_LINES,TimeToString(dt_bar,TIME_DATE|TIME_SECONDS));

               m_graphic.YAxis().Max(max);

               m_graphic.YAxis().Min(min);

               m_graphic.CurvePlotAll();

               m_graphic.Update();

              }

           }

        }

     }

  }

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

//| Move the vertical line                                           |

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

bool VLineMove(const long   chart_ID=0,   // chart's ID

               const string name="VLine", // line name

               datetime     time=0)       // line time

  {

//--- if line time is not set, move the line to the last bar

   if(!time)

      time=TimeCurrent();

//--- reset the error value

   ResetLastError();

//--- move the vertical line

   if(!ObjectMove(chart_ID,name,0,time,0))

     {

      Print(__FUNCTION__,

            ": failed to move the vertical line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

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 ---