AdvancedGetOscillator_v1
Download scriptThis script is designed as a visual trading tool for the MetaTrader platform, specifically created to help traders identify momentum and potential trend reversals by calculating an "oscillator" value. Rather than showing raw price data, it transforms complex market movements into a simplified visual format that resides in its own dedicated window on your chart.
How the Logic Works
The indicator operates through a three-step analytical process:
1. Baseline Movement Tracking The script begins by looking at two different types of moving averages based on the "median price" (the midpoint between the high and low prices of a candle).
- It calculates a Short-Term Average (covering a period of 5 bars).
- It calculates a Long-Term Average (covering a period of 34 bars).
2. Determining the Momentum Gap By subtracting the long-term average from the short-term average, the script creates a primary difference value. This value represents the "distance" between current short-term price action and the longer-term trend. When this gap is positive, it suggests upward momentum; when negative, it suggests downward momentum.
3. Smoothing and Trend Filtering To prevent the visual data from being too "noisy" or erratic, the script applies a sophisticated smoothing logic:
- Dynamic Averaging: The script uses a specific mathematical weight to blend current momentum with previous values.
- Directional Filtering: It intelligently detects whether the momentum is currently positive or negative. Depending on that direction, it applies different smoothing rules to the visual lines. This helps in creating a smoother curve that highlights the strength of the trend while filtering out minor price fluctuations that might lead to false signals.
Visual Representation
The final output provides the user with three distinct visual elements:
- A Histogram representing the raw difference between the two moving averages.
- Two Trend Lines that provide a smoothed, continuous view of the market's momentum, making it easier for a trader to see whether a trend is accelerating, slowing down, or potentially reaching an exhaustion point.
//+------------------------------------------------------------------+
//| Advanced Get Oscillator.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//----
#property indicator_separate_window
#property indicator_buffers 3
//----
#property indicator_color1 SlateBlue
#property indicator_color2 White
#property indicator_color3 White
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);
//---- 3 additional buffers are used for counting.
SetIndexBuffer(3, ExtMapBuffer4);
SetIndexBuffer(4, ExtMapBuffer5);
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtMapBuffer3);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
//----
int limit;
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//----
for(int i=0; i<limit; i++)
ExtMapBuffer4[i]=iMA(NULL,0,5,0,MODE_SMA,PRICE_MEDIAN ,i);
for(i=0; i<limit; i++)
ExtMapBuffer5[i]=iMA(NULL,0,34,0,MODE_SMA,PRICE_MEDIAN,i);
for(i=0; i<limit; i++)
ExtMapBuffer1[i]=ExtMapBuffer4[i]-ExtMapBuffer5[i];
double pr=2.0/(38+1);
int pos=Bars-2;
if (counted_bars>2) pos=Bars-counted_bars-1;
while(pos>=0)
{
if((pos==Bars-2))
{ ExtMapBuffer2[pos+1]=ExtMapBuffer1[pos+1];
ExtMapBuffer3[pos+1]=ExtMapBuffer1[pos+1];
}
if (ExtMapBuffer1[pos]>=0)
{
ExtMapBuffer2[pos]=ExtMapBuffer1[pos]*pr+ExtMapBuffer2[pos+1]*(1-pr);
ExtMapBuffer3[pos]=ExtMapBuffer3[pos+1];
}
else
{
ExtMapBuffer2[pos]=ExtMapBuffer2[pos+1];
ExtMapBuffer3[pos]=ExtMapBuffer1[pos]*pr+ExtMapBuffer3[pos+1]*(1-pr);
}
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments