bbgo_origin/pkg/strategy/scmaker/intensity.go

45 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-06-13 04:22:59 +00:00
package scmaker
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
2023-07-10 08:54:22 +00:00
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
2023-06-13 04:22:59 +00:00
"github.com/c9s/bbgo/pkg/types"
)
type IntensityStream struct {
2023-07-10 08:54:22 +00:00
*types.Float64Series
2023-06-13 04:22:59 +00:00
2023-07-10 08:54:22 +00:00
Buy, Sell *indicatorv2.RMAStream
2023-06-13 04:22:59 +00:00
window int
}
2023-07-10 08:54:22 +00:00
func Intensity(source indicatorv2.KLineSubscription, window int) *IntensityStream {
2023-06-13 04:22:59 +00:00
s := &IntensityStream{
2023-07-10 08:54:22 +00:00
Float64Series: types.NewFloat64Series(),
2023-06-13 04:22:59 +00:00
window: window,
2023-07-10 08:54:22 +00:00
Buy: indicatorv2.RMA2(types.NewFloat64Series(), window, false),
Sell: indicatorv2.RMA2(types.NewFloat64Series(), window, false),
2023-06-13 04:22:59 +00:00
}
threshold := fixedpoint.NewFromFloat(100.0)
source.AddSubscriber(func(k types.KLine) {
volume := k.Volume.Float64()
// ignore zero volume events or <= 10usd events
if volume == 0.0 || k.Close.Mul(k.Volume).Compare(threshold) <= 0 {
return
}
c := k.Close.Compare(k.Open)
if c > 0 {
s.Buy.PushAndEmit(volume)
} else if c < 0 {
s.Sell.PushAndEmit(volume)
}
s.Float64Series.PushAndEmit(k.High.Sub(k.Low).Float64())
})
return s
}