Stochastic_5M+15M
Download scriptThis script is a multi-timeframe technical analysis tool designed to help traders identify potential momentum shifts by comparing the price action of two different time periods simultaneously. Specifically, it tracks and visualizes the relationship between the 5-minute (5M) and 15-minute (15M) Stochastic oscillators.
Core Logic and Functionality
Multi-Timeframe Comparison The primary logic of this tool is to overlay the "speed" of a shorter timeframe (5 minutes) against a longer, more stable timeframe (15 minutes). By doing this, it allows a trader to see if the immediate price momentum is moving faster or slower than the broader trend.
Data Visualization The indicator processes data in several ways to make it easy to read on a chart:
- 15-Minute Baseline: The script identifies specific 15-minute intervals and plots the Stochastic value for those points. This acts as a steady reference line.
- 5-Minute Momentum: It continuously plots the 5-minute Stochastic values, allowing you to see the "heartbeat" of the current price action.
- Visual Cues (Arrows): The script automatically places visual markers (arrows) on the chart when the 5-minute Stochastic value crosses over or under the 15-minute Stochastic level. An upward arrow suggests that shorter-term momentum is breaking above the 15-minute trend, while a downward arrow suggests it is dropping below it.
Alert System Beyond just visual aids, the script includes an automated notification system. It monitors the live price feed for "crosses." When the 5-minute line officially moves from below to above (or vice versa) the 15-minute reference point, the script can trigger an audible alert. This ensures that traders do not have to watch the screen constantly to catch a momentum shift.
Summary of User Experience
For a non-programmer, this tool simplifies complex calculations into a clear visual summary:
- Identify Trend: Look at the 15-minute reference point to understand the broader context.
- Spot Momentum: Watch the 5-minute line for immediate movement.
- Execute on Crosses: Use the automatically generated arrows and audio alerts as signals for when short-term momentum is potentially overtaking the larger trend, providing a more nuanced view of market dynamics than using a single timeframe alone.
//+------------------------------------------------------------------+
//| Stochastic_5M+15M.mq4 |
//| Zen_Leow |
//| |
//+------------------------------------------------------------------+
#property copyright "Zen_Leow"
#property link ""
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Yellow
#property indicator_width1 1
#property indicator_width2 1
#property indicator_level1 80
#property indicator_level2 20
extern string _ = "Stochastic Inputs";
extern int Timeframe1 = PERIOD_M5;
extern int K_Period1 = 14;
extern int D_Period1 = 1;
extern int Slowing1 = 1;
extern string PriceField_Setting1 = "0 - Low/High or 1 - Close/Close";
extern int PriceField1 = 0;
extern string MA_Method_Setting1 = "0 - SMA, 1 - EMA, 2 - SMMA, 3 - LWMA";
extern int MA_Method1 = 0;
extern int Timeframe2 = PERIOD_M15;
extern int K_Period2 = 14;
extern int D_Period2 = 3;
extern int Slowing2 = 3;
extern string PriceField_Setting2 = "0 - Low/High or 1 - Close/Close";
extern int PriceField2 = 0;
extern string MA_Method_Setting2 = "0 - SMA, 1 - EMA, 2 - SMMA, 3 - LWMA";
extern int MA_Method2 = 0;
extern bool AudioAlertActive = true;
int ArrowCode = 251; // This is the wingdings code for the TF15 Stochastic symbol
int ArrowDist = 10;
double TF1Buffer[];
double TF2Buffer[];
double UpBuffer[];
double DownBuffer[];
double LastClosed15MStoch;
datetime TimeOfCrossConfirmation;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(4);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2, TF1Buffer);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,ArrowCode);
SetIndexBuffer(3, TF2Buffer);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexBuffer(0, UpBuffer);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexBuffer(1, DownBuffer);
//---- name for DataWindow and indicator subwindow label
string short_name="Stoch_5M+15M("+K_Period1+","+D_Period1+","+Slowing1+"--"+K_Period2+","+D_Period2+","+Slowing2+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"5M_Stoch");
SetIndexLabel(1,"15M_Stoch");
//---- Set Start point of drawing
int draw_begin1=K_Period1+Slowing1;
int draw_begin2=K_Period2+Slowing2;
SetIndexDrawBegin(2,draw_begin1);
SetIndexDrawBegin(3,draw_begin1);
SetIndexDrawBegin(0,draw_begin1);
SetIndexDrawBegin(1,draw_begin2);
//----
// Alert("Done");
LastClosed15MStoch = EMPTY_VALUE;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
PlotTimeFrame2Graph();
PlotTimeFrame1Graph();
PlotTimeFrame2Graph();
if (Time[0] > TimeOfCrossConfirmation && AudioAlertActive)
{
CheckForAlerts();
}
return(0);
}
double GetStochasticValueTF1(int index)
{
return (iStochastic( Symbol(), Timeframe1, K_Period1, D_Period1, Slowing1, MA_Method1, PriceField1, 0, index));
}
double GetStochasticValueTF2(int index)
{
return (iStochastic( Symbol(), Timeframe2, K_Period2, D_Period2, Slowing2, MA_Method2, PriceField2, 0, index));
}
// Draw the 15M Stochastic
// For this I count from bar 0 onwards.
void PlotTimeFrame2Graph()
{
int i=0; // Bar index
int x=0; // Timeframe2 index
int Counted_bars; // Number of counted bars
//--------------------------------------------------------------------
Counted_bars=IndicatorCounted(); // Number of counted bars
while(i<=Bars-Counted_bars-1) // Loop for uncounted bars
{
if (MathMod(TimeMinute(Time[i]),Timeframe2)==0)
{
TF2Buffer[i]=GetStochasticValueTF2(x);
x++;
}
else
{
TF2Buffer[i]=EMPTY_VALUE;
}
i++; // Calculating index of the next bar
}
}
// Draws 5M Stochastic and Up down arrows when cross occurs
void PlotTimeFrame1Graph()
{
int i; // Bar index
int Counted_bars; // Number of counted bars
//--------------------------------------------------------------------
Counted_bars=IndicatorCounted(); // Number of counted bars
i=Bars-Counted_bars-1; // Index of the first uncounted
while(i>=0) // Loop for uncounted bars
{
TF1Buffer[i]=GetStochasticValueTF1(i);
if (TF2Buffer[i+1]!=EMPTY_VALUE)
{
LastClosed15MStoch = TF2Buffer[i+1];
}
if (TF1Buffer[i+1] > LastClosed15MStoch)
{
if (TF1Buffer[i+2] < LastClosed15MStoch)
{
UpBuffer[i+1] = TF1Buffer[i+1] - ArrowDist;
if (UpBuffer[i+1] > 100)
{
UpBuffer[i+1] = 100;
}
if (UpBuffer[i+1] < 0)
{
UpBuffer[i+1] = 0;
}
}
else
{
UpBuffer[i+1] = EMPTY_VALUE;
}
}
else
{
UpBuffer[i+1] = EMPTY_VALUE;
}
if (TF1Buffer[i+1] < LastClosed15MStoch)
{
if (TF1Buffer[i+2] > LastClosed15MStoch)
{
DownBuffer[i+1] = TF1Buffer[i+1] + ArrowDist;
if (DownBuffer[i+1] > 100)
{
DownBuffer[i+1] = 100;
}
if (DownBuffer[i+1] < 0)
{
DownBuffer[i+1] = 0;
}
}
else
{
DownBuffer[i+1] = EMPTY_VALUE;
}
}
else
{
DownBuffer[i+1] = EMPTY_VALUE;
}
i--; // Calculating index of the next bar
}
}
// Codes here for the alert are independent from the visual indicator.
void CheckForAlerts()
{
if(GetStochasticValueTF1(1) > GetStochasticValueTF2(1))
{
if(GetStochasticValueTF1(2) < GetStochasticValueTF2(1)) // Confirmed there was a cross
{
TimeOfCrossConfirmation = TimeCurrent();
Alert("5M Stoch has crossed ABOVE 15M Stoch!!!");
}
}
if(GetStochasticValueTF1(1) < GetStochasticValueTF2(1))
{
if(GetStochasticValueTF1(2) > GetStochasticValueTF2(1)) // Confirmed there was a cross
{
TimeOfCrossConfirmation = TimeCurrent();
Alert("5M Stoch has crossed BELOW 15M Stoch!!!");
}
}
}
//+------------------------------------------------------------------+
Comments