2021-05-07 18:09:45 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
vwap implements the volume weighted average price (VWAP) indicator:
|
|
|
|
|
2021-05-08 16:56:44 +00:00
|
|
|
Volume Weighted Average Price (VWAP) Definition
|
2021-05-07 18:09:45 +00:00
|
|
|
- https://www.investopedia.com/terms/v/vwap.asp
|
2021-05-08 16:56:44 +00:00
|
|
|
|
|
|
|
Volume-Weighted Average Price (VWAP) Explained
|
2021-05-07 18:09:45 +00:00
|
|
|
- https://academy.binance.com/en/articles/volume-weighted-average-price-vwap-explained
|
|
|
|
*/
|
|
|
|
//go:generate callbackgen -type VWAP
|
|
|
|
type VWAP struct {
|
|
|
|
types.IntervalWindow
|
2021-05-22 12:20:48 +00:00
|
|
|
Values types.Float64Slice
|
2022-03-28 19:19:29 +00:00
|
|
|
Prices types.Float64Slice
|
|
|
|
Volumes types.Float64Slice
|
2021-05-07 18:09:45 +00:00
|
|
|
WeightedSum float64
|
|
|
|
VolumeSum float64
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
EndTime time.Time
|
2021-05-07 18:09:45 +00:00
|
|
|
UpdateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
func (inc *VWAP) Last() float64 {
|
|
|
|
if len(inc.Values) == 0 {
|
|
|
|
return 0.0
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
return inc.Values[len(inc.Values)-1]
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
func (inc *VWAP) Update(kLine types.KLine, priceF KLinePriceMapper) {
|
2021-05-07 18:09:45 +00:00
|
|
|
price := priceF(kLine)
|
2022-02-03 04:55:25 +00:00
|
|
|
volume := kLine.Volume.Float64()
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
inc.Prices.Push(price)
|
|
|
|
inc.Volumes.Push(volume)
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
if inc.Window != 0 && len(inc.Prices) > inc.Window {
|
|
|
|
popIndex := len(inc.Prices) - inc.Window - 1
|
|
|
|
inc.WeightedSum -= inc.Prices[popIndex] * inc.Volumes[popIndex]
|
|
|
|
inc.VolumeSum -= inc.Volumes[popIndex]
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
inc.WeightedSum += price * volume
|
|
|
|
inc.VolumeSum += volume
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
vwap := inc.WeightedSum / inc.VolumeSum
|
|
|
|
inc.Values.Push(vwap)
|
|
|
|
}
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
func (inc *VWAP) calculateAndUpdate(kLines []types.KLine) {
|
|
|
|
var priceF = KLineTypicalPriceMapper
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-03-28 19:19:29 +00:00
|
|
|
for _, k := range kLines {
|
|
|
|
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
|
|
|
|
continue
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
inc.Update(k, priceF)
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
|
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *VWAP) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inc.calculateAndUpdate(window)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *VWAP) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
|
|
|
|
func CalculateVWAP(klines []types.KLine, priceF KLinePriceMapper, window int) float64 {
|
|
|
|
vwap := VWAP{IntervalWindow: types.IntervalWindow{Window: window}}
|
|
|
|
for _, k := range klines {
|
|
|
|
vwap.Update(k, priceF)
|
|
|
|
}
|
|
|
|
return vwap.Last()
|
|
|
|
}
|