StochasticStack
Download scriptThe Stochastic Stack is a specialized technical analysis tool designed to provide a multi-layered view of market momentum. Instead of showing a single line that tracks price movement, this script overlays multiple "Stochastic" indicators simultaneously on a single window, allowing you to see the relationship between short-term and long-term trends at a glance.
Core Logic and Functionality
Multi-Layered Analysis The primary logic of this tool is based on the concept of "stacking." It calculates up to eight different Stochastic lines at once. Each line represents a different timeframe of momentum:
- Short-Term Trends: The faster-moving lines (with smaller periods) react quickly to immediate price changes.
- Long-Term Trends: The slower-moving lines (with larger periods) smooth out the "noise" of the market to show the broader direction of the trend.
Dynamic Period Scaling The script features an intelligent calculation system for its timeframes. You can define a base period and a multiplier. The tool automatically scales the subsequent lines based on this multiplier. This ensures that the gap between the "fast" data and the "slow" data remains mathematically consistent, providing a balanced visual hierarchy of the market's movement.
Signal vs. Main Mode The script includes a toggle to switch between two different types of readings:
- Main Mode: Tracks the primary price momentum.
- Signal Mode: Tracks the "signal" line, which is often used by traders to identify specific turning points or crossovers in price action.
Visual Organization To keep the chart clean and readable, the tool handles all the heavy lifting of data calculation behind the scenes. It maps each of the eight potential lines into a separate buffer, meaning you can see them as individual distinct colors on your screen. This allows you to observe "confluence"—when multiple lines move in the same direction simultaneously—which is often interpreted by traders as a stronger confirmation of a price move.
Summary for the User
In simple terms, this script takes a complex set of market data and organizes it into a visual "stack." It helps you see not just what the price is doing right now, but how that movement aligns with the broader history of the market. By looking at where these lines cluster together or diverge, you can better identify whether a current move is a quick spike or part of a larger, sustained trend.
//+------------------------------------------------------------------+
//| Copyright ? 2019, Ivan Kornilov|
//| StochasticStack.mq4|
//+------------------------------------------------------------------+
#property copyright "Copyright ? 2009, Ivan Kornilov. All rights reserved."
#property link "excelf@gmail.com"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 8
#property indicator_level1 20
#property indicator_level2 80
#property indicator_levelcolor LightSlateGray
#property indicator_color8 Silver
#property indicator_color7 MediumPurple
#property indicator_color6 DodgerBlue
#property indicator_color5 Aqua
#property indicator_color4 Lime
#property indicator_color3 Gold
#property indicator_color2 Orange
#property indicator_color1 Red
extern double nextPriod = 1.3;
extern int kPeriod1 = 7;
int dPeriod1 = 0;
int slowing1 = 0;
extern int kPeriod2 = 21;
int dPeriod2 = 0;
int slowing2 = 0;
extern int kPeriod3 = 34;
int dPeriod3 = 0;
int slowing3 = 0;
extern int kPeriod4 = 55;
int dPeriod4 = 0;
int slowing4 = 0;
extern int kPeriod5 = 72;
int dPeriod5 = 0;
int slowing5 = 0;
extern int kPeriod6 = 89;
int dPeriod6 = 0;
int slowing6 = 0;
extern int kPeriod7 = 116;
int dPeriod7 = 0;
int slowing7 = 0;
extern int kPeriod8 = 144;
int dPeriod8 = 0;
int slowing8 = 0;
extern bool modeSignal = true;
extern int countLine = 8;
double lineBuffer0[];
double lineBuffer1[];
double lineBuffer2[];
double lineBuffer3[];
double lineBuffer4[];
double lineBuffer5[];
double lineBuffer6[];
double lineBuffer7[];
#define maxBuffer 8
int init() {
if(countLine > maxBuffer) {
countLine = maxBuffer;
} else if(countLine < 1) {
countLine = 1;
}
if(nextPriod != 0) {
kPeriod2 = kPeriod1 * nextPriod;
kPeriod3 = kPeriod2 * nextPriod;
kPeriod4 = kPeriod3 * nextPriod;
kPeriod5 = kPeriod4 * nextPriod;
kPeriod6 = kPeriod5 * nextPriod;
kPeriod7 = kPeriod6 * nextPriod;
kPeriod8 = kPeriod7 * nextPriod;
}
if(dPeriod1 == 0 && slowing1 == 0) {
dPeriod1 = kPeriod1 / 2;
slowing1 = dPeriod1 / 2;
}
if(dPeriod2 == 0 && slowing2 == 0) {
dPeriod2 = kPeriod2 / 2;
slowing2 = dPeriod2 / 2;
}
if(dPeriod3 == 0 && slowing3 == 0) {
dPeriod3 = kPeriod3 / 2;
slowing3 = dPeriod3 / 2;
}
if(dPeriod4 == 0 && slowing4 == 0) {
dPeriod4 = kPeriod4 / 2;
slowing4 = dPeriod4 / 2;
}
if(dPeriod5 == 0 && slowing5 == 0) {
dPeriod5 = kPeriod5 / 2;
slowing5 = dPeriod5 / 2;
}
if(dPeriod6 == 0 && slowing6 == 0) {
dPeriod6 = kPeriod6 / 2;
slowing6 = dPeriod6 / 2;
}
if(dPeriod7 == 0 && slowing7 == 0) {
dPeriod7 = kPeriod7 / 2;
slowing7 = dPeriod7 / 2;
}
if(dPeriod8 == 0 && slowing8 == 0) {
dPeriod8 = kPeriod8 / 2;
slowing8 = dPeriod8 / 2;
}
string indicatorName = "Stochastic Stack(";
SetIndexBuffer(0, lineBuffer0);
SetIndexStyle(0, DRAW_LINE);
indicatorName = indicatorName + kPeriod1;
if(countLine > 1) {
SetIndexBuffer(1, lineBuffer1);
SetIndexStyle(1, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod2;
}
if(countLine > 2) {
SetIndexBuffer(2, lineBuffer2);
SetIndexStyle(2, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod3;
}
if(countLine > 3) {
SetIndexBuffer(3, lineBuffer3);
SetIndexStyle(3, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod4;
}
if(countLine > 4) {
SetIndexBuffer(4, lineBuffer4);
SetIndexStyle(4, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod5;
}
if(countLine > 5) {
SetIndexBuffer(5, lineBuffer5);
SetIndexStyle(5, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod6;
}
if(countLine > 6) {
SetIndexBuffer(6, lineBuffer6);
SetIndexStyle(6, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod7;
}
if(countLine > 7) {
SetIndexBuffer(7, lineBuffer7);
SetIndexStyle(7, DRAW_LINE);
indicatorName = indicatorName + "," + kPeriod8;
}
indicatorName = indicatorName + ")";
IndicatorShortName(indicatorName);
}
int start(){
int indicatorCounted = IndicatorCounted();
if (indicatorCounted < 0) {
return (-1);
}
if(indicatorCounted > 0) {
indicatorCounted--;
}
int stochasticBuffer;
if(modeSignal) {
stochasticBuffer = MODE_SIGNAL;
} else {
stochasticBuffer = MODE_MAIN;
}
int limit = Bars - indicatorCounted;
for(int i = limit - 1; i >= 0; i--) {
lineBuffer0[i] = iStochastic(NULL, 0, kPeriod1, dPeriod1, slowing1, MODE_EMA, 1, stochasticBuffer, i);
if(countLine > 1) {
lineBuffer1[i] = iStochastic(NULL, 0, kPeriod2, dPeriod2, slowing2, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 2) {
lineBuffer2[i] = iStochastic(NULL, 0, kPeriod3, dPeriod3, slowing3, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 3) {
lineBuffer3[i] = iStochastic(NULL, 0, kPeriod4, dPeriod4, slowing4, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 4) {
lineBuffer4[i] = iStochastic(NULL, 0, kPeriod5, dPeriod5, slowing5, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 5) {
lineBuffer5[i] = iStochastic(NULL, 0, kPeriod6, dPeriod6, slowing6, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 6) {
lineBuffer6[i] = iStochastic(NULL, 0, kPeriod7, dPeriod7, slowing7, MODE_EMA, 1, stochasticBuffer, i);
}
if(countLine > 7) {
lineBuffer7[i] = iStochastic(NULL, 0, kPeriod8, dPeriod8, slowing8, MODE_EMA, 1, stochasticBuffer, i);
}
}
}
Comments