forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routines.go
279 lines (258 loc) · 8.52 KB
/
routines.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
270
271
272
273
274
275
276
277
278
279
package main
import (
"fmt"
"log"
"sync"
"time"
"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/symbol"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
func printCurrencyFormat(price float64) string {
displaySymbol, err := symbol.GetSymbolByCurrencyName(bot.config.Currency.FiatDisplayCurrency)
if err != nil {
log.Printf("Failed to get display symbol: %s", err)
}
return fmt.Sprintf("%s%.8f", displaySymbol, price)
}
func printConvertCurrencyFormat(origCurrency string, origPrice float64) string {
displayCurrency := bot.config.Currency.FiatDisplayCurrency
conv, err := currency.ConvertCurrency(origPrice, origCurrency, displayCurrency)
if err != nil {
log.Printf("Failed to convert currency: %s", err)
}
displaySymbol, err := symbol.GetSymbolByCurrencyName(displayCurrency)
if err != nil {
log.Printf("Failed to get display symbol: %s", err)
}
origSymbol, err := symbol.GetSymbolByCurrencyName(origCurrency)
if err != nil {
log.Printf("Failed to get original currency symbol: %s", err)
}
return fmt.Sprintf("%s%.2f %s (%s%.2f %s)",
displaySymbol,
conv,
displayCurrency,
origSymbol,
origPrice,
origCurrency,
)
}
func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exchangeName string, err error) {
if err != nil {
log.Printf("Failed to get %s %s ticker. Error: %s",
p.Pair().String(),
exchangeName,
err)
return
}
stats.Add(exchangeName, p, assetType, result.Last, result.Volume)
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.Currency.FiatDisplayCurrency {
origCurrency := p.SecondCurrency.Upper().String()
log.Printf("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
printConvertCurrencyFormat(origCurrency, result.Last),
printConvertCurrencyFormat(origCurrency, result.Ask),
printConvertCurrencyFormat(origCurrency, result.Bid),
printConvertCurrencyFormat(origCurrency, result.High),
printConvertCurrencyFormat(origCurrency, result.Low),
result.Volume)
} else {
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.Currency.FiatDisplayCurrency {
log.Printf("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
printCurrencyFormat(result.Last),
printCurrencyFormat(result.Ask),
printCurrencyFormat(result.Bid),
printCurrencyFormat(result.High),
printCurrencyFormat(result.Low),
result.Volume)
} else {
log.Printf("%s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
result.Last,
result.Ask,
result.Bid,
result.High,
result.Low,
result.Volume)
}
}
}
func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType, exchangeName string, err error) {
if err != nil {
log.Printf("Failed to get %s %s orderbook. Error: %s",
p.Pair().String(),
exchangeName,
err)
return
}
bidsAmount, bidsValue := result.CalculateTotalBids()
asksAmount, asksValue := result.CalculateTotalAsks()
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.Currency.FiatDisplayCurrency {
origCurrency := p.SecondCurrency.Upper().String()
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
printConvertCurrencyFormat(origCurrency, bidsValue),
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
printConvertCurrencyFormat(origCurrency, asksValue),
)
} else {
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.Currency.FiatDisplayCurrency {
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
printCurrencyFormat(bidsValue),
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
printCurrencyFormat(asksValue),
)
} else {
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
bidsValue,
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
asksValue,
)
}
}
}
func relayWebsocketEvent(result interface{}, event, assetType, exchangeName string) {
evt := WebsocketEvent{
Data: result,
Event: event,
AssetType: assetType,
Exchange: exchangeName,
}
err := BroadcastWebsocketMessage(evt)
if err != nil {
log.Println(fmt.Errorf("Failed to broadcast websocket event. Error: %s",
err))
}
}
// TickerUpdaterRoutine fetches and updates the ticker for all enabled
// currency pairs and exchanges
func TickerUpdaterRoutine() {
log.Println("Starting ticker updater routine.")
var wg sync.WaitGroup
for {
wg.Add(len(bot.exchanges))
for x := range bot.exchanges {
go func(x int, wg *sync.WaitGroup) {
defer wg.Done()
if bot.exchanges[x] == nil {
return
}
exchangeName := bot.exchanges[x].GetName()
enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies()
supportsBatching := bot.exchanges[x].SupportsRESTTickerBatchUpdates()
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
return
}
processTicker := func(exch exchange.IBotExchange, update bool, c pair.CurrencyPair, assetType string) {
var result ticker.Price
var err error
if update {
result, err = exch.UpdateTicker(c, assetType)
} else {
result, err = exch.GetTickerPrice(c, assetType)
}
printTickerSummary(result, c, assetType, exchangeName, err)
if err == nil {
bot.comms.StageTickerData(exchangeName, assetType, result)
if bot.config.Webserver.Enabled {
relayWebsocketEvent(result, "ticker_update", assetType, exchangeName)
}
}
}
for y := range assetTypes {
for z := range enabledCurrencies {
if supportsBatching && z > 0 {
processTicker(bot.exchanges[x], false, enabledCurrencies[z], assetTypes[y])
continue
}
processTicker(bot.exchanges[x], true, enabledCurrencies[z], assetTypes[y])
}
}
}(x, &wg)
}
wg.Wait()
log.Println("All enabled currency tickers fetched.")
time.Sleep(time.Second * 10)
}
}
// OrderbookUpdaterRoutine fetches and updates the orderbooks for all enabled
// currency pairs and exchanges
func OrderbookUpdaterRoutine() {
log.Println("Starting orderbook updater routine.")
var wg sync.WaitGroup
for {
wg.Add(len(bot.exchanges))
for x := range bot.exchanges {
go func(x int, wg *sync.WaitGroup) {
defer wg.Done()
if bot.exchanges[x] == nil {
return
}
exchangeName := bot.exchanges[x].GetName()
enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies()
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
return
}
processOrderbook := func(exch exchange.IBotExchange, c pair.CurrencyPair, assetType string) {
result, err := exch.UpdateOrderbook(c, assetType)
printOrderbookSummary(result, c, assetType, exchangeName, err)
if err == nil {
bot.comms.StageOrderbookData(exchangeName, assetType, result)
if bot.config.Webserver.Enabled {
relayWebsocketEvent(result, "orderbook_update", assetType, exchangeName)
}
}
}
for y := range assetTypes {
for z := range enabledCurrencies {
processOrderbook(bot.exchanges[x], enabledCurrencies[z], assetTypes[y])
}
}
}(x, &wg)
}
wg.Wait()
log.Println("All enabled currency orderbooks fetched.")
time.Sleep(time.Second * 10)
}
}