-
Notifications
You must be signed in to change notification settings - Fork 4
/
Util.go
238 lines (201 loc) · 4.25 KB
/
Util.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package goghostex
import (
"bytes"
"compress/flate"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/google/uuid"
)
func ToFloat64(v interface{}) float64 {
if v == nil {
return 0.0
}
switch v.(type) {
case float64:
return v.(float64)
case string:
vStr := v.(string)
vF, _ := strconv.ParseFloat(vStr, 64)
return vF
default:
panic("to float64 error.")
}
}
func ToInt(v interface{}) int {
if v == nil {
return 0
}
switch v.(type) {
case string:
vStr := v.(string)
vInt, _ := strconv.Atoi(vStr)
return vInt
case int:
return v.(int)
case float64:
vF := v.(float64)
return int(vF)
default:
panic("to int error.")
}
}
func ToUint64(v interface{}) uint64 {
if v == nil {
return 0
}
switch v.(type) {
case int:
return uint64(v.(int))
case float64:
return uint64((v.(float64)))
case string:
uV, _ := strconv.ParseUint(v.(string), 10, 64)
return uV
default:
panic("to uint64 error.")
}
}
func ToInt64(v interface{}) int64 {
if v == nil {
return 0
}
switch v.(type) {
case float64:
return int64(v.(float64))
default:
vv := fmt.Sprint(v)
if vv == "" {
return 0
}
vvv, err := strconv.ParseInt(vv, 0, 64)
if err != nil {
return 0
}
return vvv
}
}
// FloatToString n :保留的小数点位数,去除末尾多余的0(StripTrailingZeros)
func FloatToString(v float64, n int64) string {
theN := int(n)
ret := strconv.FormatFloat(v, 'f', theN, 64)
return strconv.FormatFloat(ToFloat64(ret), 'f', -1, 64) //StripTrailingZeros
}
// FloatToPrice n :保留的小数点位数,去除末尾多余的0(StripTrailingZeros),并加入ticksize
func FloatToPrice(v float64, n int64, tickSize float64) string {
if tickSize <= 0 {
return FloatToString(v, n)
}
var intPart = float64(int64(v))
var floatPart = float64(int64((v-intPart)/tickSize)) * tickSize
var ret = strconv.FormatFloat(intPart+floatPart, 'f', int(n), 64)
return strconv.FormatFloat(ToFloat64(ret), 'f', -1, 64) //StripTrailingZeros
}
func ValuesToJson(v url.Values) ([]byte, error) {
parammap := make(map[string]interface{})
for k, vv := range v {
if len(vv) == 1 {
parammap[k] = vv[0]
} else {
parammap[k] = vv
}
}
return json.Marshal(parammap)
}
func GzipUnCompress(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
func FlateUnCompress(data []byte) ([]byte, error) {
reader := flate.NewReader(bytes.NewReader(data))
defer func() { _ = reader.Close() }()
return ioutil.ReadAll(reader)
}
func UUID() string {
return strings.Replace(uuid.New().String(), "-", "", 32)
}
func GetPrecision(minSize float64) int {
if minSize < 0.0000000001 {
return 10
}
for i := 0; ; i++ {
if minSize >= 1 {
return i
}
minSize *= 10
}
}
func GetPrecisionInt64(minSize float64) int64 {
if minSize < 0.0000000001 {
return 10
}
for i := int64(0); ; i++ {
if minSize >= 1 {
return i
}
minSize *= 10
}
}
func GetAscKline(klines []*Kline) []*Kline {
if len(klines) <= 1 {
return klines
}
// asc seq
if klines[0].Timestamp < klines[1].Timestamp {
return klines
}
ascKlines := make([]*Kline, 0)
for i := len(klines) - 1; i >= 0; i-- {
ascKlines = append(ascKlines, klines[i])
}
return ascKlines
}
func GetAscFutureKline(klines []*FutureKline) []*FutureKline {
if len(klines) <= 1 {
return klines
}
// asc seq
if klines[0].Timestamp < klines[1].Timestamp {
return klines
}
ascKlines := make([]*FutureKline, 0)
for i := len(klines) - 1; i >= 0; i-- {
ascKlines = append(ascKlines, klines[i])
}
return ascKlines
}
func GetAscFutureCandle(candles []*FutureCandle) []*FutureCandle {
if len(candles) <= 1 {
return candles
}
// asc seq
if candles[0].Timestamp < candles[1].Timestamp {
return candles
}
ascCandles := make([]*FutureCandle, 0)
for i := len(candles) - 1; i >= 0; i-- {
ascCandles = append(ascCandles, candles[i])
}
return ascCandles
}
func GetAscSwapKline(klines []*SwapKline) []*SwapKline {
if len(klines) <= 1 {
return klines
}
// asc seq
if klines[0].Timestamp < klines[1].Timestamp {
return klines
}
ascKlines := make([]*SwapKline, 0)
for i := len(klines) - 1; i >= 0; i-- {
ascKlines = append(ascKlines, klines[i])
}
return ascKlines
}