Price Oscillator
Download scriptThe Price Oscillator is a custom technical analysis tool designed to help traders visualize the momentum and relationship between two different moving averages. Instead of looking at just one price trend, this script calculates the "gap" or distance between a fast-moving average and a slow-moving average, presenting that difference as a visual histogram in a separate window on your trading platform.
How the Logic Works
The core logic relies on comparing two distinct types of averages:
- The Fast Moving Average: This is designed to react quickly to recent price changes. It tracks shorter-term price movements, helping to identify immediate shifts in market direction.
- The Slow Moving Average: This is designed to be more stable and "smooth," reacting more gradually to price changes over a longer period. It represents the broader trend of the asset.
The Calculation Process
The script performs the following steps automatically:
- Data Selection: You can choose which specific price points to use (such as the closing price, opening price, highs, or lows) for both the fast and slow averages.
- Smoothing Methods: The tool allows for different calculation styles—Simple, Exponential, Smoothed, or Weighted—giving you the flexibility to choose how "reactive" or "smooth" you want the data to appear.
- The Difference Calculation: The script subtracts the value of the Slow Moving Average from the Fast Moving Average at every point in time.
- Normalization: To ensure the data is readable and consistent across different assets, the resulting difference is normalized based on the market's "point" value.
Visual Representation
The final result is displayed as a Histogram.
- When the histogram bars are above zero, it indicates that the short-term price (Fast MA) is trending higher than the long-term average (Slow MA), often suggesting upward momentum.
- When the bars fall below zero, it indicates that the short-term price is trending lower than the long-term average, often suggesting downward momentum.
By viewing these oscillations, traders can better identify when a trend is accelerating, slowing down, or potentially reversing, providing a clearer picture of market energy than looking at individual price lines alone.
//+------------------------------------------------------------------+
//| Price Oscillator.mq4 |
//| Copyright © 2007, Serega Lykov |
//| http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Serega Lykov"
#property link "http://mtexperts.narod.ru/"
//---- property of indicator ----------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 White
//---- external parameters ------------------------------------------+
extern string _____Fast_MA__________;
extern int Period_fast_ma = 12; // the period of fast ma
extern int Method_fast_ma = 0; // the method of fast ma: 0-Simple, 1-Exponential,
// 2-Smoothed, 3-Weighted
extern int Price_fast_ma = 0; // the price for fast ma: 0-Close, 1-Open, 2-High, 3-Low,
// 4-Median, 5-Typical, 6-Weighted
extern int Shift_fast_ma = 0; // the shift for fast ma
extern string _____Slow_MA__________;
extern int Period_slow_ma = 26; // the period of slow ma
extern int Method_slow_ma = 0; // the method of slow ma: 0-Simple, 1-Exponential,
// 2-Smoothed, 3-Weighted
extern int Price_slow_ma = 0; // the price for slow ma: 0-Close, 1-Open, 2-High, 3-Low,
// 4-Median, 5-Typical, 6-Weighted
extern int Shift_slow_ma = 0; // the shift for slow ma
//---- buffers ------------------------------------------------------+
static double IndBuffer[];
//---- global variables ---------------------------------------------+
static double point;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- set a "short" name of the indicator
switch(Method_fast_ma)
{
case 1: string fast_ma = "EMA("; break;
case 2: fast_ma = "SMMA("; break;
case 3: fast_ma = "LWMA("; break;
default: fast_ma = "SMA("; Method_fast_ma = 0;
}
switch(Method_slow_ma)
{
case 1: string slow_ma = "EMA("; break;
case 2: slow_ma = "SMMA("; break;
case 3: slow_ma = "LWMA("; break;
default: slow_ma = "SMA("; Method_slow_ma = 0;
}
IndicatorShortName("Price Oscillator " + fast_ma + Period_fast_ma + ")," + slow_ma + Period_slow_ma + ")");
//---- set a accuracy of values of the indicator
IndicatorDigits(0);
//---- set a style for lines of the indicator
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
//---- set a arrays for lines of the indicator
SetIndexBuffer(0,IndBuffer);
//---- set a first bar for drawing the lines of the indicator
SetIndexDrawBegin(0,MathMax(Period_fast_ma,Period_slow_ma));
//---- set a names for lines of the indicator
SetIndexLabel(0,"Price Oscillator");
//---- initialization values of constants
point = Point;
//---- finish of initialization
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
// amount not changed after last call of the indicator of bars
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1);
// last counted bar will be recounted
if(counted_bars > 0) counted_bars--;
int limit = Bars - counted_bars;
// calculate lines of channel
for (int i=limit; i>=0; i--) IndBuffer[i] = NormalizeDouble((iMA(NULL,0,Period_fast_ma,Shift_fast_ma,Method_fast_ma,Price_fast_ma,i)-iMA(NULL,0,Period_slow_ma,Shift_slow_ma,Method_slow_ma,Price_slow_ma,i))/point,0);
// finish of iteration
return(0);
}
//+------------------------------------------------------------------+
Comments