# Accumlative Swing Index

This script implements the Swing Index and the Accumulative Swing Index from the formulas in Welles Wilder's book, "New Concepts in Technical Trading Systems".   This plots in a study panel below the chart.  You can adjust the range of the panel plot and the panel labels to suit the charts you typically will put the plot on.   My present range of -300 to 300 seems adequate for the charts I tested with.

// Accumulation Swing Index
// reference: New Concepts in Technical Trading Systems
// author:  Welles Wilder

function SwingIndex(i: integer; limit: real): real;
var
r,r1,r2,r3,r4,k,l,si,c1,c2,o1,o2,h1,h2,l1,l2: real;
begin
if i>1 then begin
c2:=Last(i);  {this bar}
o2:=Open(i);
h2:=High(i);
l2:=Low(i);
dec(i);
c1:=Last(i);  {prior bar}
o1:=Open(i);
h1:=High(i);
l1:=Low(i);

if limit<=1 then limit:=10000; {assign a default for limit}

r1:=abs(h2-c1);      {used in finding R equation}
r2:=abs(l2-c1);
r3:=abs(h2-l2);
r4:=abs(c1-o1);
k:=Max(r1,r2);

if r1>=Max(r2,r3) then r:=r1-r2/2+r4/4
else if r2>=Max(r1,r3) then r:=r2-r1/2+r4/4
else r:=r3+r4/4;

if r=0 then Result:=0
else Result:=50*((c2-c1+0.5*(c2-o2)+0.25*(c1-o1))/r)*k/limit;
end
else Result:=0;
end;

procedure Accumulation;
var
i: integer;
rg,hi,nhi,lo,nlo,si,limit: real;
s: string;
begin
if BarBegin=2 then begin {initialization}
RegisterStudy('Accum Swing Index',53);
s:=GetVariable(eSymbol);
if length(s)>2 then s:=Copy(s,1,length(s)-2);
s:=Trim(s); {remove space in eSignal symbol}

{this table of daily limits should be expanded}
if s='SP' then limit:=10000 else
if s='LC' then limit:=1500 else
if s='PB' then limit:=3000 else
if s='C' then limit:=12 else
if s='W' then limit:=20 else
if s='S' then limit:=30
else limit:=10000;

SetUser(6,limit,3);
SetUser(6,-999,1); {high scale}
SetUser(6,999,2);  {low scale}
SetUser(ePercent,true);
SetUser(ePlot2,True);  SetUser(eShow2,True);  SetUser(eShow3,True);
SetUser(eClose,False);
SetUser(eWindow,2); {plot in 2nd study panel}
SetUser(2,GetUser(2,2),1);
SetUser(3,0,1);
end
else limit:=GetUser(6,3);

nhi:=GetUser(6,1); nlo:=GetUser(6,2);
for i:=BarBegin to BarEnd do begin
si:=SwingIndex(i,limit)+GetUser(3,pred(i));
SetUser(3,si,i);
end;

rg:=BarEnd-BarLeft;
hi:=Highest(3,BarEnd,rg,i,1,0);
lo:=Lowest(3,BarEnd,rg,i,1,0);
SetUser(6,hi,1); {save high scale}
SetUser(6,lo,2); {save low scale}
rg:=(hi-lo)/100;
if rg>0 then for i:=BarBegin to BarEnd do
Plot(2,(GetUser(3,i)-lo)/rg,i); {ASI as %}
Plot(2,GetUser(2,BarEnd),succ(BarEnd));
if BarBegin=BarEnd then
if (hi<>nhi) or (lo<>nlo) then begin
if rg>0 then for i:=BarLeft to BarEnd do
Plot(2,(GetUser(3,i)-lo)/rg,i);
Plot(2,GetUser(2,BarEnd),succ(BarEnd));
Refresh(True,eRSI);
end;
end;

{*********Main Program***********}
begin
if ESPL=52 then Accumulation;
end;

You will cut the procedures and paste them into the library of your existing script and add the  ESPL=52  statement to your existing Main Program.   To use, click ESPL button 52 button.   This will also register the study so from then on it will show by the name of Accum Swing Index on the bottom of the study list, and you can click on the study name instead of the ESPL button after that.

The formula needs the daily limit for the commodity the study is applied to.  I started a small look-up table in the script for a few common futures as an example.  You can add more to the table to cover the markets you want to use the study with.