PPO (Price Percent Oscillator) for ThinkorSwim

Hi BenTen: As you know ToS "canned" indicators based on a type of average require a period "length" defined as a number of "bars". Chart bars are specified in "timeframe". As the user changes the chart timeframe, the moving averages change.
I prefer to chart a fixed time duration(s) for all indicators so the average remains a similar "filter bandwidth" and use different timeframes to zoom across various time periods. For example fix exponential moving average "length" with the following statement
expaverage (close (period=aggregationperiod.day), length=XXX) where xxx is specified to be 21, 50, 200.

I would like to modify PPO script to utilize fixed aggregation periods such as PPO(5,21,3) for nfast = 5 days, nslow=21days, and nsmooth=3days. I will also create PPO studies for (21,50,9) and PPO(120,200,50).

Does this make sense?
Thanks!

Try this and let me know if this is what you're looking for.

The timeframe is set to Hourly but you can change it to whatever timeframe you want from the indicator's settings.

Code:
# PPO
# Mobius
# V01.03.2014

declare lower;

input Period = aggregationPeriod.HOUR;
input c = close;
input AvgType = AverageType.Simple;
input nFast = 8;
input nSlow = 13;
input nSmooth = 5;

plot PPO = ((MovingAverage(AverageType = AvgType, close(period = Period), nFast) -
             MovingAverage(AverageType = AvgType, close(period = Period), nSlow)) /
             MovingAverage(AverageType = AvgType, close(period = Period), nSlow));
PPO.SetPaintingStrategy(PaintingStrategy.Histogram);
PPO.AssignValueColor(if PPO > 0
                     then color.green
                     else color.red);

plot smooth = MovingAverage(AverageType = AvgType, PPO, nSmooth);
smooth.SetPaintingStrategy(PaintingStrategy.Line);
smooth.SetDefaultColor(Color.Cyan);
# End Code PPO
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Try this and let me know if this is what you're looking for.

The timeframe is set to Hourly but you can change it to whatever timeframe you want from the indicator's settings.

Code:
# PPO
# Mobius
# V01.03.2014

declare lower;

input Period = aggregationPeriod.HOUR;
input c = close;
input AvgType = AverageType.Simple;
input nFast = 8;
input nSlow = 13;
input nSmooth = 5;

plot PPO = ((MovingAverage(AverageType = AvgType, close(period = Period), nFast) -
             MovingAverage(AverageType = AvgType, close(period = Period), nSlow)) /
             MovingAverage(AverageType = AvgType, close(period = Period), nSlow));
PPO.SetPaintingStrategy(PaintingStrategy.Histogram);
PPO.AssignValueColor(if PPO > 0
                     then color.green
                     else color.red);

plot smooth = MovingAverage(AverageType = AvgType, PPO, nSmooth);
smooth.SetPaintingStrategy(PaintingStrategy.Line);
smooth.SetDefaultColor(Color.Cyan);
# End Code PPO
Wow, thank you BenTen!!! I will test this and revert with feedback. All the best!
Marcus Crahan
 
@BenTen and I have worked on this one before - he has done a bang-up job with this one. I made some changes (sorry BenTen if I messed it up) that goes along with my other indicators. I don't like my Oscillators to be contradicting price action trend unless it is predicting. I added some color changes on the bars to denote momentum changes and arrows for ICT style "break of structures or change of character in a way. Plus, the time frame changes as the chart changes, so you won't have to go into the set-up page and alter the timeframe every time. https://tos.mx/!57rZ0Bjk
 
@BenTen thank you for your constant aggregation period version of PPO indicator. It works! Histogram gets a best messy with short time period candles. The maths are correct, display is a function of "time zoom effect".

@antwerks, thanks for your effort also. I noticed your edits in the basic PPO and had intended to integrate @BenTen's constant aggregation period version into your version. Looks like you "beat me to the punch". I will try this today and revert with feedback!
Thank you both very much (very green to ToS script use).
Marcus Crahan
 
Code:
# PPO + ROC Momentum Dashboard with Arrows
# Created by ANTWERKS from Mobius and BENTEN | June 2025
# Enhanced: Color-coded ROC trend (June 2025)

declare lower;

# === INPUTS ===
input price = close;
input avgType = AverageType.Simple;
input nFast = 8;
input nSlow = 13;
input nSmooth = 5;

input rocLength = 10;
input rocSmoothingLength = 3;
input rocOB = 5.0;
input rocOS = -5.0;

input showDashboard = yes;
input showArrows = yes;

# === PPO CALCULATION ===
def maFast = MovingAverage(avgType, price, nFast);
def maSlow = MovingAverage(avgType, price, nSlow);

def PPO = (maFast - maSlow) / maSlow;
plot PPOhist = PPO * 200;
PPOhist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
PPOhist.AssignValueColor(if PPO > 0 then Color.GREEN else Color.RED);
PPOhist.SetLineWeight(3);

