Stochastic_NoLastBar
Download scriptThis tool is designed to provide traders with a visual representation of market momentum by analyzing the relationship between recent price highs and lows. Unlike standard indicators that might fluctuate wildly with every tick of live price movement, this specific version is engineered to filter out "noise" from the most recent incomplete bar, offering a more stable look at established trends.
How the Logic Works
The script follows a structured three-step process to translate raw market data into a readable oscillator:
1. Identifying Price Extremes The tool first scans the historical price data to identify the highest and lowest points reached over a specific number of past candles (the K-Period). By identifying these boundaries, it establishes the "trading range" for that timeframe.
2. Calculating Momentum (%K Line) Once the highs and lows are identified, the script calculates where the current price sits within that range. It specifically looks at how much of the recent price movement is concentrated toward the top versus the bottom. This result is converted into a percentage (0 to 100).
- A high percentage suggests that prices are trading near the top of their recent range (potential overbought territory).
- A low percentage suggests prices are trading near the bottom of their recent range (potential oversold territory).
3. Smoothing the Data (Signal Line) To make the momentum data easier to read, the script applies a "Simple Moving Average" to the results. This creates a second, smoother line that follows the primary momentum line. Traders often use this secondary line to identify changes in direction or to confirm when a momentum shift has gained enough strength to be significant.
Key Distinctive Feature
The primary characteristic of this specific script is its handling of the "Last Bar." Many standard indicators update instantly as new prices come in, which can cause the lines to jump around erratically before a candle is finished. This version intentionally ignores that final, unfinished data point. By focusing on completed price action, it provides a cleaner, more reliable visual for traders who prefer to base their decisions on confirmed movements rather than flickering, real-time fluctuations.
//+------------------------------------------------------------------+
//| Stochastic_NoLastBar.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//| modified by Skyline on 27 OCT 2008 to remove last bar value |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 EMPTY
#property indicator_level1 80
#property indicator_level2 20
//---- input parameters
extern int KPeriod=14;
extern int DPeriod=3;
extern int Slowing=3;
//---- buffers
double MainBuffer[];
double SignalBuffer[];
double HighesBuffer[];
double LowesBuffer[];
//----
int draw_begin1=0;
int draw_begin2=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="Stoch_NoLastBar("+KPeriod+","+DPeriod+","+Slowing+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
SetIndexLabel(1,"Signal");
//----
draw_begin1=KPeriod+Slowing;
draw_begin2=draw_begin1+DPeriod;
SetIndexDrawBegin(0,draw_begin1);
SetIndexDrawBegin(1,draw_begin2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Stochastic oscillator |
//+------------------------------------------------------------------+
int start()
{
int i,k;
int counted_bars=IndicatorCounted();
double price;
//----
if(Bars<=draw_begin2) return(0);
//---- initial zero
if(counted_bars<1)
{
for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
for(i=1;i<=draw_begin2;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)
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-draw_begin1;
if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
//while(i>=0)
while(i>0)
{
double sumlow=0.0;
double sumhigh=0.0;
for(k=(i+Slowing-1);k>=i;k--)
{
sumlow+=Close[k]-LowesBuffer[k];
sumhigh+=HighesBuffer[k]-LowesBuffer[k];
}
if(sumhigh==0.0) MainBuffer[i]=100.0;
else MainBuffer[i]=sumlow/sumhigh*100;
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++)
for(i=1; i<limit; i++)
SignalBuffer[i]=iMAOnArray(MainBuffer,Bars,DPeriod,0,MODE_SMA,i);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments