Navigation:Home > Content >

RSI_histogram_v1.mq4

Time: 2015-06-24 | Download file:RSI_histogram_v1.mq4

//+------------------------------------------------------------------+
//|                                                RSI Histogram.mq4 |
//|                                              Written by Linuxser |   
//|                                       http://www.linuxser.com.ar |  
//|  Thanks mladen for all your ideas                                 |                                
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_minimum -50 
#property indicator_maximum 50
#property indicator_buffers 3
#property indicator_color1  C'140,50,180'   
#property indicator_color2   DimGray
#property indicator_style2   STYLE_DOT
#property indicator_color3   DimGray
#property indicator_style3   STYLE_DOT

//---- input parameters
extern int RSIPeriod=14;
extern int TopLevel=20;
extern int BotLevel=-20;
//---- buffers
double RSIBuffer[];
double TopBuffer[];
double BotBuffer[];
double PosBuffer[];
double NegBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(3,PosBuffer);
   SetIndexBuffer(4,NegBuffer);
//---- indicator line
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,RSIBuffer);
   SetIndexBuffer(1,TopBuffer);
   SetIndexBuffer(2,BotBuffer);

//---- name for DataWindow and indicator subwindow label
   short_name="RSI Period ("+RSIPeriod+") OB ("+TopLevel+") OS ("+BotLevel+") RSI Value:";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexLabel(4,NULL);

//----
   SetIndexDrawBegin(0,RSIPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
   double rel,negative,positive;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=Close[k]-Close[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=Close[i]-Close[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) RSIBuffer[i]=-50.0;
      else RSIBuffer[i]=50.0-100.0/(1+positive/negative);
      
      TopBuffer[i] = TopLevel;
      BotBuffer[i] = BotLevel;
      
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Recommend