Merge pull request #226 from narumiruna/feature/add-obc-indicator

This commit is contained in:
Yo-An Lin 2021-05-12 09:21:27 +08:00 committed by GitHub
commit 037f564b03
3 changed files with 138 additions and 0 deletions

76
pkg/indicator/obv.go Normal file
View File

@ -0,0 +1,76 @@
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/types"
)
/*
obv implements on-balance volume indicator
On-Balance Volume (OBV) Definition
- https://www.investopedia.com/terms/o/onbalancevolume.asp
*/
//go:generate callbackgen -type OBV
type OBV struct {
types.IntervalWindow
Values Float64Slice
PrePrice float64
EndTime time.Time
UpdateCallbacks []func(value float64)
}
func (inc *OBV) update(kLine types.KLine, priceF KLinePriceMapper) {
price := priceF(kLine)
volume := kLine.Volume
if len(inc.Values) == 0 {
inc.PrePrice = price
inc.Values.Push(volume)
return
}
var sign float64 = 0.0
if volume > inc.PrePrice {
sign = 1.0
} else if volume < inc.PrePrice {
sign = -1.0
}
obv := inc.Last() + sign*volume
inc.Values.Push(obv)
}
func (inc *OBV) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
func (inc *OBV) calculateAndUpdate(kLines []types.KLine) {
var priceF = KLineClosePriceMapper
for i, k := range kLines {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
continue
}
inc.update(k, priceF)
inc.EmitUpdate(inc.Last())
inc.EndTime = kLines[i].EndTime
}
}
func (inc *OBV) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.calculateAndUpdate(window)
}
func (inc *OBV) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -0,0 +1,15 @@
// Code generated by "callbackgen -type OBV"; DO NOT EDIT.
package indicator
import ()
func (inc *OBV) OnUpdate(cb func(value float64)) {
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
}
func (inc *OBV) EmitUpdate(value float64) {
for _, cb := range inc.UpdateCallbacks {
cb(value)
}
}

47
pkg/indicator/obv_test.go Normal file
View File

@ -0,0 +1,47 @@
package indicator
import (
"reflect"
"testing"
"github.com/c9s/bbgo/pkg/types"
)
func Test_calculateOBV(t *testing.T) {
buildKLines := func(prices, volumes []float64) (kLines []types.KLine) {
for i, p := range prices {
kLines = append(kLines, types.KLine{High: p, Low: p, Close: p, Volume: volumes[i]})
}
return kLines
}
tests := []struct {
name string
kLines []types.KLine
window int
want Float64Slice
}{
{
name: "trivial_case",
kLines: buildKLines([]float64{0}, []float64{1}),
window: 0,
want: Float64Slice{1.0},
},
{
name: "easy_case",
kLines: buildKLines([]float64{3, 2, 1, 4}, []float64{3, 2, 2, 6}),
window: 0,
want: Float64Slice{3, 1, -1, 5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obv := OBV{IntervalWindow: types.IntervalWindow{Window: tt.window}}
obv.calculateAndUpdate(tt.kLines)
if !reflect.DeepEqual(obv.Values, tt.want) {
t.Errorf("calculateAndUpdate() = %v, want %v", obv.Values, tt.want)
}
})
}
}