stochRSI_v1
Download scriptThis script is designed as a custom technical analysis tool for the MetaTrader 4 platform. Its purpose is to provide traders with a visual representation of market momentum by combining two popular mathematical models: the Relative Strength Index (RSI) and the Stochastic Oscillator.
Core Logic Overview
The primary goal of this tool is to identify whether a financial asset is currently overbought, oversold, or trending in a specific direction. It achieves this by layering the "strength" of price movements against their recent historical highs and lows.
Instead of looking at the raw price of an asset, the logic operates on the data provided by the RSI—a standard indicator that measures the speed and change of price movements. By applying stochastic principles to the RSI values, the script creates a more sensitive "oscillator" that can highlight potential turning points in the market more effectively than either indicator could do alone.
How the Calculation Works
The script processes market data through three main logical steps:
-
Data Collection (RSI Layer): The tool first calculates the RSI values over a specific period defined by the user. This establishes a baseline of how "strong" the current price movement is relative to its recent past.
-
Range Analysis (Stochastic Layer): Once the RSI values are established, the script looks back over a set number of previous bars to find the highest and lowest RSI points reached during that timeframe. It then determines where the current RSI value sits within that range.
- If the current value is near the top of its recent range, it suggests high momentum (potentially overbought).
- If the current value is near the bottom, it suggests low momentum (potentially oversold).
-
Smoothing and Signaling: To prevent the visual lines from being too erratic or "noisy," the script calculates a moving average of these results. This creates two distinct lines on your chart:
- The Main Line: Represents the primary Stochastic RSI value.
- The Signal Line: A smoothed average of the main line that helps traders identify specific crossover points, which are often used as entry or exit cues.
User Customization
The script allows for manual adjustments to several key factors:
- RSI Periods: Determines how far back the "strength" calculation looks.
- Percent K and D: These control the sensitivity of the Stochastic movement and the smoothness of the signal line, allowing users to balance responsiveness against false signals based on their specific trading style.
//+------------------------------------------------------------------+
//| 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 Black
#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,2);
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