def PPOsignal = MovingAverage(avgType, PPO, nSmooth);
plot SignalLine = PPOsignal;
SignalLine.SetDefaultColor(Color.CYAN);
SignalLine.SetLineWeight(1);

# === ROC CALCULATION ===
def ROCraw = (price - price[rocLength]) / price[rocLength] * 100;
def ROCsmoothed = Average(ROCraw, rocSmoothingLength);
plot ROCplot = ROCsmoothed;

# NEW COLORING: green if rising, red if falling, gray if flat
ROCplot.AssignValueColor(
if ROCsmoothed > ROCsmoothed[1] then Color.GREEN
else if ROCsmoothed < ROCsmoothed[1] then Color.RED
else Color.GRAY);
ROCplot.SetLineWeight(2);

# === MOMENTUM SIGNAL LOGIC ===
def isBullish = PPO > PPOsignal and ROCsmoothed > 0;
def isBearish = PPO < PPOsignal and ROCsmoothed < 0;

# === ARROWS ON CHART ===
plot BullArrow = if showArrows and isBullish and !isBullish[1] then 0 else Double.NaN;
BullArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullArrow.SetDefaultColor(Color.GREEN);
BullArrow.SetLineWeight(2);

plot BearArrow = if showArrows and isBearish and !isBearish[1] then 0 else Double.NaN;
BearArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearArrow.SetDefaultColor(Color.RED);
BearArrow.SetLineWeight(2);

# === LABELS ===
AddLabel(showDashboard, "PPO: " + Round(PPO, 2), if PPO > 0 then Color.GREEN else Color.RED);
AddLabel(showDashboard, "Signal: " + Round(PPOsignal, 2), Color.CYAN);
AddLabel(showDashboard, "ROC: " + Round(ROCsmoothed, 2) + "%",
if ROCsmoothed >= rocOB then Color.LIGHT_GREEN
else if ROCsmoothed <= rocOS then Color.PINK
else Color.GRAY);

AddLabel(showDashboard,
if isBullish then "Momentum: Bullish"
else if isBearish then "Momentum: Bearish"
else "Momentum: Neutral",
if isBullish then Color.GREEN
else if isBearish then Color.RED
else Color.YELLOW);
This is an excellent script, i modified it and combined it with MACD, would love to get feedback if anyone found it to be useful, works great on SPY & QQQ , I Use 10 minute timeframe to trade.

Code:
# Created by Rafiq from ANTWERKS from Mobius and BENTEN | March 2026


Declare lower;

# --- Inputs ---
input length = 20;
input nFast = 8; input nSlow = 13; input nSmooth = 5;
input rocLength = 10; input rocSmoothingLength = 3;
input macdFast = 12; input macdSlow = 26; input macdSignal = 9;

# --- 1. MACD Trend Filter ---
def macdValue = MACD(macdFast, macdSlow, macdSignal).Value;
def isBullTrend = macdValue > 0;
def isBearTrend = macdValue < 0;

# --- 2. Breakout Logic ---
def priceH = highest(high[1], length);
def priceL = lowest(low[1], length);
def bullishBreakout = high > priceH;
def bearishBreakout = low < priceL;

# Tracks if we are currently in a breakout state
rec isBullBreakoutActive = if bullishBreakout then 1 else if bearishBreakout then 0 else isBullBreakoutActive[1];
rec isBearBreakoutActive = if bearishBreakout then 1 else if bullishBreakout then 0 else isBearBreakoutActive[1];

# --- 3. Momentum Logic ---
def PPO = (MovingAverage(AverageType.SIMPLE, close, nFast) - MovingAverage(AverageType.SIMPLE, close, nSlow)) / MovingAverage(AverageType.SIMPLE, close, nSlow);
def PPOsignal = MovingAverage(AverageType.SIMPLE, PPO, nSmooth);
def ROCsmoothed = Average((close - close[rocLength]) / close[rocLength] * 100, rocSmoothingLength);
def isBullishMomentum = PPO > PPOsignal and ROCsmoothed > 0;
def isBearishMomentum = PPO < PPOsignal and ROCsmoothed < 0;

# --- 4. Signal Integration ---
plot UpSignal = if bullishBreakout and isBullishMomentum and isBullTrend then 0 else Double.NaN;
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpSignal.SetDefaultColor(Color.GREEN);
UpSignal.SetLineWeight(5);

plot DownSignal = if bearishBreakout and isBearishMomentum and isBearTrend then 0 else Double.NaN;
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownSignal.SetDefaultColor(Color.RED);
DownSignal.SetLineWeight(5);

