forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
112 lines (92 loc) · 2.3 KB
/
stats.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
package main
import (
"sort"
)
type ExchangeInfo struct {
Exchange string
CryptoCurrency string
FiatCurrency string
Price float64
Volume float64
}
var ExchInfo []ExchangeInfo
type ByPrice []ExchangeInfo
func (this ByPrice) Len() int {
return len(this)
}
func (this ByPrice) Less(i, j int) bool {
return this[i].Price < this[j].Price
}
func (this ByPrice) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
type ByVolume []ExchangeInfo
func (this ByVolume) Len() int {
return len(this)
}
func (this ByVolume) Less(i, j int) bool {
return this[i].Volume < this[j].Volume
}
func (this ByVolume) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
if !IsFiatCurrency(fiat) {
return
}
if len(ExchInfo) == 0 {
AppendExchangeInfo(exchange, crypto, fiat, price, volume)
} else {
if ExchangeInfoAlreadyExists(exchange, crypto, fiat, price, volume) {
return
} else {
AppendExchangeInfo(exchange, crypto, fiat, price, volume)
}
}
}
func AppendExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
exch := ExchangeInfo{}
exch.Exchange = exchange
exch.CryptoCurrency = crypto
exch.FiatCurrency = fiat
exch.Price = price
exch.Volume = volume
ExchInfo = append(ExchInfo, exch)
}
func ExchangeInfoAlreadyExists(exchange, crypto, fiat string, price, volume float64) bool {
for i, _ := range ExchInfo {
if ExchInfo[i].Exchange == exchange && ExchInfo[i].CryptoCurrency == crypto && ExchInfo[i].FiatCurrency == fiat {
ExchInfo[i].Price, ExchInfo[i].Volume = price, volume
return true
}
}
return false
}
func SortExchangesByVolume(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}
for _, x := range ExchInfo {
if x.CryptoCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
}
}
if reverse {
sort.Sort(sort.Reverse(ByVolume(info)))
} else {
sort.Sort(ByVolume(info))
}
return info
}
func SortExchangesByPrice(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}
for _, x := range ExchInfo {
if x.CryptoCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
}
}
if reverse {
sort.Sort(sort.Reverse(ByPrice(info)))
} else {
sort.Sort(ByPrice(info))
}
return info
}