2021-05-07 18:09:45 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
2021-05-07 18:09:45 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-12-13 05:02:38 +00:00
|
|
|
// vwap implements the volume weighted average price (VWAP) indicator:
|
|
|
|
//
|
|
|
|
// Volume Weighted Average Price (VWAP) Definition
|
|
|
|
// - https://www.investopedia.com/terms/v/vwap.asp
|
|
|
|
//
|
|
|
|
// Volume-Weighted Average Price (VWAP) Explained
|
|
|
|
// - https://academy.binance.com/en/articles/volume-weighted-average-price-vwap-explained
|
|
|
|
//
|
|
|
|
// The Volume Weighted Average Price (VWAP) is a technical analysis indicator that is used to measure the average price of a security
|
|
|
|
// over a specified period of time, with the weighting factors determined by the volume of the security. It is calculated by taking the
|
|
|
|
// sum of the product of the price and volume for each trade, and then dividing that sum by the total volume of the security over the
|
|
|
|
// specified period of time. This resulting average is then plotted on the price chart as a line, which can be used to make predictions
|
|
|
|
// about future price movements. The VWAP is typically more accurate than other simple moving averages, as it takes into account the
|
|
|
|
// volume of the security, but may be less reliable in markets with low trading volume.
|
2021-05-07 18:09:45 +00:00
|
|
|
|
|
|
|
//go:generate callbackgen -type VWAP
|
|
|
|
type VWAP struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2021-05-07 18:09:45 +00:00
|
|
|
types.IntervalWindow
|
2022-08-25 09:31:42 +00:00
|
|
|
Values floats.Slice
|
|
|
|
Prices floats.Slice
|
|
|
|
Volumes floats.Slice
|
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-04-18 04:08:21 +00:00
|
|
|
func (inc *VWAP) Update(price, volume float64) {
|
2022-06-29 12:49:02 +00:00
|
|
|
if len(inc.Prices) == 0 {
|
|
|
|
inc.SeriesBase.Series = inc
|
|
|
|
}
|
2022-04-18 04:08:21 +00:00
|
|
|
inc.Prices.Push(price)
|
|
|
|
inc.Volumes.Push(volume)
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
|
|
|
inc.WeightedSum += price * volume
|
|
|
|
inc.VolumeSum += volume
|
|
|
|
|
|
|
|
vwap := inc.WeightedSum / inc.VolumeSum
|
|
|
|
inc.Values.Push(vwap)
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (inc *VWAP) Last(i int) float64 {
|
|
|
|
return inc.Values.Last(i)
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 04:14:17 +00:00
|
|
|
func (inc *VWAP) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return inc.Last(i)
|
2022-04-04 04:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *VWAP) Length() int {
|
|
|
|
return len(inc.Values)
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &VWAP{}
|
2022-04-04 04:14:17 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *VWAP) PushK(k types.KLine) {
|
|
|
|
inc.Update(KLineTypicalPriceMapper(k), k.Volume.Float64())
|
|
|
|
}
|
2021-05-07 18:09:45 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *VWAP) CalculateAndUpdate(allKLines []types.KLine) {
|
|
|
|
for _, k := range allKLines {
|
2022-04-14 21:43:04 +00:00
|
|
|
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
|
2022-03-28 19:19:29 +00:00
|
|
|
continue
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
2022-07-13 17:12:36 +00:00
|
|
|
|
|
|
|
inc.PushK(k)
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.EmitUpdate(inc.Last(0))
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.EndTime = allKLines[len(allKLines)-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
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2021-05-07 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *VWAP) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|
2022-03-28 19:19:29 +00:00
|
|
|
|
2022-08-24 09:53:22 +00:00
|
|
|
func calculateVWAP(klines []types.KLine, priceF KLineValueMapper, window int) float64 {
|
2022-03-28 19:19:29 +00:00
|
|
|
vwap := VWAP{IntervalWindow: types.IntervalWindow{Window: window}}
|
|
|
|
for _, k := range klines {
|
2022-04-18 04:08:21 +00:00
|
|
|
vwap.Update(priceF(k), k.Volume.Float64())
|
2022-03-28 19:19:29 +00:00
|
|
|
}
|
2023-05-31 11:35:44 +00:00
|
|
|
return vwap.Last(0)
|
2022-03-28 19:19:29 +00:00
|
|
|
}
|