MACD Divergence Indicator for Think or Swim

Following on the heels of my Swing Points indicator, I wrote an indicator that looks for divergences between the MACD and price. Every time we get a lower swing low, the MACD is checked to see if it also prints a lower low. Similar for highs; higher high without higher MACD is a divergence. Here are two pictures of the indicator at work. I have my Pro SwingPoints plotted in gray, and the ProMACDDivergence plotted as a red dot for bearish divergence, and a green dot for bullish divergence

First, a 5min chart of ES:

macddivergencesignals

And next a daily chart of ES:

macddivergencesignals_daily

This is not really a strategy I am interested in personally, but many people use MACD as a part of their trading system. So, I won’t go into the theory of how to use it or sing its praises here. There are plenty of sites out there with that info. I’ll talk about the construction of the indicator so you can “try this at home”.

As I said before, you could define your swing points as follows:

Def swinghigh = if high > high[1] and high > high[2] and high > high[-1] and high > high[-2] then 1 else 0;

and

Def swinglow = if low < low[1] and low< low[2] and low < low[-1] and low < low[-2] then 1 else 0;

If you want x more bars to define the swing points, you add more high > high[x] and low < low[x] checks.

Next, you use the “reference” Thinkscript command to access the built-in code for the MACD indicator. To use reference, simply type “reference” and the name of the built-in study you want to access, followed by a “.” and the name of the output parameter you want to get. For this example, I wanted MACD and “value” as my output, so I used “reference MACD.value”. To find all of the possible output parameters and how to call them, add the built-in study to any to a chart, and then look at the names of all of the outputs:

macdinputs

You can also specify both inputs and outputs, such as “reference MACD(macdlength=5).diff”, which would change the period of the moving average of MACD from the default of 9 to 5, and return the histogram value. If you don’t include an input in a reference call, Thinkscript will just use the default value.

Meanwhile, back at the ranch, I wrote two recursive functions to check for and track the value of MACD at each swing point:

rec blMACD = if swinglow then reference MACD.value else blMACD[1];
rec brMACD = if swinghigh then reference MACD.value else brMACD[1];

This code will check if the current bar is a swing point according to your definition, and if so, it returns the value of the MACD there. If it is not a swing point, it just rolls the prior swing point MACD value forward. I also wrote similar functions to track the prior value for swing lows and swing highs using the same type of logic. The final task is to check at each swing low to see if it is lower than the previous swing low, but has a higher value of MACD for bullish divergence, and vice versa for bearish divergence, i.e. higher swing high, lower MACD value. If the check for divergence is true, I plot the respective low/high of the swing point bar, colored red for bearish and green for bullish.

This should be enough info for you to write a study of your own if you want. Feel free to ask questions if you need any help; I’m always glad to teach people who want to learn! Alternatively, if you “gotta have it now”, you can get it as “Jackson-ware”. Donate $20 and I’ll email the script out to you. If you donate, be sure to email me and let me know what you donated for so I can send it to you! The Pro MACDDivergence indicator has all the source code I used, including the generalized method to change the number of bars used to identify swing points as a simple input.

Tags: , , ,

17 Responses to “MACD Divergence Indicator for Think or Swim”

  1. New Downloads Site « Read the Prospectus Says:

    [...] Read the Prospectus Trading, Think or Swim Programming and other Rocket Science « MACD Divergence Indicator for Think or Swim [...]

  2. manatrader Says:

    That is really great, thanks :)

  3. Prospectus Says:

    Glad you like it!

    Once you have the swing points framework in place, you can check for any kind of divergence you want: MACD, RSI, On balance volume, anything you can code or reference. Pretty cool.

  4. CCI Divergence Indicator for Think or Swim « Read the Prospectus Says:

    [...] on the heels of my Swing Points and MACD Divergence indicators, here’s two more indicators that look for divergences between the CCI and price, [...]

  5. RSI Divergence Indicator for Think or Swim « Read the Prospectus Says:

    [...] a similar vein to my MACD and CCI Divergence indicators, here’s an indicator that checks for divergences between RSI [...]

  6. Hong Says:

    Hi,

    I want to donate and get this script, but I can’t find your email address. Can you send me an email? Thanks.

  7. Prospectus Says:

    My contact info is here:

    http://readtheprospectus.wordpress.com/about/

    You can also click the “donate” button from the sidebar on the blog.

  8. Hong Says:

    Oops, I think I found your email. ;-)

    ****************@gmail.com

    edit: obfuscating for the spambots. But you are
    correct.

  9. Chris Fischer Says:

    have some trouble getting the MACD divergence TOS script to work. I cut and pasted everything into the editor but is says “At least one plot must be defined”. Please advise.

  10. Prospectus Says:

    Sent you an email

  11. leo Says:

    same problem here, i have troubles to make it works. Any help?

  12. Terry Hunt Says:

    I purchased the mcad divergence and imported into tos and it does not look any thing like the picture above.. all I get is red and green arrows.

  13. Prospectus Says:

    The trend lines that are in that chart are just illustrative. I drew them by hand to highlight what the indicator is doing. The study uses the math to look for the same thing and then prints a dot / arrow if a divergence is found. I’m working on alternate ways to calculate and show the divergences to make it more visible like my drawing is. At any rate, Ii you are not happy with the indicator, let me know and I’ll refund your donation.

    On Feb 12, 2013, at 10:31 PM, Read the Prospectus

  14. Terry Hunt Says:

    Thanks for the responce, no I just needed it clarified alittle, thanks .

  15. Prospectus Says:

    Sure, don’t hesitate to ask anything else!

    On Feb 14, 2013, at 12:16 AM, Read the Prospectus

  16. Ravi Says:

    Hi,
    I am interested in this script, can you use this script to scan for stocks which are signaled divergence?

  17. Prospectus Says:

    Not yet. I have one in the works though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 108 other followers

%d bloggers like this: