Accumulation/Distribution Divergence for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
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:

BLscgtM.png


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;
def ADBP = SumData;
# dont use built in function, it's slow.  Avoid calling TotalSum on each subsequent bar.
# def ADBP = AccumDistBuyPr().AccDist;
def hVal;
def lVal;

if bn == 1 then
{
    hVal = nan;
    lVal = nan;
}
else if bn == 2 then
{
    hVal = ADBP;
    lVal = ADBP;
}
else
{
    if sft == 0 then
    {
        hVal = ADBP;
        lVal = ADBP;
    }
    else
    {
        hVal = Max(hVal[1], ADBP);
lVal = Min(lVal[1], ADBP);
    }
}

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;

plot pADBP = ADBP;

#def openADBP = if sft == 0 then ADBP else openADBP[1];
#def PO = openADBP;
 
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.

cbyOxvB.png
 
@BenTen how is this different than QQE. I generally getting a sense that some scripts is a derivation or a nuance of another script..
 
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;
def AD = TotalSum(data);
def AccDist = (AD - lowestall(AD)) /(highestall(AD) - lowestall(AD));

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;
def AD = TotalSum(data);
plot AccDist = (AD - lowestall(AD)) /(highestall(AD) - lowestall(AD));

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.

Study name - >R_AD_scan

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;
def AD = TotalSum(data);
def AccDist = (AD - lowestall(AD)) /(highestall(AD) - lowestall(AD));

plot scantrigger = AccDist > X and AccDist < Y ;
 
Hello MerryDay,
can you please help me to create a scanner for below study.

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
 
@kakamora add the following statements to the bottom of your study
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:

BLscgtM.png


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;
def ADBP = SumData;
# dont use built in function, it's slow.  Avoid calling TotalSum on each subsequent bar.
# def ADBP = AccumDistBuyPr().AccDist;
def hVal;
def lVal;

if bn == 1 then
{
    hVal = nan;
    lVal = nan;
}
else if bn == 2 then
{
    hVal = ADBP;
    lVal = ADBP;
}
else
{
    if sft == 0 then
    {
        hVal = ADBP;
        lVal = ADBP;
    }
    else
    {
        hVal = Max(hVal[1], ADBP);
lVal = Min(lVal[1], ADBP);
    }
}

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;

plot pADBP = ADBP;

#def openADBP = if sft == 0 then ADBP else openADBP[1];
#def PO = openADBP;
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:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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