MTF_Stochastic_SmL_v1
Download scriptThis tool is designed to enhance your trading charts by providing a Multi-Timeframe (MTF) Stochastic Oscillator. While standard indicators typically only show data based on the specific timeframe you are currently viewing, this script allows you to view the momentum and price strength of a different timeframe simultaneously.
How the Logic Works
Multi-Timeframe Integration The core purpose of this script is "time jumping." It fetches Stochastic data from a higher or lower timeframe (such as viewing Hourly data while on a 5-minute chart) and projects it onto your current screen. This helps traders identify broader market trends without having to constantly switch back and forth between different windows.
Dynamic Data Retrieval The script identifies the specific point in time for the chosen timeframe and pulls the corresponding Stochastic values. It specifically tracks two main lines:
- The Main Line: Represents the primary momentum of the price.
- The Signal Line: Acts as a smoothed average to help identify potential turning points or crossovers.
Linear Interpolation (Smoothing) One of the most sophisticated parts of this logic is how it handles the "gap" between timeframes. Because a 4-hour candle covers many smaller 5-minute candles, simply plotting the data can result in "steppy" or jagged lines where the value only changes once every few bars.
To solve this, the script uses Linear Interpolation. It calculates the mathematical distance between two points of data from the higher timeframe and creates a smooth transition for all the smaller bars in between. This ensures that the indicator moves fluidly across your chart rather than jumping abruptly, making it much easier to read visual trends and identify precise entry or exit levels.
Key Visual Elements
- Color Coding: The script displays two distinct lines (typically Light Sea Green and Red) to distinguish between momentum and its signal.
- Reference Levels: It utilizes the standard 80 (Overbought) and 20 (Oversold) boundaries to help you identify when the market may be reaching an extreme point.
//+------------------------------------------------------------------+
//| MTF Stochastic_SmL.mq4 |
//| smooz line 2007, Christof Risch (iya) |
//| Stochastic indicator from any timeframe |
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?t=30109"
#property indicator_separate_window
//----
#property indicator_buffers 2
#property indicator_color1 LightSeaGreen
#property indicator_color2 Red
#property indicator_level1 80
#property indicator_level2 20
#property indicator_maximum 100
#property indicator_minimum 0
//---- input parameters
extern int TimeFrame =0,
KPeriod =5,
DPeriod =3,
Slowing =3,
MAMethod =0,
PriceField =0;
//---- indicator buffers
double Buffer1[],
Buffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator lines
SetIndexBuffer(0,Buffer1);
SetIndexBuffer(1,Buffer2);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
//---- name for DataWindow and indicator subwindow label
string TimeFrameStr;
switch(TimeFrame)
{
case 1: TimeFrameStr="Period M1"; break;
case 5: TimeFrameStr="Period M5"; break;
case 15: TimeFrameStr="Period M15"; break;
case 30: TimeFrameStr="Period M30"; break;
case 60: TimeFrameStr="Period H1"; break;
case 240: TimeFrameStr="Period H4"; break;
case 1440: TimeFrameStr="Period D1"; break;
case 10080: TimeFrameStr="Period W1"; break;
case 43200: TimeFrameStr="Period MN1"; break;
default: TimeFrameStr="Current Timeframe";
}
IndicatorShortName(TimeFrameStr+" Stoch("+KPeriod+","+DPeriod+","+Slowing+")");
}
//+------------------------------------------------------------------+
//| MTF Stochastic |
//+------------------------------------------------------------------+
int start()
{
for(int i=Bars-1-IndicatorCounted(); i>=0; i--)
{
int shift1=iBarShift(NULL,TimeFrame,Time[i]),
time1 =iTime (NULL,TimeFrame,shift1),
shift2=iBarShift(NULL,0,time1);
//----
Buffer1[shift2]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,shift1);
Buffer2[shift2]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,1,shift1);
//----
// linear interpolation for indicators from a higher timeframe
if(TimeFrame<=Period())
continue;
//----
// current candle
if(shift1==0)
for(int n=1; shift2-n>=0; n++)
{
Buffer1[shift2-n]=Buffer1[shift2];
Buffer2[shift2-n]=Buffer2[shift2];
}
// count number of intermediate bars
for(n=1; shift2+n < Bars && Time[shift2+n] > iTime(NULL,TimeFrame,iBarShift(NULL,TimeFrame,Time[shift2+n],true)); n++)
continue;
//----
// apply interpolation
double factor=1.0/n;
for(int k=1; k < n; k++)
{
Buffer1[shift2+k]=k*factor*Buffer1[shift2+n] + (1.0-k*factor)*Buffer1[shift2];
Buffer2[shift2+k]=k*factor*Buffer2[shift2+n] + (1.0-k*factor)*Buffer2[shift2];
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments