-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
price_volume_slice.go
208 lines (166 loc) · 4.86 KB
/
price_volume_slice.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package types
import (
"encoding/json"
"fmt"
"sort"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type PriceVolume struct {
Price, Volume fixedpoint.Value
}
func (p PriceVolume) InQuote() fixedpoint.Value {
return p.Price.Mul(p.Volume)
}
func (p PriceVolume) Equals(b PriceVolume) bool {
return p.Price.Eq(b.Price) && p.Volume.Eq(b.Volume)
}
func (p PriceVolume) String() string {
return fmt.Sprintf("PriceVolume{ Price: %s, Volume: %s }", p.Price.String(), p.Volume.String())
}
type PriceVolumeSlice []PriceVolume
func (slice PriceVolumeSlice) Len() int { return len(slice) }
func (slice PriceVolumeSlice) Less(i, j int) bool { return slice[i].Price.Compare(slice[j].Price) < 0 }
func (slice PriceVolumeSlice) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] }
// Trim removes the pairs that volume = 0
func (slice PriceVolumeSlice) Trim() (pvs PriceVolumeSlice) {
for _, pv := range slice {
if pv.Volume.Sign() > 0 {
pvs = append(pvs, pv)
}
}
return pvs
}
func (slice PriceVolumeSlice) CopyDepth(depth int) PriceVolumeSlice {
if depth == 0 || depth > len(slice) {
return slice.Copy()
}
var s = make(PriceVolumeSlice, depth)
copy(s, slice[:depth])
return s
}
func (slice PriceVolumeSlice) Copy() PriceVolumeSlice {
var s = make(PriceVolumeSlice, len(slice))
copy(s, slice)
return s
}
func (slice PriceVolumeSlice) Second() (PriceVolume, bool) {
if len(slice) > 1 {
return slice[1], true
}
return PriceVolume{}, false
}
func (slice PriceVolumeSlice) First() (PriceVolume, bool) {
if len(slice) > 0 {
return slice[0], true
}
return PriceVolume{}, false
}
func (slice PriceVolumeSlice) IndexByQuoteVolumeDepth(requiredQuoteVolume fixedpoint.Value) int {
var totalQuoteVolume = fixedpoint.Zero
for x, pv := range slice {
// this should use float64 multiply
quoteVolume := fixedpoint.Mul(pv.Volume, pv.Price)
totalQuoteVolume = totalQuoteVolume.Add(quoteVolume)
if totalQuoteVolume.Compare(requiredQuoteVolume) >= 0 {
return x
}
}
// depth not enough
return -1
}
func (slice PriceVolumeSlice) SumDepth() fixedpoint.Value {
var total = fixedpoint.Zero
for _, pv := range slice {
total = total.Add(pv.Volume)
}
return total
}
func (slice PriceVolumeSlice) SumDepthInQuote() fixedpoint.Value {
var total = fixedpoint.Zero
for _, pv := range slice {
quoteVolume := fixedpoint.Mul(pv.Price, pv.Volume)
total = total.Add(quoteVolume)
}
return total
}
func (slice PriceVolumeSlice) IndexByVolumeDepth(requiredVolume fixedpoint.Value) int {
var tv = fixedpoint.Zero
for x, el := range slice {
tv = tv.Add(el.Volume)
if tv.Compare(requiredVolume) >= 0 {
return x
}
}
// depth not enough
return -1
}
func (slice PriceVolumeSlice) InsertAt(idx int, pv PriceVolume) PriceVolumeSlice {
rear := append([]PriceVolume{}, slice[idx:]...)
newSlice := append(slice[:idx], pv)
return append(newSlice, rear...)
}
func (slice PriceVolumeSlice) Remove(price fixedpoint.Value, descending bool) PriceVolumeSlice {
matched, idx := slice.Find(price, descending)
if matched.Price.Compare(price) != 0 || matched.Price.IsZero() {
return slice
}
return append(slice[:idx], slice[idx+1:]...)
}
// Find finds the pair by the given price, this function is a read-only
// operation, so we use the value receiver to avoid copy value from the pointer
// If the price is not found, it will return the index where the price can be inserted at.
// true for descending (bid orders), false for ascending (ask orders)
func (slice PriceVolumeSlice) Find(price fixedpoint.Value, descending bool) (pv PriceVolume, idx int) {
idx = sort.Search(len(slice), func(i int) bool {
if descending {
return slice[i].Price.Compare(price) <= 0
}
return slice[i].Price.Compare(price) >= 0
})
if idx >= len(slice) || slice[idx].Price.Compare(price) != 0 {
return pv, idx
}
pv = slice[idx]
return pv, idx
}
func (slice PriceVolumeSlice) Upsert(pv PriceVolume, descending bool) PriceVolumeSlice {
if len(slice) == 0 {
return append(slice, pv)
}
price := pv.Price
_, idx := slice.Find(price, descending)
if idx >= len(slice) || slice[idx].Price.Compare(price) != 0 {
return slice.InsertAt(idx, pv)
}
slice[idx].Volume = pv.Volume
return slice
}
func (slice *PriceVolumeSlice) UnmarshalJSON(b []byte) error {
s, err := ParsePriceVolumeSliceJSON(b)
if err != nil {
return err
}
*slice = s
return nil
}
// ParsePriceVolumeSliceJSON tries to parse a 2 dimensional string array into a PriceVolumeSlice
//
// [["9000", "10"], ["9900", "10"], ... ]
func ParsePriceVolumeSliceJSON(b []byte) (slice PriceVolumeSlice, err error) {
var as [][]fixedpoint.Value
err = json.Unmarshal(b, &as)
if err != nil {
return slice, err
}
for _, a := range as {
var pv PriceVolume
pv.Price = a[0]
pv.Volume = a[1]
// kucoin returns price in 0, we should skip
if pv.Price.Eq(fixedpoint.Zero) {
continue
}
slice = append(slice, pv)
}
return slice, nil
}