RSI_EMA_COL

Author: AHGDP
Indicators Used
Relative strength indexMoving average indicator
Miscellaneous
It issuies visual alerts to the screenIt plays sound alertsIt sends emails
0 Views
0 Downloads
0 Favorites
RSI_EMA_COL
//+------------------------------------------------------------------+
//|                                          RSI_EMA_COL.mq4 
//+------------------------------------------------------------------+
#property copyright "AHGDP"
#property link      "RSI_EMA_COL"

#property indicator_separate_window
#property indicator_buffers   4
#property indicator_color1 DimGray
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Gold

#property indicator_maximum  65
#property indicator_minimum  35

//=================================================================================================
#property indicator_level1 50  
#property indicator_level2 65
#property indicator_level3 35
#property indicator_levelcolor  BurlyWood 
#property indicator_levelstyle STYLE_DOT
#property indicator_levelwidth 0
//=================================================================================================


extern int       RSIPeriod     = 50 ;
extern int       EMAPeriod     = 25 ;
extern int       COHLMTW6      = 0 ;
extern int       MA_SiEmSmWe3  = 1 ;
extern string    timeFrame     = "0" ;
extern int       overBought    = 50 ;
extern int       overSold      = 50 ;
extern bool      showArrows    = false ;
extern color     Arrowcolor    = Orchid ;   
extern bool      alertsOn      = false ;
extern bool      alertsMessage = false ;
extern bool      alertsSound   = false ;
extern bool      alertsEmail   = false ;

extern int    LineSize1  = 2 ;
extern int    LineSize2  = 2 ;
extern int    CountBars  = 300 ; 

double   RSIBuffer[];
double   EMABuffer[];
double   Upper[];
double   Lower[];

int      TimeFrame;
datetime TimeArray[];
int      maxArrows;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int init()
   {
   SetIndexStyle (0,DRAW_LINE, STYLE_SOLID, LineSize1);
   SetIndexBuffer(0,RSIBuffer);
   SetIndexStyle (1,DRAW_LINE, STYLE_SOLID, LineSize1);
   SetIndexBuffer(1,Upper);
   SetIndexStyle (2,DRAW_LINE, STYLE_SOLID, LineSize1);
   SetIndexBuffer(2,Lower);
   SetIndexStyle (3,DRAW_LINE, STYLE_SOLID, LineSize2);
   SetIndexBuffer(3,EMABuffer);
   SetIndexLabel(0,"RSI");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,"EMA of RSI");
         
   TimeFrame         = stringToTimeFrame(timeFrame);
   string shortName  = "RSI ("+TimeFrameToString(TimeFrame)+","+RSIPeriod+","+PriceTypeToString(COHLMTW6);
   
   if (overBought < overSold)
      {
      overBought = overSold;
      }
   if (overBought < 100)
      {
      shortName  = shortName+","+overBought;
      }
   if (overSold   >   0)
      {
      shortName  = shortName+","+overSold;
      }
   IndicatorShortName(shortName+")");
   return(0);
   }


