forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange.go
269 lines (241 loc) · 6.81 KB
/
exchange.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"errors"
"log"
"sync"
"github.com/thrasher-/gocryptotrader/common"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/anx"
"github.com/thrasher-/gocryptotrader/exchanges/binance"
"github.com/thrasher-/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-/gocryptotrader/exchanges/bitflyer"
"github.com/thrasher-/gocryptotrader/exchanges/bithumb"
"github.com/thrasher-/gocryptotrader/exchanges/bitmex"
"github.com/thrasher-/gocryptotrader/exchanges/bitstamp"
"github.com/thrasher-/gocryptotrader/exchanges/bittrex"
"github.com/thrasher-/gocryptotrader/exchanges/btcc"
"github.com/thrasher-/gocryptotrader/exchanges/btcmarkets"
"github.com/thrasher-/gocryptotrader/exchanges/coinbasepro"
"github.com/thrasher-/gocryptotrader/exchanges/coinut"
"github.com/thrasher-/gocryptotrader/exchanges/exmo"
"github.com/thrasher-/gocryptotrader/exchanges/gateio"
"github.com/thrasher-/gocryptotrader/exchanges/gemini"
"github.com/thrasher-/gocryptotrader/exchanges/hitbtc"
"github.com/thrasher-/gocryptotrader/exchanges/huobi"
"github.com/thrasher-/gocryptotrader/exchanges/huobihadax"
"github.com/thrasher-/gocryptotrader/exchanges/itbit"
"github.com/thrasher-/gocryptotrader/exchanges/kraken"
"github.com/thrasher-/gocryptotrader/exchanges/lakebtc"
"github.com/thrasher-/gocryptotrader/exchanges/liqui"
"github.com/thrasher-/gocryptotrader/exchanges/localbitcoins"
"github.com/thrasher-/gocryptotrader/exchanges/okcoin"
"github.com/thrasher-/gocryptotrader/exchanges/okex"
"github.com/thrasher-/gocryptotrader/exchanges/poloniex"
"github.com/thrasher-/gocryptotrader/exchanges/wex"
"github.com/thrasher-/gocryptotrader/exchanges/yobit"
"github.com/thrasher-/gocryptotrader/exchanges/zb"
)
// vars related to exchange functions
var (
ErrNoExchangesLoaded = errors.New("no exchanges have been loaded")
ErrExchangeNotFound = errors.New("exchange not found")
ErrExchangeAlreadyLoaded = errors.New("exchange already loaded")
ErrExchangeFailedToLoad = errors.New("exchange failed to load")
)
// CheckExchangeExists returns true whether or not an exchange has already
// been loaded
func CheckExchangeExists(exchName string) bool {
for x := range bot.exchanges {
if common.StringToLower(bot.exchanges[x].GetName()) == common.StringToLower(exchName) {
return true
}
}
return false
}
// GetExchangeByName returns an exchange given an exchange name
func GetExchangeByName(exchName string) exchange.IBotExchange {
for x := range bot.exchanges {
if common.StringToLower(bot.exchanges[x].GetName()) == common.StringToLower(exchName) {
return bot.exchanges[x]
}
}
return nil
}
// ReloadExchange loads an exchange config by name
func ReloadExchange(name string) error {
nameLower := common.StringToLower(name)
if len(bot.exchanges) == 0 {
return ErrNoExchangesLoaded
}
if !CheckExchangeExists(nameLower) {
return ErrExchangeNotFound
}
exchCfg, err := bot.config.GetExchangeConfig(name)
if err != nil {
return err
}
e := GetExchangeByName(nameLower)
e.Setup(exchCfg)
log.Printf("%s exchange reloaded successfully.\n", name)
return nil
}
// UnloadExchange unloads an exchange by name
func UnloadExchange(name string) error {
nameLower := common.StringToLower(name)
if len(bot.exchanges) == 0 {
return ErrNoExchangesLoaded
}
if !CheckExchangeExists(nameLower) {
return ErrExchangeNotFound
}
exchCfg, err := bot.config.GetExchangeConfig(name)
if err != nil {
return err
}
exchCfg.Enabled = false
err = bot.config.UpdateExchangeConfig(exchCfg)
if err != nil {
return err
}
for x := range bot.exchanges {
if bot.exchanges[x].GetName() == name {
bot.exchanges[x].SetEnabled(false)
bot.exchanges = append(bot.exchanges[:x], bot.exchanges[x+1:]...)
return nil
}
}
return ErrExchangeNotFound
}
// LoadExchange loads an exchange by name
func LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error {
nameLower := common.StringToLower(name)
var exch exchange.IBotExchange
if len(bot.exchanges) > 0 {
if CheckExchangeExists(nameLower) {
return ErrExchangeAlreadyLoaded
}
}
switch nameLower {
case "anx":
exch = new(anx.ANX)
case "binance":
exch = new(binance.Binance)
case "bitfinex":
exch = new(bitfinex.Bitfinex)
case "bitflyer":
exch = new(bitflyer.Bitflyer)
case "bithumb":
exch = new(bithumb.Bithumb)
case "bitmex":
exch = new(bitmex.Bitmex)
case "bitstamp":
exch = new(bitstamp.Bitstamp)
case "bittrex":
exch = new(bittrex.Bittrex)
case "btcc":
exch = new(btcc.BTCC)
case "btc markets":
exch = new(btcmarkets.BTCMarkets)
case "coinut":
exch = new(coinut.COINUT)
case "exmo":
exch = new(exmo.EXMO)
case "coinbasepro":
exch = new(coinbasepro.CoinbasePro)
case "gateio":
exch = new(gateio.Gateio)
case "gemini":
exch = new(gemini.Gemini)
case "hitbtc":
exch = new(hitbtc.HitBTC)
case "huobi":
exch = new(huobi.HUOBI)
case "huobihadax":
exch = new(huobihadax.HUOBIHADAX)
case "itbit":
exch = new(itbit.ItBit)
case "kraken":
exch = new(kraken.Kraken)
case "lakebtc":
exch = new(lakebtc.LakeBTC)
case "liqui":
exch = new(liqui.Liqui)
case "localbitcoins":
exch = new(localbitcoins.LocalBitcoins)
case "okcoin china":
exch = new(okcoin.OKCoin)
case "okcoin international":
exch = new(okcoin.OKCoin)
case "okex":
exch = new(okex.OKEX)
case "poloniex":
exch = new(poloniex.Poloniex)
case "wex":
exch = new(wex.WEX)
case "yobit":
exch = new(yobit.Yobit)
case "zb":
exch = new(zb.ZB)
default:
return ErrExchangeNotFound
}
if exch == nil {
return ErrExchangeFailedToLoad
}
exch.SetDefaults()
bot.exchanges = append(bot.exchanges, exch)
exchCfg, err := bot.config.GetExchangeConfig(name)
if err != nil {
return err
}
exchCfg.Enabled = true
exch.Setup(exchCfg)
if useWG {
exch.Start(wg)
} else {
wg := sync.WaitGroup{}
exch.Start(&wg)
wg.Wait()
}
return nil
}
// SetupExchanges sets up the exchanges used by the bot
func SetupExchanges() {
var wg sync.WaitGroup
for _, exch := range bot.config.Exchanges {
if CheckExchangeExists(exch.Name) {
e := GetExchangeByName(exch.Name)
if e == nil {
log.Println(ErrExchangeNotFound)
continue
}
err := ReloadExchange(exch.Name)
if err != nil {
log.Printf("ReloadExchange %s failed: %s", exch.Name, err)
continue
}
if !e.IsEnabled() {
UnloadExchange(exch.Name)
continue
}
return
}
if !exch.Enabled {
log.Printf("%s: Exchange support: Disabled", exch.Name)
continue
} else {
err := LoadExchange(exch.Name, true, &wg)
if err != nil {
log.Printf("LoadExchange %s failed: %s", exch.Name, err)
continue
}
}
log.Printf(
"%s: Exchange support: Enabled (Authenticated API support: %s - Verbose mode: %s).\n",
exch.Name,
common.IsEnabled(exch.AuthenticatedAPISupport),
common.IsEnabled(exch.Verbose),
)
}
wg.Wait()
}