Author: Copyright 2015, Dodonov Vitaly (mql_5)
Indicators Used
Relative strength index
Miscellaneous
It issuies visual alerts to the screenIt sends emailsIt plays sound alerts
0 Views
0 Downloads
0 Favorites
RSI Trend
//+------------------------------------------------------------------+
//|                                                    RSI Trend.mq5 |
//|                           Copyright 2021, Dodonov Vitaly (mql_5) |
//|                                           https://www.ellizii.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Dodonov Vitaly (mql_5)"
#property link      "https://www.ellizii.ru"
#property version   "1.20"
#property description "RSI TrendMT4"
//#property icon "\\Images\\rsi_trend.ico";

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 2
#property indicator_plots   2
//--- plot r1
#property indicator_label1  "r1"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5
//--- plot r2
#property indicator_label2  "r2"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  5

//+------------------------------------------------------------------+
//|                   Alert message                                  |
//+------------------------------------------------------------------+
enum ENUM_ALERT
  {
   ALERT_ON=0,
   ALERT_OFF=1
  };
//+------------------------------------------------------------------+
//|              Push Notification                                   |
//+------------------------------------------------------------------+
enum ENUM_PUSH
  {
   PUSH_ON=0,
   PUSH_OFF=1
  };
//+------------------------------------------------------------------+
//|                    Send email message                            |
//+------------------------------------------------------------------+
enum ENUM_MAIL
  {
   MAIL_ON=0,
   MAIL_OFF=1
  };
//+------------------------------------------------------------------+
//|                     Play sound                                   |
//+------------------------------------------------------------------+

enum ENUM_SOUND
  {
   SOUND_ON=0,
   SOUND_OFF=1
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//--- input parameters
input string n1 = "||+---------- Parameters ---------+||";
input int       Rsi_period=12;
input string    SymbolPrefix = "";
input string    SymbolPostfix = "";
input string n2 = "||+---------- Nitifications ---------+||";
input ENUM_ALERT  SoundAlert              = ALERT_OFF;
input ENUM_PUSH   PushNotification        = PUSH_OFF;
input ENUM_MAIL   EmailAlert              = MAIL_OFF;
input ENUM_SOUND  Sound                   = SOUND_OFF;
input string      SoundFile               = "Alert.wav";
//--- indicator buffers
double         r1Buffer[];
double         r2Buffer[];
double         rsi[];
//--- variables
bool up,dw;
double level_up,level_dw;
string trd_t1,trd_t2;
string symb;
ENUM_TIMEFRAMES timeframe;
int rsi_handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,r1Buffer);
   SetIndexBuffer(1,r2Buffer);
   ArraySetAsSeries(r1Buffer,true);
   ArraySetAsSeries(r2Buffer,true);
//--- trend directions
   up=false;
   dw=false;
//--- rsi levels
   level_dw=29;
   level_up=69;
//--- message
   trd_t1="Trend Up";
   trd_t2="Trend Down";
//---
   symb = SymbolPrefix+Symbol()+SymbolPostfix;
   timeframe = Period();
   rsi_handle = iRSI(symb,timeframe,Rsi_period,PRICE_CLOSE);
//---
   IndicatorSetString(INDICATOR_SHORTNAME,"RSI Trend("+IntegerToString(Rsi_period)+")");

   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[])
  {
   int i=0;
   if(prev_calculated==0)
      i=rates_total-2-Rsi_period;
   else
      i=1;
//---
   while(i>=0)
     {
     CopyBuffer(rsi_handle,0,i+1,2,rsi);
      double rsi_c = rsi[0]; //iRSI(Symbol(),Period(),Rsi_period,PRICE_CLOSE,i+1);
      double rsi_p = rsi[1];//iRSI(Symbol(),Period(),Rsi_period,PRICE_CLOSE,i+2);

      if(up && rsi_c<level_dw && rsi_p>level_dw)
        {
         dw=true;
         up=false;
         if(i==0)
            doAlert(symb,timeframe,i,trd_t2);
        }
      else
         if(dw && rsi_c>level_up && rsi_p<level_up)
           {
            up=true;
            dw=false;
            if(i==0)
               doAlert(symb,timeframe,i,trd_t1);
           }
         else
            if(!up && !dw && rsi_c<level_dw && rsi_p>level_dw)
              {
               dw=true;
               if(i==0)
                  doAlert(symb,timeframe,i,trd_t2);
              }
            else
               if(!up && !dw && rsi_c>level_up && rsi_p<level_up)
                 {
                  up=true;
                  if(i==0)
                     doAlert(symb,timeframe,i,trd_t1);
                 }

      if(up && !dw)
        {
         r1Buffer[i]=1;
         r2Buffer[i]=EMPTY_VALUE;
        }
      else
         if(dw && !up)
           {
            r2Buffer[i]=1;
            r1Buffer[i]=EMPTY_VALUE;
           }
      i--;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void doAlert(string symbol,ENUM_TIMEFRAMES tf,int index,string message)
  {

   if(previousAlert!=message || previousTime<iTime(symb,tf,index))
     {
      previousAlert  = message;
      previousTime   = iTime(symb,tf,index);
      message=MQLInfoString(MQL_PROGRAM_NAME)+" | "+symbol+" | "+IntegerToString(tf)+" | "+message;

      if(PushNotification==PUSH_ON)
         SendNotification(message);

      if(SoundAlert==ALERT_ON)
         Alert(message);

      if(EmailAlert==MAIL_ON)
         SendMail(MQLInfoString(MQL_PROGRAM_NAME),message);

      if(Sound==SOUND_ON)
         PlaySound(SoundFile);
     }
  }
string         previousAlert;
datetime       previousTime;
//+------------------------------------------------------------------+

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