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
 
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.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
657 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