# Choppiness Index Programing

Author: Optuma Team Last updated: Jan 23, 2024 09:00

The following code is an example of the Choppiness Index Programing

``````var
Plot1 : TPlot;
ATRData : TDataList;
ChopPeriod : TIntegerProperty;

procedure DefineTool(Tool : TTool);
begin
Tool.Name := 'Choppiness Index';
Tool.Key := '210189E5-77AF-4616-B5A1-04829030C11E';
Tool.MouseClicks := 0;
Tool.Hint := '';
Tool.ToolGroup := 'Price';
Tool.ToolType := ttDataView;
end;

procedure Init(Tool : TTool);
var
aLine : TLine;
begin
Plot1.Color := 2;
Plot1.PlotStyle := Line;
Plot1.FillColor := 2;
Plot1.Caption  := 'Choppiness Plot';

ChopPeriod := Tool.AddInteger('CHOP', 'Choppiness Period', 14);
end;

function Sum(aList : TDataList; iStartIndex : Integer; iCount : Integer) : Double;
var
j : integer;
begin
Result := 0;
for j := iStartIndex downto iStartIndex - iCount + 1 do
begin
if (j >= 0) and (j  aList.Count) then
Result := Result + aList[j].Close;
end;
end;

function Max(aList : TDataList; iStartIndex : Integer; iCount : Integer) : Double;
var
j : integer;
rMax : Real;
begin
Result := 0;
for j := iStartIndex downto iStartIndex - iCount + 1 do
begin
if (j >= 0) and (j  aList.Count) then
begin
rMax := aList.Row[j].High;
if (j > 0) and (aList.Row[j-1].Close > rMax) then
rMax := aList.Row[j-1].Close;
if Result  rMax then
Result := rMax;
end;
end;
end;

function Min(aList : TDataList; iStartIndex : Integer; iCount : Integer) : Double;
var
j : integer;
rMin : Real;
begin
Result := 1E1000;
for j := iStartIndex downto iStartIndex - iCount + 1 do
begin
if (j >= 0) and (j  aList.Count) then
begin
rMin := aList.Row[j].Low;
if (j > 0) and (aList.Row[j-1].Close  rMin) then
rMin := aList.Row[j-1].Close;
if Result > rMin then
Result := rMin;
end;
end;
end;

procedure Process(Tool : TTool; ProcessStart : Integer; ProcessEnd : Integer; DataIn : TDataList);
var
i : Integer;
rAtr, rMaxHi, rMinLo : Double;
begin
ATRData := ATR('BARS=1');
for i:= ProcessStart to ProcessEnd do
begin
rAtr := Sum(ATRData, i, ChopPeriod.Variable);
rMaxHi := Max(DataIn, i, ChopPeriod.Variable);
rMinLo := Min(DataIn, i, ChopPeriod.Variable);
Plot1.Row[i].Date := DataIn.Row[i].Date;
Plot1.Row[i].Hidden := i  ChopPeriod.Variable;
if (rAtr  0) and (rMaxHi  rMinLo) then
Plot1.Row[i].Close := 100 *Log10(rAtr/(rMaxHi-rMinLo))/Log10(ChopPeriod.Variable)
else
Plot1.Row[i].Close := 0;
end;

end;
``````