-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
60 lines (49 loc) · 1.46 KB
/
utils.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
package main
import (
"math/rand"
"time"
)
func copyMap(original map[string]float64) map[string]float64 {
copied := make(map[string]float64)
for key, value := range original {
copied[key] = value
}
return copied
}
func generateRandomFloat64InRange(min, max float64) float64 {
return min + rand.Float64()*(max-min)
}
const CryptoSymbol = "SOL"
const FakeCryptoRate = 55.0 // For example 1 SOL = 55 USD
const EventIntervalMs = 100 // Emit event from EventInterval / 2 to EventInterval ms
func generateOrderBookData() OrderBookEvent {
maxBidPrice := FakeCryptoRate + rand.Float64()
minAskPrice := maxBidPrice + rand.Float64()
// Simulate a potential opportunity
// coef should be equal 1 when deal with real data
coef := generateRandomFloat64InRange(1, 1.015)
return OrderBookEvent{
MaxBid: OrderBookData{
ExchangeID: exchanges[rand.Intn(len(exchanges))],
Price: maxBidPrice * coef,
},
MinAsk: OrderBookData{
ExchangeID: exchanges[rand.Intn(len(exchanges))],
Price: minAskPrice,
},
}
}
func watchOrderBook(channel chan OrderBookEvent) {
defer close(channel)
for {
orderBookData := generateOrderBookData()
halfInterval := EventIntervalMs / 2
select {
case channel <- orderBookData:
case <-time.After(EventIntervalMs * time.Millisecond):
// return
}
sleepTime := time.Duration(rand.Intn(halfInterval)+halfInterval) * time.Millisecond
time.Sleep(sleepTime)
}
}