diff --git a/formula/barslast.go b/formula/barslast.go index 7369beaef8aa964673037c5d2a1f09b65c15181f..1c9a3a0cf63da233ae8eecab5821b3e77cbb0aaf 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 b483fb7030e8abee44885b512598f020701af29c..0d02adbd1857ebb8d04911be390a57dabc2ad878 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 35e6007ef454b872a5e8381af1de36befdcba22c..d5d589144935be74017bbe66d2b70dad42f24370 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 637b4059fe633709c90125196d8e73230d3a5d61..5171b60f7b99a384ea4dd8318652e6cc0fd9f6e6 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 2dc2156412102e15ad91162a36cb1e823219ce6d..ab071d2f2efaf4b4f72a6bc07518b3dd094bc351 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 8cddf866fae9a13068820c958259606dc94d84f2..23860a22a0f03ea5c2ecb0c18665564f42468aa7 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 65d8390e6918f420f4ba598b300e4d637d75f259..e38e9133a5a5d7439c133e30b410e8d0810dad1f 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)