-
Notifications
You must be signed in to change notification settings - Fork 126
/
FilterVWAP.go
129 lines (112 loc) · 3.04 KB
/
FilterVWAP.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package filters
import (
"math"
"strconv"
"time"
"github.com/diadata-org/diadata/pkg/dia"
log "github.com/sirupsen/logrus"
)
// FilterVWAP ...
type FilterVWAP struct {
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
filterName string
nativeDenomination bool
modified bool
}
// NewFilterVWAP ...
func NewFilterVWAP(asset dia.Asset, exchange string, currentTime time.Time, param int, nativeDenomination bool) *FilterVWAP {
s := &FilterVWAP{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAP" + strconv.Itoa(param),
nativeDenomination: nativeDenomination,
}
return s
}
// Compute ...
func (s *FilterVWAP) Compute(trade dia.Trade) {
s.compute(trade)
}
func (filter *FilterVWAP) compute(trade dia.Trade) {
filter.modified = true
if filter.lastTrade != (dia.Trade{}) {
if trade.Time.Before(filter.currentTime) {
log.Errorln("FilterVWAP: Ignoring Trade out of order ", filter.currentTime, trade.Time)
return
}
}
filter.fill(trade)
filter.lastTrade = trade
}
// fill just adds a trade to the prices and volumes slices.
func (filter *FilterVWAP) fill(trade dia.Trade) {
// filter.currentTime is the timestamp of the last filled trade.
filter.processDataPoint(trade)
filter.currentTime = trade.Time
}
func (filter *FilterVWAP) processDataPoint(trade dia.Trade) {
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}
filter.volumes = append([]float64{trade.Volume}, filter.volumes...)
}
// FinalCompute ...
func (s *FilterVWAP) FinalCompute(t time.Time) float64 {
return s.finalCompute(t)
}
func (s *FilterVWAP) finalCompute(t time.Time) float64 {
log.Infof("computed value %v at time %v ", s.value, t)
if s.lastTrade == (dia.Trade{}) {
return 0.0
}
var totalVolume float64 = 0
var priceVolume []float64
var totalPriceVolume float64 = 0
for index, price := range s.prices {
priceVolume = append(priceVolume, price*math.Abs(s.volumes[index]))
}
for _, v := range s.volumes {
totalVolume += math.Abs(v)
}
for _, v := range priceVolume {
totalPriceVolume += v
}
if totalVolume > 0 {
s.value = totalPriceVolume / totalVolume
}
return s.value
}
// FilterPointForBlock ...
func (s *FilterVWAP) FilterPointForBlock() *dia.FilterPoint {
return s.filterPointForBlock()
}
func (s *FilterVWAP) filterPointForBlock() *dia.FilterPoint {
if s.exchange != "" {
return &dia.FilterPoint{
Value: s.value,
Name: "VWAP" + strconv.Itoa(s.param),
Time: s.currentTime,
Asset: s.asset,
}
} else {
return &dia.FilterPoint{
Value: s.value,
Name: "VWAP" + strconv.Itoa(s.param),
Time: s.currentTime,
Asset: s.asset,
}
}
}