forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routines.go
269 lines (246 loc) · 7.97 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
package main
import (
"fmt"
"log"
"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.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.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 printSummary(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.FiatDisplayCurrency {
origCurrency := p.SecondCurrency.Upper().String()
log.Printf("%s %s %s: 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.FiatDisplayCurrency {
log.Printf("%s %s %s: 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: 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.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.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")
for {
for x := range bot.exchanges {
if bot.exchanges[x] == nil {
continue
}
exchangeName := bot.exchanges[x].GetName()
enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies()
var result ticker.Price
var err error
var assetTypes []string
assetTypes, err = exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
}
for y := range enabledCurrencies {
currency := enabledCurrencies[y]
if len(assetTypes) > 1 {
for z := range assetTypes {
result, err = bot.exchanges[x].UpdateTicker(currency,
assetTypes[z])
printSummary(result, currency, assetTypes[z], exchangeName, err)
if err == nil {
relayWebsocketEvent(result, "ticker_update", assetTypes[z], exchangeName)
}
}
} else {
result, err = bot.exchanges[x].UpdateTicker(currency,
assetTypes[0])
printSummary(result, currency, assetTypes[0], exchangeName, err)
if err == nil {
relayWebsocketEvent(result, "ticker_update", assetTypes[0], exchangeName)
}
}
}
}
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")
for {
for x := range bot.exchanges {
if bot.exchanges[x] == nil {
continue
}
exchangeName := bot.exchanges[x].GetName()
enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies()
var result orderbook.Base
var err error
var assetTypes []string
assetTypes, err = exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
}
for y := range enabledCurrencies {
currency := enabledCurrencies[y]
if len(assetTypes) > 1 {
for z := range assetTypes {
result, err = bot.exchanges[x].UpdateOrderbook(currency,
assetTypes[z])
printOrderbookSummary(result, currency, assetTypes[z], exchangeName, err)
if err == nil {
relayWebsocketEvent(result, "orderbook_update", assetTypes[z], exchangeName)
}
}
} else {
result, err = bot.exchanges[x].UpdateOrderbook(currency,
assetTypes[0])
printOrderbookSummary(result, currency, assetTypes[0], exchangeName, err)
if err == nil {
relayWebsocketEvent(result, "orderbook_update", assetTypes[0], exchangeName)
}
}
}
}
time.Sleep(time.Second * 10)
}
}