%Kslow = 3 period Modified Moving Average of %Kfast
%D = 3 period Modified Moving Average of %Kslow
I am not a programmer but I will try to get DiNapoli's MACD configured next.
Code: Select all
//---------------------------------------------------------------------------
// Stochastic indicator
//---------------------------------------------------------------------------
library Stochastic;
uses
graphics, windows, TechnicalFunctions, IndicatorInterfaceUnit;
var
// External variables
KPeriod: integer;
DPeriod: integer;
Slowing: integer;
ApplyTo: integer;
// Buffers
Kfast, Kslow, Dline, HighesBuffer, LowesBuffer: TIndexBuffer;
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('DiNapoli Preferred Stochastics');
SetOutputWindow(ow_SeparateWindow);
SetFixedMinMaxValues(0, 100);
//AddLevel(20, psDot, 1, cl_GridColor);
//AddLevel(80, psDot, 1, cl_GridColor);
SetEmptyValue(105);
// register options
AddSeparator('Common');
RegOption('%K period', ot_Integer, KPeriod);
SetOptionRange('%K period', 1, MaxInt);
KPeriod := 8;
RegOption('%D period', ot_Integer, DPeriod);
SetOptionRange('%D period', 1, MaxInt);
DPeriod := 3;
RegOption('Slowing', ot_Integer, Slowing);
SetOptionRange('Slowing', 1, MaxInt);
Slowing := 3;
RegOption('Apply to', ot_EnumType, ApplyTo);
AddOptionValue('Apply to', 'Low/High');
AddOptionValue('Apply to', 'Close/Close');
ApplyTo := 0;
// create buffers
Kfast := CreateIndexBuffer;
Kslow := CreateIndexBuffer;
Dline := CreateIndexBuffer;
HighesBuffer := CreateIndexBuffer;
LowesBuffer := CreateIndexBuffer;
IndicatorBuffers(2);
SetIndexBuffer(0, Kslow);
SetIndexBuffer(1, Dline);
SetIndexStyle(0, ds_line, psDot, 1, RGB($FF, $90, $1E));
SetIndexLabel(0, 'K Line');
SetIndexStyle(1, ds_Line, psSolid, 1, RGB($1E, $90, $FF));
SetIndexLabel(1, 'D Line');
end;
//---------------------------------------------------------------------------
// Deinitialize indicator
//---------------------------------------------------------------------------
procedure Done; stdcall;
begin
end;
//--------------------------------------------------------------------------------
function iMAonArray(buffer: TIndexBuffer; total:integer; period:integer; ma_shift:integer; MAType:integer; shift:integer; prev:double): double;
var
i, N: integer;
sum, weight, q1, k: double;
begin
case MAType of
0: //SMA
begin
sum := 0;
for i:=shift to shift+period-1 do
begin
q1:= buffer[i];
sum:=sum+q1;
result :=sum/period;
end;
end; // case 0
1: //EMA
begin
k := 2/(period + 1);
if prev = 0 then
begin
result := buffer[shift];
end
else
begin
q1:= buffer[shift];
result := prev + k*(q1-prev);
end;
end;
2: // WMA
begin
sum := 0;
weight := 0;
for i:=shift to shift+period-1 do
begin
q1:= buffer[i];
N:=(period - i+shift);
sum := sum + q1*N;
weight := weight + N;
end;
result := sum/weight;
end;
3: // SSMA
begin
if prev = 0 then
result := GetMA(shift, 0, period, TMAType(MAType), TPriceType(0))
else
begin
q1:= buffer[shift];
result := ((period-1)*prev+q1)/period; //pas
end;
end;
end; //case
end;
//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
sumlow, sumhigh, Plow, Phigh: double;
i: integer;
begin
if (Bars < KPeriod) or (Bars < Slowing) or (Bars < DPeriod) then
exit;
if ApplyTo = 0 then
begin
// find highest and lowest price for period KPeriod
Phigh := High(index);
Plow := Low(index);
for i:=1 to KPeriod - 1 do
begin
if High(index + i) >= Phigh then
begin
Phigh := High(index + i);
end;
if Low(index + i) <= Plow then
begin
Plow := Low(index + i);
end;
end;
end
else
begin
Phigh := Close(index);
Plow := Close(index);
for i:=1 to KPeriod - 1 do
begin
if Close(index + i) >= Phigh then
begin
Phigh := Close(index + i);
end;
if Close(index + i) <= Plow then
begin
Plow := Close(index + i);
end;
end;
end;
HighesBuffer[index] := Phigh;
LowesBuffer[index] := Plow;
// count Kfast line value
sumlow := 0;
sumhigh := 0;
for i:=index+Slowing - 1 downto index do
begin
sumlow:=sumlow+Close(i)-LowesBuffer[i];
sumhigh:=sumhigh+HighesBuffer[i]-LowesBuffer[i];
end;
if(sumhigh=0.0) then Kfast[index]:=100.0
else Kfast[index]:=sumlow/sumhigh*100;
Kslow[index]:=iMAOnArray(Kfast,Bars,DPeriod,0,3,index,Kslow[index+1]);
Dline[index]:=iMAOnArray(Kslow,Bars,DPeriod,0,3,index,Dline[index+1]);
end;
exports
Init, Done, Calculate;
begin
end.