# --- 5. Conditional MOM Bubbles ---
# Only show if momentum changes AND it doesn't contradict the active breakout direction
def bullTrigger = isBullishMomentum and !isBullishMomentum[1];
def bearTrigger = isBearishMomentum and !isBearishMomentum[1];

AddChartBubble(bullTrigger and !isBearBreakoutActive, 0, "MOM", Color.GREEN, yes);
AddChartBubble(bearTrigger and !isBullBreakoutActive, 0, "MOM", Color.RED, no);
 
Last edited by a moderator:
This is an excellent script, i modified it and combined it with MACD, would love to get feedback if anyone found it to be useful, works great on SPY & QQQ , I Use 10 minute timeframe to trade.

Code:
# Created by Rafiq from ANTWERKS from Mobius and BENTEN | March 2026


Declare lower;

# --- Inputs ---
input length = 20;
input nFast = 8; input nSlow = 13; input nSmooth = 5;
input rocLength = 10; input rocSmoothingLength = 3;
input macdFast = 12; input macdSlow = 26; input macdSignal = 9;

# --- 1. MACD Trend Filter ---
def macdValue = MACD(macdFast, macdSlow, macdSignal).Value;
def isBullTrend = macdValue > 0;
def isBearTrend = macdValue < 0;

# --- 2. Breakout Logic ---
def priceH = highest(high[1], length);
def priceL = lowest(low[1], length);
def bullishBreakout = high > priceH;
def bearishBreakout = low < priceL;

# Tracks if we are currently in a breakout state
rec isBullBreakoutActive = if bullishBreakout then 1 else if bearishBreakout then 0 else isBullBreakoutActive[1];
rec isBearBreakoutActive = if bearishBreakout then 1 else if bullishBreakout then 0 else isBearBreakoutActive[1];

# --- 3. Momentum Logic ---
def PPO = (MovingAverage(AverageType.SIMPLE, close, nFast) - MovingAverage(AverageType.SIMPLE, close, nSlow)) / MovingAverage(AverageType.SIMPLE, close, nSlow);
def PPOsignal = MovingAverage(AverageType.SIMPLE, PPO, nSmooth);
def ROCsmoothed = Average((close - close[rocLength]) / close[rocLength] * 100, rocSmoothingLength);
def isBullishMomentum = PPO > PPOsignal and ROCsmoothed > 0;
def isBearishMomentum = PPO < PPOsignal and ROCsmoothed < 0;

# --- 4. Signal Integration ---
plot UpSignal = if bullishBreakout and isBullishMomentum and isBullTrend then 0 else Double.NaN;
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpSignal.SetDefaultColor(Color.GREEN);
UpSignal.SetLineWeight(5);

plot DownSignal = if bearishBreakout and isBearishMomentum and isBearTrend then 0 else Double.NaN;
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownSignal.SetDefaultColor(Color.RED);
DownSignal.SetLineWeight(5);

# --- 5. Conditional MOM Bubbles ---
# Only show if momentum changes AND it doesn't contradict the active breakout direction
def bullTrigger = isBullishMomentum and !isBullishMomentum[1];
def bearTrigger = isBearishMomentum and !isBearishMomentum[1];

AddChartBubble(bullTrigger and !isBearBreakoutActive, 0, "MOM", Color.GREEN, yes);
AddChartBubble(bearTrigger and !isBullBreakoutActive, 0, "MOM", Color.RED, no);
Be careful.
The biggest downside is: Redundancy / lag. PPO and MACD are closely related anyways. So by adding MACD, you are now waiting on momentum confirming momentum instead of adding a genuinely new dimension.
That can create:
  • later entries
  • missed early turns
  • over-filtering
Especially because your MACD uses 12, 26, 9 while PPO uses 8, 13, 5. So you now have:
  • faster momentum model (PPO)
  • slower momentum model (MACD)
That means MACD will often delay valid early entries. Plus. You can always eyeball MACD in another plot and not slow down PPO.
 
Hello BenTen and Antwerks:
Cycling back to your PPO script, will appreciate if you could explain some conundrums:
(my tos program chart is setup to autoscale and percentage display is off)
PPO histogram value at current period displays 0.038. Chart units are in thousandths of a unit with histogram amplitude "38".
Comparing the same ticker PPO computed and graphed in StockCharts (using same slow, fast and signal periods and same exponential weighting) plots as 3.8% ie difference by a factor of 10.

If i switch ToS charting configuration to be "manual" there are no fields apparent to specify what the min and max scale values are for each chart (price, PPO1, PPO2, PPO3)
If I switch TOS charting to display price as a percentage, only the price chart is scaled in percentage.

PPO script chart should be graphed +/- 100 IMO in order that the histogram has uniform scale to allow valid comparison between PPO of different holdings. It seems that autoscalling PPO defeats the purpose of using PPO compared to MACD.