int deinit()
   {
   DeleteArrows();
   return(0);
   }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
   {
   int    counted_bars=IndicatorCounted();
   int    limit;
   int    i,y;
   
   if(counted_bars<0) return(-1);
   limit=Bars-counted_bars;
     SetIndexDrawBegin(0,Bars-CountBars);
     SetIndexDrawBegin(1,Bars-CountBars);
     SetIndexDrawBegin(2,Bars-CountBars);
     SetIndexDrawBegin(3,Bars-CountBars);
     
   ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
  
   for(i=0,y=0; i<limit; i++)
      {
      if(Time[i]<TimeArray[y]) y++;
         {
         RSIBuffer[i] = iRSI(NULL,TimeFrame,RSIPeriod,COHLMTW6,y);
         }
      }
   for(i=0,y=0; i<limit; i++)
      {
      if(Time[i]<TimeArray[y]) y++;
         {
         EMABuffer[i] = iMAOnArray(RSIBuffer,Bars,EMAPeriod,0,MA_SiEmSmWe3,i);
         }
      }
  
   for(i=limit; i>=0; i--)
      {
      if (RSIBuffer[i] >EMABuffer[i]) //overBought
         { 
         Upper[i] = RSIBuffer[i]; Upper[i+1] = RSIBuffer[i+1];
         }
      else
         { Upper[i] = EMPTY_VALUE;
         if (Upper[i+2] == EMPTY_VALUE)
            {
            Upper[i+1]  = EMPTY_VALUE; 
            }
         if (RSIBuffer[i] < EMABuffer[i])   //overSold
            { 
            Lower[i] = RSIBuffer[i]; Lower[i+1] = RSIBuffer[i+1];
            }
         else 
            {
            Lower[i] = EMPTY_VALUE;
            if (Lower[i+2] == EMPTY_VALUE)
               {
               Lower[i+1]  = EMPTY_VALUE;
               }
            }
         }    
      }
   
   
   if (showArrows)
      for (i=0; i<WindowBarsPerChart() ;i++)
         {
         if (RSIBuffer[i]>EMABuffer[i] && RSIBuffer[i+1]<EMABuffer[i]) //overBought
            {
            DrawArrow(i,"down");
            }
         if (RSIBuffer[i]<EMABuffer[i]   && RSIBuffer[i+1]>EMABuffer[i])//overSold
            {
            DrawArrow(i,"up");
            }
         }
   if (alertsOn)
      {
     if (RSIBuffer[0]>overBought && RSIBuffer[1]<overBought) 
         {
         doAlert(overBought+" line crossed up");
         }
      if (RSIBuffer[0]<overSold   && RSIBuffer[1]>overSold)
         {
         doAlert(overBought+" line crossed down");
         }
      }
   return(0);
   }


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

void DrawArrow(int i,string type)
   {
   maxArrows++;
   ObjectCreate("RSISignal"+maxArrows,OBJ_ARROW,0,Time[i],0);
   if (type=="up")
      {
      ObjectSet("RSISignal"+maxArrows,OBJPROP_PRICE1,High[i]+(8*Point));
      ObjectSet("RSISignal"+maxArrows,OBJPROP_ARROWCODE,234);
      ObjectSet("RSISignal"+maxArrows,OBJPROP_COLOR,Arrowcolor);
      }
   else
      {
      ObjectSet("RSISignal"+maxArrows,OBJPROP_PRICE1,Low[i]-(6*Point));
      ObjectSet("RSISignal"+maxArrows,OBJPROP_ARROWCODE,233);
      ObjectSet("RSISignal"+maxArrows,OBJPROP_COLOR,Arrowcolor);
      }
   }
void DeleteArrows()
   {
   while(maxArrows>0) 
      { 
      ObjectDelete("RSISignal"+maxArrows); maxArrows--; 
      }
   }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void doAlert(string doWhat)
   {
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) 
      {
      previousAlert  = doWhat;
      previousTime   = Time[0];
      message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," RSI ",doWhat);
      
      if (alertsMessage) 
         {
         Alert(message);
         }
     if (alertsSound)
         {
         PlaySound("alert2.wav");
         }
      if (alertsEmail)
         {
         SendMail(StringConcatenate(Symbol()," RSI crossing"),message);
         }
      }        
   }

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


string PriceTypeToString(int pt)
   {
   string answer;
   switch(pt)
      {
      case 0:  answer = "Close"    ; break; 
      case 1:  answer = "Open"     ; break;
      case 2:  answer = "High"     ; break;
      case 3:  answer = "Low"      ; break;
      case 4:  answer = "Median"   ; break;
      case 5:  answer = "Typical"  ; break;
      case 6:  answer = "Wighted"  ; break;
      default: answer = "Invalid price field requested";
                                    Alert(answer);
      }
   return(answer);
   }
int stringToTimeFrame(string tfs)
   {
   int tf=0;
   tfs = StringUpperCase(tfs);
   
   if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
   if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
   if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
   if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
   if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
   if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
   if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
   if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
   if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
   if (tf<Period()) tf=Period();
   
   return(tf);
   }
   
string TimeFrameToString(int tf)
   {
   string tfs="Current time frame";
   switch(tf) 
      {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
      }
   return(tfs);
   }

string StringUpperCase(string str)
   {
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
      char = StringGetChar(s, lenght);
      if((char > 96 && char < 123) || (char > 223 && char < 256))
         {
         s = StringSetChar(s, lenght, char - 32);
         }
      else 
      if(char > -33 && char < 0)
         {
         s = StringSetChar(s, lenght, char + 224);
         }                              
      lenght--;
   }
  
   return(s);
}

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