MACD Divergence Indicator for ThinkorSwim

I am writing a simple divergence indicator with following logic:
  1. Search for highest high in price in last 3 months (this can be any period)
  2. Compare with current price and ensure current price is equal or above the highest high
  3. Compare MACD between the two points and ensure MACD value for current bar is less than the highest bar.
Coded as below:

Code:
input length = 30;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;

def highestHigh = highest(high, length);
def MACD = MACD(fastLength, slowLength, MACDLength).Diff;
def MACDHist = MACD(fastLength, slowLength, MACDLength).Hist;

plot Scan = high > highestHigh and MACD - MACD[1] < 0 and MACDHist - MACDHist[1] < 0;

Issue I am facing is how to get the MACD value for the highest high price bar? I don't think the current code logic is right. MACD[1] will give value for the 1 bar ago and not the highest high price bar.

Appreciate all the help. Thanks
 
Last edited by a moderator:

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

I think the best way to go about this is to do something akin to this:
Code:
def highestHigh = highest(high, length);
def local_MACDDiff = MACD(fastLength, slowLength, MACDLength).Diff;
def local_MACDHist = MACD(fastLength, slowLength, MACDLength).Hist;
def stored_macd_diff = if high = highestHigh then local_MACD_diff[-1] else stored_macd_diff[1];
plot scan = if high == highestHigh and local_MACD_diff < stored_macd_diff[1] then 1 else double.nan;

Some notes
1. Don't use study names for your local variables. it just gets confusing. I defined local_macd rather than using the macd name... keeps my brain focused on which is which.
2. the value of stored_macd is updated only when the high is the same as the highestHigh -- that's how we set and reset the value
3. I used a future value of local_macd_diff when setting the stored_macd_diff. I don't know if this will do what is needed or not. I'm trying to ensure that, even when the value is checked later (local_MACD_diff < stored_macd_diff[1]) it is checked against the value for the correct bar. We have to check against a past value of stored_macd_diff since if high == highestHigh we will have reset stored_macd_diff to the new (current bar) value.

But I haven't had enough coffee to test this in ToS. It's all just typed here.

best of luck,
mashume
 
@BenTen Thanks for the script. Unfortunately I am unable to figure out how to use it as a scanner. Basically I want to scan for daily/weekly divergence on equities over a period of last 30/60/90 days. Should I save the script as a study and create a scan using it for various time frames? But how do specify the time frame? Thanks
 
@BenTen Thanks for the script. Unfortunately I am unable to figure out how to use it as a scanner. Basically I want to scan for daily/weekly divergence on equities over a period of last 30/60/90 days. Should I save the script as a study and create a scan using it for various time frames? But how do specify the time frame? Thanks

You can specify the timeframe via the Scan tab.

zqUg3mK.png
 
multi time frame version.....
https://usethinkscript.com/threads/...divergence-for-thinkorswim.12091/#post-121568
the above link is not correct,,,,

Code:
# MACDDIVERGENCE_R1V1
# R1V1   2011.05.14:22:15 - TONY LIPTON
#        monitors macd divergenge for entry

#        MAJOR CODE CONTRIBUTION by KumoBob aka Bob Campbell
#        http://fibonacci-financial.blogspot.com/

INPUT VFL = 12;
INPUT VSL = 26;
input vtp = aggregationPeriod.DAY;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

DEF Value = MovingAverage(averageType, close(period = vtp), VFL) - MovingAverage(averageType, close(period = vtp), VsL);
DEF Avg = MovingAverage(averageType, Value, MACDLength);

DEF Diff = Value - Avg;

DEF VLB = (VFL + VSL);#/2;

DEF vh34 = highest(diff,VLB);
DEF vl34 = lowest(diff,VLB);


input ChartBubblesON = NO;
input LinesON = Yes;

rec top;
rec bot;

top = if
diff == VH34
then High(period = vtp)  else top[1];
bot = if
diff == VL34
then Low(period = vtp)  else bot[1];


plot topline = if !LinesOn then Double.NaN else top;
topline.SetLineWeight(1);
topline.assignValueColor(if ((top < HIGH(period = vtp))) then COLOr.PLUM  else color.DARK_GREEN);
topline.SetPaintingStrategy(PaintingStrategy.points);
plot bottomline =  if !LinesOn then Double.NaN else bot;
bottomline.SetLineWeight(1);
bottomline.assignValueColor(if ((LOW(period = vtp) < bot)) then color.BLUE else color.DARK_RED);
bottomline.SetPaintingStrategy(PaintingStrategy.dashes);
# _____________________________________________________________
# Thinkscript is the property of ThinkorSwim and TDAmeritrade
# Sections of this script may have been copied or modified from
# one or more Thinkscript studies in part or in their entirety.
# _____________________________________________________________
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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