Why does the PPO script output not in units between -100 and 100 when it is normalized to the slow EMA period value x 100? Perhaps solving this connundrum would correct the 10x factor difference between StockCharts and TOS PPO calculations?
PS ideally I would leave ToS in autoscale charting price not as percentage, and control PPO charting within the script to be a value between -100 and 100. If the PPO value is in "thousandths" on the percent scale, it should be graphed on the zero Yaxis value since the momentum difference between the fast and slow periods defined is "near zero". Autoscale with a factor of 10 "error" throws a monkey wrench into interpreting this momentum oscillator IMO.
I will appreciate what your feedback on these remarks are.
 
my bad: 10x factor difference is not in PPO values calculated between ToS PPO script and StockCharts PPO calculation... Further comparative testing shows that PPO values for a given ticker and fast and slow ema periods, calculated by the ToS script and StockCharts are the same numerical values.
the 10x factor mentioned above is an artifact caused by StockCharts plotting in % and ToS plotting in "thousandths units".
still looking for a way to get ToS to graph PPO script histogram in percent with user specified max and min limits.
 
Hello BenTen and Antwerks:

The attached file scales the PPO output to percent units.

Try it and let me know if there are bugs in the display.
Code:
# PPO fixed aggregation_period — Percent Scaled
# Mobius (modified)
# 2026-04-13

declare lower;

input Period   = AggregationPeriod.DAY;
input AvgType  = AverageType.exponential;
input nFast    = 105;
input nSlow    = 170;
input nSmooth  = 5;

# --- PPO calculation (ratio)
def ppoRaw =
    (MovingAverage(AvgType, close(period = Period), nFast) -
     MovingAverage(AvgType, close(period = Period), nSlow)) /
     MovingAverage(AvgType, close(period = Period), nSlow);

# --- Convert to percent
plot PPO = ppoRaw * 100;
PPO.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
PPO.SetLineWeight(3);
PPO.AssignValueColor(if PPO > 0 then Color.GREEN else Color.RED);

# --- Signal line (also percent-based)
plot Smooth = MovingAverage(AvgType, PPO, nSmooth);
Smooth.SetDefaultColor(Color.CYAN);
Smooth.SetLineWeight(2);

# --- Optional: visually anchor zero
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.DARK_GRAY);
ZeroLine.SetStyle(Curve.SHORT_DASH);
 
Hello BenTen and Antwerks:

The attached file scales the PPO output to percent units.

Try it and let me know if there are bugs in the display.
Code:
# PPO fixed aggregation_period — Percent Scaled
# Mobius (modified)
# 2026-04-13

declare lower;

input Period   = AggregationPeriod.DAY;
input AvgType  = AverageType.exponential;
input nFast    = 105;
input nSlow    = 170;
input nSmooth  = 5;

# --- PPO calculation (ratio)
def ppoRaw =
    (MovingAverage(AvgType, close(period = Period), nFast) -
     MovingAverage(AvgType, close(period = Period), nSlow)) /
     MovingAverage(AvgType, close(period = Period), nSlow);

# --- Convert to percent
plot PPO = ppoRaw * 100;
PPO.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
PPO.SetLineWeight(3);
PPO.AssignValueColor(if PPO > 0 then Color.GREEN else Color.RED);

# --- Signal line (also percent-based)
plot Smooth = MovingAverage(AvgType, PPO, nSmooth);
Smooth.SetDefaultColor(Color.CYAN);
Smooth.SetLineWeight(2);

# --- Optional: visually anchor zero
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.DARK_GRAY);
ZeroLine.SetStyle(Curve.SHORT_DASH);
Yours needs some scaling work it seems, your is top ben's second my version last
 
Hi Antwerks:
I can't read any numerical values shown in the screen capture so I can't comment on scaling differences.

The version I uploaded today is scaled in percent.

previous versions output was autoscaled, but typically was in thousandths (10x larger than percent units).

Do you agree that PPO output should be scaled in percent?
 
Hi Antwerks:
I can't read any numerical values shown in the screen capture so I can't comment on scaling differences.

The version I uploaded today is scaled in percent.

previous versions output was autoscaled, but typically was in thousandths (10x larger than percent units).

Do you agree that PPO output should be scaled in percent?
PPO is normalized but not percentage-scaled — multiply by 100 to convert it into standard, interpretable percent terms. Mine is a ratio osc and not a "percent osc" same but not - just change this line to multiply by 100:
plot PPO = 100 * (fastMA - slowMA) / slowMA;

If you do this: thresholds change

def neutral = AbsValue(PPO) < 0.001;

becomes: def neutral = AbsValue(PPO) < 0.1;

2. Exhaustion logic scales up

highestPPO * 0.8

Still works — just operates in percent now

3. Labels become more intuitive. Instead of:

PPO = 0.018

You get: PPO = 1.8%

Much clearer for decision-making
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1153 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top