Percentage_Price_Oscillator
Download scriptThe Percentage Price Oscillator (PPO) is a momentum-based technical analysis tool designed to help traders understand the speed and strength of price movements. Unlike some indicators that show absolute price values, this script focuses on the relative percentage change between two different moving averages, making it easier to compare trends across different assets or timeframes.
Core Logic Overview
The indicator operates by comparing a "Fast" moving average to a "Slow" moving average. By calculating the difference between these two and dividing it by the value of the slow average, the script produces a percentage-based oscillator. This tells you not just which way the price is moving, but how aggressively it is doing so relative to its recent history.
Key Components of the Calculation
- Fast and Slow Averages: The script takes raw price data (such as the closing price) and processes it into two distinct averages. The "Fast" average reacts quickly to recent price fluctuations, while the "Slow" average represents the broader, longer-term trend.
- The Percentage Calculation: By subtracting the Slow average from the Fast average and dividing by the Slow average, the tool creates a figure that oscillates around a zero line.
- When the value is above zero, it suggests bullish momentum (the short-term price is rising faster than the long-term trend).
- When the value is below zero, it suggests bearish momentum (the short-term price is falling faster than the long-term trend).
- The Signal Line: To filter out "noise" and prevent false signals from minor market jitters, the script includes a second smoothing layer. It takes the resulting PPO value and runs it through an additional moving average calculation. This "Signal Line" helps traders identify when momentum is shifting or peaking.
Visual Representation
The indicator appears in a separate window on your trading chart rather than overlapping the price candles. It provides two distinct lines:
- PPO Line (Red): The primary momentum oscillator.
- Signal Line (DodgerBlue): A smoothed version of the PPO used to identify trend reversals.
Customization Options
The script is highly flexible, allowing users to adjust several parameters to fit different trading styles:
- Smoothing Methods: Users can choose from various mathematical models for the moving averages to determine how "smooth" or "reactive" the lines should be.
- Lengths and Phases: These settings allow you to define how many bars of data are included in the calculation (the depth) and how sensitive the indicator is to sudden price changes.
- Price Type: You can choose which specific price data the script looks at—such as the Close, Open, High, Low, or even a combination of these (like Median or Weighted prices).
//+------------------------------------------------------------------+
//| Percentage_Price_Oscillator.mq5 |
//| Copyright © 2007 Tom Balfe |
//| redcarsarasota@yahoo.com |
//+------------------------------------------------------------------+
//---- Copyright
#property copyright "Copyright © 2007 Tom Balfe"
//---- link to the website of the author
#property link "redcarsarasota@yahoo.com"
#property description "This is a momentum indicator."
#property description "Signal line is EMA of PPO."
#property description "Follows formula: PPO=(FastEMA-SlowEMA)/SlowEMA"
//---- Indicator version number
#property version "1.00"
//---- drawing indicator in a separate window
#property indicator_separate_window
//---- two buffers are used for the indicator calculation and drawing
#property indicator_buffers 2
//---- two plots are used
#property indicator_plots 2
//+----------------------------------------------+
//| Parameters of displaying horizontal levels |
//+----------------------------------------------+
#property indicator_level1 0
#property indicator_levelcolor clrBlueViolet
#property indicator_levelstyle STYLE_SOLID
//+----------------------------------------------+
//| Indicator 1 drawing parameters |
//+----------------------------------------------+
//---- drawing indicator 1 as a line
#property indicator_type1 DRAW_LINE
//---- Red is used for the line indicator color
#property indicator_color1 clrRed
//---- the line of the indicator 1 is a continuous curve
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà 1 ðàâíà 1
#property indicator_width1 1
//---- displaying of the bullish label of the indicator
#property indicator_label1 "PPO"
//+----------------------------------------------+
//| Indicator 2 drawing parameters |
//+----------------------------------------------+
//---- dawing the indicator 2 as a line
#property indicator_type2 DRAW_LINE
//---- DodgerBlue is used for the line indicator color
#property indicator_color2 clrDodgerBlue
//---- Indicator line is a solid one
#property indicator_style2 STYLE_SOLID
//---- indicator 2 line width is equal to 2
#property indicator_width2 2
//---- displaying of the bullish label of the indicator
#property indicator_label2 "Signal PPO"
//+----------------------------------------------+
//| CXMA class description |
//+----------------------------------------------+
#include <SmoothAlgorithms.mqh>
//+----------------------------------------------+
//---- declaration of the CXMA class variables from the SmoothAlgorithms.mqh file
CXMA XMA1,XMA2,XMA3;
//+----------------------------------------------+
//| declaration of enumerations |
//+----------------------------------------------+
/*enum Smooth_Method - enumeration is declared in SmoothAlgorithms.mqh
{
MODE_SMA_, //SMA
MODE_EMA_, //EMA
MODE_SMMA_, //SMMA
MODE_LWMA_, //LWMA
MODE_JJMA, //JJMA
MODE_JurX, //JurX
MODE_ParMA, //ParMA
MODE_T3, //T3
MODE_VIDYA, //VIDYA
MODE_AMA, //AMA
}; */
//+----------------------------------------------+
//| declaration of enumerations |
//+----------------------------------------------+
enum Applied_price_ //Type of constant
{
PRICE_CLOSE_ = 1, //Close
PRICE_OPEN_, //Open
PRICE_HIGH_, //High
PRICE_LOW_, //Low
PRICE_MEDIAN_, //Median Price (HL/2)
PRICE_TYPICAL_, //Typical Price (HLC/3)
PRICE_WEIGHTED_, //Weighted Close (HLCC/4)
PRICE_SIMPL_, //Simpl Price (OC/2)
PRICE_QUARTER_, //Quarted Price (HLOC/4)
PRICE_TRENDFOLLOW0_, //TrendFollow_1 Price
PRICE_TRENDFOLLOW1_ //TrendFollow_2 Price
};
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input Smooth_Method FastMethod=MODE_EMA; //method of fast averaging
input uint FastLength=12; //depth of the fast averaging
input int FastPhase=15; //Fast averaging parameter,
//for JJMA, it varies within the range -100 ... +100 and influences on the quality of the transient period;
// For VIDIA, it is a CMO period, for AMA, it is a slow moving average period
input Smooth_Method SlowMethod=MODE_EMA; //method of slow averaging
input uint SlowLength=26; //depth of slow averaging
input int SlowPhase=15; //Slow averaging parameter
//for JJMA, it varies within the range -100 ... +100 and influences on the quality of the transient period;
// For VIDIA, it is a CMO period, for AMA, it is a slow moving average period
input Smooth_Method SignMethod=MODE_EMA; //Smoothing method
input uint SignLength=9; //Smoothing depth
input int SignPhase=15; //Smoothing parameter,
//for JJMA, it varies within the range -100 ... +100 and influences on the quality of the transient period;
// For VIDIA, it is a CMO period, for AMA, it is a slow moving average period
input Applied_price_ IPC=PRICE_CLOSE;//price constant
input int Shift=0; // horizontal shift of the indicator in bars
//+----------------------------------------------+
//---- declaration of dynamic arrays that
// will be used as indicator buffers
double PPOBuffer[],SignBuffer[];
//---- Declaration of integer variables of data starting point
int min_rates_,min_rates_total;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- Initialization of variables of the start of data calculation
min_rates_=int(MathMax(XMA1.GetStartBars(FastMethod,FastLength,FastPhase),XMA1.GetStartBars(SlowMethod,SlowLength,SlowPhase)));
min_rates_total=min_rates_+XMA1.GetStartBars(SignMethod,SignLength,SignPhase);
//---- set SignBuffer dynamic array as an indicator buffer
SetIndexBuffer(0,PPOBuffer,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by Shift
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- performing shift of the beginning of counting of drawing the indicator 1 by 1
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- Setting the indicator values that won't be visible on a chart
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- Set the PPOBuffer dynamic array as an indicator buffer
SetIndexBuffer(1,SignBuffer,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by Shift
PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- performing shift of the beginning of counting of drawing the indicator 2 by 1
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//---- Setting the indicator values that won't be visible on a chart
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- Creation of the name to be displayed in a separate sub-window and in a pop up help
IndicatorSetString(INDICATOR_SHORTNAME,"Percentage_Price_Oscillator");
//--- Determining the accuracy of displaying the indicator values
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//----
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(
const int rates_total, // amount of history in bars at the current tick
const int prev_calculated,// amount of history in bars at the previous tick
const datetime &time[],
const double &open[],
const double& high[], // price array of maximums of price for the calculation of indicator
const double& low[], // price array of price lows for the indicator calculation
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[]
)
{
//---- checking the number of bars to be enough for calculation
if(rates_total<min_rates_total) return(0);
//---- declaration of local variables
int first;
//---- Calculation of the starting number 'first' for the cycle of recalculation of bars
if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation
{
first=0; // starting number for calculation of all bars
}
else first=prev_calculated-1; // Starting index for the calculation of new bars
//---- main cycle of calculation of the indicator
for(int bar=first; bar<rates_total && !IsStopped(); bar++)
{
//---- Calling the PriceSeries function to get the input price price_
double price=PriceSeries(IPC,bar,open,low,high,close);
double fast=XMA1.XMASeries(0,prev_calculated,rates_total,FastMethod,FastPhase,FastLength,price,bar,false);
double slow=XMA2.XMASeries(0,prev_calculated,rates_total,SlowMethod,SlowPhase,SlowLength,price,bar,false);
if(!slow) slow=1;
double ppo=(fast-slow)/slow;
PPOBuffer[bar]=ppo;
SignBuffer[bar]=XMA3.XMASeries(min_rates_,prev_calculated,rates_total,SignMethod,SignPhase,SignLength,ppo,bar,false);
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments