Williams AD is the accumulated sum of positive "accumulational" and negative "distributional" price movements.

Calculation:

To calculate the accumulation/distribution indicator, first you have to find a "True Range High" (TRH) and "True Range Low" (TRL):

TRH (i) = MAX (HIGH (i) || CLOSE (i - 1))

TRL (i) = MIN (LOW (i) || CLOSE (i - 1))

Then you must find the current value of accumulation/distribution by comparing today and yesterday's closing prices.

1. If the current closing price is higher than the previous one, then:

CurА/D = CLOSE (i) - ТRL (i)

2. If the current closing price is lower than the previous one, then:

CurА/D = CLOSE (i) - ТRH (i)

3. If current and previous closing prices coincide then:

CurА/D = 0

Williams accumulation/distribution indicator is a growing sum of these values for each day:

WА/D (i) = CurА/D + WА/D (i - 1)

Where:

TRH (i) — the True Range High;

TRL (i) — the True Range Low;

MIN — the minimum value;

MAX — the maximum value;

|| — the logical OR;

LOW (i) — the minimum price of the current bar;

HIGH (i) — the maximum price of the current bar;

CLOSE (i) — the closing price of the current bar;

CLOSE (i - 1) — the closing price of the previous bar;

CurА/D — means current value of accumulation/distribution;

WА/D (i) — the current value of Williams Accumulation/Distribution indicator;

WА/D (i - 1) — the value of Williams Accumulation/Distribution indicator on the previous bar.

- Code: Select all
`function Init()`

indicator:name("Williams Accumulation/Distribution (WAD)");

indicator:description("Williams Accumulation/Distribution (WAD)");

indicator:requiredSource(core.Bar);

indicator:type(core.Oscillator);

indicator.parameters:addColor("clrWAD", "Color of WAD", "Color of WAD", core.rgb(0, 255, 0));

end

local first;

local source = nil;

local WAD;

function Prepare()

source = instance.source;

first=source:first()+2;

local name = profile:id() .. "(" .. source:name() .. ")";

instance:name(name);

WAD = instance:addStream("WAD", core.Line, name .. ".WAD", "WAD", instance.parameters.clrWAD, first);

end

function Update(period, mode)

if (period>first+1) then

local TRH=math.max(source.high[period],source.close[period-1]);

local TRL=math.min(source.low[period],source.close[period-1]);

local AD;

if source.close[period]>source.close[period-1]+source:pipSize() then

AD=source.close[period]-TRL;

elseif source.close[period]<source.close[period-1]-source:pipSize() then

AD=source.close[period]-TRH;

else

AD=0.;

end

WAD[period]=WAD[period-1]+AD;

else

WAD[period]=0.;

end

end