MACD Using Percentages For ThinkOrSwim

LetsMakeMoney

New member
VIP
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
 

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