MTF_Stochastic_SmL
Download scriptThis tool is a specialized technical analysis indicator designed for the MetaTrader 4 platform. Its primary purpose is to allow traders to view Stochastic Oscillator data from any specific timeframe (such as Daily, Weekly, or Hourly) directly on their current chart, regardless of which timeframe they are presently viewing.
Core Functionality
The script addresses a common challenge in trading: the difficulty of monitoring high-level market trends while looking at low-level price action. Instead of forcing the user to constantly switch between different charts to see "the big picture," this indicator pulls that higher-timeframe data into the current view.
Key Logic Components
1. Multi-Timeframe Integration The logic identifies the specific timeframe the user wants to monitor (e.g., viewing a Daily Stochastic on a 15-minute chart). It automatically syncs the data points from that larger timeframe so they align correctly with the smaller price candles on your active screen.
2. Signal Calculation The indicator calculates two primary lines—typically used to identify overbought or oversold conditions:
- Main Line: Represents the primary momentum of the market.
- Signal Line: Acts as a moving average of the main line, helping to smooth out fluctuations and identify potential trend reversals.
3. Linear Interpolation (Smoothing) One of the most sophisticated features of this script is its "interpolation" logic. When viewing a higher timeframe on a lower timeframe chart, data points can often appear "blocky" or jump abruptly because one large candle represents many small ones.
- To solve this, the script calculates the gaps between these large data points.
- It then mathematically distributes the values across the intermediate candles.
- This creates a smooth, continuous line that flows naturally across your chart, providing a much more fluid visual representation of market momentum than standard multi-timeframe tools.
User Benefits
By using this logic, traders can keep their eyes on one single chart while simultaneously observing broader market trends. The interpolation ensures that the transition between different timeframes feels organic rather than erratic, making it easier to spot high-probability entry and exit points based on combined momentum data.
//+------------------------------------------------------------------+
//| MTF Stochastic_SmL.mq4 |
//| smooz line 2007, Christof Risch (iya) |
//| Stochastic indicator from any timeframe. FF |
//+------------------------------------------------------------------+
#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, // {1=M1, 5=M5, 15=M15, ..., 1440=D1, 10080=W1, 43200=MN1}
KPeriod = 5,
DPeriod = 3,
Slowing = 3,
MAMethod = 0, // {0=SMA, 1=EMA, 2=SMMA, 3=LWMA}
PriceField = 0; // {0=Hi/Low, 1=Close/Close}
//---- 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