This script is designed to function as a "Fast Stochastic" technical indicator. Its primary purpose is to help traders gauge the momentum of a market by identifying where the current price sits relative to its recent high and low range. Instead of looking at just one single point in time, it analyzes a specific window of historical data to determine if a market move is overextended or gaining strength.
Core Logic and Workflow
The indicator operates through a multi-step calculation process to transform raw price data into two visual lines on a separate chart window:
1. Identifying Extremes (Highs and Lows) The script first looks back at a specific number of previous candles (defined by the "KPeriod" setting). For every point in time, it scans this look-back period to find the absolute highest price reached and the absolute lowest price recorded. It stores these two values as the boundaries for the current move.
2. Calculating the Main Line (%K) Once the highs and lows are established, the script determines where the current closing price sits between those two extremes.
- If the price is near the recent high, the value moves toward 100.
- If the price is near the recent low, the value moves toward 0. The result is a percentage-based value that represents the "relative position" of the current price. This creates the primary moving line on your chart.
3. Generating the Signal Line To smooth out the fluctuations of the main line and make it easier to read, the script performs a secondary calculation. It takes the results from the Main Line and calculates a Simple Moving Average (SMA) based on a second time frame (the "DPeriod" setting). This creates a secondary "Signal" line.
How it is Visualized
The indicator provides three distinct visual components to assist in analysis:
- The Main Buffer: A line representing the immediate momentum of the price.
- The Signal Line: A smoother version of the main line used to identify potential trend changes or "crossovers."
- Reference Levels: The chart includes horizontal boundaries at 20 and 80, which are traditionally used to identify "oversold" (low) and "overbought" (high) conditions.
By combining these elements, the script allows a user to see not just where the price is going, but how fast it is moving relative to its recent past, helping to filter out minor market noise from significant trend movements.
//+------------------------------------------------------------------+
//| Fast Stochastic.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#include <stdlib.mqh>
//----
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 80
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int KPeriod = 5;
extern int DPeriod = 3;
//---- buffers
double MainBuffer[];
double SignalBuffer[];
double HighesBuffer[];
double LowesBuffer[];
//----
int draw_begin = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 2 additional buffers are used for counting.
IndicatorBuffers(4);
SetIndexBuffer(2, HighesBuffer);
SetIndexBuffer(3, LowesBuffer);
//---- indicator lines
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, MainBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, SignalBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="FastStoch(" + KPeriod + "," + DPeriod + ")";
IndicatorShortName(short_name);
SetIndexLabel(0, short_name);
SetIndexLabel(1, "Signal");
//----
draw_begin = KPeriod + DPeriod;
SetIndexDrawBegin(0, KPeriod);
SetIndexDrawBegin(1, draw_begin);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Stochastic oscillator |
//+------------------------------------------------------------------+
int start()
{
int i, k;
int counted_bars = IndicatorCounted();
double price;
//----
if(Bars <= draw_begin)
return(0);
//---- initial zero
if(counted_bars < 1)
{
for(i = 1; i <= KPeriod; i++)
MainBuffer[Bars-i] = 0;
for(i = 1; i <= draw_begin; i++)
SignalBuffer[Bars-i] = 0;
}
//---- minimums counting
i = Bars - KPeriod;
if(counted_bars > KPeriod)
i = Bars - counted_bars - 1;
while(i >= 0)
{
double min = 1000000;
k = i + KPeriod - 1;
while(k >= i)
{
price = Low[k];
if(min > price)
min = price;
k--;
}
LowesBuffer[i] = min;
i--;
}
//---- maximums counting
i = Bars - KPeriod;
if(counted_bars > KPeriod)
i = Bars - counted_bars - 1;
while(i >= 0)
{
double max=-1000000;
k = i + KPeriod - 1;
while(k >= i)
{
price = High[k];
if(max < price)
max = price;
k--;
}
HighesBuffer[i] = max;
i--;
}
//---- %K line
i = Bars - KPeriod;
if(counted_bars > KPeriod)
i = Bars - counted_bars - 1;
while(i >= 0)
{
if(!CompareDoubles((HighesBuffer[i] - LowesBuffer[i]), 0.0))
MainBuffer[i] = 100*(Close[i] - LowesBuffer[i]) / (HighesBuffer[i] - LowesBuffer[i]);
i--;
}
//---- last counted bar will be recounted
if(counted_bars > 0)
counted_bars--;
int limit = Bars - counted_bars;
//---- signal line is simple movimg average
for(i = 0; i < limit; i++)
SignalBuffer[i] = iMAOnArray(MainBuffer, Bars, DPeriod, 0, MODE_SMA, i);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments