LazarusでBollinger Bands(7本)を作成しましたが
ファイルサイズが大きすぎて、Forumにアップロード
できませんでした。
//------------------------------------------
// BB7sma
//------------------------------------------
library BB7sma;
uses
Interfaces, graphics, IndicatorInterfaceUnit,
TechnicalFunctions;
var
// External variables
period: integer = 21;
Shift: integer = 0;
Dev1: double = 1;
Dev2: double = 2;
Dev3: double = 3;
ApplyToPrice: integer;
// Buffers
Up3, Up2, Up1, SMA,
Low1, Low2, Low3: TIndexBuffer;
//------------------------------------------
// Initialize indicator
//------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('BB7sma');
SetOutputWindow(ow_ChartWindow);
// register options
AddSeparator('Common');
RegOption('Period', ot_Integer, period);
SetOptionRange('Period', 1, MaxInt);
RegOption('Deviaton1', ot_Double, Dev1);
SetOptionRange('Deviation1', 0.1, 200);
RegOption('Deviation2', ot_Double, Dev2);
SetOptionRange('Deviation2', 0.1, 200);
RegOption('Deviation3', ot_Double, Dev3);
SetOptionRange('Deviation3', 0.1, 200);
RegOption('Shift', ot_Integer, Shift);
RegApplyToPriceOption(ApplyToPrice);
// create buffers
Up3 := CreateIndexBuffer;
Up2 := CreateIndexBuffer;
Up1 := CreateIndexBuffer;
SMA := CreateIndexBuffer;
Low1 := CreateIndexBuffer;
Low2 := CreateIndexBuffer;
Low3 := CreateIndexBuffer;
IndicatorBuffers(7);
SetIndexBuffer(0, Up3);
SetIndexStyle(0, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(0, 'Upper3');
SetIndexBuffer(1, Up2);
SetIndexStyle(1, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(1, 'Upper2');
SetIndexBuffer(2, Up1);
SetIndexStyle(2, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(2, 'Upper1');
SetIndexBuffer(3, SMA);
SetIndexStyle(3, ds_Line, psSolid, 2, clTeal);
SetIndexLabel(3, 'SMA');
SetIndexBuffer(4, Low1);
SetIndexStyle(4, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(4, 'Lower1');
SetIndexBuffer(5, Low2);
SetIndexStyle(5, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(5, 'Lower2');
SetIndexBuffer(6, Low3);
SetIndexStyle(6, ds_Line, psSolid, 1, clTeal);
SetIndexLabel(6, 'Lower3');
end;
//------------------------------------------
//parameters changed
//------------------------------------------
procedure OnParamsChange; stdcall;
begin
SetBufferShift(1, Shift);
SetBufferShift(2, Shift);
SetBufferShift(3, Shift);
SetBufferShift(4, Shift);
SetBufferShift(5, Shift);
SetBufferShift(6, Shift);
end;
//------------------------------------------
// Calculate requested bar
//------------------------------------------
procedure Calculate(index: integer); stdcall;
var
i: integer;
sum, value, sd1, sd2, sd3: double;
function GetPrice(index: integer): double;
begin
result := TechnicalFunctions.GetPrice(index,
TPriceType(ApplyToPrice));
end;
begin
if (index + period) >= Bars then
exit;
value := GetMA(index, 0, period, ma_SMA,
TPriceType(ApplyToPrice), SMA[index + 1]);
SMA[index] := value;
// calculate bands
if value <> 0 then
begin
sum := 0;
for i:=index to index + period - 1 do
sum := sum + sqr(abs(GetPrice(i) - value));
sd1 := sqrt(sum/period)*Dev1;
sd2 := sqrt(sum/period)*Dev2;
sd3 := sqrt(sum/period)*Dev3;
Up1[index] := SMA[index] + sd1;
Low1[index] := SMA[index] - sd1;
Up2[index] := SMA[index] + sd2;
Low2[index] := SMA[index] - sd2;
Up3[index] := SMA[index] + sd3;
Low3[index] := SMA[index] - sd3;
end
else
begin
Up1[index] := 0;
Low1[index] := 0;
Up2[index] := 0;
Low2[index] := 0;
Up3[index] := 0;
Low3[index] := 0;
end;
end;
exports
Init, OnParamsChange, Calculate;
end.
