mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
update adx indicator and test case
This commit is contained in:
parent
474a8ab864
commit
e632fa087e
|
@ -8,7 +8,8 @@ import (
|
|||
)
|
||||
|
||||
type ADXStream struct {
|
||||
*types.Float64Series
|
||||
*RMAStream
|
||||
|
||||
Plus, Minus *types.Float64Series
|
||||
|
||||
window int
|
||||
|
@ -19,45 +20,47 @@ func ADX(source KLineSubscription, window int) *ADXStream {
|
|||
var (
|
||||
atr = ATR2(source, window)
|
||||
dmp = types.NewFloat64Series()
|
||||
dmm = types.NewFloat64Series()
|
||||
dmn = types.NewFloat64Series()
|
||||
adx = types.NewFloat64Series()
|
||||
sdmp = RMA2(dmp, window, true)
|
||||
sdmm = RMA2(dmm, window, true)
|
||||
sadx = RMA2(adx, window, true)
|
||||
sdmn = RMA2(dmn, window, true)
|
||||
s = &ADXStream{
|
||||
window: window,
|
||||
Plus: types.NewFloat64Series(),
|
||||
Minus: types.NewFloat64Series(),
|
||||
Float64Series: types.NewFloat64Series(),
|
||||
prevHigh: fixedpoint.Zero,
|
||||
prevLow: fixedpoint.Zero,
|
||||
RMAStream: RMA2(adx, window, true),
|
||||
}
|
||||
)
|
||||
|
||||
source.AddSubscriber(func(k types.KLine) {
|
||||
up, down := k.High.Sub(s.prevHigh), -k.Low.Sub(s.prevLow)
|
||||
if up.Compare(down) > 0 && up > 0 {
|
||||
if s.prevHigh.IsZero() || s.prevLow.IsZero() {
|
||||
s.prevHigh, s.prevLow = k.High, k.Low
|
||||
return
|
||||
}
|
||||
|
||||
up, dn := k.High.Sub(s.prevHigh), s.prevLow.Sub(k.Low)
|
||||
if up.Compare(dn) > 0 && up > 0 {
|
||||
dmp.PushAndEmit(up.Float64())
|
||||
} else {
|
||||
dmp.PushAndEmit(0.0)
|
||||
}
|
||||
|
||||
if down.Compare(up) > 0 && down > 0 {
|
||||
dmm.PushAndEmit(down.Float64())
|
||||
if dn.Compare(up) > 0 && dn > 0 {
|
||||
dmn.PushAndEmit(dn.Float64())
|
||||
} else {
|
||||
dmm.PushAndEmit(0.0)
|
||||
dmn.PushAndEmit(0.0)
|
||||
}
|
||||
|
||||
s.Plus.PushAndEmit(sdmp.Last(0) / atr.Last(0) * 100)
|
||||
s.Minus.PushAndEmit(sdmm.Last(0) / atr.Last(0) * 100)
|
||||
|
||||
sum := s.Plus.Last(0) + s.Minus.Last(0)
|
||||
if sum == 0 {
|
||||
sum = 1
|
||||
s.Plus.PushAndEmit(sdmp.Last(0) * 100 / atr.Last(0))
|
||||
s.Minus.PushAndEmit(sdmn.Last(0) * 100 / atr.Last(0))
|
||||
dx := math.Abs(s.Plus.Last(0)-s.Minus.Last(0)) / (s.Plus.Last(0) + s.Minus.Last(0))
|
||||
if !math.IsNaN(dx) {
|
||||
adx.PushAndEmit(dx * 100.0)
|
||||
} else {
|
||||
adx.PushAndEmit(0.0)
|
||||
}
|
||||
adx.PushAndEmit(math.Abs(s.Plus.Last(0)-s.Minus.Last(0)) / sum)
|
||||
|
||||
s.PushAndEmit(sadx.Last(0) * 100)
|
||||
s.prevHigh, s.prevLow = k.High, k.Low
|
||||
s.Truncate()
|
||||
})
|
||||
|
|
|
@ -36,7 +36,7 @@ func Test_ADX(t *testing.T) {
|
|||
name: "test_binance_btcusdt_1h",
|
||||
kLines: buildKLines(bytes),
|
||||
window: 7,
|
||||
want: 31.895091,
|
||||
want: 34.83952,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user