Delphi 计算火币网KDJ指标数值

感谢七爷的无私帮助和纠正。
为了尽可能地提高数据复用,可能给多个指标运算。所以获取的周期K线数据保存到了一个数组。定义如下:

  //存储K线数据的结构
  TKline=record
    Open  :Extended;
    High  :Extended;
    Low   :Extended;
    Close :Extended;
    Amount:Extended;
  end;
  TKlineArray = array of TKline;
  //一些拖沓的常量
const
  KLINE_PERIOD_1MINUTE_CODE  = '001';
  KLINE_PERIOD_5MINUTE_CODE  = '005';
  KLINE_PERIOD_15MINUTE_CODE = '015';
  KLINE_PERIOD_30MINUTE_CODE = '030';
  KLINE_PERIOD_1HOUR_CODE    = '060';
  KLINE_PERIOD_1DAY_CODE     = '100';
  KLINE_PERIOD_1MONTH_CODE   = '200';
  KLINE_PERIOD_1YEAR_CODE    = '300';
  KLINE_PERIOD_1MINUTE       = 1;
  KLINE_PERIOD_5MINUTE       = 2;
  KLINE_PERIOD_15MINUTE      = 3;
  KLINE_PERIOD_30MINUTE      = 4;
  KLINE_PERIOD_1HOUR         = 5;
  KLINE_PERIOD_1DAY          = 6;
  KLINE_PERIOD_1MONTH        = 7;
  KLINE_PERIOD_1YEAR         = 8;

获取15分周期到数组

{ 获取火币K线数据 
    Exp:
         var klinearr:TKlineArray;
         klinearr := GetHuobiKLine(KLINE_PERIOD_15MINUTE);
}
function GetHuobiKLine(KLINE_PERIOD:Integer):TKlineArray;
var
  sPeriodCode:string;
  sURL,sSource:string;
  jo:ISuperObject;
  arrKline:TSuperArray;
  i:Integer;
begin
  SetLength(Result,0);
  case KLINE_PERIOD of
    KLINE_PERIOD_1MINUTE:  sPeriodCode := KLINE_PERIOD_1MINUTE_CODE;
    KLINE_PERIOD_5MINUTE:  sPeriodCode := KLINE_PERIOD_5MINUTE_CODE;
    KLINE_PERIOD_15MINUTE: sPeriodCode := KLINE_PERIOD_15MINUTE_CODE;
    KLINE_PERIOD_30MINUTE: sPeriodCode := KLINE_PERIOD_30MINUTE_CODE;
    KLINE_PERIOD_1HOUR:    sPeriodCode := KLINE_PERIOD_1HOUR_CODE;
    KLINE_PERIOD_1DAY:     sPeriodCode := KLINE_PERIOD_1DAY_CODE;
    KLINE_PERIOD_1MONTH:   sPeriodCode := KLINE_PERIOD_1MONTH_CODE;
    KLINE_PERIOD_1YEAR:    sPeriodCode := KLINE_PERIOD_1YEAR_CODE;
  end;
  sURL    := 'http://api.huobi.com/staticmarket/btc_kline_'+sPeriodCode+'_json.js';
  sSource := GetPageSrc(sURL);
  if pos('[[', sSource) <> 0 then
  begin
    jo := SO(sSource);
    if Assigned(jo) then
    begin
      arrKline := jo.AsArray;
      SetLength(Result,arrKline.Length);
      for i :=0 to arrKline.Length-1 do
      begin
        Result[i].Open    := arrKline[i].AsArray[1].AsDouble;
        Result[i].High    := arrKline[i].AsArray[2].AsDouble;
        Result[i].Low     := arrKline[i].AsArray[3].AsDouble;
        Result[i].Close   := arrKline[i].AsArray[4].AsDouble;
        Result[i].Amount  := arrKline[i].AsArray[5].AsDouble;
      end;
    end;
  end;
end;

KDJ数据结构和计算

type
{ K线数据结构 }
  TKDJ=record
    K : Extended;
    D : Extended;
    J : Extended;
    PDK : Extended;
    PDD : Extended;
    PDJ : Extended;
  end;
  TKDJArray = array of TKDJ;

{ 计算KDJ函数 返回最后一个 }
function GetKDJOriginal(KlineArr: TKlineArray; PERIOD_NUM: Integer): TKDJ;
var
  RSV_P,H9_P,L9_P,PDK,PDD,PDJ:Extended;
  i,j:Integer;
  Arr: TKlineArray;
begin
  FillChar(Result, SizeOf(TKDJ), #0);
//  SetLength(Result,2);
  Arr := KlineArr;
  if High(Arr) <> -1 then
  begin
    PDK := 50;
    PDD := 50;
    for I :=Low(Arr) to High(Arr) do
    begin
      Result.PDK := PDK;
      Result.PDD := PDD;
      Result.PDJ := PDJ;
      if I < PERIOD_NUM then Continue;
      H9_P := 0;
      L9_P := 0;
      for J := (I-PERIOD_NUM+1) to I do
      begin
        if H9_P=0 then H9_P := Arr[J].High;
        if L9_P=0 then L9_P := Arr[J].Low;
        H9_P := Max(H9_P, Arr[J].High);
        L9_P := Min(L9_P, Arr[J].Low);
      end;
      RSV_P := ((Arr[I].Close - L9_P) / (H9_P - L9_P)) * 100;
      Result.K   := 2/3*PDK + 1/3*RSV_P;
      Result.D   := 2/3*PDD + 1/3*Result.K;
      Result.J   := (Result.K*3)-(Result.D*2);
      PDK        := TruncTo(Result.K, 2);
      PDD        := TruncTo(Result.D, 2);
      PDJ        := TruncTo(Result.J, 2);
    end;
    Result.K := TruncTo(Result.K, 2);
    Result.D := TruncTo(Result.D, 2);
    Result.J := TruncTo(Result.J, 2);
  end;
end;

//使用例子
MyKDJ := GetKDJOriginal(klinearr,9);//KDJ(9,3,3)

你可能感兴趣的:(Delphi,量化交易手札)