StocRSI 2_v2
Download scriptThis indicator is designed to provide a visual representation of market momentum by combining three distinct mathematical concepts: price movement analysis, relative strength, and smoothed stochastic oscillations. Rather than looking at just one price point, it analyzes the history of price changes to determine whether a trend is gaining strength or losing steam.
How the Logic Works
1. Price Movement Analysis The script begins by examining a specific window of recent price candles. It calculates two primary components:
- Upward Movement: The sum of all positive price increases during this period.
- Downward Movement: The sum of all negative price decreases during this period.
By averaging these movements, the indicator establishes a baseline for the current direction of the market.
2. Relative Strength Calculation Once the upward and downward movements are identified, the script converts them into a ratio. This represents the "Relative Strength" of the current price action—essentially determining what percentage of the total movement is moving in one direction versus the other. This value is then smoothed over time to prevent erratic jumps caused by minor market noise.
3. Stochastic Smoothing (The Final Output) To make the indicator more useful for visual trading, the script applies a "Stochastic" logic to the Relative Strength values calculated in the previous steps. It identifies the highest and lowest Relative Strength points over a set timeframe and calculates where the current value sits between those extremes.
Finally, the script performs one last smoothing pass. This ensures that the resulting line is fluid and less reactive to "fake-outs," allowing for a clearer view of the underlying momentum.
Summary of Behavior
The final output is a line that oscillates based on the balance of buying and selling pressure. When the values are high, it suggests strong upward momentum; when low, it suggests downward momentum. Because it uses three layers of calculation—Price Change $\rightarrow$ Relative Strength $\rightarrow$ Stochastic Smoothing—it aims to provide a more refined view of market trends than a standard indicator would offer on its own.
//+------------------------------------------------------------------+
//| StocRSI 2.mq4 |
//| Emerald King |
//| mailto:info@emerald-king.com |
//+------------------------------------------------------------------+
#property copyright "Emerald King"
#property link "mailto:info@emerald-king.com"
//----
#property indicator_separate_window
#property indicator_color1 Red
//---- input parameters
extern int Len1=8;
extern int Len2=8;
extern int Len3=5;
extern int CountBars=950;
//----
double CU=0;
double CD=0;
double CUAve=0;
double CDAve=0;
double CUAveB=0;
double CDAveB=0;
double Count=0;
double RSI0[1000];
double HiR=0;
double LoR=0;
double StocR=0;
double StocRSI=0;
double Price=0;
int x=0;
double TempA=0;
double TempB=0;
double TempC=0;
int Lookback=0;
//----
double val1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
Lookback=MathMax(Len1, Len2);
Lookback=MathMax(Lookback, Len3);
SetIndexBuffer(0,val1);
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (CountBars>Bars) CountBars=Bars;
SetIndexDrawBegin(0,Bars-CountBars+Lookback);
int i;
int counted_bars=IndicatorCounted();
//----
if(CountBars<=Lookback) return(0);
//---- initial zero
if(counted_bars<1)
{
for(i=1;i<=Lookback;i++) val1[CountBars-i]=0.0;
}
i=CountBars-Lookback-1;
while(i>=0)
{
CU=0;
CD=0;
CUAveB=CUAve;
CDAveB=CDAve;
//----
for(x=i; x<i+Len1-1;x++)
{
if (Close[x] > Close[x + 1]) CU=CU + (Close[x] - Close[x + 1]);
if (Close[x] < Close[x + 1]) CD=CD + (Close[x + 1] - Close[x]);
}
CU=CU/Len1;
CD=CD/Len1;
CUAve=CU;
CDAve=CD;
//----
if ((CUAve + CDAve)!=0) RSI0[i]=CUAve/(CUAve+ CDAve);
if (i > Lookback)
{
CUAve=(CUAveB *(Len1 - 1) + CU)/Len1;
CDAve=(CDAveB *(Len1 - 1) + CD)/Len1;
}
if ((CUAve + CDAve)!=0) RSI0[i]=CUAve/(CUAve+ CDAve);
//----
for(x=i; x<i+Len2-1; x++)
{
if(x==i )
{
TempA=RSI0[x];
TempB=RSI0[x];
}
if(RSI0[x] > TempA )
TempA=RSI0[x];
if(RSI0[x] < TempB )
TempB=RSI0[x];
}
HiR=TempA;
LoR=TempB;
//----
if (HiR!=LoR) StocR=(RSI0[i] - LoR)/ (HiR - LoR);
//----
StocRSI=0;
TempC=0;
//----
for(x=i; x<i+Len3-1;x++)
{
StocRSI=StocRSI + (Len3 - x) * StocR;
TempC=TempC + (x+1);
}
if(TempC!=0)StocRSI=StocRSI/TempC;
if(StocRSI > 0 )
val1[i]=val1[i+1];
else
val1[i]=StocRSI;
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments