# Accumulation/Distribution Divergence for ThinkorSwim

#### BenTen

Staff member
Staff
VIP
Typically, the Accum Dist indicator is used to identify divergences between price activity and the indicator itself. As with other divergences, if the market reaches new highs while the indicator is stagnant or falling, the current trend may be weakening suggesting a possible reversal. Conversely, if the market reaches new lows while the indicator is stagnant or rising, the trend may be weakening perhaps indicating a reversal.

Based on this description, I asked @korygill to help me plot the previous day high and low of the default AccumDistBuyPr indicator provided by ThinkorSwim. Since we're looking for possible divergences, we would need to also plot the previous day high and low of the stock. Luckily, there is already a built-in indicator to help you with that. It's called DailyHighLow.

Putting the two together, you will have this:

### thinkScript Code

Code:
``````#
# Accumulation_Distribution_Divergence
# Assembled by Kory Gill (@korygill) for BenTen at usethinkscript.com
#

declare lower;
declare once_per_bar;

input OpenTime = 0930;

def bn = BarNumber();
def nan = double.NaN;
def sft = SecondsFromTime(Opentime);
# plot p1 = sft;

def data = if close > close[1] then close - Min(close[1], low) else if close<close[1] then close - Max(close[1], high) else 0;
def SumData = if bn == 1 then data else SumData[1] + data;
# dont use built in function, it's slow.  Avoid calling TotalSum on each subsequent bar.
def hVal;
def lVal;

if bn == 1 then
{
hVal = nan;
lVal = nan;
}
else if bn == 2 then
{
}
else
{
if sft == 0 then
{
}
else
{
}
}

def pdh = if sft[-1] == 0 then hVal else pdh[1];
def pdl = if sft[-1] == 0 then lVal else pdl[1];

plot ppdh = pdh;
plot ppdl = pdl;
ppdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdh.SetDefaultColor(GetColor(1));
ppdl.SetDefaultColor(GetColor(0));

#plot ph = hVal;
#plot pl = lVal;

Last edited by a moderator:
This is interesting. Do you have any example very you saw divergence?

Looking forward to diazlaz enhancements

@pk1729 Take a look at the screenshot in the original thread. I saw a few from it. What I usually do is compare the current Buying Pressure line (in white at the lower study) to the current stock price. Sort of like looking at the rate of change for both.

Here we have the same screenshot from above but highlighted with possible divergences.

@BenTen how is this different than QQE. I generally getting a sense that some scripts is a derivation or a nuance of another script..

@mansor This is completely different, it doesn't use RSI.

Objective, the script provided previously provides a lower graph with values. I just want the scanner to advise me when the AccuDist and SlowD lines are at a certain value:
1. If SlowD > X and < Y, then alert; else if <X and > Y then ignore
2. If AccuDist > X and < Y, then alert; else if <X and > Y then ignore
3. Would like that these conditions be separate studies for the scanner so I can easily change the parameters independently and accordingly.
Hope this clarifies what I would like. Thanks again for taking the time to help out.
4. Would like the capability to change the time frame accordingly

I've attached a snap shot of the script which provides two values AccuDist (green) and SlowD (pink). These are the values I want identified at certain key levels.

@Leo1015 Now that's the way! You have written 90% of the scan! Cut&Paste these scripts into Create Studies and Save. In scan look up the name of each study and scan for scantrigger is true
Ruby:
``````# AccuDist Scan Only by Leo1015 6/21
input X = 1;
input Y = 0;
def data = if close > close[1] then close - Min(close[1], low) else if close < close[1] then close - Max(close[1], high) else 0;

plot scantrigger = AccDist > X and AccDist < Y ;``````

Ruby:
``````# SlowD Scan Only by Leo1015 6/21
input X = 1;
input Y = 0;
def stoch = StochasticSlow("over bought" = 100, "over sold" = 0, "k period" = 14, "d period" = 1).SlowD;
def SlowD = (stoch - lowestall(stoch)) /(highestall(stoch) - lowestall(stoch));

plot scantrigger = SlowD > X and SlowD < Y ;``````

Last edited:
Leo1015 You had the logic and the syntax exactly right! Very impressive!
Ruby:
``````# AccuDist WatchList by Leo1015 6/21
def data = if close > close[1] then close - Min(close[1], low) else if close < close[1] then close - Max(close[1], high) else 0;

AssignBackgroundColor(if AccDist > 0 then color.green else if AccDist < 0 then color.red else color.gray);``````

Ruby:
``````# SlowD Scan Only by Leo1015 6/21
def stoch = StochasticSlow("over bought" = 100, "over sold" = 0, "k period" = 14, "d period" = 1).SlowD;
plot SlowD = (stoch - lowestall(stoch)) /(highestall(stoch) - lowestall(stoch));

