StochasticRSI
Download scriptThis tool is a sophisticated technical analysis indicator designed to help traders identify momentum and potential price reversals by combining two well-known market oscillators: the Relative Strength Index (RSI) and the Stochastic Oscillator.
Core Functionality
Rather than looking at price action directly, this script analyzes the "strength" of price movements over time. It operates by first calculating the RSI—a metric that measures the speed and change of price movements—and then applying a Stochastic formula to that data. This creates a "double-filtered" view of the market, helping to smooth out noise and highlight clearer trends.
How the Logic Works
The script processes information in three distinct stages:
1. Data Input & Range Identification The algorithm looks back at a specific number of past candles (defined by your settings) to determine the current RSI value. Within that same timeframe, it scans to find the highest and lowest RSI values reached. By comparing the current RSI to this high-low range, the script determines where the "momentum" currently sits—essentially asking, "Is the current strength near its recent peak or its recent valley?"
2. Normalization and Calculation Once the high and low points are identified, the script calculates a percentage-based position. This tells the user how far the current momentum has traveled relative to its recent boundaries. It then applies a mathematical formula to convert this into a specific visual value, ensuring the data is scaled appropriately for the chart window.
3. Smoothing and Triggering To prevent the indicator from being too "jumpy" or reactive to minor price fluctuations, the script applies a Weighted Moving Average (WMA). This creates two distinct lines on your chart:
- The Main Signal: A smoothed line representing the primary Stochastic RSI value.
- The Trigger: A secondary line that follows the previous period's value, acting as a reference point to help identify when the main signal is shifting direction.
Visual Representation
The results are displayed in a separate window beneath your main price chart. It provides two lines (traditionally colored Red and Blue) that fluctuate within a defined range. This allows traders to see at a glance whether the market is becoming overbought, oversold, or entering a new trend phase without cluttering the primary price candles.
//+------------------------------------------------------------------+
//| StochasticRSI.mq4 |
//| |
//| Stochastic RSI |
//| |
//| Algorithm taken from book |
//| "Cybernetics Analysis for Stock and Futures" |
//| by John F. Ehlers |
//| |
//| contact@mqlsoft.com |
//| http://www.mqlsoft.com/ |
//+------------------------------------------------------------------+
#property copyright "Coded by Witold Wozniak"
#property link "www.mqlsoft.com"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_level1 0
#property indicator_minimum -1
#property indicator_maximum 1
double StocRSI[];
double Trigger[];
double Value3[];
extern int RSILength = 8;
extern int StocLength = 8;
extern int WMALength = 8;
int buffers = 0;
int drawBegin = 0;
int init() {
drawBegin = RSILength;
initBuffer(StocRSI, "Stochastic RSI", DRAW_LINE);
initBuffer(Trigger, "Trigger", DRAW_LINE);
initBuffer(Value3);
IndicatorBuffers(buffers);
IndicatorShortName("Stochastic RSI [" + RSILength + ", " + StocLength + ", " + WMALength + "]");
return (0);
}
int start() {
if (Bars <= drawBegin) return (0);
int countedBars = IndicatorCounted();
if (countedBars < 0) return (-1);
if (countedBars > 0) countedBars--;
int s, limit = Bars - countedBars - 1;
for (s = limit; s >= 0; s--) {
double rsi = iRSI(0, 0, RSILength, MODE_CLOSE, s);
double hh = rsi, ll = rsi;
for (int i = 0; i < StocLength; i++) {
double tmp = iRSI(0, 0, RSILength, MODE_CLOSE, s + i);
hh = MathMax(hh, tmp);
ll = MathMin(ll, tmp);
}
double Value1 = rsi - ll;
double Value2 = hh - ll;
Value3[s] = 0.0;
if (Value2 != 0.0) {
Value3[s] = Value1 / Value2;
}
}
for (s = limit - 1; s >= 0; s--) {
StocRSI[s] = 2.0 * (iMAOnArray(Value3, 0, WMALength, 0, MODE_LWMA, s) - 0.5);
Trigger[s] = StocRSI[s + 1];
}
return (0);
}
void initBuffer(double array[], string label = "", int type = DRAW_NONE, int arrow = 0, int style = EMPTY, int width = EMPTY, color clr = CLR_NONE) {
SetIndexBuffer(buffers, array);
SetIndexLabel(buffers, label);
SetIndexEmptyValue(buffers, EMPTY_VALUE);
SetIndexDrawBegin(buffers, drawBegin);
SetIndexShift(buffers, 0);
SetIndexStyle(buffers, type, style, width);
SetIndexArrow(buffers, arrow);
buffers++;
}
Comments