sto_AllStochastics_v2_mtf
Download scriptThis script is a versatile technical analysis tool designed to provide a comprehensive view of market momentum and price trends. It functions as an advanced "Stochastic Oscillator" that gives users the unique ability to customize the underlying mathematical engine used to calculate price movement.
Core Functionality
At its heart, the script tracks where the current price sits relative to a specific high and low range over a set period. While a standard Stochastic indicator uses simple averages, this tool allows you to replace those basic averages with a wide variety of sophisticated "Moving Average" models.
Key Features
- Multi-Model Versatility: The script includes 18 different types of moving averages. This means instead of being locked into one way of calculating trends, you can choose from various methods—ranging from standard Simple Moving Averages (SMA) to complex options like Hull Moving Averages (HMA), Linear Regression Lines (LSMA), and Zero-Lag indicators.
- Dual-Layer Smoothing: The tool applies smoothing to both the primary "Stochastic" line and its "Signal" line. This allows you to refine the data to remove market noise while maintaining the responsiveness needed to catch timely trends.
- Multi-Timeframe Analysis: One of the script's advanced features is its ability to display data from different timeframes (e.g., viewing Hourly trends while looking at a Minute chart). It automatically handles the mathematical conversion required to align these different scales accurately on your screen.
- Dynamic Labeling: The tool automatically updates its display name to reflect exactly which timeframe, price type, and moving average models you have currently selected, making it easy to keep track of your settings during active trading.
How It Works for the User
- Input Selection: You define how many bars to look back (Period) and which specific mathematical model to use for both the main line and the signal line.
- Processing: The script scans the historical high and low prices, calculates the percentage position of the current price, and then passes that data through your chosen moving average "filters."
- Visual Output: The result is plotted in a separate window on your chart. You can look for instances where the lines cross or reach specific levels (like 30 or 70) to identify potential buying or selling opportunities based on your preferred technical logic.
//+------------------------------------------------------------------+
//| AllStochastics_v2.mq4 |
//| Copyright © 2007-08, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
// List of MAs:
// MA_Method= 0: SMA - Simple Moving Average
// MA_Method= 1: EMA - Exponential Moving Average
// MA_Method= 2: Wilder - Wilder Exponential Moving Average
// MA_Method= 3: LWMA - Linear Weighted Moving Average
// MA_Method= 4: SineWMA - Sine Weighted Moving Average
// MA_Method= 5: TriMA - Triangular Moving Average
// MA_Method= 6: LSMA - Least Square Moving Average (or EPMA, Linear Regression Line)
// MA_Method= 7: SMMA - Smoothed Moving Average
// MA_Method= 8: HMA - Hull Moving Average by Alan Hull
// MA_Method= 9: ZeroLagEMA - Zero-Lag Exponential Moving Average
// MA_Method=10: DEMA - Double Exponential Moving Average by Patrick Mulloy
// MA_Method=11: T3 - T3 by T.Tillson
// MA_Method=12: ITrend - Instantaneous Trendline by J.Ehlers
// MA_Method=13: Median - Moving Median
// MA_Method=14: GeoMean - Geometric Mean
// MA_Method=15: REMA - Regularized EMA by Chris Satchwell
// MA_Method=16: ILRS - Integral of Linear Regression Slope
// MA_Method=17: IE/2 - Combination of LSMA and ILRS
#property copyright "Copyright © 2007-08, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LightBlue
#property indicator_width1 2
#property indicator_color2 Orange
#property indicator_width2 1
#property indicator_style2 2
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 30
#property indicator_level2 70
//---- indicator parameters
extern int TimeFrame = 0;
extern int Sto_Period = 5;
extern int Smooth = 3;
extern int Signal = 3;
extern int SmoothMode = 0;
extern int SignalMode = 0;
extern int Price = 0;
//---- indicator buffers
double Sto[];
double Sig[];
//----
double tmp[][12];
int draw_begin, pBars, mcnt_bars;
string TF, fast_name, slow_name;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
draw_begin=Sto_Period+Smooth+Signal;
//---- indicator name
switch(SmoothMode)
{
case 1 : fast_name="EMA"; break;
case 2 : fast_name="Wilder"; break;
case 3 : fast_name="LWMA"; break;
case 4 : fast_name="SineWMA"; break;
case 5 : fast_name="TriMA"; break;
case 6 : fast_name="LSMA"; break;
case 7 : fast_name="SMMA"; break;
case 8 : fast_name="HMA"; break;
case 9 : fast_name="ZeroLagEMA"; break;
case 10: fast_name="DEMA"; break;
case 11: fast_name="T3"; break;
case 12: fast_name="InstTrend"; break;
case 13: fast_name="Median"; break;
case 14: fast_name="GeometricMean"; break;
case 15: fast_name="REMA"; break;
case 16: fast_name="ILRS"; break;
case 17: fast_name="IE/2"; break;
default: SmoothMode=0; fast_name="SMA";
}
switch(SignalMode)
{
case 1 : slow_name="EMA"; break;
case 2 : slow_name="Wilder"; break;
case 3 : slow_name="LWMA"; break;
case 4 : slow_name="SineWMA"; break;
case 5 : slow_name="TriMA"; break;
case 6 : slow_name="LSMA"; break;
case 7 : slow_name="SMMA"; break;
case 8 : slow_name="HMA"; break;
case 9 : slow_name="ZeroLagEMA"; break;
case 10: slow_name="DEMA"; break;
case 11: slow_name="T3"; break;
case 12: slow_name="InstTrend"; break;
case 13: slow_name="Median"; break;
case 14: slow_name="GeometricMean"; break;
case 15: slow_name="REMA"; break;
case 16: slow_name="ILRS"; break;
case 17: slow_name="IE/2"; break;
default: SignalMode=0; slow_name="SMA";
}
switch(TimeFrame)
{
case 1 : TF = "M1" ; break;
case 5 : TF = "M5" ; break;
case 15 : TF = "M15"; break;
case 30 : TF = "M30"; break;
case 60 : TF = "H1" ; break;
case 240 : TF = "H4" ; break;
case 1440 : TF = "D1" ; break;
case 10080 : TF = "W1" ; break;
case 43200 : TF = "MN1"; break;
default : TF = "Current";
}
IndicatorShortName("AllStochastics["+TF+"] ("+Price+","+Sto_Period+","+fast_name+"("+Smooth+"),"+slow_name+"("+Signal+"))");
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
SetIndexLabel(0,"Sto");
SetIndexLabel(1,"Signal");
//---- indicator buffers mapping
SetIndexBuffer(0,Sto);
SetIndexBuffer(1,Sig);
//---- initialization done
if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();
return(0);
}
//+------------------------------------------------------------------+
//| AllStochastics_v2 |
//+------------------------------------------------------------------+
int start()
{
int limit, y, i, shift, cnt_bars=IndicatorCounted();
double K[], mSto[], mSig[];
int mBars = iBars(NULL,TimeFrame);
if(mBars != pBars)
{
ArrayResize(K,mBars);
ArrayResize(mSto,mBars);
ArrayResize(mSig,mBars);
ArrayResize(tmp,mBars);
pBars = mBars;
}
if(cnt_bars<1)
{
for(i=1;i<=draw_begin;i++) {Sto[Bars-i]=0; Sig[Bars-i]=0;}
mcnt_bars = 1;
}
if(mcnt_bars > 1) mcnt_bars--;
for(y=mcnt_bars-1;y<mBars;y++)
{
shift = mBars-y-1;
double aPrice = iMA(NULL,TimeFrame,1,0,0,Price,shift);
double up = 0;
double dn = 10000000000;
for(i=0;i<Sto_Period;i++)
{
up = MathMax(up,iHigh(NULL,TimeFrame,shift+i));
dn = MathMin(dn,iLow(NULL,TimeFrame,shift+i));
}
if(up-dn > 0) K[y] = 100*(aPrice - dn)/(up - dn); else K[y] = 0;
switch(SmoothMode)
{
case 1 : mSto[y] = EMA(K[y],mSto,Smooth,y); break;
case 2 : mSto[y] = Wilder(K,mSto,Smooth,y); break;
case 3 : mSto[y] = LWMA(K,Smooth,y); break;
case 4 : mSto[y] = SineWMA(K,Smooth,y); break;
case 5 : mSto[y] = TriMA(K,Smooth,y); break;
case 6 : mSto[y] = LSMA(K,Smooth,y); break;
case 7 : mSto[y] = SMMA(K,mSto,Smooth,y); break;
case 8 : mSto[y] = HMA(K,Smooth,y); break;
case 9 : mSto[y] = ZeroLagEMA(K,mSto,Smooth,y); break;
case 10: mSto[y] = DEMA(0,K[y],Smooth,1,y); break;
case 11: mSto[y] = T3(0,K[y],Smooth,0.7,y); break;
case 12: mSto[y] = ITrend(K,mSto,Smooth,y); break;
case 13: mSto[y] = Median(K,Smooth,y); break;
case 14: mSto[y] = GeoMean(K,Smooth,y); break;
case 15: mSto[y] = REMA(K[y],mSto,Smooth,0.5,y); break;
case 16: mSto[y] = ILRS(K,Smooth,y); break;
case 17: mSto[y] = IE2(K,Smooth,y); break;
default: mSto[y] = SMA(K,Smooth,y); break;
}
switch(SignalMode)
{
case 1 : mSig[y] = EMA(mSto[y],mSig,Signal,y); break;
case 2 : mSig[y] = Wilder(mSto,mSig,Signal,y); break;
case 3 : mSig[y] = LWMA(mSto,Signal,y); break;
case 4 : mSig[y] = SineWMA(mSto,Signal,y); break;
case 5 : mSig[y] = TriMA(mSto,Signal,y); break;
case 6 : mSig[y] = LSMA(mSto,Signal,y); break;
case 7 : mSig[y] = SMMA(mSto,mSig,Signal,y); break;
case 8 : mSig[y] = HMA(mSto,Signal,y); break;
case 9 : mSig[y] = ZeroLagEMA(mSto,mSig,Signal,y); break;
case 10: mSig[y] = DEMA(6,mSto[y],Signal,1,y); break;
case 11: mSig[y] = T3(6,mSto[y],Signal,0.7,y); break;
case 12: mSig[y] = ITrend(mSto,mSig,Signal,y); break;
case 13: mSig[y] = Median(mSto,Signal,y); break;
case 14: mSig[y] = GeoMean(mSto,Signal,y); break;
case 15: mSig[y] = REMA(mSto[y],mSig,Signal,0.5,y); break;
case 16: mSig[y] = ILRS(mSto,Signal,y); break;
case 17: mSig[y] = IE2(mSto,Signal,y); break;
default: mSig[y] = SMA(mSto,Signal,y); break;
}
if(TimeFrame == Period()) {Sto[shift] = mSto[y]; Sig[shift] = mSig[y];}
}
mcnt_bars = mBars-1;
if(TimeFrame > Period())
{
if(cnt_bars>0) cnt_bars--;
limit = Bars-cnt_bars+TimeFrame/Period()-1;
for(shift=0,y=0;shift<limit;shift++)
{
if (Time[shift] < iTime(NULL,TimeFrame,y)) y++;
Sto[shift] = mSto[mBars-y-1];
Sig[shift] = mSig[mBars-y-1];
}
}
//---- done
return(0);
}
// MA_Method=0: SMA - Simple Moving Average
double SMA(double array[],int per,int bar)
{
double Sum = 0;
for(int i = 0;i < per;i++) Sum += array[bar-i];
//Print("SMA=",Sum/per);
return(Sum/per);
}
// MA_Method=1: EMA - Exponential Moving Average
double EMA(double price,double array[],int per,int bar)
{
if(bar == 2) double ema = price;
else
if(bar > 2) ema = array[bar-1] + 2.0/(1+per)*(price - array[bar-1]);
return(ema);
}
// MA_Method=2: Wilder - Wilder Exponential Moving Average
double Wilder(double array1[],double array2[],int per,int bar)
{
if(bar == per) double wilder = SMA(array1,per,bar);
else
if(bar > per) wilder = array2[bar-1] + (array1[bar] - array2[bar-1])/per;
return(wilder);
}
// MA_Method=3: LWMA - Linear Weighted Moving Average
double LWMA(double array[],int per,int bar)
{
double Sum = 0;
double Weight = 0;
for(int i = 0;i < per;i++)
{
Weight+= (per - i);
Sum += array[bar-i]*(per - i);
}
if(Weight>0) double lwma = Sum/Weight;
else lwma = 0;
return(lwma);
}
// MA_Method=4: SineWMA - Sine Weighted Moving Average
double SineWMA(double array[],int per,int bar)
{
double pi = 3.1415926535;
double Sum = 0;
double Weight = 0;
double del = 0.5*pi/per;
for(int i = 0;i < per;i++)
{
Weight+= MathSin(pi*(i+1)/(per+1));
Sum += array[bar-i]*MathSin(pi*(i+1)/(per+1));
}
if(Weight>0) double swma = Sum/Weight;
else swma = 0;
return(swma);
}
// MA_Method=5: TriMA - Triangular Moving Average
double TriMA(double array[],int per,int bar)
{
double sma[];
int len = MathCeil((per+1)*0.5);
ArrayResize(sma,len);
double sum=0;
for(int i = 0;i < len;i++)
{
sma[i] = SMA(array,len,bar-i);
sum +=sma[i];
}
double trima = sum/len;
return(trima);
}
// MA_Method=6: LSMA - Least Square Moving Average (or EPMA, Linear Regression Line)
double LSMA(double array[],int per,int bar)
{
double Sum=0;
for(int i=per; i>=1; i--) Sum += (i-(per+1)/3.0)*array[bar-per+i];
double lsma = Sum*6/(per*(per+1));
return(lsma);
}
// MA_Method=7: SMMA - Smoothed Moving Average
double SMMA(double array1[],double array2[],int per,int bar)
{
if(bar == per) double smma = SMA(array1,per,bar);
else
if(bar > per)
{
double Sum = 0;
for(int i = 0;i < per;i++) Sum += array1[bar-i-1];
smma = (Sum - array2[bar-1] + array1[bar])/per;
}
return(smma);
}
// MA_Method=8: HMA - Hull Moving Average by Alan Hull
double HMA(double array[],int per,int bar)
{
double tmp[];
int len = MathSqrt(per);
ArrayResize(tmp,len);
if(bar == per) double hma = array[bar];
else
if(bar > per)
{
for(int i = 0; i < len;i++) tmp[len-i-1] = 2*LWMA(array,per/2,bar-i) - LWMA(array,per,bar-i);
hma = LWMA(tmp,len,len-1);
}
return(hma);
}
// MA_Method=9: ZeroLagEMA - Zero-Lag Exponential Moving Average
double ZeroLagEMA(double array1[],double array2[],int per,int bar)
{
double alfa = 2.0/(1+per);
int lag = 0.5*(per - 1);
if(bar == lag) double zema = array1[bar];
else
if(bar > lag) zema = alfa*(2*array1[bar] - array1[bar-lag]) + (1-alfa)*array2[bar-1];
return(zema);
}
// MA_Method=10: DEMA - Double Exponential Moving Average by Patrick Mulloy
double DEMA(int num,double price,int per,double v,int bar)
{
if(bar == 2) {double dema = price; tmp[bar][num] = dema; tmp[bar][num+1] = dema;}
else
if(bar > 2)
{
tmp[bar][num] = tmp[bar-1][num] + 2.0/(1+per)*(price - tmp[bar-1][num]);
tmp[bar][num+1] = tmp[bar-1][num+1] + 2.0/(1+per)*(tmp[bar][num] - tmp[bar-1][num+1]);
dema = (1+v)*tmp[bar][num] - v*tmp[bar][num+1];
}
return(dema);
}
// MA_Method=11: T3 by T.Tillson
double T3(int num,double price,int per,double v,int bar)
{
if(bar == 2)
{
double T3 = price;
for(int k=0;k<=5;k++) tmp[bar][k] = T3;
}
else
if(bar > 2)
{
double dema1 = DEMA(num,price,per,v,bar);
double dema2 = DEMA(num+2,dema1,per,v,bar);
T3 = DEMA(num+4,dema2,per,v,bar);
}
return(T3);
}
// MA_Method=12: ITrend - Instantaneous Trendline by J.Ehlers
double ITrend(double price[],double array[],int per,int bar)
{
double alfa = 2.0/(per+1);
if(bar > 7)
double it = (alfa - alfa*alfa/4)*price[bar]+ 0.5*alfa*alfa*price[bar-1]-(alfa - 0.75*alfa*alfa)*price[bar-2]+
2*(1-alfa)*array[bar-1] - (1-alfa)*(1-alfa)*array[bar-2];
else
it = (price[bar] + 2*price[bar-1]+ price[bar-2])/4;
return(it);
}
// MA_Method=13: Median - Moving Median
double Median(double price[],int per,int bar)
{
double array[];
ArrayResize(array,per);
for(int i = 0; i < per;i++) array[i] = price[bar-i];
ArraySort(array);
int num = MathRound((per-1)/2);
if(MathMod(per,2)>0) double median = array[num]; else median = 0.5*(array[num]+array[num+1]);
return(median);
}
// MA_Method=14: GeoMean - Geometric Mean
double GeoMean(double price[],int per,int bar)
{
double gmean = MathPow(price[bar],1.0/per);
for(int i = 1; i < per;i++) gmean *= MathPow(price[bar-i],1.0/per);
return(gmean);
}
// MA_Method=15: REMA - Regularized EMA by Chris Satchwell
double REMA(double price,double array[],int per,double lambda,int bar)
{
double alpha = 2.0/(per + 1);
if(bar <= 3) double rema = price;
else
if(bar > 3)
rema = (array[bar-1]*(1+2*lambda) + alpha*(price - array[bar-1]) - lambda*array[bar-2])/(1+lambda);
return(rema);
}
// MA_Method=16: ILRS - Integral of Linear Regression Slope
double ILRS(double price[],int per,int bar)
{
double sum = per*(per-1)*0.5;
double sum2 = (per-1)*per*(2*per-1)/6.0;
double sum1 = 0;
double sumy = 0;
for(int i=0;i<per;i++)
{
sum1 += i*price[bar-i];
sumy += price[bar-i];
}
double num1 = per*sum1 - sum*sumy;
double num2 = sum*sum - per*sum2;
if(num2 != 0) double slope = num1/num2; else slope = 0;
double ilrs = slope + SMA(price,per,bar);
return(ilrs);
}
// MA_Method=17: IE/2 - Combination of LSMA and ILRS
double IE2(double price[],int per,int bar)
{
double ie = 0.5*(ILRS(price,per,bar) + LSMA(price,per,bar));
return(ie);
}
Comments