Posts Tagged ‘divergence’

Multi-Divergence Indicator for Think or Swim: MACD, RSI, CCI, On Balance Volume and many more

January 11, 2015


I figured out a way to combine all my divergence indicators into one single study. The new Multi-divergence script uses slopes of linear regression lines just like in the new MACD divergence indicator. This script supersedes all the other divergence scripts. The difference is now it can perform a regression on any one study of your choice from a list of ToS indicators. The indicators currently supported for divergence analysis are:

MACD, RSI, CCI, Momentum, Moneyflow, On Balance Volume, Rate of Change, Stochastic Momentum Index, Ultimate Oscillator, Volume Flow Indicator, Volume Oscillator, Volume Rate of Change, Volume-Weighted MACD, Williams Percent-R, and Woodie’s CCI

(Note that it doesn’t scan all of these for divergence at once. Just the one indicator you select. If you want to have RSI and MACD at the same time for example, you put the script on your chart twice and select the appropriate indicator for each one separately.)

If there is another built-in ToS indicator you want to have available for divergence analysis, let me know and I can add it in future updates. You can use the Multi-divergence indicator on real time charts and in scans of watch lists, and I put in alert logic so it can ping you when a divergence occurs.

This indicator is for blog donors only.  You can find it on my google site under Released Thinkscript Studies down in the Donors Only section. If you already donated in the past you can use your password to access it. If you want to become a donor (or throw me some more coin) you can do so by clicking the Donate button:

As always, if you are a DIY’er, feel free to ask questions in the comments and I’ll help answer.

UPDATE: MACD Divergence Indicator for Think or Swim

December 10, 2014

This indicator has been updated!  See the new post here.

This has been a long time in coming.  My family life has been in upheaval for a couple years now.  I finally had a bit of time so I wanted to get this done.  Thanks for staying with me.

Many people have asked for a version of the MACD Divergence Indicator that can run in a scan.  The old one I had ran on recursive logic and so wasn’t supported in scans.  This new one uses a different philosophy that runs in real time.  Before I would use my Swing Points and check the value of the MACD against them.  Higher swing highs and lower corresponding values of MACD on those bars would signal a bearish divergence.  Now I am using Linear Regression slopes to compare divergences.  Here’s the theory of how it works:

A linear regression is a way to fit a straight line through some data such that you get the least amount of average distance from the line. If the slope of the linear regression is up, then values are generally trending upward over the set of data you put in. If the slope is negative, then the values trend downward.

So I take a linear regression of price, then get the slope of the LR, and I also take a linear regression of the standard MACD indicator and get that slope. When the price slope is positive and the MACD slope is negative, we have a bearish MACD divergence. If price slope is negative and MACD slope is positive, we have a bullish divergence. If price slope and MACD slope are the same, we have a trend continuation (up/up or down/down). This chart shows this theory in action:



Here’s what my indicator actually looks like.  The small arrows are short term divergences, the larger arrows are the long term divergences.  The short and long timeframes are inputs, so you can set them at whatever you want.  This chart uses 20 and 50 as the inputs, but you can experiment with what works best for what you are trading:


As with all divergences, just because it is there doesn’t mean that the trend must reverse. Sometimes divergences can go on for a long time. This information is good to give you a sense that a trend might reverse, and you can plan your own entry and stop accordingly.

Now, to set set up a custom scan, you follow the instructions in these pictures:



Then when your scan runs, you will get flagged if the divergence you asked for is currently found.

This indicator is for blog donors only.  You can find it on my google site under Released Thinkscript Studies down in the Donors Only section.

RSI Divergence Indicator for Think or Swim

January 12, 2010

This indicator has been updated!  See the new post here.

In a similar vein to my MACD and CCI Divergence indicators, here’s an indicator that checks for divergences between RSI and price. It’s basically just a modified version of the CCI divergence indicator I did, but using RSI instead. Also added is a filter that only plots signals if a bearish divergence is in overbought territory, and if a bullish divergence is in the oversold area. There are two indicators. The first, “Pro_RSIDivergence_v1STUDY.ts” looks for when the RSI values diverge from price making new swing highs or lows. The second, “Pro_LowerRSIDivergence_v1STUDY.ts” reverses the process, looking for price to diverge from new swing highs or lows on the RSI indicator itself, and is plotted in the lower panel. I would tend to use the upper one only, but you may want the other one, so I did it. I also added the alert code block that I highlighted earlier, so you can let it run on a chart and get alerted if a divergence shows up.

The two studies are found in “” and can be downloaded in the “Donors Only” section of “Released Thinkscript Studies” on my Google site. As always, home-gamers can ask me any questions if they want to make one for free by themselves. The CCI tutorial covers all the same coding I used for this one.

CCI Divergence Indicator for Think or Swim

May 21, 2009

This indicator has been updated!  See the new post here.

I got commissioned to write more divergence indicators, and the customer was generous enough to allow me to share it with the rest of the donors. I say donors because it builds on my donor-only fully variable swing points indicator, so I’m not releasing it freely as a sponsored indicator. If you are a past (or future 🙂 ) donor, you can get it from the “Released Thinkscript Studies” page, called “”. I like this “donor share” idea and will keep doing it as those who commission custom work are willing to share. If you don’t care to donate or you just want to do-it-yourself, it’s not complicated to write your own if you already have a swing points logic built, like I outlined previously. I’ll walk through the additional steps below.

Following 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, and divergences between Price and CCI. There’s one upper study and one lower study.

Every time we get a lower swing low in price, the CCI is checked to see if it also prints a lower value. Similar for highs; higher high without higher CCI is a divergence. Here is a picture of the indicator at work. On the upper frame, the ProCCIDivergence is plotted as a red dot for bearish divergence, and a green dot for bullish divergence. On the lower frame, the opposite divergence is checked. If the CCI has a a lower swing low, the price lows are checked to see if they also print a lower value. Similar for highs; higher CCI swing high without higher price high is a divergence. The colors on the lower plot look backwards, but remember–the lower indicator is plotting a bullish divergence for the value of CCI, NOT the value of price. And as CCI rises, price generally declines.


To make the top study, just follow the MACD divergence tutorial, but in the recursive divergence functions, change the references from MACD to CCI in this way:

input l=14; #This is the CCI input length
rec blCCI = if swinglow then reference CCI(length=l).CCI else blCCI[1];
rec brCCI = if swinghigh then reference CCI(length=l).CCI else brCCI[1];

And there you go. If you also want the lower study, you change it up a bit. The swing point checks are reversed in that you check CCI values for swing highs and lows, and then you define your bullish and bearish divergence recursive functions above to get the value of price if you are at a CCI swing high or CCI swing low, like this:

input l=14; #This is the CCI input length
def CCI=reference CCI(length=l).CCI;
Def swinghigh = if CCI > CCI[1] and CCI > CCI[2] and CCI > CCI[-1] and CCI > CCI[-2] then 1 else 0;
Def swinglow = if CCI < CCI[1] and CCI < CCI[2] and CCI < CCI[-1] and CCI < CCI[-2] then 1 else 0;
rec blPRICE = if swinglow then low else blPRICE[1];
rec brPRICE = if swinghigh then high else brPRICE[1];

Then plot what you want, and go from there! Personally, I think the lower study is less useful than the upper, but you may feel differently.

That’s it! If you are making your own, leave a comment if you have any questions. If you just want to grab mine, donate away:

MACD Divergence Indicator for Think or Swim

May 6, 2009

This indicator has been updated!  See the new post here.

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:


And next a daily chart of ES:


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;


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:


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.