forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticker.go
149 lines (132 loc) · 4.16 KB
/
ticker.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
package main
import (
"errors"
"log"
"strconv"
)
var (
ErrTickerForExchangeNotFound = "Ticker for exchange does not exist."
ErrPrimaryCurrencyNotFound = "Error primary currency for ticker not found."
ErrSecondaryCurrencyNotFound = "Error secondary currency for ticker not found."
)
type TickerPrice struct {
FirstCurrency string `json:"FirstCurrency"`
SecondCurrency string `json:"SecondCurrency"`
CurrencyPair string `json:"CurrencyPair"`
Last float64 `json:"Last"`
High float64 `json:"High"`
Low float64 `json:"Low"`
Bid float64 `json:"Bid"`
Ask float64 `json:"Ask"`
Volume float64 `json:"Volume"`
PriceATH float64 `json:"PriceATH"`
}
type Ticker struct {
Price map[string]map[string]TickerPrice
ExchangeName string
}
func (t *Ticker) PriceToString(firstCurrency, secondCurrency, priceType string) string {
switch priceType {
case "last":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Last, 'f', -1, 64)
case "high":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].High, 'f', -1, 64)
case "low":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Low, 'f', -1, 64)
case "bid":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Bid, 'f', -1, 64)
case "ask":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Ask, 'f', -1, 64)
case "volume":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Volume, 'f', -1, 64)
case "ath":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].PriceATH, 'f', -1, 64)
default:
return ""
}
}
func GetTicker(exchange, firstCurrency, secondCurrency string) (TickerPrice, error) {
ticker, err := GetTickerByExchange(exchange)
if err != nil {
return TickerPrice{}, err
}
if !FirstCurrencyExists(exchange, firstCurrency) {
return TickerPrice{}, errors.New(ErrPrimaryCurrencyNotFound)
}
if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) {
return TickerPrice{}, errors.New(ErrSecondaryCurrencyNotFound)
}
return ticker.Price[firstCurrency][secondCurrency], nil
}
func GetTickerByExchange(exchange string) (*Ticker, error) {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
return &y, nil
}
}
return nil, errors.New(ErrTickerForExchangeNotFound)
}
func FirstCurrencyExists(exchange, currency string) bool {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[currency]; ok {
return true
}
}
}
return false
}
func SecondCurrencyExists(exchange, primary, secondary string) bool {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[primary]; ok {
if _, ok := y.Price[primary][secondary]; ok {
return true
}
}
}
}
return false
}
func CreateNewTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) {
ticker := Ticker{}
ticker.ExchangeName = exchangeName
ticker.Price = make(map[string]map[string]TickerPrice)
sMap := make(map[string]TickerPrice)
sMap[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = sMap
bot.tickers = append(bot.tickers, ticker)
}
func ProcessTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) {
tickerNew.CurrencyPair = tickerNew.FirstCurrency + tickerNew.SecondCurrency
if len(bot.tickers) == 0 {
CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew)
return
} else {
ticker, err := GetTickerByExchange(exchangeName)
if err != nil {
CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew)
return
}
if FirstCurrencyExists(exchangeName, firstCurrency) {
if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) {
second := ticker.Price[firstCurrency]
second[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = second
return
}
}
sMap := make(map[string]TickerPrice)
sMap[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = sMap
}
}
func HandleTickerEvents() {
bot.tickerChan = make(chan Ticker)
for {
select {
case ticker := <-bot.tickerChan:
log.Printf("Ticker update recv %v..\n", ticker)
}
}
}