Crossing of two EMA Four timeframes Arrow

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Crossing of two EMA Four timeframes Arrow
ÿþ//+------------------------------------------------------------------+

//|                    Crossing of two EMA Four timeframes Arrow.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 8

#property indicator_plots   8

//--- plot H1_Up

#property indicator_label1  "H1_Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDeepSkyBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot H2_Up

#property indicator_label2  "H2_Up"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrDeepSkyBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot H3_Up

#property indicator_label3  "H3_Up"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrDeepSkyBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot H4_Up

#property indicator_label4  "H4_Up"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrDeepSkyBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot H1_Dn

#property indicator_label5  "H1_Dn"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrTomato

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- plot H2_Dn

#property indicator_label6  "H2_Dn"

#property indicator_type6   DRAW_ARROW

#property indicator_color6  clrTomato

#property indicator_style6  STYLE_SOLID

#property indicator_width6  1

//--- plot H3_Dn

#property indicator_label7  "H3_Dn"

#property indicator_type7   DRAW_ARROW

#property indicator_color7  clrTomato

#property indicator_style7  STYLE_SOLID

#property indicator_width7  1

//--- plot H4_Dn

#property indicator_label8  "H4_Dn"

#property indicator_type8   DRAW_ARROW

#property indicator_color8  clrTomato

#property indicator_style8  STYLE_SOLID

#property indicator_width8  1

//--- input parameters

//--- indicator buffers

double   H1_UpBuffer[];

double   H2_UpBuffer[];

double   H3_UpBuffer[];

double   H4_UpBuffer[];

double   H1_DnBuffer[];

double   H2_DnBuffer[];

double   H3_DnBuffer[];

double   H4_DnBuffer[];

//---

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

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

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

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

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

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

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

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

//---

bool     m_init_error               = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,H1_UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,H2_UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,H3_UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,H4_UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,H1_DnBuffer,INDICATOR_DATA);

   SetIndexBuffer(5,H2_DnBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,H3_DnBuffer,INDICATOR_DATA);

   SetIndexBuffer(7,H4_DnBuffer,INDICATOR_DATA);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,200);

   PlotIndexSetInteger(1,PLOT_ARROW,200);

   PlotIndexSetInteger(2,PLOT_ARROW,200);

   PlotIndexSetInteger(3,PLOT_ARROW,200);

   PlotIndexSetInteger(4,PLOT_ARROW,202);

   PlotIndexSetInteger(5,PLOT_ARROW,202);

   PlotIndexSetInteger(6,PLOT_ARROW,202);

   PlotIndexSetInteger(7,PLOT_ARROW,202);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,10);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,20);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,30);

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,40);

   PlotIndexSetInteger(4,PLOT_ARROW_SHIFT,-10);

   PlotIndexSetInteger(5,PLOT_ARROW_SHIFT,-20);

   PlotIndexSetInteger(6,PLOT_ARROW_SHIFT,-30);

   PlotIndexSetInteger(7,PLOT_ARROW_SHIFT,-40);

