mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 07:26:48 +00:00
* Adds extra properties to Websocket ticker. Adds new properties to binance and bitfinex, but doesn't test anything yet. Changes names and properties of ticker package to make more streamlined * Adds support for coinbasepro, coinut, gateio, gitbtc, huobi, hadax, kraken, okex, okcoin. Adds quoteVolume * Adds poloniex and ZB ticker datas * Updates ANX, Binance, Bitfinex, Bistamp, Bittrex, BTCMarkets, BTSE, CoinbasePRo, Coinut, Exmo tickers. It looks like a whole bunch of stuff is wrong in how tickers are done though :/ * Updates tickers everywhere. Will revert batch ones * Re-Preseves ticker batching * Minor fixes to ticks and removal of comment * Logging errors instead of returning mid loop. Adds bitfinex batch ticker processing. Fixes unrelated okgroup wallet bug * Removes bad code I wrote preventing function from running if feature not enabled * Fixes issue with bitmex and rebase issues * Fixes bitmex iterator error, splits hitbtc ticker requests * Fixes okgroup currency pair formatting. Updates okgroup to use ticker batching. Fixes okgroup ticker issues due to assetTypes. Fixes okgroup ws pinging. Fixes Kraken's currency pairs formatting. Reverts ANXs auto parsing back to strings because ANX json makes me cry. Minor property improvements for coinut, coinbasepro, btse, exmo. Protects wshandler manageSubscriptions() from running and returning an error when feature not supported * Updates config example to reflect the underscore dash situation * Fixes a config delimiter oopsie. Simplifies ANX wrapper ticker parsing. Fixes bittrex date parsing. Simplifies okcoin switch to if. * Fixes super fun issue where kraken has updated their currency pair format and must add new delimiter support. Fixes super fun issue where okex has updated their currency pair format and must add new delimiter support. Fixes super fun issue where okcoin has updated their currency pair format and must add new delimiter support. * Updates config example for kraken * Adds lbank batch ticker support. Adds details to errors * Updates FetchTradablePairs to use the config delimiter to prevent issues if the delimiter ever changes * Fixes nil reference bug. Uses NAme not GetNAme * Fixes hardcoded delimiter in Binance. Expands bitfinex websocket ticker fields. Updates Bitstamp rate limits. Expands BTSE ticker data fields. Fixes typo in coibasepro. Expands Coinut ticker data. Renames currency to curr as it conflicts with package name. Expands GateIO websocket ticker data. Fixes ticker data implementation for huobi and huobi hadax. Reverts ticker map to string instead of assetType. Fixes real stupid bug I introduced which preveted subscriptions from running :glitch_crab: Adds Price ATH to ws ticker type. Adds quotevolume to yobit ticker. Uses delimiter in ZB rather than hardcoded field. * Fixes bug in syncer where if the websocket is already connected, then UsingWebsocket is not set * Updates broken tests * Simplifies poloniex frozen check * Updates configtest.json with new delimiters * Renames shorthand properties of structs. Fixes delimiter referencing * Fixes some bugs and nits around variable declaration, currency pairs and config upscaling * Adds config upgrade path for okcoin and okex. Reverts configtest.json. * Fixes okex futures currency formatting by no longer using global currency format. updates Run code to adapt. Removes BTSE value * Adds ":" as a delimiter for when a delimiter only shows up SOMETIMES * Adds support for optional delimiter
194 lines
5.8 KiB
Go
194 lines
5.8 KiB
Go
package ticker
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
// const values for the ticker package
|
|
const (
|
|
errExchangeTickerNotFound = "ticker for exchange does not exist"
|
|
errPairNotSet = "ticker currency pair not set"
|
|
errAssetTypeNotSet = "ticker asset type not set"
|
|
errBaseCurrencyNotFound = "ticker base currency not found"
|
|
errQuoteCurrencyNotFound = "ticker quote currency not found"
|
|
)
|
|
|
|
// Vars for the ticker package
|
|
var (
|
|
Tickers []Ticker
|
|
m sync.Mutex
|
|
)
|
|
|
|
// Price struct stores the currency pair and pricing information
|
|
type Price struct {
|
|
Last float64 `json:"Last"`
|
|
High float64 `json:"High"`
|
|
Low float64 `json:"Low"`
|
|
Bid float64 `json:"Bid"`
|
|
Ask float64 `json:"Ask"`
|
|
Volume float64 `json:"Volume"`
|
|
QuoteVolume float64 `json:"QuoteVolume"`
|
|
PriceATH float64 `json:"PriceATH"`
|
|
Open float64 `json:"Open"`
|
|
Close float64 `json:"Close"`
|
|
Pair currency.Pair `json:"Pair"`
|
|
LastUpdated time.Time
|
|
}
|
|
|
|
// Ticker struct holds the ticker information for a currency pair and type
|
|
type Ticker struct {
|
|
Price map[string]map[string]map[string]Price
|
|
ExchangeName string
|
|
}
|
|
|
|
// PriceToString returns the string version of a stored price field
|
|
func (t *Ticker) PriceToString(p currency.Pair, priceType string, tickerType asset.Item) string {
|
|
priceType = strings.ToLower(priceType)
|
|
|
|
switch priceType {
|
|
case "last":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].Last, 'f', -1, 64)
|
|
case "high":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].High, 'f', -1, 64)
|
|
case "low":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].Low, 'f', -1, 64)
|
|
case "bid":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].Bid, 'f', -1, 64)
|
|
case "ask":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].Ask, 'f', -1, 64)
|
|
case "volume":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].Volume, 'f', -1, 64)
|
|
case "ath":
|
|
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()].PriceATH, 'f', -1, 64)
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// GetTicker checks and returns a requested ticker if it exists
|
|
func GetTicker(exchange string, p currency.Pair, tickerType asset.Item) (Price, error) {
|
|
ticker, err := GetTickerByExchange(exchange)
|
|
if err != nil {
|
|
return Price{}, err
|
|
}
|
|
|
|
if !BaseCurrencyExists(exchange, p.Base) {
|
|
return Price{}, errors.New(errBaseCurrencyNotFound)
|
|
}
|
|
|
|
if !QuoteCurrencyExists(exchange, p) {
|
|
return Price{}, errors.New(errQuoteCurrencyNotFound)
|
|
}
|
|
|
|
return ticker.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()], nil
|
|
}
|
|
|
|
// GetTickerByExchange returns an exchange Ticker
|
|
func GetTickerByExchange(exchange string) (*Ticker, error) {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for x := range Tickers {
|
|
if Tickers[x].ExchangeName == exchange {
|
|
return &Tickers[x], nil
|
|
}
|
|
}
|
|
return nil, errors.New(errExchangeTickerNotFound)
|
|
}
|
|
|
|
// BaseCurrencyExists checks to see if the base currency of the ticker map
|
|
// exists
|
|
func BaseCurrencyExists(exchange string, currency currency.Code) bool {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for _, y := range Tickers {
|
|
if y.ExchangeName == exchange {
|
|
if _, ok := y.Price[currency.Upper().String()]; ok {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// QuoteCurrencyExists checks to see if the quote currency of the ticker map
|
|
// exists
|
|
func QuoteCurrencyExists(exchange string, p currency.Pair) bool {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for _, y := range Tickers {
|
|
if y.ExchangeName == exchange {
|
|
if _, ok := y.Price[p.Base.Upper().String()]; ok {
|
|
if _, ok := y.Price[p.Base.Upper().String()][p.Quote.Upper().String()]; ok {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CreateNewTicker creates a new Ticker
|
|
func CreateNewTicker(exchangeName string, tickerNew *Price, tickerType asset.Item) Ticker {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
ticker := Ticker{}
|
|
ticker.ExchangeName = exchangeName
|
|
ticker.Price = make(map[string]map[string]map[string]Price)
|
|
a := make(map[string]map[string]Price)
|
|
b := make(map[string]Price)
|
|
b[tickerType.String()] = *tickerNew
|
|
a[tickerNew.Pair.Quote.Upper().String()] = b
|
|
ticker.Price[tickerNew.Pair.Base.Upper().String()] = a
|
|
Tickers = append(Tickers, ticker)
|
|
return ticker
|
|
}
|
|
|
|
// ProcessTicker processes incoming tickers, creating or updating the Tickers
|
|
// list
|
|
func ProcessTicker(exchangeName string, tickerNew *Price, assetType asset.Item) error {
|
|
if tickerNew.Pair.IsEmpty() {
|
|
return fmt.Errorf("%v %v", exchangeName, errPairNotSet)
|
|
}
|
|
|
|
if assetType == "" {
|
|
return fmt.Errorf("%v %v %v", exchangeName, tickerNew.Pair.String(), errAssetTypeNotSet)
|
|
}
|
|
|
|
if tickerNew.LastUpdated.IsZero() {
|
|
tickerNew.LastUpdated = time.Now()
|
|
}
|
|
|
|
ticker, err := GetTickerByExchange(exchangeName)
|
|
if err != nil {
|
|
CreateNewTicker(exchangeName, tickerNew, assetType)
|
|
return nil
|
|
}
|
|
|
|
if BaseCurrencyExists(exchangeName, tickerNew.Pair.Base) {
|
|
m.Lock()
|
|
a := make(map[string]Price)
|
|
a[assetType.String()] = *tickerNew
|
|
ticker.Price[tickerNew.Pair.Base.Upper().String()][tickerNew.Pair.Quote.Upper().String()] = a
|
|
m.Unlock()
|
|
return nil
|
|
}
|
|
|
|
m.Lock()
|
|
|
|
a := make(map[string]map[string]Price)
|
|
b := make(map[string]Price)
|
|
b[assetType.String()] = *tickerNew
|
|
a[tickerNew.Pair.Quote.Upper().String()] = b
|
|
ticker.Price[tickerNew.Pair.Base.Upper().String()] = a
|
|
m.Unlock()
|
|
return nil
|
|
}
|