mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
150 lines
4.2 KiB
Go
150 lines
4.2 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|