PPO (Price Percent Oscillator) for ThinkorSwim

@grasshopper123 @wpwright I did was a simple google search, I am assuming that you did also and already saw this Easycator PPO w/ fast and slow lines and it wasn't what you wanted.

However here it is for anyone in the future that has this request:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2016
#
## created by North Vanhooser

declare lower;

input fastPeriod   = 12;
input slowPeriod   = 26;
input signalPeriod = 9;
input price        = close;

def fastEma   = ExpAverage( price, fastPeriod );
def slowEma   = ExpAverage( price, slowPeriod );
def periodOK  = fastPeriod < slowPeriod;
AddLabel( !periodOK, "ERROR: fastPeriod MUST be less than slowPeriod" );
def _ppo      = if periodOK then ((fastEma - slowEma) / slowEma) * 100 else 0;
def _signal   = ExpAverage( _ppo, signalPeriod );

plot Ppo      = _ppo;
Ppo.SetDefaultColor( Color.BLUE );
Ppo.HideBubble();

plot PpoEma   = _signal;
PpoEma.SetDefaultColor( Color.CYAN );
PpoEma.HideBubble();

plot zeroLine = 0;
zeroLine.HideBubble();
zeroLine.AssignValueColor( Color.BLACK );

plot PpoH1    = _ppo - _signal;
PpoH1.SetPaintingStrategy( PaintingStrategy.HISTOGRAM );
PpoH1.DefineColor("Up", Color.UPTICK);
PpoH1.DefineColor("Down", Color.DOWNTICK);
PpoH1.DefineColor("Flat", Color.GRAY);
PpoH1.AssignValueColor( if PpoH1 > PpoH1[1] then
                           PpoH1.Color("Up")
                       else
                         if PpoH1 < PpoH1[1] then
                           PpoH1.Color("Down")
                       else
                           PpoH1.Color("Flat"));
PpoH1.HideBubble();
PpoH1.SetLineWeight( 3 );
View attachment 2001

Here is the shared link: http://tos.mx/zQ6P90
Thanks for sharing. Is it possible to add bullish/bearish arrows each time PPO (1) crosses above/below EMA (2) or when PPOH1 changes color from Down to UP (bullish) or from UP to Down (Bearish)? essentially , there will be two different set of arrows. (PPO/EMA) and PPOH1 UP/DOWN color change. Thanks again. Here is the shared link that indicator: http://tos.mx/zQ6P90
1725879714754.png
 
Last edited:
The Percentage Price Oscillator (PPO) is similar to Moving Average Convergence Divergence (MACD).
They are both momentum indicators used to gauge the strength and direction of a trend in a stock's price. Many times, they present with the same signals.

The PPO is considered superior in that the PPO is expressed as a percentage, making it easier to compare across different stocks and timeframes. Whereas the MACD has little comparitive value.

View attachment 5664

This script for ToS was created by Mobius.

thinkScript Code

Code:
# PPO
# Mobius
# V01.03.2014

declare lower;

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

