Author: Copyright � 2004, MetaQuotes Software Corp.
RSI

This script is designed to function as a technical analysis tool that measures the speed and change of price movements. In simpler terms, it helps traders identify whether an asset—such as a currency or stock—is being bought or sold with excessive force, which can indicate whether a market is "overbought" or "oversold."

How the Logic Works

The script operates by analyzing historical price data over a specific period of time (the default being 14 candles or bars). It breaks down the price action into two primary components:

1. Tracking Upward and Downward Movement The tool looks at each individual price change from one candle to the next.

  • If the price goes up, it records the amount of that gain as "positive."
  • If the price goes down, it records the amount of that loss as "negative."

2. Smoothing the Data Instead of just looking at one single jump in price, the script calculates an average of these gains and losses over the chosen timeframe. It uses a "smoothed" moving average approach. This means that instead of reacting wildly to every tiny flicker in price, it balances out previous results with new data to create a more stable, readable trend line.

3. Calculating the Strength Index Once the script has the average positive and negative movements, it performs a final calculation to produce a single value on a scale of 0 to 100:

  • If the gains are significantly stronger than the losses, the indicator moves toward the top of the scale (closer to 100).
  • If the losses are significantly stronger than the gains, the indicator moves toward the bottom of the scale (closer to 0).

Visual Outcome

The result is a continuous line displayed in a separate window below your main price chart. By observing where this line sits on the 0–100 scale, you can visually gauge the momentum of the market at any given moment, helping to identify potential turning points in price trends.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
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