mirror of
https://github.com/c9s/bbgo.git
synced 2024-09-20 08:11:08 +00:00
move MovingAverageIndicator
This commit is contained in:
parent
a1c027471e
commit
90515855eb
|
@ -1,56 +1,2 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type MovingAverageIndicator struct {
|
||||
store *MarketDataStore
|
||||
Period int
|
||||
}
|
||||
|
||||
func NewMovingAverageIndicator(period int) *MovingAverageIndicator {
|
||||
return &MovingAverageIndicator{
|
||||
Period: period,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *MovingAverageIndicator) handleUpdate(kline types.KLine) {
|
||||
klines, ok := i.store.KLineWindows[Interval(kline.Interval)]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if len(klines) < i.Period {
|
||||
return
|
||||
}
|
||||
|
||||
// calculate ma
|
||||
}
|
||||
|
||||
type IndicatorValue struct {
|
||||
Value float64
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
func calculateMovingAverage(klines types.KLineWindow, period int) (values []IndicatorValue) {
|
||||
for idx := range klines[period:] {
|
||||
offset := idx + period
|
||||
sum := klines[offset-period : offset].ReduceClose()
|
||||
values = append(values, IndicatorValue{
|
||||
Time: klines[offset].GetEndTime(),
|
||||
Value: math.Round(sum / float64(period)),
|
||||
})
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func (i *MovingAverageIndicator) SubscribeStore(store *MarketDataStore) {
|
||||
i.store = store
|
||||
|
||||
// register kline update callback
|
||||
store.OnUpdate(i.handleUpdate)
|
||||
}
|
||||
|
|
57
pkg/indicator/moving_average.go
Normal file
57
pkg/indicator/moving_average.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type MovingAverageIndicator struct {
|
||||
store *bbgo.MarketDataStore
|
||||
Period int
|
||||
}
|
||||
|
||||
func NewMovingAverageIndicator(period int) *MovingAverageIndicator {
|
||||
return &MovingAverageIndicator{
|
||||
Period: period,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *MovingAverageIndicator) handleUpdate(kline types.KLine) {
|
||||
klines, ok := i.store.KLineWindows[bbgo.Interval(kline.Interval)]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if len(klines) < i.Period {
|
||||
return
|
||||
}
|
||||
|
||||
// calculate ma
|
||||
}
|
||||
|
||||
type IndicatorValue struct {
|
||||
Value float64
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
func calculateMovingAverage(klines types.KLineWindow, period int) (values []IndicatorValue) {
|
||||
for idx := range klines[period:] {
|
||||
offset := idx + period
|
||||
sum := klines[offset-period : offset].ReduceClose()
|
||||
values = append(values, IndicatorValue{
|
||||
Time: klines[offset].GetEndTime(),
|
||||
Value: math.Round(sum / float64(period)),
|
||||
})
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func (i *MovingAverageIndicator) SubscribeStore(store *bbgo.MarketDataStore) {
|
||||
i.store = store
|
||||
|
||||
// register kline update callback
|
||||
store.OnUpdate(i.handleUpdate)
|
||||
}
|
Loading…
Reference in New Issue
Block a user