//--- set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- create handle of the indicator iMA

   handle_EMA_10_H1=iMA(Symbol(),PERIOD_H1,10,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_10_H1==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_10_H2=iMA(Symbol(),PERIOD_H2,10,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_10_H2==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_10_H3=iMA(Symbol(),PERIOD_H3,10,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_10_H3==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_10_H4=iMA(Symbol(),PERIOD_H4,10,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_10_H4==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H4),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_30_H1=iMA(Symbol(),PERIOD_H1,30,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_30_H1==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_30_H2=iMA(Symbol(),PERIOD_H2,30,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_30_H2==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_30_H3=iMA(Symbol(),PERIOD_H3,30,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_30_H3==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H1),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_EMA_30_H4=iMA(Symbol(),PERIOD_H4,30,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_EMA_30_H4==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(PERIOD_H4),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_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_init_error)

      return(0);

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

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

     {

      H1_UpBuffer[i]=EMPTY_VALUE;

      H2_UpBuffer[i]=EMPTY_VALUE;

      H3_UpBuffer[i]=EMPTY_VALUE;

      H4_UpBuffer[i]=EMPTY_VALUE;

      H1_DnBuffer[i]=EMPTY_VALUE;

      H2_DnBuffer[i]=EMPTY_VALUE;

      H3_DnBuffer[i]=EMPTY_VALUE;

      H4_DnBuffer[i]=EMPTY_VALUE;

     }

//--- H1

   int bars_calculated_ema_10_h1=BarsCalculated(handle_EMA_10_H1);

   int bars_calculated_ema_30_h1=BarsCalculated(handle_EMA_30_H1);

   if(bars_calculated_ema_10_h1>0 && bars_calculated_ema_30_h1>0 && bars_calculated_ema_10_h1==bars_calculated_ema_30_h1)

     {

      double ema_10_h1[],ema_30_h1[];

      ArraySetAsSeries(ema_10_h1,true);

      ArraySetAsSeries(ema_30_h1,true);

      int start_pos=0,count=6;

      if(iGetArray(handle_EMA_10_H1,0,start_pos,count,ema_10_h1) && iGetArray(handle_EMA_30_H1,0,start_pos,count,ema_30_h1))

        {

         //--- Up

         if(ema_10_h1[1]<ema_30_h1[1] && ema_10_h1[0]>ema_30_h1[0])

            H1_UpBuffer[rates_total-1]=low[rates_total-1];

         //--- Dn

         if(ema_10_h1[1]>ema_30_h1[1] && ema_10_h1[0]<ema_30_h1[0])

            H1_DnBuffer[rates_total-1]=high[rates_total-1];

        }

     }

//--- H2

   int bars_calculated_ema_10_h2=BarsCalculated(handle_EMA_10_H2);

   int bars_calculated_ema_30_h2=BarsCalculated(handle_EMA_30_H2);

   if(bars_calculated_ema_10_h2>0 && bars_calculated_ema_30_h2>0 && bars_calculated_ema_10_h2==bars_calculated_ema_30_h2)

     {

      double ema_10_h2[],ema_30_h2[];

      ArraySetAsSeries(ema_10_h2,true);

      ArraySetAsSeries(ema_30_h2,true);

      int start_pos=0,count=6;

      if(iGetArray(handle_EMA_10_H2,0,start_pos,count,ema_10_h2) && iGetArray(handle_EMA_30_H2,0,start_pos,count,ema_30_h2))

        {

         //--- Up

         if(ema_10_h2[1]<ema_30_h2[1] && ema_10_h2[0]>ema_30_h2[0])

            H2_UpBuffer[rates_total-1]=low[rates_total-1];

         //--- Dn

         if(ema_10_h2[1]>ema_30_h2[1] && ema_10_h2[0]<ema_30_h2[0])

            H2_DnBuffer[rates_total-1]=high[rates_total-1];

        }

     }

//--- H3

   int bars_calculated_ema_10_h3=BarsCalculated(handle_EMA_10_H3);

   int bars_calculated_ema_30_h3=BarsCalculated(handle_EMA_30_H3);

   if(bars_calculated_ema_10_h3>0 && bars_calculated_ema_30_h3>0 && bars_calculated_ema_10_h3==bars_calculated_ema_30_h3)

     {

      double ema_10_h3[],ema_30_h3[];

      ArraySetAsSeries(ema_10_h3,true);

      ArraySetAsSeries(ema_30_h3,true);

      int start_pos=0,count=6;

      if(iGetArray(handle_EMA_10_H3,0,start_pos,count,ema_10_h3) && iGetArray(handle_EMA_30_H3,0,start_pos,count,ema_30_h3))

        {

         //--- Up

         if(ema_10_h3[1]<ema_30_h3[1] && ema_10_h3[0]>ema_30_h3[0])

            H3_UpBuffer[rates_total-1]=low[rates_total-1];

         //--- Dn

         if(ema_10_h3[1]>ema_30_h3[1] && ema_10_h3[0]<ema_30_h3[0])

            H3_DnBuffer[rates_total-1]=high[rates_total-1];

        }

     }

//--- H4

   int bars_calculated_ema_10_h4=BarsCalculated(handle_EMA_10_H4);

   int bars_calculated_ema_30_h4=BarsCalculated(handle_EMA_30_H4);

   if(bars_calculated_ema_10_h4>0 && bars_calculated_ema_30_h4>0 && bars_calculated_ema_10_h4==bars_calculated_ema_30_h4)

     {

      double ema_10_h4[],ema_30_h4[];

      ArraySetAsSeries(ema_10_h4,true);

      ArraySetAsSeries(ema_30_h4,true);

      int start_pos=0,count=6;

      if(iGetArray(handle_EMA_10_H4,0,start_pos,count,ema_10_h4) && iGetArray(handle_EMA_30_H4,0,start_pos,count,ema_30_h4))

        {

         //--- Up

         if(ema_10_h4[1]<ema_30_h4[1] && ema_10_h4[0]>ema_30_h4[0])

            H4_UpBuffer[rates_total-1]=low[rates_total-1];

         //--- Dn

         if(ema_10_h4[1]>ema_30_h4[1] && ema_10_h4[0]<ema_30_h4[0])

            H4_DnBuffer[rates_total-1]=high[rates_total-1];

        }

     }

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

   return(rates_total);

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const int start_pos,

               const int count,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);

   if(copied!=count)

     {

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

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",

                  __FILE__,__FUNCTION__,count,copied,GetLastError());

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

      return(false);

     }

   return(result);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_EMA_10_H1!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_10_H1);

   if(handle_EMA_10_H2!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_10_H2);

   if(handle_EMA_10_H3!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_10_H3);

   if(handle_EMA_10_H4!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_10_H4);

   if(handle_EMA_30_H1!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_30_H1);

   if(handle_EMA_30_H2!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_30_H2);

   if(handle_EMA_30_H3!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_30_H3);

   if(handle_EMA_30_H4!=INVALID_HANDLE)

      IndicatorRelease(handle_EMA_30_H4);

  }

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

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