MACD and W% indicator

michael9

New member
Hi, I'm working on creating an indicator that looks for a crossover of W% from below to above -80 as well as an increasing value of MACD, within a preset # of bars of one another. I have the code below pretty close to working, but I notice some errors in the indicator and I can't figure out what is going wrong with the logic. For example, if I look at AAPL, the indicator seems to be working correctly on the WEEK chart, but not on the DAY chart. It has triggered on the DAY despite a steadily decreasing MACD value around the same time as the W% crossover occurs. What am I missing? Thanks in advance!



Code:
declare lower;

input LookBack = 7;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Diff = MACD(fastLength, slowLength, MACDLength, averageTypeMACD).Diff;

def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);

input length = 10;
input overBought = -20;
input overSold = -80;

def hh = Highest(high, length);
def ll = Lowest(low, length);
def result = if hh == ll then -100 else (hh - close) / (hh - ll) * (-100);

def WR = if result > 0 then 0 else result;

def WRcheck = fold n = 0 to LookBack
with p = 0
do p + ((GetValue(WR, n) > -80) and (GetValue(WR, n + 1) < -80));

def MACDCheck = fold i = 0 to LookBack
with q = 0
do q + (GetValue(Value, i + 1) < GetValue(Value, i));

plot Data = (WRcheck >= 1 and MACDCheck >= 1);
 
Here is a perfect example. This is current AAPL chart. At #1 the indicator appears to have fired correctly. But at #2 it has fired, but the MACD is decreasing not increasing so it should not have.

n6y2IzK.png
 

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

I think I figured out the answer to my question. For some reason the "Value" of the MACD M line was not working consistently. I had assumed that if the slope was increasing, that Value[0] would be greater than Value[1] but that did not seem to be the case. I played with using ATan function, but that also gave inconsisent results. The best calculation I found was to use (Value[0]-Value[1])/2 and this worked the way I would expect. Revised code below:
Code:
declare lower;
declare once_per_bar;

input LookBack = 7;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.EXPONENTIAL;

def Diff = MACD(fastLength, slowLength, MACDLength, averageTypeMACD).Diff;

def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def height = avg - avg[1];

#old angle calculation, not reliable for my purposes:
#def "Angle, deg" = ATan(height/1) * 180 / Double.Pi;

def "Angle, deg" = (Value[0]-Value[1])/2;

input length = 14;
input overBought = -20;
input overSold = -80;

def hh = Highest(high, length);
def ll = Lowest(low, length);
def result = if hh == ll then -100 else (hh - close) / (hh - ll) * (-100);

def WR = if result > 0 then 0 else result;

def WRcheck = fold n = 0 to LookBack with p = 0 do p + ((GetValue(WR, n) > -80) and (GetValue(WR, n + 1) < -80));

def MACDCheck = "Angle, deg" > 0;

plot Data = (WRcheck >= 1 and MACDCheck >= 1);
Data.SetDefaultColor(Color.GREEN);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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