mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 15:10:19 +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
191 lines
4.9 KiB
Go
191 lines
4.9 KiB
Go
package currency
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
)
|
|
|
|
// NewPairDelimiter splits the desired currency string at delimeter, the returns
|
|
// a Pair struct
|
|
func NewPairDelimiter(currencyPair, delimiter string) Pair {
|
|
result := strings.Split(currencyPair, delimiter)
|
|
return Pair{
|
|
Delimiter: delimiter,
|
|
Base: NewCode(result[0]),
|
|
Quote: NewCode(result[1]),
|
|
}
|
|
}
|
|
|
|
// NewPairFromStrings returns a CurrencyPair without a delimiter
|
|
func NewPairFromStrings(baseCurrency, quoteCurrency string) Pair {
|
|
return Pair{
|
|
Base: NewCode(baseCurrency),
|
|
Quote: NewCode(quoteCurrency),
|
|
}
|
|
}
|
|
|
|
// NewPair returns a currency pair from currency codes
|
|
func NewPair(baseCurrency, quoteCurrency Code) Pair {
|
|
return Pair{
|
|
Base: baseCurrency,
|
|
Quote: quoteCurrency,
|
|
}
|
|
}
|
|
|
|
// NewPairWithDelimiter returns a CurrencyPair with a delimiter
|
|
func NewPairWithDelimiter(base, quote, delimiter string) Pair {
|
|
return Pair{
|
|
Base: NewCode(base),
|
|
Quote: NewCode(quote),
|
|
Delimiter: delimiter,
|
|
}
|
|
}
|
|
|
|
// NewPairFromIndex returns a CurrencyPair via a currency string and specific
|
|
// index
|
|
func NewPairFromIndex(currencyPair, index string) (Pair, error) {
|
|
i := strings.Index(currencyPair, index)
|
|
if i == -1 {
|
|
return Pair{},
|
|
fmt.Errorf("index %s not found in currency pair string", index)
|
|
}
|
|
if i == 0 {
|
|
return NewPairFromStrings(currencyPair[0:len(index)],
|
|
currencyPair[len(index):]),
|
|
nil
|
|
}
|
|
return NewPairFromStrings(currencyPair[0:i], currencyPair[i:]), nil
|
|
}
|
|
|
|
// NewPairFromString converts currency string into a new CurrencyPair
|
|
// with or without delimeter
|
|
func NewPairFromString(currencyPair string) Pair {
|
|
delimiters := []string{"_", "-", "/", ":"}
|
|
var delimiter string
|
|
for _, x := range delimiters {
|
|
if strings.Contains(currencyPair, x) {
|
|
delimiter = x
|
|
return NewPairDelimiter(currencyPair, delimiter)
|
|
}
|
|
}
|
|
return NewPairFromStrings(currencyPair[0:3], currencyPair[3:])
|
|
}
|
|
|
|
// Pair holds currency pair information
|
|
type Pair struct {
|
|
Delimiter string `json:"delimiter"`
|
|
Base Code `json:"base"`
|
|
Quote Code `json:"quote"`
|
|
}
|
|
|
|
// String returns a currency pair string
|
|
func (p Pair) String() string {
|
|
return p.Base.String() + p.Delimiter + p.Quote.String()
|
|
}
|
|
|
|
// Lower converts the pair object to lowercase
|
|
func (p Pair) Lower() Pair {
|
|
return Pair{
|
|
Delimiter: p.Delimiter,
|
|
Base: p.Base.Lower(),
|
|
Quote: p.Quote.Lower(),
|
|
}
|
|
}
|
|
|
|
// Upper converts the pair object to uppercase
|
|
func (p Pair) Upper() Pair {
|
|
return Pair{
|
|
Delimiter: p.Delimiter,
|
|
Base: p.Base.Upper(),
|
|
Quote: p.Quote.Upper(),
|
|
}
|
|
}
|
|
|
|
// UnmarshalJSON comforms type to the umarshaler interface
|
|
func (p *Pair) UnmarshalJSON(d []byte) error {
|
|
var pair string
|
|
err := common.JSONDecode(d, &pair)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*p = NewPairFromString(pair)
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON conforms type to the marshaler interface
|
|
func (p Pair) MarshalJSON() ([]byte, error) {
|
|
return common.JSONEncode(p.String())
|
|
}
|
|
|
|
// Format changes the currency based on user preferences overriding the default
|
|
// String() display
|
|
func (p Pair) Format(delimiter string, uppercase bool) Pair {
|
|
p.Delimiter = delimiter
|
|
|
|
if uppercase {
|
|
return p.Upper()
|
|
}
|
|
return p.Lower()
|
|
}
|
|
|
|
// Equal compares two currency pairs and returns whether or not they are equal
|
|
func (p Pair) Equal(cPair Pair) bool {
|
|
return p.Base.Item == cPair.Base.Item && p.Quote.Item == cPair.Quote.Item
|
|
}
|
|
|
|
// EqualIncludeReciprocal compares two currency pairs and returns whether or not
|
|
// they are the same including reciprocal currencies.
|
|
func (p Pair) EqualIncludeReciprocal(cPair Pair) bool {
|
|
if p.Base.Item == cPair.Base.Item &&
|
|
p.Quote.Item == cPair.Quote.Item ||
|
|
p.Base.Item == cPair.Quote.Item &&
|
|
p.Quote.Item == cPair.Base.Item {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// IsCryptoPair checks to see if the pair is a crypto pair e.g. BTCLTC
|
|
func (p Pair) IsCryptoPair() bool {
|
|
return storage.IsCryptocurrency(p.Base) &&
|
|
storage.IsCryptocurrency(p.Quote)
|
|
}
|
|
|
|
// IsCryptoFiatPair checks to see if the pair is a crypto fiat pair e.g. BTCUSD
|
|
func (p Pair) IsCryptoFiatPair() bool {
|
|
return storage.IsCryptocurrency(p.Base) &&
|
|
storage.IsFiatCurrency(p.Quote) ||
|
|
storage.IsFiatCurrency(p.Base) &&
|
|
storage.IsCryptocurrency(p.Quote)
|
|
}
|
|
|
|
// IsFiatPair checks to see if the pair is a fiat pair e.g. EURUSD
|
|
func (p Pair) IsFiatPair() bool {
|
|
return storage.IsFiatCurrency(p.Base) && storage.IsFiatCurrency(p.Quote)
|
|
}
|
|
|
|
// IsInvalid checks invalid pair if base and quote are the same
|
|
func (p Pair) IsInvalid() bool {
|
|
return p.Base.Item == p.Quote.Item
|
|
}
|
|
|
|
// Swap turns the currency pair into its reciprocal
|
|
func (p Pair) Swap() Pair {
|
|
p.Base, p.Quote = p.Quote, p.Base
|
|
return p
|
|
}
|
|
|
|
// IsEmpty returns whether or not the pair is empty or is missing a currency
|
|
// code
|
|
func (p Pair) IsEmpty() bool {
|
|
return p.Base.IsEmpty() || p.Quote.IsEmpty()
|
|
}
|
|
|
|
// ContainsCurrency checks to see if a pair contains a specific currency
|
|
func (p Pair) ContainsCurrency(c Code) bool {
|
|
return p.Base.Item == c.Item || p.Quote.Item == c.Item
|
|
}
|