📄 teesar.pas
字号:
unit TeeSAR;
{$I TeeDefs.inc}
interface
uses Classes, TeEngine, OHLChart, Graphics;
type
TSARFunction=class(TTeeFunction)
private
FAF, // Acceleration Factor
FMS : Double;
procedure SetAF(const Value: Double);
procedure SetMS(const Value: Double); // Max Step
public
Constructor Create(AOwner: TComponent); override;
procedure AddPoints(Source: TChartSeries); override;
published
property AccelerationFactor : Double read FAF write SetAF;
property MaxStep : Double read FMS write SetMS;
end;
{
+ Info:
http://www.linnsoft.com/tour/techind/sar.htm
http://calderone.eresmas.com/parabolic_sar.htm
http://es.biz.yahoo.com/edu/tech/osciladores1014.html
http://www.trade10.com/parabolic_s&r.htm
http://www.market-analyst.com/kb/article.php/Parabolic_Stop_and_Reverse/
http://www.incrediblecharts.com/technical/parabolic_sar_construction.htm
http://trader.online.pl/
http://www.amibroker.com/library/formula.php?id=242
http://www.amibroker.com/library/formula.php?id=241
}
implementation
constructor TSARFunction.Create(AOwner: TComponent);
begin
inherited;
CanUsePeriod := false;
SingleSource := True;
FAF := 0.02;
FMS := 0.20;
end;
function Min(x, y , z: Double): Double;
begin
if x < y then result := x
else result := y;
if z < result then result := z;
end;
function Max(x, y , z: Double): Double;
begin
if x > y then result := x
else result := y;
if z > result then result := z;
end;
// http://www.amibroker.com/library/formula.php?id=241
procedure TSARFunction.AddPoints(Source: TChartSeries);
var
af, ep, hp, lp: Double;
psar : array of Double;
i, reverse : integer;
LongPos : boolean;
begin
ParentSeries.Clear;
if Source.Count <= 2 then exit;
SetLength(psar, Source.Count);
try
psar[0] := TOHLCSeries(Source).CloseValues[0];
psar[1] := TOHLCSeries(Source).HighValues[1];
LongPos := true;
af := FAF;
ep := TOHLCSeries(Source).LowValues[0];
hp := TOHLCSeries(Source).HighValues[0];
lp := ep;
for i := 2 to Source.Count-1 do begin
if LongPos then
psar[i] := psar[i-1] + af * (hp - psar[i-1])
else
psar[i] := psar[i-1] + af * (lp - psar[i-1]);
reverse := 0;
// Check for reversal
if LongPos then begin
if TOHLCSeries(Source).LowValues[i] < psar[i] then begin
LongPos := false;
reverse := 1; // reverse position to short
psar[i] := hp; // sar is high point in prev trade
lp := TOHLCSeries(Source).LowValues[i];
af := FAF;
end;{if}
end
else begin
if TOHLCSeries(Source).HighValues[i] > psar[i] then begin
LongPos := true;
reverse := 1; // reverse position to long
psar[i] := lp;
hp := TOHLCSeries(Source).HighValues[i];
af := FAF;
end;{if}
end;
if reverse = 0 then begin
if LongPos then begin
if TOHLCSeries(Source).HighValues[i] > hp then begin
hp := TOHLCSeries(Source).HighValues[i];
af := af + FAF;
af := Min(af, FMS, FMS);
end;{if}
psar[i] := Min(psar[i], TOHLCSeries(Source).LowValues[i-1], TOHLCSeries(Source).LowValues[i-2]);
end
else begin
if TOHLCSeries(Source).LowValues[i] < lp then begin
lp := TOHLCSeries(Source).LowValues[i];
af := af + FAF;
af := Min(af, FMS, FMS);
end;{if}
psar[i] := Max(psar[i], TOHLCSeries(Source).HighValues[i-1], TOHLCSeries(Source).HighValues[i-2]);
end;
end;{if}
end;{for}
for i:= 2 to Source.Count-1 do
AddFunctionXY(Source.YMandatory, Source.NotMandatoryValueList.Value[i], psar[i]);
finally
psar := nil;
end;{try}
end;
procedure TSARFunction.SetAF(const Value: Double);
begin
if FAF <> Value then begin
FAF := Value;
ReCalculate;
end;{if}
end;
procedure TSARFunction.SetMS(const Value: Double);
begin
if FAF <> Value then begin
FMS := Value;
ReCalculate;
end;{if}
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -