MA Horizontal Line Two TimeFrames

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MA Horizontal Line Two TimeFrames
ÿþ//+------------------------------------------------------------------+

//|                            MA Horizontal Line Two TimeFrames.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             "MA One"

input ENUM_TIMEFRAMES      Inp_MA_One_period          = PERIOD_CURRENT;    // MA One: timeframe

input int                  Inp_MA_One_ma_period       = 12;                // MA One: averaging period

input int                  Inp_MA_One_ma_shift        = -3;                // MA One: horizontal shift

input ENUM_MA_METHOD       Inp_MA_One_ma_method       = MODE_SMA;          // MA One: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_One_applied_price   = PRICE_CLOSE;       // MA One: type of price

input group             "Line One"

input color                InpOneColor                = clrRed;            // Line One: color

input ENUM_LINE_STYLE      InpOneStyle                = STYLE_DASH;        // Line One: style

input int                  InpOneWidth                = 3;                 // Line One: width

input group             "MA Two"

input ENUM_TIMEFRAMES      Inp_MA_Two_period          = PERIOD_H1;         // MA Two: timeframe

input int                  Inp_MA_Two_ma_period       = 12;                // MA Two: averaging period

input int                  Inp_MA_Two_ma_shift        = 3;                 // MA Two: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Two_ma_method       = MODE_SMA;          // MA Two: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Two_applied_price   = PRICE_CLOSE;       // MA Two: type of price

input group             "Line Two"

input color                InpTwoColor                = clrBlue;           // Line Two: color

input ENUM_LINE_STYLE      InpTwoStyle                = STYLE_DASH;        // Line Two: style

input int                  InpTwoWidth                = 3;                 // Line Two: width

//---

int      handle_iMA_current;                          // variable for storing the handle of the iMA indicator

int      handle_iMA_other;                            // variable for storing the handle of the iMA indicator

string   m_hline_current_name="";

string   m_hline_other_name="";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create handle of the indicator iMA

   handle_iMA_current=iMA(Symbol(),Inp_MA_One_period,Inp_MA_One_ma_period,Inp_MA_One_ma_shift,

                          Inp_MA_One_ma_method,Inp_MA_One_applied_price);

//--- if the handle is not created

   if(handle_iMA_current==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iMA indicator ('One') for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Inp_MA_One_period),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_other=iMA(Symbol(),Inp_MA_Two_period,Inp_MA_Two_ma_period,Inp_MA_Two_ma_shift,

                        Inp_MA_Two_ma_method,Inp_MA_Two_applied_price);

//--- if the handle is not created

   if(handle_iMA_other==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iMA indicator ('Two') for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Inp_MA_Two_period),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   ENUM_TIMEFRAMES timeframe=(Inp_MA_One_period==PERIOD_CURRENT)?Period():Inp_MA_One_period;

   m_hline_current_name="One "+StringSubstr(EnumToString(timeframe),7,-1)+

                        StringFormat("(%d, %d, %s, %s)",Inp_MA_One_ma_period, Inp_MA_One_ma_shift,EnumToString(Inp_MA_One_ma_method),EnumToString(Inp_MA_One_applied_price));

   timeframe=(Inp_MA_Two_period==PERIOD_CURRENT)?Period():Inp_MA_Two_period;

   m_hline_other_name="Two "+StringSubstr(EnumToString(timeframe),7,-1)+

                      StringFormat("(%d, %d, %s, %s)",Inp_MA_Two_ma_period, Inp_MA_Two_ma_shift,EnumToString(Inp_MA_Two_ma_method),EnumToString(Inp_MA_Two_applied_price));

   HLineCreate(0,m_hline_current_name,0,0,InpOneColor,InpOneStyle,InpOneWidth);

   HLineCreate(0,m_hline_other_name,0,0,InpTwoColor,InpTwoStyle,InpTwoWidth);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMA_current!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_current);

   if(handle_iMA_other!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_other);

   HLineDelete(0,m_hline_current_name);

   HLineDelete(0,m_hline_other_name);

  }

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

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

  {

//---

   double price_current[];

//--- reset error code

   ResetLastError();

//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(handle_iMA_current,0,-Inp_MA_One_ma_shift,1,price_current)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(0);

     }

   double price_other[];

//--- reset error code

   ResetLastError();

//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(handle_iMA_other,0,-Inp_MA_Two_ma_shift,1,price_other)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(0);

     }

//---

   HLineMove(0,m_hline_current_name,price_current[0]);

   HLineMove(0,m_hline_other_name,price_other[0]);

//--- 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=false,     // 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 the 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);

  }

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

//| Delete a horizontal line                                         |

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- delete a horizontal line

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a 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 ---