yay.
Code: Select all
//---------------------------------------------------------------------------
// MACD indicator
//---------------------------------------------------------------------------
library MACD;
uses
SysUtils, classes, graphics, windows, IndicatorInterfaceUnit;
var
// External variables
FastEMAPeriod : double;
SlowEMAPeriod : double;
SignalPeriod: double;
ApplyToPrice: integer;
// Buffers
FastEMA, SlowEMA, _MACD, Signal : TIndexBuffer;
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('DiNapoli DEMA');
SetOutputWindow(ow_SeparateWindow);
AddLevel(0, psDot, 1, cl_GridColor);
// register options
AddSeparator('Common');
RegOption('Fast EMA period', ot_Double, FastEMAPeriod);
SetOptionRange('Fast EMA period', 1, MaxInt);
SetOptionDigits('Fast EMA period', 4);
FastEMAPeriod := 8.3897;
RegOption('Slow EMA period', ot_Double, SlowEMAPeriod);
SetOptionRange('Slow EMA period', 1, MaxInt);
SetOptionDigits('Slow EMA period', 4);
SlowEMAPeriod := 17.5185;
RegOption('Signal period', ot_Double, SignalPeriod);
SetOptionRange('Signal period', 1, MaxInt);
SetOptionDigits('Signal period', 4);
SignalPeriod := 9.0503;
RegOption('Apply to price', ot_EnumType, ApplyToPrice);
AddOptionValue('Apply to price', 'Close');
AddOptionValue('Apply to price', 'Open');
AddOptionValue('Apply to price', 'High');
AddOptionValue('Apply to price', 'Low');
AddOptionValue('Apply to price', '(High + Low)/2');
AddOptionValue('Apply to price', '(High + Low + Close)/3');
AddOptionValue('Apply to price', '(High + Low + Close + Close)/4');
ApplyToPrice := 0;
// create buffers
FastEMA := CreateIndexBuffer;
SlowEMA := CreateIndexBuffer;
_MACD := CreateIndexBuffer;
Signal := CreateIndexBuffer;
IndicatorBuffers(2);
SetIndexBuffer(0, _MACD);
SetIndexBuffer(1, Signal);
SetIndexStyle(0, ds_Line, psDot, 1, clRed);
SetIndexLabel(0, 'MACD');
SetIndexStyle(1, ds_Line, psSolid, 1, clSilver);
SetIndexLabel(1, 'Signal Line');
end;
//---------------------------------------------------------------------------
// Deinitialize indicator
//---------------------------------------------------------------------------
procedure Done; stdcall;
begin
end;
//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
i: integer;
k, sum: double;
function GetPrice(index: integer): double;
begin
case ApplyToPrice of
0: result := Close(index);
1: result := Open(index);
2: result := High(index);
3: result := Low(index);
4: result := (High(index) + Low(index))/2;
5: result := (High(index) + Low(index) + Close(index))/3;
6: result := (High(index) + Low(index) + Close(index)*2)/4;
else result := 0;
end;
end;
begin
// calculate fast EMA
k := 2/(FastEMAPeriod + 1);
if index = Bars - 1 then
FastEMA[index] := GetPrice(index)
else
FastEMA[index] := FastEMA[index + 1] + k*(GetPrice(index) - FastEMA[index + 1]);
// calculate slow EMA
k := 2/(SlowEMAPeriod + 1);
if index = Bars - 1 then
SlowEMA[index] := GetPrice(index)
else
SlowEMA[index] := SlowEMA[index + 1] + k*(GetPrice(index) - SlowEMA[index + 1]);
// calculate MACD
_MACD[index] := FastEMA[index] - SlowEMA[index];
// calculate Signal
k := 2/(SignalPeriod + 1);
//sum := 0;
//for i:=index to index + SignalPeriod -1 do
if index = Bars - 1 then
// sum := sum + _MACD[i];
Signal[index] := _MACD[index]
else
Signal[index] := Signal[index + 1] + k*(_MACD[index] - Signal[index + 1]);
end;
exports
Init, Done, Calculate;
end.