# Delphi 计算MACD指标数值

2016-08-28 19:03:01来源:CSDN作者:MichaelJScofield人点击

``{ MACD单个数据结构 }Type  TMACD = record    DIF: Extended;    DEA: Extended;    BAR: Extended;  end;{ 获取MACD }function GetMACD(KlineArr: TKlineArray):TMACD;var  EMA9,EMA12,EMA26,EMA9Ex,EMA12Ex,EMA26Ex: Extended;  i: Integer;  arrKline: TKlineArray;begin  FillChar(Result, SizeOf(TMACD), #0);  arrKline := KlineArr;  if High(arrKline) <> -1 then  begin    for i := Low(arrKline) to High(arrKline) do    begin      if i=0 then Continue;      if i=1 then      begin        EMA12 := arrKline[i-1].Close+(arrKline[i].Close-arrKline[i-1].Close)*2/13;        EMA26 := arrKline[i-1].Close+(arrKline[i].Close-arrKline[i-1].Close)*2/27;        Result.DIF := EMA12-EMA26;        Result.DEA := 0+(Result.DIF)*2/10;        Result.BAR := 2*((Result.DIF)-(Result.DEA));      end      else if i>1 then      begin        EMA12 := EMA12Ex*11/13+arrKline[i].Close*2/13;        EMA26 := EMA26Ex*25/27+arrKline[i].Close*2/27;        Result.DIF := RoundTo(EMA12-EMA26,-4);        Result.DEA := Result.DEA*8/10 +Result.DIF*2/10;        Result.BAR := 2*(Result.DIF-Result.DEA);      end;      EMA12Ex := EMA12;      EMA26Ex := EMA26;    end;    Result.DIF := RoundTo(Result.DIF,-2);    Result.DEA := RoundTo(Result.DEA,-2);    Result.BAR := RoundTo(Result.BAR,-2);  end;end;``