Author: Copyright � 2004, MetaQuotes Software Corp.
I_RSI

This tool is designed to provide a visual representation of market momentum by calculating the Relative Strength Index (RSI). It helps traders identify whether an asset is being overbought, oversold, or experiencing a specific trend strength by measuring the speed and change of price movements over a set period.

Core Logic Overview

The script functions by analyzing the difference between consecutive closing prices. Instead of simply looking at whether the price went up or down, it separates these movements into two distinct categories: positive gains and negative losses.

How the Calculation Works

To provide a smooth and accurate reading, the logic follows a structured three-step process:

  1. Initial Accumulation: When the script first begins its analysis, it looks back at a specific window of historical data (defined by the "Period" setting, which defaults to 14). It sums up all the gains and all the losses within this window to establish a baseline.
  2. Smoothing Process: As new price data arrives, the script doesn't just look at the most recent change in isolation. It uses a smoothing technique where it blends the current price movement with the previous values. This prevents the indicator from reacting too wildly to minor "noise" in the market and ensures a more fluid line on your chart.
  3. Momentum Ratio: Finally, the script converts these accumulated gains and losses into a percentage value between 0 and 100.
    • If the positive gains significantly outweigh the negative losses, the indicator moves toward the top of the scale.
    • If the negative losses are dominant, the indicator drops toward the bottom.

Visual Output

The result is displayed in a separate window below your main price chart. The line represents the "strength" of the current trend relative to its recent history. By observing where this line sits on the 0–100 scale, you can better understand the prevailing momentum of the market at any given moment.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
I_RSI
//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
 
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int RSIPeriod=14;
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
   SetIndexBuffer(1,PosBuffer);
   SetIndexBuffer(2,NegBuffer);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSIBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="RSI("+RSIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   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]=0.0;
      else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments