Sends when a trend changes

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
Miscellaneous
It sends emails
0 Views
0 Downloads
0 Favorites
Sends when a trend changes
ÿþ//+------------------------------------------------------------------+

//|                                   Sends when a trend changes.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//---

#include <lib_cisnewbar.mqh>

#include <Arrays\ArrayObj.mqh>

//---

CArrayObj      m_array_obj;                  // object of CArrayObj class

//--- input parameters

input group                                          "Moving Average"

input ENUM_TIMEFRAMES      Inp_MA_period           = PERIOD_M15;  // MA: timeframe

input int                  Inp_MA_Fast_ma_period   = 100;         // MA Fast: averaging period

input int                  Inp_MA_Slow_ma_period   = 200;         // MA Slow: averaging period

input int                  Inp_MA_Fast_ma_shift    = 0;           // MA Fast: horizontal shift

input int                  Inp_MA_Slow_ma_shift    = 0;           // MA Slow: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method        = MODE_SMA;    // MA: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price    = PRICE_CLOSE; // MA: type of price

input group                                          "Sends"

input bool                 InpUseEmail             = true;        // Use email

input string               InpSymbols              = "EURUSD,EURJPY,GBPUSD,NZDUSD,USDJPY"; // Symbols:

//---

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

string   arr_symbols[];

int      arr_handles_fast[];

int      arr_handles_slow[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(!InpUseEmail)

      return(INIT_SUCCEEDED);

//---

   string to_split=InpSymbols;      // A string to split into substrings

   string sep=",";                  // A separator as a character

   ushort u_sep;                    // The code of the separator character

   string result[];                 // An array to get strings

   StringTrimLeft(to_split);

   StringTrimRight(to_split);

//--- Get the separator code

   u_sep=StringGetCharacter(sep,0);

//--- Split the string to substrings

   int k=StringSplit(to_split,u_sep,result);

//--- Now output all obtained strings

   if(k>0)

     {

      for(int i=0; i<k; i++)

        {

         SymbolSelect(result[i],true);

         //--- create handle of the indicator iMA Fast

         int handle_iMA_Fast=iMA(result[i],Inp_MA_period,Inp_MA_Fast_ma_period,Inp_MA_Fast_ma_shift,

                                 Inp_MA_ma_method,Inp_MA_applied_price);

         //--- if the handle is not created

         if(handle_iMA_Fast==INVALID_HANDLE)

           {

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

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

                        result[i],

                        EnumToString(Inp_MA_period),

                        GetLastError());

           }

         //--- create handle of the indicator iMA Slow

         int handle_iMA_Slow=iMA(result[i],Inp_MA_period,Inp_MA_Slow_ma_period,Inp_MA_Slow_ma_shift,

                                 Inp_MA_ma_method,Inp_MA_applied_price);

         //--- if the handle is not created

         if(handle_iMA_Slow==INVALID_HANDLE)

           {

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

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

                        result[i],

                        EnumToString(Inp_MA_period),

                        GetLastError());

           }

         //---

         if(handle_iMA_Fast!=INVALID_HANDLE && handle_iMA_Slow!=INVALID_HANDLE)

           {

            CisNewBar *is_new_bar=new CisNewBar;

            if(is_new_bar==NULL)

              {

               Print("Object create error");

               continue;

              }

            is_new_bar.SetSymbol(result[i]);

            is_new_bar.SetPeriod(Inp_MA_period);



            m_array_obj.Add(is_new_bar);

            //---

            int size=ArraySize(arr_symbols);

            ArrayResize(arr_symbols,size+1);

            ArrayResize(arr_handles_fast,size+1);

            ArrayResize(arr_handles_slow,size+1);

            arr_symbols[size]=result[i];

            arr_handles_fast[size]=handle_iMA_Fast;

            arr_handles_slow[size]=handle_iMA_Slow;

            //---

           }

        }

     }

//---

   int size=ArraySize(arr_symbols);

//--- Now output all obtained strings

   if(size>0)

      for(int i=0; i<size; i++)

         PrintFormat("arr_symbols[%d]=\"%s\"",i,arr_symbols[i]);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   m_array_obj.Clear();

  }

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

//| 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(!InpUseEmail)

      return(rates_total);

//---

   datetime time_current=TimeCurrent();

   if(time_current-m_last_signal>10)

     {

      //--- search signals

      for(int i=0; i<m_array_obj.Total(); i++)

        {

         CisNewBar *new_bar=m_array_obj.At(i);

         if(new_bar==NULL)

           {

            //--- Error reading from array

            Print("Object CisNewBar create error");

            return(rates_total);

           }

         if(new_bar.isNewBar()>0)

           {

            double fast[],slow[];

            ArraySetAsSeries(fast,true);

            ArraySetAsSeries(slow,true);

            int start_pos=0,count=3;

            if(!iGetArray(arr_handles_fast[i],0,start_pos,count,fast) ||

               !iGetArray(arr_handles_slow[i],0,start_pos,count,slow))

              {

               return(rates_total);

              }

            if(fast[2]>slow[2] && fast[1]<=slow[1])

              {

               SendMail(arr_symbols[i]+" SELL ","");

              }

            else

               if(fast[2]<slow[2]&&fast[1]>=slow[1])

                 {

                  SendMail(arr_symbols[i]+" BUY ","");

                 }

            //---



           }

        }

      //---

      m_last_signal=time_current;

     }

//--- 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);

  }

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

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