From bd74ca32ed07c7ac624697edb66bcfbc6a22c1fd Mon Sep 17 00:00:00 2001 From: wangfeng Date: Fri, 17 Feb 2023 08:53:17 +0800 Subject: [PATCH] =?UTF-8?q?#I6CC21=20=E5=AE=9E=E7=8E=B0MACD=E6=8C=87?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- indicator/macd.go | 31 +++++++++++++++++++++++++++++++ indicator/macd_test.go | 14 ++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 indicator/macd.go create mode 100644 indicator/macd_test.go diff --git a/indicator/macd.go b/indicator/macd.go new file mode 100644 index 0000000..e14d02c --- /dev/null +++ b/indicator/macd.go @@ -0,0 +1,31 @@ +package indicator + +import ( + "gitee.com/quant1x/pandas" + . "gitee.com/quant1x/pandas/formula" +) + +// MACD 指标 +// +// DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG); +// 输出DIF:收盘价的SHORT日指数移动平均-收盘价的LONG日指数移动平均 +// DEA:EMA(DIF,MID); +// 输出DEA:DIF的MID日指数移动平均 +// MACD:(DIF-DEA)*2,COLORSTICK; +// 输出平滑异同平均线:(DIF-DEA)*2,COLORSTICK +// 系统默认12, 26, 9 +// 这里采用5,13,3 +func MACD(df pandas.DataFrame, SHORT, LONG, MID int) pandas.DataFrame { + var ( + CLOSE = df.ColAsNDArray("close") + //HIGH = df.ColAsNDArray("high") + //LOW = df.ColAsNDArray("low") + ) + //DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG); + DIF := EMA(CLOSE, SHORT).Sub(EMA(CLOSE, LONG)) + //DEA:EMA(DIF,MID) + DEA := EMA(DIF, MID) + //MACD:(DIF-DEA)*2,COLORSTICK; + MACD := DIF.Sub(DEA).Mul(2) + return pandas.NewDataFrame(DIF, DEA, MACD) +} diff --git a/indicator/macd_test.go b/indicator/macd_test.go new file mode 100644 index 0000000..1d6d457 --- /dev/null +++ b/indicator/macd_test.go @@ -0,0 +1,14 @@ +package indicator + +import ( + "fmt" + "gitee.com/quant1x/pandas/data/cache" + "testing" +) + +func TestMACD(t *testing.T) { + df := cache.KLine("sz002528") + fmt.Println(df) + df1 := MACD(df, 5, 13, 3) + fmt.Println(df1) +} -- Gitee