Repository navigation
Expand file tree
/
Copy pathtickers.go
More file actions
60 lines (56 loc) · 1.64 KB
/
Copy pathtickers.go
File metadata and controls
60 lines (56 loc) · 1.64 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
package banexg
// BuildSymbolSet converts symbols to a lookup set. Returns nil for empty input.
func BuildSymbolSet(symbols []string) map[string]struct{} {
if len(symbols) == 0 {
return nil
}
result := make(map[string]struct{}, len(symbols))
for _, sym := range symbols {
result[sym] = struct{}{}
}
return result
}
// FilterTickers filters tickers by symbolSet when provided.
func FilterTickers(tickers []*Ticker, symbolSet map[string]struct{}) []*Ticker {
if len(tickers) == 0 {
return nil
}
result := make([]*Ticker, 0, len(tickers))
for _, ticker := range tickers {
if ticker == nil || ticker.Symbol == "" {
continue
}
if symbolSet != nil {
if _, ok := symbolSet[ticker.Symbol]; !ok {
continue
}
}
result = append(result, ticker)
}
return result
}
// TickersToLastPrices converts tickers to last price list; filters by symbolSet when provided.
func TickersToLastPrices(tickers []*Ticker, symbolSet map[string]struct{}) []*LastPrice {
if len(tickers) == 0 {
return nil
}
filtered := FilterTickers(tickers, symbolSet)
result := make([]*LastPrice, 0, len(filtered))
for _, ticker := range filtered {
result = append(result, &LastPrice{
Symbol: ticker.Symbol,
Timestamp: ticker.TimeStamp,
Price: ticker.Last,
Info: ticker.Info,
})
}
return result
}
// TickersToPriceMap converts tickers to symbol->last price map; filters by symbolSet when provided.
func TickersToPriceMap(tickers []*Ticker, symbolSet map[string]struct{}) map[string]float64 {
result := make(map[string]float64)
for _, ticker := range FilterTickers(tickers, symbolSet) {
result[ticker.Symbol] = ticker.Last
}
return result
}