#MTF Stochastic_ks01

Download script
#MTF Stochastic_ks01

This script is designed for traders using the MetaTrader 4 platform who want to visualize high-level market trends without switching between different charts. Specifically, it enhances the standard "Stochastic Oscillator"—a tool used to determine if a market is overbought or oversold—by allowing users to view data from a higher timeframe while staying on their current chart.

Core Functionality

The primary purpose of this tool is Multi-Timeframe (MTF) Analysis. Normally, if you are looking at a 15-minute chart, you can only see 15-minute data. This script breaks that limitation by pulling the Stochastic values from much larger periods (such as Daily, Weekly, or Monthly charts) and projecting them onto your smaller timeframe chart.

How the Logic Works

To ensure the visual data is accurate and easy to read, the script employs three main logical steps:

1. Data Retrieval The tool looks at the specific "TimeFrame" you choose in the settings. It identifies where the current candle on your small chart aligns with the larger timeframe's history. It then fetches the two standard lines of the Stochastic Oscillator (typically a fast-moving line and a slower moving average) from that larger timeframe.

2. Handling Different Time Scales Since one "candle" on a Daily chart covers many "candles" on a 15-minute chart, simply placing the data point would result in jagged, disconnected steps or blocks. To solve this, the script calculates exactly how many smaller candles exist within a single larger timeframe period.

3. Linear Interpolation (Smoothing) This is the most sophisticated part of the logic. Instead of showing "steps" (where the line stays flat and then jumps), the script performs Linear Interpolation. It mathematically distributes the value between the start and the end of the large timeframe period.

  • The Result: The indicator draws a smooth, diagonal line connecting the values. This gives you a fluid visual representation of how the higher timeframe trend is evolving in real-time relative to your current view.

Summary for the User

By using this script, you can see the "Big Picture" (e.g., the Daily trend) overlaid directly on your "Small Picture" (e.g., the 5-minute execution chart). It removes the need to constantly flip back and forth between windows, providing a more cohesive view of market momentum and price strength.

Price Data Components
Series array that contains open time of each bar
Indicators Used
Stochastic oscillator
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
#MTF Stochastic_ks01
//+------------------------------------------------------------------+
//|                                               MTF Stochastic.mq4 |
//|													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,		// {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 >= 10; i--)
	for(int i = Bars-1-IndicatorCounted(); i >= 0; i--)
	{
		int shift1 = iBarShift(NULL,TimeFrame,Time[i]),
			 time1  = iTime    (NULL,TimeFrame,shift1),
			 time2  = iTime    (NULL,TimeFrame,shift1-1),
			 shift2 = iBarShift(NULL,0,time1),
			 shift3 = iBarShift(NULL,0,time2);
			 
		int n;
		double factor;

      if (shift1 == 0)
      {
			Buffer1[0] = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,0);
			Buffer2[0] = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,1,0);
		}
		else
		{		
       int shift4 = shift3 + 1;
    
		Buffer1[shift4] = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,shift1);
		Buffer2[shift4] = 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
	if(shift1==0)
		{	
		  int time4  = iTime    (NULL,TimeFrame,0);
		  int shift5 = iBarShift(NULL,0,time4);
		  n = shift5 +1;

			Buffer1[1] = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,0);
			Buffer2[1] = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,1,0);
	  
		    
		  factor = 1.0 / n;
		  
		  for(int k = 1; k < n ; k++)
		   {
  		    Buffer1[k] = k*factor*Buffer1[n] + (1.0-k*factor)*Buffer1[1];
		    Buffer2[k] = k*factor*Buffer2[n] + (1.0-k*factor)*Buffer2[1];
		    }
         			
		 }
	else
	  {

//		for(int n = 1; shift2+n < Bars && Time[shift2+n] > iTime(NULL,TimeFrame,iBarShift(NULL,TimeFrame,Time[shift2+n],true)); n++)
//			continue;

      n = shift2 - shift3;

//      shift3 = shift2 + n -1;
	//----
	//	apply interpolation
		factor = 1.0 / n;
		for( k = 1; k < n; k++)
		{
			Buffer1[shift4+k] = k*factor*Buffer1[shift4+n] + (1.0-k*factor)*Buffer1[shift4];
			Buffer2[shift4+k] = k*factor*Buffer2[shift4+n] + (1.0-k*factor)*Buffer2[shift4];
		}
      }
	}


	return(0);
}


Comments