Accumulation/Distribution Divergence for ThinkorSwim

BenTen

Administrative
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:

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:

pk1729

Member
VIP
This is interesting. Do you have any example very you saw divergence?

Looking forward to diazlaz enhancements :)
 

BenTen

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

Piper2808t

Member
2019 Donor
VIP
Hi great indicator may this be placed in a column when accumulation dist crosses above pplh line and when it crosses below ppld line thanks much
 

mansor

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

Leo1015

Member
VIP
@BenTen , nice indicator. Is there a scanner script with watchlist script as well or can this study be put into the TOS scanner, if so what paramaters do you recommend? Thanks
 

MerryDay

Administrative
Staff member
Staff
VIP
@Leo1015
Not sure what you are asking as divergences are more of an eye-balling exercise.
However, if you can articulate what you are looking for, you can write a scan. Therefore, please re-write your request using the three parts of defining filters in studies, scanners, watch lists:

1. Tell us what you are looking for using conditional statements. Start your request with "if" "then" and do not forget the "else".
2. Use ONLY mathematical terms. If "price is close to" is NOT a math. If price is greater than, between, 5% higher are all examples.
3. Use defined stock market variables and functions. close, high, volume are defined. average, highest, and such are defined.

The above is 90% of what is necessary for writing conditions for studies, scanners, watch lists. The last 10% is the exact syntax that TOS requires. But if you provide the 90%, almost anyone on here will be able to clean up any syntax.
 

Leo1015

Member
VIP
Hi Merry,

Thanks for your feedback. I use the following script below for accumulation and distribution which I got from this site. I've tried several times to insert this code into the scan section of TOS as a study, selecting AccDist value is greater than or equal to X but it always provides an error. I've also have tried crosses below, cross above, but I still get the same error.

I would just like the scanner portion to advise me when the AccDist (plot) is greater than or equal to Value(condition) X .
In a separate study, I would also like to do the same for the SlowD (plot)

Would you know how to configure it in the TOS scanner or provide a code which would do this task? I hope this helped clarify what I'm looking for.

accumulation and distribution Code:

declare lower;

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));
AccDist.SetDefaultColor(GetColor(1));

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));

plot one = 1;
plot zero = 0;
 

MerryDay

Administrative
Staff member
Staff
VIP
@Leo1015 There is nothing in that study to scan for. Please verbalize using the criteria in my post above to state mathematically what conditions you are looking for.
 

Leo1015

Member
VIP
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.
 

MerryDay

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

MerryDay

Administrative
Staff member
Staff
VIP
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);
 

iselloptions

New member
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:

MerryDay

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

New member
@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
 

Similar threads

Top