OHLC Previous Candle

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
OHLC Previous Candle
ÿþ//+------------------------------------------------------------------+

//|                                         OHLC Previous Candle.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Highest Line"

input string            InpName_Highest      = "Highest";         // Highest Line: name

input color             InpColor_Highest     = clrRed;            // Highest Line: color

input ENUM_LINE_STYLE   InpStyle_Highest     = STYLE_DASH;        // Highest Line: style

input int               InpWidth_Highest     = 3;                 // Highest Line: width

input bool              InpBack_Highest      = false;             // Highest Line: background line

input bool              InpSelection_Highest = false;             // Highest Line: highlight to move

input bool              InpHidden_Highest    = true;              // Highest Line: hidden in the object list

input long              InpZOrder_Highest    = 0;                 // Highest Line: Priority for mouse click

input group             "High Line"

input string            InpName_High         = "High";            // High Line: name

input color             InpColor_High        = clrTomato;         // High Line: color

input ENUM_LINE_STYLE   InpStyle_High        = STYLE_DASH;        // High Line: style

input int               InpWidth_High        = 3;                 // High Line: width

input bool              InpBack_High         = false;             // High Line: background line

input bool              InpSelection_High    = false;             // High Line: highlight to move

input bool              InpHidden_High       = true;              // High Line: hidden in the object list

input long              InpZOrder_High       = 0;                 // High Line: Priority for mouse click

input group             "Low Line"

input string            InpName_Low          = "Low";             // Low Line: name

input color             InpColor_Low         = clrCornflowerBlue; // Low Line: color

input ENUM_LINE_STYLE   InpStyle_Low         = STYLE_DASH;        // Low Line: style

input int               InpWidth_Low         = 3;                 // Low Line: width

input bool              InpBack_Low          = false;             // Low Line: background line

input bool              InpSelection_Low     = false;             // Low Line: highlight to move

input bool              InpHidden_Low        = true;              // Low Line: hidden in the object list

input long              InpZOrder_Low        = 0;                 // Low Line: Priority for mouse click

input group             "Lowest Line"

input string            InpName_Lowest       = "Lowest";          // Lowest Line: name

input color             InpColor_Lowest      = clrBlue;           // Lowest Line: color

input ENUM_LINE_STYLE   InpStyle_Lowest      = STYLE_DASH;        // Lowest Line: style

input int               InpWidth_Lowest      = 3;                 // Lowest Line: width

input bool              InpBack_Lowest       = false;             // Lowest Line: background line

input bool              InpSelection_Lowest  = false;             // Lowest Line: highlight to move

input bool              InpHidden_Lowest     = true;              // Lowest Line: hidden in the object list

input long              InpZOrder_Lowest     = 0;                 // Lowest Line: Priority for mouse click

//---

datetime m_prev_bars = 0;                       // "0" -> D'1970.01.01 00:00';

string   m_prefix    ="OHLC Previous Candle ";  // prefix

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- Highest

   HLineCreate(ChartID(),

               m_prefix+InpName_Highest,

               0,

               0,

               InpColor_Highest,

               InpStyle_Highest,

               InpWidth_Highest,

               InpBack_Highest,

               InpSelection_Highest,

               InpHidden_Highest,

               InpZOrder_Highest);

//--- High

   HLineCreate(ChartID(),

               m_prefix+InpName_High,

               0,

               0,

               InpColor_High,

               InpStyle_High,

               InpWidth_High,

               InpBack_High,

               InpSelection_High,

               InpHidden_High,

               InpZOrder_High);

//--- Low

   HLineCreate(ChartID(),

               m_prefix+InpName_Low,

               0,

               0,

               InpColor_Low,

               InpStyle_Low,

               InpWidth_Low,

               InpBack_Low,

               InpSelection_Low,

               InpHidden_Low,

               InpZOrder_Low);

//---Lowest

   HLineCreate(ChartID(),

               m_prefix+InpName_Lowest,

               0,

               0,

               InpColor_Lowest,

               InpStyle_Lowest,

               InpWidth_Lowest,

               InpBack_Lowest,

               InpSelection_Lowest,

               InpHidden_Lowest,

               InpZOrder_Lowest);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(ChartID(),m_prefix,-1,OBJ_HLINE);

  }

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

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

  {

//---

   if(prev_calculated==0)

      m_prev_bars=0;

//--- we work only at the time of the birth of new bar

   if(time[rates_total-1]==m_prev_bars)

      return(rates_total);

   m_prev_bars=time[rates_total-1];

//--- Highest

   if(ObjectFind(ChartID(),m_prefix+InpName_Highest)<0)

     {

      HLineCreate(ChartID(),

                  m_prefix+InpName_Highest,

                  0,

                  0,

                  InpColor_Highest,

                  InpStyle_Highest,

                  InpWidth_Highest,

                  InpBack_Highest,

                  InpSelection_Highest,

                  InpHidden_Highest,

                  InpZOrder_Highest);

     }

   else

      HLineMove(ChartID(),m_prefix+InpName_Highest,high[rates_total-1-1]);

//--- High

   if(ObjectFind(ChartID(),m_prefix+InpName_High)<0)

     {

      HLineCreate(ChartID(),

                  m_prefix+InpName_High,

                  0,

                  0,

                  InpColor_High,

                  InpStyle_High,

                  InpWidth_High,

                  InpBack_High,

                  InpSelection_High,

                  InpHidden_High,

                  InpZOrder_High);

     }

   else

      HLineMove(ChartID(),m_prefix+InpName_High,(open[rates_total-1-1]<close[rates_total-1-1])?close[rates_total-1-1]:open[rates_total-1-1]);

//--- Low

   if(ObjectFind(ChartID(),m_prefix+InpName_Low)<0)

     {

      HLineCreate(ChartID(),

                  m_prefix+InpName_Low,

                  0,

                  0,

                  InpColor_Low,

                  InpStyle_Low,

                  InpWidth_Low,

                  InpBack_Low,

                  InpSelection_Low,

                  InpHidden_Low,

                  InpZOrder_Low);

     }

   else

      HLineMove(ChartID(),m_prefix+InpName_Low,(open[rates_total-1-1]<close[rates_total-1-1])?open[rates_total-1-1]:close[rates_total-1-1]);

//--- Lowest

   if(ObjectFind(ChartID(),m_prefix+InpName_Lowest)<0)

     {

      HLineCreate(ChartID(),

                  m_prefix+InpName_Lowest,

                  0,

                  0,

                  InpColor_Lowest,

                  InpStyle_Lowest,

                  InpWidth_Lowest,

                  InpBack_Lowest,

                  InpSelection_Lowest,

                  InpHidden_Lowest,

                  InpZOrder_Lowest);

     }

   else

      HLineMove(ChartID(),m_prefix+InpName_Lowest,low[rates_total-1-1]);

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Create the horizontal line                                       |

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

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

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

                 const int             sub_window=0,      // subwindow index

                 double                price=0,           // line price

                 const color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 const bool            back=false,        // in the background

                 const bool            selection=true,    // highlight to move

                 const bool            hidden=true,       // hidden in the object list

                 const long            z_order=0)         // priority for mouse click

  {

//--- if the price is not set, set it at the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- create a horizontal line

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))

     {

      Print(__FUNCTION__,

            ": failed to create a horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the line by mouse

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- set text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move horizontal line                                             |

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

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

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

               double       price=0)      // line price

  {

//--- if the line price is not set, move it to the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move a horizontal line

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

     {

      Print(__FUNCTION__,

            ": failed to move the horizontal 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 ---