-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.go
More file actions
188 lines (162 loc) · 3.77 KB
/
graph.go
File metadata and controls
188 lines (162 loc) · 3.77 KB
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
package main
import (
"context"
"fmt"
"github.com/0fs/c-bot/utils"
"github.com/adshao/go-binance/v2"
"log"
"sync"
)
type CurrencyGraph struct {
nodes map[string]*Currency
edges map[Currency][]*Edge
lock sync.RWMutex
}
type Edge struct {
symbol string
currency *Currency
fee float64
}
type Currency struct {
value string
books *binance.WsBookTickerEvent
}
type Fee struct {
m float64 // Maker
t float64 // Taker
}
var color map[*Currency]struct{}
var cycles map[int][]*Currency
var p map[*Currency]*Currency
var ncycle int
// AddNode adds a node to the graph
func (g *CurrencyGraph) AddNode(n *Currency) {
g.lock.Lock()
if g.nodes == nil {
g.nodes = make(map[string]*Currency)
}
if _, ok := g.nodes[n.value]; !ok {
g.nodes[n.value] = n
}
g.lock.Unlock()
}
// AddEdge adds an edge to the graph
func (g *CurrencyGraph) AddEdge(symbol string, n1, n2 *Currency) {
g.lock.Lock()
if g.edges == nil {
g.edges = make(map[Currency][]*Edge)
}
g.edges[*n1] = append(g.edges[*n1], &Edge{
symbol: symbol,
currency: n2,
fee: fees[symbol].m,
})
g.edges[*n2] = append(g.edges[*n2], &Edge{
symbol: symbol,
currency: n1,
fee: fees[symbol].t,
})
g.lock.Unlock()
}
func (n *Currency) String() string {
return n.value
}
func (g *CurrencyGraph) String() {
g.lock.RLock()
s := ""
for _, node := range g.nodes {
s += node.String()
near := g.edges[*node]
for j, item := range near {
if j != 0 {
for i := 0; i < len(node.String()); i++ {
s += " "
}
}
s += fmt.Sprintf(" -> %s - Symbol: %s, Fee: %.1f%%", item.currency.String(), item.symbol, item.fee*100)
fmt.Println(s)
s = ""
}
}
g.lock.RUnlock()
}
func initFeesMap() {
log.Println("Initializing fees map...")
rs, err := spotClient.NewTradeFeeService().Do(context.Background())
if err != nil {
log.Fatal(err)
}
for _, item := range rs {
fees[item.Symbol] = Fee{
m: utils.Stf(item.MakerCommission),
t: utils.Stf(item.TakerCommission),
}
}
log.Println("Done.")
log.Println()
}
func initCurrencyGraph() {
log.Println("Initializing currency graph...")
rs, err := spotClient.NewExchangeInfoService().Do(context.Background())
if err != nil {
log.Fatal(err)
}
existNodes := make(map[string]*Currency)
for _, symbol := range rs.Symbols {
if _, ok := existNodes[symbol.BaseAsset]; !ok {
existNodes[symbol.BaseAsset] = &Currency{value: symbol.BaseAsset}
currencyGraph.AddNode(existNodes[symbol.BaseAsset])
}
if _, ok := existNodes[symbol.QuoteAsset]; !ok {
existNodes[symbol.QuoteAsset] = &Currency{value: symbol.QuoteAsset}
currencyGraph.AddNode(existNodes[symbol.QuoteAsset])
}
if binance.SymbolStatusType(symbol.Status) == binance.SymbolStatusTypeTrading {
currencyGraph.AddEdge(symbol.Symbol, existNodes[symbol.BaseAsset], existNodes[symbol.QuoteAsset])
if _, ok := symbols[symbol.Symbol]; !ok {
symbols[symbol.Symbol] = &SymbolInfo{
symbol: symbol,
book: nil,
}
}
}
}
log.Println("Done.")
log.Println()
}
func findCycles(currency *Currency) {
ncycle = 0
color = make(map[*Currency]struct{})
cycles = make(map[int][]*Currency)
p = make(map[*Currency]*Currency)
dfs(currency, 0)
delete(cycles, ncycle) // TODO: check
}
func saveCycle(f *Currency, s *Currency) int {
delete(cycles, ncycle)
cycles[ncycle] = append(cycles[ncycle], s)
for v := f; v != s; v = p[v] {
cycles[ncycle] = append(cycles[ncycle], v)
}
cycles[ncycle] = append(cycles[ncycle], s)
return len(cycles[ncycle])
}
func dfs(c *Currency, depth int) {
depth++
if depth > cycleDepth {
return
}
color[c] = struct{}{}
for _, e := range currencyGraph.edges[*c] {
to := e.currency
if _, ok := color[to]; !ok {
p[to] = c
dfs(to, depth)
} else {
if saveCycle(c, to) > cycleDepth {
ncycle++
}
}
}
delete(color, c)
}