mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 23:16:52 +00:00
* Initial currency overhaul before service system implementation * Remove redundant currency string in orderbook.Base Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times Add error handling for process orderbook * Remove redundant currency string in ticker.Price Unexport lastupdated field in ticker.Price Add error handling for process ticker function and fix tests * Phase Two Update * Update translations to use map type - thankyou to kempeng for spotting this * Change pair method name from Display -> Format for better readability * Fixes misspelling and tests * Implement requested changes from GloriousCode * Remove reduntant function and streamlined return in currency_translation.go * Revert pair method naming conventions * Change currency naming conventions * Changed code type to exported Item type with underlying string to reduce complexity * Added interim orderbook process method to orderbook.Base type * Changed feebuilder struct field to currency.Pair * Adds fall over system for backup fx providers * deprecate function and children and fix linter issue with btcmarkets * Fixed requested changes * Fix bug and move mtx for rates * Fixed after rebase oopsies * Fix linter issues * Fixes race conditions in testing functions * Final phase coinmarketcap update * fix linter issues * Implement requested changes * Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates * Add a collection of tests to improve codecov * After rebase oopsy fixes for btse * Fix requested changes * fix after rebase oopsies and add more efficient comparison checks within currency pair * Fix linter issues
194 lines
4.8 KiB
Go
194 lines
4.8 KiB
Go
package orderbook
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-/gocryptotrader/currency"
|
|
)
|
|
|
|
// Const values for orderbook package
|
|
const (
|
|
ErrOrderbookForExchangeNotFound = "ticker for exchange does not exist"
|
|
ErrPrimaryCurrencyNotFound = "primary currency for orderbook not found"
|
|
ErrSecondaryCurrencyNotFound = "secondary currency for orderbook not found"
|
|
|
|
Spot = "SPOT"
|
|
)
|
|
|
|
// Vars for the orderbook package
|
|
var (
|
|
Orderbooks []Orderbook
|
|
m sync.Mutex
|
|
)
|
|
|
|
// Item stores the amount and price values
|
|
type Item struct {
|
|
Amount float64
|
|
Price float64
|
|
ID int64
|
|
}
|
|
|
|
// Base holds the fields for the orderbook base
|
|
type Base struct {
|
|
Pair currency.Pair `json:"pair"`
|
|
Bids []Item `json:"bids"`
|
|
Asks []Item `json:"asks"`
|
|
LastUpdated time.Time `json:"lastUpdated"`
|
|
AssetType string `json:"assetType"`
|
|
ExchangeName string `json:"exchangeName"`
|
|
}
|
|
|
|
// Orderbook holds the orderbook information for a currency pair and type
|
|
type Orderbook struct {
|
|
Orderbook map[*currency.Item]map[*currency.Item]map[string]Base
|
|
ExchangeName string
|
|
}
|
|
|
|
// TotalBidsAmount returns the total amount of bids and the total orderbook
|
|
// bids value
|
|
func (o *Base) TotalBidsAmount() (amountCollated, total float64) {
|
|
for _, x := range o.Bids {
|
|
amountCollated += x.Amount
|
|
total += x.Amount * x.Price
|
|
}
|
|
return amountCollated, total
|
|
}
|
|
|
|
// TotalAsksAmount returns the total amount of asks and the total orderbook
|
|
// asks value
|
|
func (o *Base) TotalAsksAmount() (amountCollated, total float64) {
|
|
for _, x := range o.Asks {
|
|
amountCollated += x.Amount
|
|
total += x.Amount * x.Price
|
|
}
|
|
return amountCollated, total
|
|
}
|
|
|
|
// Update updates the bids and asks
|
|
func (o *Base) Update(bids, asks []Item) {
|
|
o.Bids = bids
|
|
o.Asks = asks
|
|
o.LastUpdated = time.Now()
|
|
}
|
|
|
|
// Get checks and returns the orderbook given an exchange name and currency pair
|
|
// if it exists
|
|
func Get(exchange string, p currency.Pair, orderbookType string) (Base, error) {
|
|
orderbook, err := GetByExchange(exchange)
|
|
if err != nil {
|
|
return Base{}, err
|
|
}
|
|
|
|
if !BaseCurrencyExists(exchange, p.Base) {
|
|
return Base{}, errors.New(ErrPrimaryCurrencyNotFound)
|
|
}
|
|
|
|
if !QuoteCurrencyExists(exchange, p) {
|
|
return Base{}, errors.New(ErrSecondaryCurrencyNotFound)
|
|
}
|
|
|
|
return orderbook.Orderbook[p.Base.Item][p.Quote.Item][orderbookType], nil
|
|
}
|
|
|
|
// GetByExchange returns an exchange orderbook
|
|
func GetByExchange(exchange string) (*Orderbook, error) {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for x := range Orderbooks {
|
|
if Orderbooks[x].ExchangeName == exchange {
|
|
return &Orderbooks[x], nil
|
|
}
|
|
}
|
|
return nil, errors.New(ErrOrderbookForExchangeNotFound)
|
|
}
|
|
|
|
// BaseCurrencyExists checks to see if the base currency of the orderbook map
|
|
// exists
|
|
func BaseCurrencyExists(exchange string, currency currency.Code) bool {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for _, y := range Orderbooks {
|
|
if y.ExchangeName == exchange {
|
|
if _, ok := y.Orderbook[currency.Item]; ok {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// QuoteCurrencyExists checks to see if the quote currency of the orderbook
|
|
// map exists
|
|
func QuoteCurrencyExists(exchange string, p currency.Pair) bool {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
for _, y := range Orderbooks {
|
|
if y.ExchangeName == exchange {
|
|
if _, ok := y.Orderbook[p.Base.Item]; ok {
|
|
if _, ok := y.Orderbook[p.Base.Item][p.Quote.Item]; ok {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CreateNewOrderbook creates a new orderbook
|
|
func CreateNewOrderbook(exchangeName string, orderbookNew Base, orderbookType string) *Orderbook {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
orderbook := Orderbook{}
|
|
orderbook.ExchangeName = exchangeName
|
|
orderbook.Orderbook = make(map[*currency.Item]map[*currency.Item]map[string]Base)
|
|
a := make(map[*currency.Item]map[string]Base)
|
|
b := make(map[string]Base)
|
|
b[orderbookType] = orderbookNew
|
|
a[orderbookNew.Pair.Quote.Item] = b
|
|
orderbook.Orderbook[orderbookNew.Pair.Base.Item] = a
|
|
Orderbooks = append(Orderbooks, orderbook)
|
|
return &orderbook
|
|
}
|
|
|
|
// Process processes incoming orderbooks, creating or updating the orderbook
|
|
// list
|
|
func (o Base) Process() error {
|
|
if o.Pair.IsEmpty() {
|
|
return errors.New("orderbook currency pair not populated")
|
|
}
|
|
|
|
if o.AssetType == "" {
|
|
return errors.New("orderbook asset type not set")
|
|
}
|
|
|
|
if o.LastUpdated.IsZero() {
|
|
o.LastUpdated = time.Now()
|
|
}
|
|
|
|
orderbook, err := GetByExchange(o.ExchangeName)
|
|
if err != nil {
|
|
CreateNewOrderbook(o.ExchangeName, o, o.AssetType)
|
|
return nil
|
|
}
|
|
|
|
if BaseCurrencyExists(o.ExchangeName, o.Pair.Base) {
|
|
m.Lock()
|
|
a := make(map[string]Base)
|
|
a[o.AssetType] = o
|
|
orderbook.Orderbook[o.Pair.Base.Item][o.Pair.Quote.Item] = a
|
|
m.Unlock()
|
|
return nil
|
|
}
|
|
|
|
m.Lock()
|
|
a := make(map[*currency.Item]map[string]Base)
|
|
b := make(map[string]Base)
|
|
b[o.AssetType] = o
|
|
a[o.Pair.Quote.Item] = b
|
|
orderbook.Orderbook[o.Pair.Base.Item] = a
|
|
m.Unlock()
|
|
return nil
|
|
}
|