Stochastic RSI

Download script
Author: tonyc2a@yahoo.com
Stochastic RSI

This custom indicator is designed to provide traders with a sophisticated way to measure momentum and identify potential overbought or oversold conditions in the market. By combining two popular technical analysis tools—the Relative Strength Index (RSI) and the Stochastic Oscillator—this script creates a specialized "Stochastic RSI" that smoothes out price action to reveal clearer trends.

How the Logic Works

The script operates by processing market data through a multi-layered calculation:

1. The Foundation (RSI Analysis) First, the tool calculates the standard Relative Strength Index (RSI) based on a user-defined period. This identifies how strongly an asset is being purchased or sold relative to its recent price history.

2. Finding the Range (Stochastic Filtering) Instead of looking at raw prices, this script looks at the range of the RSI values over a specific timeframe. It identifies the highest and lowest RSI values reached during that window. This step allows the indicator to determine where the current RSI sits relative to its recent high and low extremes.

3. Calculating the Main Line (StochRSI) The script then calculates the position of the current RSI within that high-low range. It expresses this as a percentage (from 0 to 100). This result is plotted as the primary "StochRSI" line, which helps traders visualize whether the momentum is nearing an extreme "ceiling" (overbought) or "floor" (oversold).

4. The Signal Smoothing To prevent the indicator from being too reactive to minor price fluctuations, the script calculates a secondary "Signal" line. This is done by taking an average of the previous values of the main StochRSI line over a set period.

Visual Interpretation

On your trading chart, you will see two distinct lines in a separate window:

  • The Primary Line (Maroon): Represents the current relative momentum position.
  • The Signal Line (Blue): Represents the moving average of that momentum.

By observing where these two lines move in relation to one another and the 0–100 scale, traders can more effectively identify turning points and trend shifts in the market.

Indicators Used
Relative strength index
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Stochastic RSI
//+------------------------------------------------------------------+
//|                                               Stochastic RSI.mq4 |
//|                                                tonyc2a@yahoo.com |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "tonyc2a@yahoo.com"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_minimum -5
#property indicator_maximum 105
#property indicator_color1 Maroon
#property indicator_color2 Blue

//---- input parameters
extern int RSI_Periods=14;
extern int Percent_K=14;
extern int Percent_D=9;
extern int NumOfBars=0;

//---- buffers
double Buffer1[];
double Buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
   SetIndexBuffer(0,Buffer1);
   SetIndexLabel(0,"StochRSI");
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(1,Buffer2);
   SetIndexLabel(1,"Signal");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   double Current_RSI,Lowest_RSI,Highest_RSI,sum_K;
//---- TODO: add your code here
   if(NumOfBars==0)NumOfBars=Bars;
   for(int i=NumOfBars-MathMax(RSI_Periods,Percent_K)-1;i>=0;i--){
      Current_RSI=iRSI(NULL,0,RSI_Periods,PRICE_TYPICAL,i);
      Highest_RSI=Current_RSI;
      Lowest_RSI=Current_RSI;
      
      for(int x=0;x<=Percent_K;x++){
         Lowest_RSI=MathMin(Lowest_RSI,iRSI(NULL,0,RSI_Periods,PRICE_TYPICAL,i+x));
         Highest_RSI=MathMax(Highest_RSI,iRSI(NULL,0,RSI_Periods,PRICE_TYPICAL,i+x));
         }

      sum_K=0;
      for(x=0;x<=Percent_D;x++) sum_K=sum_K+Buffer1[i+x];

      Buffer1[i]=((Current_RSI-Lowest_RSI)/(Highest_RSI-Lowest_RSI))*100;
      Buffer2[i]=sum_K/Percent_D;
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments