From 40e83e84e26a809959c62f6651d87c4b78336396 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Wed, 8 Mar 2023 13:30:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E5=87=BD=E6=95=B0=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E4=BB=8E=E6=95=B0=E7=BB=84=E6=94=B9=E6=88=90?= =?UTF-8?q?series?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/barslast.go | 7 ++++++- formula/barslast_test.go | 2 +- formula/barslastcount.go | 6 +++--- formula/barssincen.go | 5 ++--- formula/dma_test.go | 2 +- formula/ema_test.go | 2 +- formula/sma_test.go | 2 +- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/formula/barslast.go b/formula/barslast.go index 7369bea..1c9a3a0 100644 --- a/formula/barslast.go +++ b/formula/barslast.go @@ -7,7 +7,12 @@ import ( // BARSLAST 上一次条件成立到当前的周期, BARSLAST(C/REF(C,1)>=1.1) 上一次涨停到今天的天数 // // 为了测试SMA,BARSLAST必须要先实现, 给SMA提供序列换参数, 以便验证, python那边还没实现 -func BARSLAST(S stat.Series) []stat.DType { +func BARSLAST(S stat.Series) stat.Series { + ns := BARSLAST2(S) + return stat.NewSeries[stat.DType](ns...) +} + +func BARSLAST2(S stat.Series) []stat.DType { fs := S.DTypes() as := stat.Repeat[stat.DType](1, S.Len()) bs := stat.Repeat[stat.DType](0, S.Len()) diff --git a/formula/barslast_test.go b/formula/barslast_test.go index b483fb7..0d02adb 100644 --- a/formula/barslast_test.go +++ b/formula/barslast_test.go @@ -10,5 +10,5 @@ func TestBARSLAST(t *testing.T) { f0 := []float64{1, 2, 3, 4, 5, 6, 0, 8, 9, 10, 11, 12} i0 := CompareGt(f0, 100) s0 := stat.NewSeries[bool](i0...) - fmt.Println(BARSLAST(s0)) + fmt.Println(BARSLAST2(s0)) } diff --git a/formula/barslastcount.go b/formula/barslastcount.go index 35e6007..d5d5891 100644 --- a/formula/barslastcount.go +++ b/formula/barslastcount.go @@ -5,10 +5,10 @@ import ( ) // BARSLASTCOUNT 统计连续满足S条件的周期数 -func BARSLASTCOUNT(S stat.Series) []int64 { +func BARSLASTCOUNT(S stat.Series) stat.Series { s := S.DTypes() slen := len(s) - rt := stat.Repeat[int64](0, slen+1) + rt := stat.Repeat[stat.Int](0, slen+1) for i := 0; i < slen; i++ { if s[i] != 0 { rt[i+1] = rt[i] + 1 @@ -16,5 +16,5 @@ func BARSLASTCOUNT(S stat.Series) []int64 { rt[i+1] = rt[i+1] } } - return rt[1:] + return stat.NewSeries[stat.Int](rt[1:]...) } diff --git a/formula/barssincen.go b/formula/barssincen.go index 637b405..5171b60 100644 --- a/formula/barssincen.go +++ b/formula/barssincen.go @@ -5,7 +5,7 @@ import ( ) // BARSSINCEN N周期内第一次S条件成立到现在的周期数,N为常量 -func BARSSINCEN(S stat.Series, N any) []stat.Int { +func BARSSINCEN(S stat.Series, N any) stat.Series { ret := S.Rolling(N).Apply(func(X stat.Series, M stat.DType) stat.DType { x := X.DTypes() n := int(M) @@ -20,6 +20,5 @@ func BARSSINCEN(S stat.Series, N any) []stat.Int { return stat.DType(r) }) r1 := ret.FillNa(0, true) - r2 := r1.Ints() - return r2 + return r1 } diff --git a/formula/dma_test.go b/formula/dma_test.go index 2dc2156..ab071d2 100644 --- a/formula/dma_test.go +++ b/formula/dma_test.go @@ -35,7 +35,7 @@ func TestDMA(t *testing.T) { } x0[idx] = t }) - n := BARSLAST(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x0)) + n := BARSLAST2(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x0)) fmt.Println(n[len(n)-10:]) x := DMA(CLOSE, pandas.NewSeries(stat.SERIES_TYPE_DTYPE, "", n)) diff --git a/formula/ema_test.go b/formula/ema_test.go index 8cddf86..23860a2 100644 --- a/formula/ema_test.go +++ b/formula/ema_test.go @@ -32,7 +32,7 @@ func TestEMA(t *testing.T) { x0[idx] = t }) //x := stat.Where(v2, as, bs) - n := BARSLAST(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x0)) + n := BARSLAST2(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x0)) fmt.Println(n[len(n)-10:]) x := EMA(CLOSE, pandas.NewSeries(stat.SERIES_TYPE_DTYPE, "", n)) diff --git a/formula/sma_test.go b/formula/sma_test.go index 65d8390..e38e913 100644 --- a/formula/sma_test.go +++ b/formula/sma_test.go @@ -34,7 +34,7 @@ func TestSMA(t *testing.T) { x[idx] = t }) //x := stat.Where(v2, as, bs) - n := BARSLAST(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x)) + n := BARSLAST2(pandas.NewSeries(stat.SERIES_TYPE_FLOAT32, "", x)) fmt.Println(n[len(n)-10:]) //r1 := SMA(CLOSE, pandas.NewSeries(pandas.SERIES_TYPE_FLOAT32, "", n), 1) r1 := SMA(CLOSE, 7, 1) -- Gitee