AssignBackgroundColor(if SlowD > 0 then color.green else if SlowD < 0 then color.red else color.gray);``````

I play around with the study in post#1 and I realize the numbers are a bit different the TOS Accumulation/Distribution. Does it matter much?

Last edited by a moderator:
@iselloptions This study is based on the previous day high and low of the default AccumDistBuyPr indicator and 'seems' to be working correctly.
Could you provide screenshots of what you are seeing?

Unsure of how to upload screenshots to the forum, Here are directions.

@iselloptions This study is based on the previous day high and low of the default AccumDistBuyPr indicator and 'seems' to be working correctly.
Could you provide screenshots of what you are seeing?

Unsure of how to upload screenshots to the forum, Here are directions.
hey so i kind of figured it out. your right it is the same as the accumdistbuypr. The difference i was seeing is that thinkorswim's updates every tick while the custom divergence script updates only when the recent candle is complete. realized the same for rsi divergence script we have on another thread. not sure if its a big deal, especially on smaller time frames

@kakamora It is not really possible to determine why a code won't scan if we have no code to troubleshoot.
Hello MerryDay, I have created the study with below given your code. and using that study to create a scanner as shown in above screenshot.

Ruby:
# AccuDist Scan Only by Leo1015 6/21
input X = 1;
input Y = 0;
def data = if close > close[1] then close - Min(close[1], low) else if close < close[1] then close - Max(close[1], high) else 0;

plot scantrigger = AccDist > X and AccDist < Y ;

@kakamora You are scanning for:
plot scantrigger = AccDist > 1 and AccDist < 0 ;
It will never be true. Fix the signs in your study:
plot scantrigger = AccDist < X and AccDist > Y ;
And the scan now works:

#### Attachments

• Screenshot (153).png
213.7 KB · Views: 292
Hello MerryDay,

Code:
``````declare lower;

input max_distday = 9;
input accumulation_or_distribution = {"accum", default "dist"};

def uVolume = close("\$UVOL");
def dVolume = close("\$DVOL");

plot baseline = 1.8;
plot distribution_day = max_distday;

plot volume;

switch(accumulation_or_distribution){
case accum:
volume = uVolume / dVolume;
default:
volume = dVolume / uVolume;
}

volume.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
volume.DefineColor("Positive", Color.UPTICK);
volume.DefineColor("Negative", Color.DOWNTICK);
volume.AssignValueColor(if volume >= max_distday then volume.color("Positive") else volume.color("Negative"));

#end code``````

I am trying to create the scanner when Volume is greater than or equal to max_distday, but some how it's not successful. your help would be greatly appreciated

Code:
``````plot scan = volume >= max_distday ;
scan.hide();``````

Based on this description, I asked @korygill to help me plot the previous day high and low of the default AccumDistBuyPr indicator provided by ThinkorSwim. Since we're looking for possible divergences, we would need to also plot the previous day high and low of the stock. Luckily, there is already a built-in indicator to help you with that. It's called DailyHighLow.

Putting the two together, you will have this:

### thinkScript Code

Code:
``````#
# Accumulation_Distribution_Divergence
# Assembled by Kory Gill (@korygill) for BenTen at usethinkscript.com
#

declare lower;
declare once_per_bar;

input OpenTime = 0930;

def bn = BarNumber();
def nan = double.NaN;
def sft = SecondsFromTime(Opentime);
# plot p1 = sft;

def data = if close > close[1] then close - Min(close[1], low) else if close<close[1] then close - Max(close[1], high) else 0;
def SumData = if bn == 1 then data else SumData[1] + data;
# dont use built in function, it's slow.  Avoid calling TotalSum on each subsequent bar.
def hVal;
def lVal;

if bn == 1 then
{
hVal = nan;
lVal = nan;
}
else if bn == 2 then
{
}
else
{
if sft == 0 then
{
}
else
{
}
}

def pdh = if sft[-1] == 0 then hVal else pdh[1];
def pdl = if sft[-1] == 0 then lVal else pdl[1];

plot ppdh = pdh;
plot ppdl = pdl;
ppdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppdh.SetDefaultColor(GetColor(1));
ppdl.SetDefaultColor(GetColor(0));

#plot ph = hVal;
#plot pl = lVal;

Hello @BenTen,

I am a newbie here. Thank you for your code. Is there a way this can be applied to any time frame chart ( i.e., Daily, or weekly)? It should be independent of the start time. If so, can you please help with it? Also, can you please help if I choose to consider the time period form 0930 till 1600 for the divergence.

Much appreciated

Last edited by a moderator:

87k+ Posts
238 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?