plot PPO = ((MovingAverage(AverageType = AvgType, c, nFast) -
             MovingAverage(AverageType = AvgType, c, nSlow)) /
             MovingAverage(AverageType = AvgType, c, 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

Shareable Link

https://tos.mx/hv8hVf
Nice oscillator
 
The idea here is that while the stocks may have greatly different prices, the percentage difference between the averages are comparable. A 5% spread between the 12 and 26 ema is the same relative difference by percentage, regardless of the price differences between stocks.

My intent is to do away with the problem of trying to compare the MACD of different stocks that have widely different price levels.

So I converted the difference between the fast and slow ema into a percentage difference.

Here's my script...
Cp9qvql.png


Code:
declare lower;

input FastLength = 12; # Faster of the two moving averages
input SlowLength = 26; # Slower of the two moving averages
input FastAvgSmoothingLength = 9;
input FastEMA_Length = 8;
def FastEMAavg = ExpAverage(close,FastEMA_Length);
def P1L = 1;
def PP5L = 0.5;
def ZL = 0;
def MP5L = -0.5;
def M1L = -1;
def SlowAvg = ExpAverage(close,SlowLength); # ema of the slow moving average
def FastAvg = ExpAverage(close,FastLength); # ema of the fast moving average
plot FastSlowPctDiff = ((FastAvg-SlowAvg)/SlowAvg) * 100; # Percent difference between FastAvg and SlowAvg
def FastPctDiff = ((FastAvg - FastAvg[1])/FastAvg[1]) * 100; # Percent difference betwee current and previous bar
plot FastPctDiffAvg = ExpAverage(FastPctDiff,FastAvgSmoothingLength);
FastPctDiffAvg.SetDefaultColor(color.cyan);
def PDGTP = if FastSlowPctDiff > FastSlowPctDiff[1] then 1 else 0;
def PDLTP = if FastSlowPctDiff < FastSlowPctDiff[1] then 1 else 0;
FastSlowPctDiff.AssignValueColor(if PDGTP then Color.GREEN else if PDLTP then Color.RED else Color.YELLOW);
FastSlowPctDiff.SetLineWeight(2);
plot ZLine = ZL;
ZLine.SetDefaultColor(Color.white);
plot P1Line = P1L;
P1Line.SetDefaultColor(color.green);
Plot M1Line = M1L;
M1Line.SetDefaultColor(color.red);
Plot PP5Line = PP5L;
PP5Line.SetDefaultColor(Color.yellow);
PP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
Plot MP5Line = MP5L;
MP5Line.SetDefaultColor(color.yellow);
MP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
plot FastEMAImproving = if FastEMAavg > FastEMAavg[1] then 1 else 0;
FastEMAImproving.Hide();
AddLabel(yes,FastEMA_Length + " EMA Improving", if FastEMAImproving then color.green else if FastEMAavg == FastEMAavg[1]  then color.gray else color.red);
AddLabel(yes,FastLength + " EMA Improving",if FastAvg > FastAvg[1] then color.green else color.red);
def BullConditionGo = if FastPctDiffAvg > FastPctDiffAvg[1] and FastSlowPctDiff > FastSlowPctDiff[1] and FastAvg > FastAvg[1] then 1 else 0;
AddLabel(yes, if BullConditionGo then "   Bull Go   " else "Bull No Go",if BullConditionGo then color.green else color.red);
def BullConditionGoPlus = if BullConditionGo and FastSlowPctDiff > 0 then 1 else 0;
AddLabel(yes,if BullConditionGoPlus then "Bull Go Plus" else "          ", if BullConditionGoPlus then color.green else color.gray);
def BearConditionGo = if FastPctDiffAvg < FastPctDiffAvg[1] and FastSlowPctDiff < FastSlowPctDiff[1] and FastAvg < FastAvg[1] then 1 else 0;
AddLabel(yes, if BearConditionGo then "   Bear Go   " else "Bear No Go",if BearConditionGo then color.green else color.red);
def BearConditionGoPlus = if BearConditionGo and FastSlowPctDiff < 0 then 1 else 0;
AddLabel(yes,if BearConditionGoPlus then "Bear Go Plus" else "          ", if BearConditionGoPlus then color.green else color.gray);
# Conditions for scanning
plot BullGoPlus = if BullConditionGoPlus then 1 else 0;
BullGoPlus.Hide();
plot BearGoPlus = if BearConditionGoPlus then 1 else 0;
BearGoPlus.Hide();
plot highestPos = if Highest(FastSlowPctDiff,5) <= 1 then 1 else 0;
highestPos.Hide();
plot lowestPos = if Highest(FastSlowPctDiff,5) >= 0 then 1 else 0;
lowestPos.Hide();
def TanAngle = ATan(close);
AddLabel(yes,"'Angle Improving'", if (TanAngle > TanAngle[1]) then color.green else color.red);
AddLabel(yes,"Previous 'Angle': " + TanAngle[1],color.gray);
AddLabel(yes,"Current 'Angle': " + TanAngle,color.gray);
 
Last edited by a moderator:
Very interesting indicator. Good work! When I use your code, my red and green zigzag line showing what is happening turns out to be much thinner than the one you show. Is there a way to thicken that line?
 
Very interesting indicator. Good work! When I use your code, my red and green zigzag line showing what is happening turns out to be much thinner than the one you show. Is there a way to thicken that line?


1. click on the beaker at the top of your chart
2. click on the gear icon to the right of your study
3. change the Width of the variable FastSlowPctDiff to your preference
4. click on Save as default to save your preference for all future charts
1U71t7i.png
 
1. click on the beaker at the top of your chart
2. click on the gear icon to the right of your study
3. change the Width of the variable FastSlowPctDiff to your preference
4. click on Save as default to save your preference for all future charts
1U71t7i.png
Thanks! That works.
 
The idea here is that while the stocks may have greatly different prices, the percentage difference between the averages are comparable. A 5% spread between the 12 and 26 ema is the same relative difference by percentage, regardless of the price differences between stocks.

My intent is to do away with the problem of trying to compare the MACD of different stocks that have widely different price levels.

So I converted the difference between the fast and slow ema into a percentage difference.

Here's my script...
Cp9qvql.png


Code:
declare lower;

input FastLength = 12; # Faster of the two moving averages
input SlowLength = 26; # Slower of the two moving averages
input FastAvgSmoothingLength = 9;
input FastEMA_Length = 8;
def FastEMAavg = ExpAverage(close,FastEMA_Length);
def P1L = 1;
def PP5L = 0.5;
def ZL = 0;
def MP5L = -0.5;
def M1L = -1;
def SlowAvg = ExpAverage(close,SlowLength); # ema of the slow moving average
def FastAvg = ExpAverage(close,FastLength); # ema of the fast moving average
plot FastSlowPctDiff = ((FastAvg-SlowAvg)/SlowAvg) * 100; # Percent difference between FastAvg and SlowAvg
def FastPctDiff = ((FastAvg - FastAvg[1])/FastAvg[1]) * 100; # Percent difference betwee current and previous bar
plot FastPctDiffAvg = ExpAverage(FastPctDiff,FastAvgSmoothingLength);
FastPctDiffAvg.SetDefaultColor(color.cyan);
def PDGTP = if FastSlowPctDiff > FastSlowPctDiff[1] then 1 else 0;
def PDLTP = if FastSlowPctDiff < FastSlowPctDiff[1] then 1 else 0;
FastSlowPctDiff.AssignValueColor(if PDGTP then Color.GREEN else if PDLTP then Color.RED else Color.YELLOW);
FastSlowPctDiff.SetLineWeight(2);
plot ZLine = ZL;
ZLine.SetDefaultColor(Color.white);
plot P1Line = P1L;
P1Line.SetDefaultColor(color.green);
Plot M1Line = M1L;
M1Line.SetDefaultColor(color.red);
Plot PP5Line = PP5L;
PP5Line.SetDefaultColor(Color.yellow);
PP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
Plot MP5Line = MP5L;
MP5Line.SetDefaultColor(color.yellow);
MP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
plot FastEMAImproving = if FastEMAavg > FastEMAavg[1] then 1 else 0;
FastEMAImproving.Hide();
AddLabel(yes,FastEMA_Length + " EMA Improving", if FastEMAImproving then color.green else if FastEMAavg == FastEMAavg[1]  then color.gray else color.red);
AddLabel(yes,FastLength + " EMA Improving",if FastAvg > FastAvg[1] then color.green else color.red);
def BullConditionGo = if FastPctDiffAvg > FastPctDiffAvg[1] and FastSlowPctDiff > FastSlowPctDiff[1] and FastAvg > FastAvg[1] then 1 else 0;
AddLabel(yes, if BullConditionGo then "   Bull Go   " else "Bull No Go",if BullConditionGo then color.green else color.red);
def BullConditionGoPlus = if BullConditionGo and FastSlowPctDiff > 0 then 1 else 0;
AddLabel(yes,if BullConditionGoPlus then "Bull Go Plus" else "          ", if BullConditionGoPlus then color.green else color.gray);
def BearConditionGo = if FastPctDiffAvg < FastPctDiffAvg[1] and FastSlowPctDiff < FastSlowPctDiff[1] and FastAvg < FastAvg[1] then 1 else 0;
AddLabel(yes, if BearConditionGo then "   Bear Go   " else "Bear No Go",if BearConditionGo then color.green else color.red);
def BearConditionGoPlus = if BearConditionGo and FastSlowPctDiff < 0 then 1 else 0;
AddLabel(yes,if BearConditionGoPlus then "Bear Go Plus" else "          ", if BearConditionGoPlus then color.green else color.gray);
# Conditions for scanning
plot BullGoPlus = if BullConditionGoPlus then 1 else 0;
BullGoPlus.Hide();
plot BearGoPlus = if BearConditionGoPlus then 1 else 0;
BearGoPlus.Hide();
plot highestPos = if Highest(FastSlowPctDiff,5) <= 1 then 1 else 0;
highestPos.Hide();
plot lowestPos = if Highest(FastSlowPctDiff,5) >= 0 then 1 else 0;
lowestPos.Hide();
def TanAngle = ATan(close);
AddLabel(yes,"'Angle Improving'", if (TanAngle > TanAngle[1]) then color.green else color.red);
AddLabel(yes,"Previous 'Angle': " + TanAngle[1],color.gray);
AddLabel(yes,"Current 'Angle': " + TanAngle,color.gray);

just as an fyi, your conversion to the percentage basis is an old indicator called the PPO (price percent oscillator) just in case you want to see what others have done with that in the past

@Thinker
 

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