mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
fix: tsi, add test
This commit is contained in:
parent
75caa6565e
commit
2811dbb580
|
@ -12,7 +12,9 @@ import (
|
|||
//go:generate callbackgen -type TSI
|
||||
type TSI struct {
|
||||
types.SeriesBase
|
||||
types.IntervalWindow
|
||||
types.Interval
|
||||
FastWindow int
|
||||
SlowWindow int
|
||||
PrevValue float64
|
||||
Values floats.Slice
|
||||
Pcs *EWMA
|
||||
|
@ -24,48 +26,58 @@ type TSI struct {
|
|||
|
||||
func (inc *TSI) Update(value float64) {
|
||||
if inc.Pcs == nil {
|
||||
if inc.FastWindow == 0 {
|
||||
inc.FastWindow = 13
|
||||
}
|
||||
if inc.SlowWindow == 0 {
|
||||
inc.SlowWindow = 25
|
||||
}
|
||||
inc.Pcs = &EWMA{
|
||||
IntervalWindow: types.IntervalWindow{
|
||||
Window: 25,
|
||||
Window: inc.SlowWindow,
|
||||
Interval: inc.Interval,
|
||||
},
|
||||
}
|
||||
inc.Pcds = &EWMA{
|
||||
IntervalWindow: types.IntervalWindow{
|
||||
Window: 13,
|
||||
Window: inc.FastWindow,
|
||||
Interval: inc.Interval,
|
||||
},
|
||||
}
|
||||
inc.Apcs = &EWMA{
|
||||
IntervalWindow: types.IntervalWindow{
|
||||
Window: 25,
|
||||
Window: inc.SlowWindow,
|
||||
Interval: inc.Interval,
|
||||
},
|
||||
}
|
||||
inc.Apcds = &EWMA{
|
||||
IntervalWindow: types.IntervalWindow{
|
||||
Window: 13,
|
||||
Window: inc.FastWindow,
|
||||
Interval: inc.Interval,
|
||||
},
|
||||
}
|
||||
inc.SeriesBase.Series = inc
|
||||
}
|
||||
if inc.PrevValue == 0 {
|
||||
inc.PrevValue = value
|
||||
return
|
||||
}
|
||||
pc := value - inc.PrevValue
|
||||
inc.PrevValue = value
|
||||
inc.Pcs.Update(pc)
|
||||
inc.Pcds.Update(inc.Pcs.Last())
|
||||
apc := math.Abs(pc)
|
||||
inc.Apcs.Update(apc)
|
||||
|
||||
inc.Pcds.Update(inc.Pcs.Last())
|
||||
inc.Apcds.Update(inc.Apcs.Last())
|
||||
|
||||
tsi := (inc.Pcds.Last() / inc.Apcds.Last()) * 100.
|
||||
inc.Values.Push(tsi)
|
||||
if inc.Values.Length() > MaxNumOfEWMA {
|
||||
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
|
||||
}
|
||||
inc.PrevValue = value
|
||||
}
|
||||
|
||||
func (inc *TSI) Length() int {
|
||||
return inc.Values.Length()
|
||||
}
|
||||
|
||||
func (inc *TSI) Last() float64 {
|
||||
|
|
|
@ -1 +1,34 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
/*
|
||||
import pandas as pd
|
||||
|
||||
data = pd.Series([0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9])
|
||||
ma1 = data.diff(1).ewm(span=25, adjust=False).mean()
|
||||
ma2 = ma1.ewm(span=13, adjust=False).mean()
|
||||
ma3 = data.diff(1).abs().ewm(span=25, adjust=False).mean()
|
||||
ma4 = ma3.ewm(span=13, adjust=False).mean()
|
||||
print(ma2/ma4*100.)
|
||||
*/
|
||||
|
||||
func Test_TSI(t *testing.T) {
|
||||
var randomPrices = []byte(`[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`)
|
||||
var input []fixedpoint.Value
|
||||
if err := json.Unmarshal(randomPrices, &input); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tsi := TSI{}
|
||||
klines := buildKLines(input)
|
||||
tsi.CalculateAndUpdate(klines)
|
||||
assert.Equal(t, tsi.Length(), 29)
|
||||
Delta := 1.5e-2
|
||||
assert.InDelta(t, tsi.Last(), 22.89, Delta)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user