delphi 编写 kdj指标
时间: 2023-08-04 17:18:53 浏览: 138
以下是Delphi语言实现KDJ指标的示例代码:
```delphi
function KDJ(const close, low, high: array of double; const n, m1, m2: integer): TArrayOfDouble;
var
i: integer;
rsv, k, d: double;
hh, ll: double;
sma1, sma2: double;
begin
SetLength(Result, Length(close));
if (n <= 0) or (m1 <= 0) or (m2 <= 0) then Exit;
// 初始化KDJ指标的起始值
rsv := 50.0;
k := 50.0;
d := 50.0;
hh := high[0];
ll := low[0];
// 计算RSV值
for i := 0 to n - 1 do
begin
if high[i] > hh then hh := high[i];
if low[i] < ll then ll := low[i];
end;
if hh - ll > 0 then
rsv := (close[n - 1] - ll) / (hh - ll) * 100.0;
// 计算K值和D值
sma1 := rsv;
sma2 := rsv;
for i := n to Length(close) - 1 do
begin
// 计算RSV值
hh := high[i - n + 1];
ll := low[i - n + 1];
for j := i - n + 2 to i do
begin
if high[j] > hh then hh := high[j];
if low[j] < ll then ll := low[j];
end;
if hh - ll > 0 then
rsv := (close[i] - ll) / (hh - ll) * 100.0;
// 计算K值和D值
sma1 := (m1 - 1) / m1 * sma1 + 1 / m1 * rsv;
sma2 := (m2 - 1) / m2 * sma2 + 1 / m2 * sma1;
k := sma1 * 2 / 3 + k / 3;
d := sma2 * 2 / 3 + d / 3;
Result[i] := k;
end;
end;
```
其中,`close`、`low`和`high`分别为收盘价、最低价和最高价的数组,`n`为RSV计算的周期数,`m1`和`m2`为平滑系数,`Result`为KDJ指标的计算结果。
阅读全文