forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs.go
195 lines (172 loc) · 4.1 KB
/
pairs.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
package currency
import (
"encoding/json"
"math/rand"
"strings"
"github.com/thrasher-corp/gocryptotrader/log"
)
// NewPairsFromStrings takes in currency pair strings and returns a currency
// pair list
func NewPairsFromStrings(pairs []string) (Pairs, error) {
var newPairs Pairs
for i := range pairs {
if pairs[i] == "" {
continue
}
newPair, err := NewPairFromString(pairs[i])
if err != nil {
return nil, err
}
newPairs = append(newPairs, newPair)
}
return newPairs, nil
}
// Strings returns a slice of strings referring to each currency pair
func (p Pairs) Strings() []string {
var list []string
for i := range p {
list = append(list, p[i].String())
}
return list
}
// Join returns a comma separated list of currency pairs
func (p Pairs) Join() string {
return strings.Join(p.Strings(), ",")
}
// Format formats the pair list to the exchange format configuration
func (p Pairs) Format(delimiter, index string, uppercase bool) Pairs {
var pairs Pairs
for i := range p {
var formattedPair = Pair{
Delimiter: delimiter,
Base: p[i].Base,
Quote: p[i].Quote,
}
if index != "" {
newP, err := NewPairFromIndex(p[i].String(), index)
if err != nil {
log.Errorf(log.Global,
"failed to create NewPairFromIndex. Err: %s\n", err)
continue
}
formattedPair.Base = newP.Base
formattedPair.Quote = newP.Quote
}
if uppercase {
pairs = append(pairs, formattedPair.Upper())
} else {
pairs = append(pairs, formattedPair.Lower())
}
}
return pairs
}
// UnmarshalJSON comforms type to the umarshaler interface
func (p *Pairs) UnmarshalJSON(d []byte) error {
var pairs string
err := json.Unmarshal(d, &pairs)
if err != nil {
return err
}
// If no pairs enabled in config just continue
if pairs == "" {
return nil
}
var allThePairs Pairs
oldPairs := strings.Split(pairs, ",")
for i := range oldPairs {
pair, err := NewPairFromString(oldPairs[i])
if err != nil {
return err
}
allThePairs = append(allThePairs, pair)
}
*p = allThePairs
return nil
}
// MarshalJSON conforms type to the marshaler interface
func (p Pairs) MarshalJSON() ([]byte, error) {
return json.Marshal(p.Join())
}
// Upper returns an upper formatted pair list
func (p Pairs) Upper() Pairs {
var upper Pairs
for i := range p {
upper = append(upper, p[i].Upper())
}
return upper
}
// Contains checks to see if a specified pair exists inside a currency pair
// array
func (p Pairs) Contains(check Pair, exact bool) bool {
for i := range p {
if exact {
if p[i].Equal(check) {
return true
}
} else {
if p[i].EqualIncludeReciprocal(check) {
return true
}
}
}
return false
}
// RemovePairsByFilter checks to see if a pair contains a specific currency
// and removes it from the list of pairs
func (p Pairs) RemovePairsByFilter(filter Code) Pairs {
var pairs Pairs
for i := range p {
if p[i].ContainsCurrency(filter) {
continue
}
pairs = append(pairs, p[i])
}
return pairs
}
// Remove removes the specified pair from the list of pairs if it exists
func (p Pairs) Remove(pair Pair) Pairs {
var pairs Pairs
for x := range p {
if p[x].Equal(pair) {
continue
}
pairs = append(pairs, p[x])
}
return pairs
}
// Add adds a specified pair to the list of pairs if it doesn't exist
func (p Pairs) Add(pair Pair) Pairs {
if p.Contains(pair, true) {
return p
}
p = append(p, pair)
return p
}
// FindDifferences returns pairs which are new or have been removed
func (p Pairs) FindDifferences(pairs Pairs) (newPairs, removedPairs Pairs) {
for x := range pairs {
if pairs[x].String() == "" {
continue
}
if !p.Contains(pairs[x], true) {
newPairs = append(newPairs, pairs[x])
}
}
for x := range p {
if p[x].String() == "" {
continue
}
if !pairs.Contains(p[x], true) {
removedPairs = append(removedPairs, p[x])
}
}
return
}
// GetRandomPair returns a random pair from a list of pairs
func (p Pairs) GetRandomPair() Pair {
pairsLen := len(p)
if pairsLen == 0 {
return Pair{Base: NewCode(""), Quote: NewCode("")}
}
return p[rand.Intn(pairsLen)] // nolint:gosec // basic number generation required, no need for crypo/rand
}