MA trend MultiTimeframe

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
MA trend MultiTimeframe
ÿþ//+------------------------------------------------------------------+

//|                                      MA trend MultiTimeframe.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 6

#property indicator_plots   3

//--- plot TF_0

#property indicator_label1  "TF_0"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot TF_1

#property indicator_label2  "TF_1"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkViolet

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot TF_2

#property indicator_label3  "TF_2"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLimeGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- input parameters

input group             "MA 0"

input ENUM_TIMEFRAMES      Inp_MA_0_period         = PERIOD_M15;     // MA 0: timeframe

input int                  Inp_MA_0_ma_period      = 12;             // MA 0: averaging period

input int                  Inp_MA_0_ma_shift       = 0;              // MA 0: horizontal shift

input ENUM_MA_METHOD       Inp_MA_0_ma_method      = MODE_SMA;       // MA 0: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_0_applied_price  = PRICE_CLOSE;    // MA 0: type of price

input group             "MA 1"

input ENUM_TIMEFRAMES      Inp_MA_1_period         = PERIOD_H1;      // MA 1: timeframe

input int                  Inp_MA_1_ma_period      = 12;             // MA 1: averaging period

input int                  Inp_MA_1_ma_shift       = 0;              // MA 1: horizontal shift

input ENUM_MA_METHOD       Inp_MA_1_ma_method      = MODE_SMA;       // MA 1: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_1_applied_price  = PRICE_CLOSE;    // MA 1: type of price

input group             "MA 2"

input ENUM_TIMEFRAMES      Inp_MA_2_period         = PERIOD_H6;      // MA 2: timeframe

input int                  Inp_MA_2_ma_period      = 12;             // MA 2: averaging period

input int                  Inp_MA_2_ma_shift       = 0;              // MA 2: horizontal shift

input ENUM_MA_METHOD       Inp_MA_2_ma_method      = MODE_SMA;       // MA 2: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_2_applied_price  = PRICE_CLOSE;    // MA 2: type of price

//--- indicator buffers

double   MA_0_Buffer[];

double   MA_1_Buffer[];

double   MA_2_Buffer[];

double   iMABuffer_0[];

double   iMABuffer_1[];

double   iMABuffer_2[];

//---

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

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

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

int      bars_calculated=0;                     // we will keep the number of values in the Moving Average indicator

//---

bool     m_global_error=false;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,MA_0_Buffer,INDICATOR_DATA);

   SetIndexBuffer(1,MA_1_Buffer,INDICATOR_DATA);

   SetIndexBuffer(2,MA_2_Buffer,INDICATOR_DATA);

   SetIndexBuffer(3,iMABuffer_0,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,iMABuffer_1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,iMABuffer_2,INDICATOR_CALCULATIONS);

//--- set labels for the line

   PlotIndexSetString(0,PLOT_LABEL,StringSubstr(EnumToString(Inp_MA_0_period),7,-1));

   PlotIndexSetString(1,PLOT_LABEL,StringSubstr(EnumToString(Inp_MA_1_period),7,-1));

   PlotIndexSetString(2,PLOT_LABEL,StringSubstr(EnumToString(Inp_MA_2_period),7,-1));

//---

   if(Period()>Inp_MA_0_period || Period()>Inp_MA_1_period || Period()>Inp_MA_2_period)

     {

      PrintFormat("The current timeframe %s cannot be larger %s or %s or %s",

                  StringSubstr(EnumToString(Period()),7,-1),

                  StringSubstr(EnumToString(Inp_MA_0_period),7,-1),

                  StringSubstr(EnumToString(Inp_MA_1_period),7,-1),

                  StringSubstr(EnumToString(Inp_MA_2_period),7,-1));

      //--- the indicator is stopped early

      m_global_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_iMA_0=iMA(Symbol(),Inp_MA_0_period,Inp_MA_0_ma_period,Inp_MA_0_ma_shift,

                    Inp_MA_0_ma_method,Inp_MA_0_applied_price);

//--- if the handle is not created

   if(handle_iMA_0==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_0_period),

                  GetLastError());

      //--- the indicator is stopped early

      m_global_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_iMA_1=iMA(Symbol(),Inp_MA_1_period,Inp_MA_1_ma_period,Inp_MA_1_ma_shift,

                    Inp_MA_1_ma_method,Inp_MA_1_applied_price);

//--- if the handle is not created

   if(handle_iMA_1==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_1_period),

                  GetLastError());

      //--- the indicator is stopped early

      m_global_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_iMA_2=iMA(Symbol(),Inp_MA_2_period,Inp_MA_2_ma_period,Inp_MA_2_ma_shift,

                    Inp_MA_2_ma_method,Inp_MA_2_applied_price);

//--- if the handle is not created

   if(handle_iMA_2==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Inp_MA_2_period),

                  GetLastError());

      //--- the indicator is stopped early

      m_global_error=true;

      return(INIT_SUCCEEDED);

     }

//---

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

  {

   if(m_global_error)

      return(0);

//--- determine the number of values calculated in the indicator

   int calculated_0=BarsCalculated(handle_iMA_0);

   if(calculated_0<=0)

     {

      PrintFormat("BarsCalculated(\"MA 0\") returned %d, error code %d",calculated_0,GetLastError());

      return(0);

     }

   int calculated_1=BarsCalculated(handle_iMA_1);

   if(calculated_1<=0)

     {

      PrintFormat("BarsCalculated(\"MA 1\") returned %d, error code %d",calculated_1,GetLastError());

      return(0);

     }

   int calculated_2=BarsCalculated(handle_iMA_2);

   if(calculated_2<=0)

     {

      PrintFormat("BarsCalculated(\"MA 2\") returned %d, error code %d",calculated_2,GetLastError());

      return(0);

     }

   int calculated=calculated_0;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      double arr_0[1],arr_1[1],arr_2[1];

      if(CopyBuffer(handle_iMA_0,0,time[i],1,arr_0)<0 || CopyBuffer(handle_iMA_1,0,time[i],1,arr_1)<0 || CopyBuffer(handle_iMA_2,0,time[i],1,arr_2)<0)

         return(0);

      else

        {

         MA_0_Buffer[i]=arr_0[0];

         MA_1_Buffer[i]=arr_1[0];

         MA_2_Buffer[i]=arr_2[0];

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the MA indicator                  |

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

bool FillArrayFromBuffer(double &values[],   // indicator buffer of Moving Average values

                         int shift,          // shift

                         int ind_handle,     // handle of the iMA indicator

                         int amount          // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<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(false);

     }

//--- everything is fine

   return(true);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMA_0!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_0);

   if(handle_iMA_1!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_1);

   if(handle_iMA_2!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_2);

  }

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

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