-
Notifications
You must be signed in to change notification settings - Fork 126
/
FilterVWAPIR.go
157 lines (134 loc) · 3.57 KB
/
FilterVWAPIR.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package filters
import (
"math"
"strconv"
"time"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
log "github.com/sirupsen/logrus"
)
// FilterVWAP ...
type FilterVWAPIR struct {
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
modified bool
filterName string
asset dia.Asset
nativeDenomination bool
}
func NewFilterVWAPIR(asset dia.Asset, exchange string, currentTime time.Time, param int, nativeDenomination bool) *FilterVWAPIR {
s := &FilterVWAPIR{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAPIR" + strconv.Itoa(param),
nativeDenomination: nativeDenomination,
}
return s
}
func (s *FilterVWAPIR) Compute(trade dia.Trade) {
s.compute(trade)
}
func (filter *FilterVWAPIR) compute(trade dia.Trade) {
filter.modified = true
if filter.lastTrade != (dia.Trade{}) {
if trade.Time.Before(filter.currentTime) {
log.Errorln("FilterVWAPIR: 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 *FilterVWAPIR) fill(trade dia.Trade) {
// filter.currentTime is the timestamp of the last filled trade.
filter.processDataPoint(trade)
filter.currentTime = trade.Time
}
func (filter *FilterVWAPIR) 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...)
}
func (s *FilterVWAPIR) FinalCompute(t time.Time) float64 {
log.Info("final compute of time ", t)
return s.finalCompute(t)
}
func (s *FilterVWAPIR) finalCompute(t time.Time) float64 {
if s.lastTrade == (dia.Trade{}) {
return 0.0
}
if len(s.prices) == 0 {
return 0.0
}
if len(s.prices) < 2 {
s.value = s.prices[0]
return s.prices[0]
}
cleanPrices, bounds := removeOutliers(s.prices)
if len(bounds) < 2 {
return 0.0
}
cleanedVolumes := s.volumes[bounds[0]:bounds[1]]
priceVolume := []float64{}
for index, price := range cleanPrices {
priceVolume = append(priceVolume, price*math.Abs(cleanedVolumes[index]))
}
var total float64 = 0
var totalVolume float64 = 0
for _, v := range cleanedVolumes {
totalVolume += math.Abs(v)
}
for _, v := range priceVolume {
total += v
}
s.value = total / totalVolume
// Reset filters
s.prices = []float64{}
s.volumes = []float64{}
return s.value
}
func (s *FilterVWAPIR) FilterPointForBlock() *dia.FilterPoint {
return s.filterPointForBlock()
}
func (s *FilterVWAPIR) filterPointForBlock() *dia.FilterPoint {
if s.exchange != "" {
return &dia.FilterPoint{
Value: s.value,
Name: s.filterName,
Time: s.currentTime,
Asset: s.asset,
}
} else {
return &dia.FilterPoint{
Value: s.value,
Name: s.filterName,
Time: s.currentTime,
Asset: s.asset,
}
}
}
func (filter *FilterVWAPIR) save(ds models.Datastore) error {
if filter.modified {
filter.modified = false
err := ds.SetFilter(filter.filterName, filter.asset, filter.exchange, filter.value, filter.currentTime)
if err != nil {
log.Errorln("FilterVWAPIR: Error:", err)
}
return err
} else {
return nil
}
}