mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 23:16:52 +00:00
Engine: Dispatch service (#346)
* Added dispatch service * Added orderbook streaming capabilities * Assigned correct orderbook.base exchange name * Fixed Requested niterinos Add in cli orderbook QA tool to gctcli Add exchange orderbook streaming * Add ticker streaming support through dispatch package * Added in some more info on error returns for orderbook.go * fix linter issues * Fix some issues * Update * Fix requested * move dispatch out of exchanges folder to its own independant folder * Fix requested * change orderbook string to tickers * Limit orderbooks to 50 and made dispatch system more stateless in operation * lower cases for update/retrieve/sub exchange name * Adds in asset validation and lower case conversion on cli * Remove comment * Moved timer to a higher scope so its not constantly initialised just reset per instance and removed returning unused channel on error * Rm unused release function in dispatch.go Reset timer and bleed buffered timer chan if needed in dispatch.go Added in ticker.Stop() and timer.Stop() functions for worker routine return in dispatch.go Index aggregated bid and ask functions for orderbook.go Added in dummy slice for wsorderbook_test.go * Moved drain to above Reset so potential race would not occur in dispatch.go Fix various linter issues dispatch.go * Fix some issues * change to start/stop service, added in service state change via cli, updated logger * fix requested * Add worker amount init spawning * fix linter issues * Fix more linter issues * More fixes * Fix race issue on releasing pipe channel on a close after shutting down dispatcher system * Moved all types to dispatch_types.go && remove panic * Moved types into serperate file && improve test coverage * RM unnecessary select case for draining channel && fixed error string * Added orderbook_types file and improved code coverage * gofmt file * reinstated select cases on drain because I am silly * Remove error for drop worker * Added more test cases * not checking error issue fix * remove func causing race in test, this has required protection via an exported function * set Gemini websocket orderbook exchange name
This commit is contained in:
committed by
Adrian Gallagher
parent
4a0fcc7f0f
commit
db317a2447
@@ -387,7 +387,7 @@ func TestUpdateOrderbook(t *testing.T) {
|
||||
Quote: currency.USD}
|
||||
|
||||
_, err := a.UpdateOrderbook(q, "spot")
|
||||
if err != nil {
|
||||
t.Fatalf("Update for orderbook failed: %v", err)
|
||||
if err == nil {
|
||||
t.Fatalf("error cannot be nil as the endpoint returns no orderbook information")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,6 +247,7 @@ func (b *Binance) SeedLocalCache(p currency.Pair) error {
|
||||
newOrderBook.LastUpdated = time.Unix(orderbookNew.LastUpdateID, 0)
|
||||
newOrderBook.Pair = p
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.ExchangeName = b.GetName()
|
||||
|
||||
return b.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
}
|
||||
|
||||
@@ -482,6 +482,8 @@ func (b *Bitfinex) WsInsertSnapshot(p currency.Pair, assetType asset.Item, books
|
||||
newOrderBook.AssetType = assetType
|
||||
newOrderBook.Bids = bid
|
||||
newOrderBook.Pair = p
|
||||
newOrderBook.ExchangeName = b.GetName()
|
||||
|
||||
err := b.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bitfinex.go error - %s", err)
|
||||
|
||||
@@ -361,6 +361,8 @@ func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, currencyPai
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = assetType
|
||||
newOrderBook.Pair = currencyPair
|
||||
newOrderBook.ExchangeName = b.GetName()
|
||||
|
||||
err := b.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bitmex_websocket.go process orderbook error - %s",
|
||||
|
||||
@@ -247,6 +247,7 @@ func (b *Bitstamp) seedOrderBook() error {
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.Pair = p[x]
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.ExchangeName = b.GetName()
|
||||
|
||||
err = b.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
|
||||
@@ -216,6 +216,7 @@ func (c *CoinbasePro) ProcessSnapshot(snapshot *WebsocketOrderbookSnapshot) erro
|
||||
pair := currency.NewPairFromString(snapshot.ProductID)
|
||||
base.AssetType = asset.Spot
|
||||
base.Pair = pair
|
||||
base.ExchangeName = c.GetName()
|
||||
|
||||
err := c.Websocket.Orderbook.LoadSnapshot(&base)
|
||||
if err != nil {
|
||||
|
||||
@@ -288,6 +288,7 @@ func (c *COINUT) WsProcessOrderbookSnapshot(ob *WsOrderbookSnapshot) error {
|
||||
c.GetPairFormat(asset.Spot, true),
|
||||
)
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.ExchangeName = c.GetName()
|
||||
|
||||
return c.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
}
|
||||
|
||||
@@ -237,6 +237,7 @@ func (g *Gateio) WsHandleData() {
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.Pair = currency.NewPairFromString(c)
|
||||
newOrderBook.ExchangeName = g.GetName()
|
||||
|
||||
err = g.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
|
||||
@@ -282,6 +282,7 @@ func (g *Gemini) wsProcessUpdate(result WsMarketUpdateResponse, pair currency.Pa
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.Pair = pair
|
||||
newOrderBook.ExchangeName = g.GetName()
|
||||
err := g.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
g.Websocket.DataHandler <- err
|
||||
|
||||
@@ -250,6 +250,7 @@ func (h *HitBTC) WsProcessOrderbookSnapshot(ob WsOrderbook) error {
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.Pair = p
|
||||
newOrderBook.ExchangeName = h.GetName()
|
||||
|
||||
err := h.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,235 +2,259 @@ package orderbook
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// const values for orderbook package
|
||||
const (
|
||||
errExchangeOrderbookNotFound = "orderbook for exchange does not exist"
|
||||
errPairNotSet = "orderbook currency pair not set"
|
||||
errAssetTypeNotSet = "orderbook asset type not set"
|
||||
errBaseCurrencyNotFound = "orderbook base currency not found"
|
||||
errQuoteCurrencyNotFound = "orderbook quote currency not found"
|
||||
)
|
||||
|
||||
// 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
|
||||
// Get checks and returns the orderbook given an exchange name and currency pair
|
||||
// if it exists
|
||||
func Get(exchange string, p currency.Pair, a asset.Item) (Base, error) {
|
||||
o, err := service.Retrieve(exchange, p, a)
|
||||
if err != nil {
|
||||
return Base{}, err
|
||||
}
|
||||
return *o, nil
|
||||
}
|
||||
|
||||
// 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 asset.Item `json:"assetType"`
|
||||
ExchangeName string `json:"exchangeName"`
|
||||
// SubscribeOrderbook subcribes to an orderbook and returns a communication
|
||||
// channel to stream orderbook data updates
|
||||
func SubscribeOrderbook(exchange string, p currency.Pair, a asset.Item) (dispatch.Pipe, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
book, ok := service.Books[exchange][p.Base.Item][p.Quote.Item][a]
|
||||
if !ok {
|
||||
return dispatch.Pipe{}, fmt.Errorf("orderbook item not found for %s %s %s",
|
||||
exchange,
|
||||
p,
|
||||
a)
|
||||
}
|
||||
|
||||
return service.mux.Subscribe(book.Main)
|
||||
}
|
||||
|
||||
// Orderbook holds the orderbook information for a currency pair and type
|
||||
type Orderbook struct {
|
||||
Orderbook map[*currency.Item]map[*currency.Item]map[asset.Item]Base
|
||||
ExchangeName string
|
||||
// SubscribeToExchangeOrderbooks subcribes to all orderbooks on an exchange
|
||||
func SubscribeToExchangeOrderbooks(exchange string) (dispatch.Pipe, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
id, ok := service.Exchange[exchange]
|
||||
if !ok {
|
||||
return dispatch.Pipe{}, fmt.Errorf("%s exchange orderbooks not found",
|
||||
exchange)
|
||||
}
|
||||
|
||||
return service.mux.Subscribe(id)
|
||||
}
|
||||
|
||||
type byOBPrice []Item
|
||||
// Update stores orderbook data
|
||||
func (s *Service) Update(b *Base) error {
|
||||
var ids []uuid.UUID
|
||||
|
||||
func (a byOBPrice) Len() int { return len(a) }
|
||||
func (a byOBPrice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byOBPrice) Less(i, j int) bool { return a[i].Price < a[j].Price }
|
||||
|
||||
// Verify ensures that the orderbook items are correctly sorted
|
||||
// Bids should always go from a high price to a low price and
|
||||
// asks should always go from a low price to a higher price
|
||||
func (o *Base) Verify() {
|
||||
var lastPrice float64
|
||||
var sortBids, sortAsks bool
|
||||
for x := range o.Bids {
|
||||
if lastPrice != 0 && o.Bids[x].Price >= lastPrice {
|
||||
sortBids = true
|
||||
break
|
||||
s.Lock()
|
||||
switch {
|
||||
case s.Books[b.ExchangeName] == nil:
|
||||
s.Books[b.ExchangeName] = make(map[*currency.Item]map[*currency.Item]map[asset.Item]*Book)
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Book)
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item] = make(map[asset.Item]*Book)
|
||||
err := s.SetNewData(b)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
lastPrice = o.Bids[x].Price
|
||||
}
|
||||
|
||||
lastPrice = 0
|
||||
for x := range o.Asks {
|
||||
if lastPrice != 0 && o.Asks[x].Price <= lastPrice {
|
||||
sortAsks = true
|
||||
break
|
||||
case s.Books[b.ExchangeName][b.Pair.Base.Item] == nil:
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Book)
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item] = make(map[asset.Item]*Book)
|
||||
err := s.SetNewData(b)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
lastPrice = o.Asks[x].Price
|
||||
|
||||
case s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item] == nil:
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item] = make(map[asset.Item]*Book)
|
||||
err := s.SetNewData(b)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
case s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType] == nil:
|
||||
err := s.SetNewData(b)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType].b.Bids = b.Bids
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType].b.Asks = b.Asks
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType].b.LastUpdated = b.LastUpdated
|
||||
ids = s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType].Assoc
|
||||
ids = append(ids, s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType].Main)
|
||||
}
|
||||
s.Unlock()
|
||||
return s.mux.Publish(ids, b)
|
||||
}
|
||||
|
||||
// SetNewData sets new data
|
||||
func (s *Service) SetNewData(b *Base) error {
|
||||
ids, err := s.GetAssociations(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
singleID, err := s.mux.GetID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sortBids {
|
||||
sort.Sort(sort.Reverse(byOBPrice(o.Bids)))
|
||||
s.Books[b.ExchangeName][b.Pair.Base.Item][b.Pair.Quote.Item][b.AssetType] = &Book{b: b,
|
||||
Main: singleID,
|
||||
Assoc: ids}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAssociations links a singular book with it's dispatch associations
|
||||
func (s *Service) GetAssociations(b *Base) ([]uuid.UUID, error) {
|
||||
if b == nil {
|
||||
return nil, errors.New("orderbook is nil")
|
||||
}
|
||||
|
||||
if sortAsks {
|
||||
sort.Sort((byOBPrice(o.Asks)))
|
||||
var ids []uuid.UUID
|
||||
exchangeID, ok := s.Exchange[b.ExchangeName]
|
||||
if !ok {
|
||||
var err error
|
||||
exchangeID, err = s.mux.GetID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.Exchange[b.ExchangeName] = exchangeID
|
||||
}
|
||||
|
||||
ids = append(ids, exchangeID)
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// Retrieve gets orderbook data from the slice
|
||||
func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Base, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
if s.Books[exchange] == nil {
|
||||
return nil, fmt.Errorf("no orderbooks for %s exchange", exchange)
|
||||
}
|
||||
|
||||
if s.Books[exchange][p.Base.Item] == nil {
|
||||
return nil, fmt.Errorf("no orderbooks associated with base currency %s",
|
||||
p.Base)
|
||||
}
|
||||
|
||||
if s.Books[exchange][p.Base.Item][p.Quote.Item] == nil {
|
||||
return nil, fmt.Errorf("no orderbooks associated with quote currency %s",
|
||||
p.Quote)
|
||||
}
|
||||
|
||||
if s.Books[exchange][p.Base.Item][p.Quote.Item][a] == nil {
|
||||
return nil, fmt.Errorf("no orderbooks associated with asset type %s",
|
||||
a)
|
||||
}
|
||||
|
||||
return s.Books[exchange][p.Base.Item][p.Quote.Item][a].b, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
func (b *Base) TotalBidsAmount() (amountCollated, total float64) {
|
||||
for x := range b.Bids {
|
||||
amountCollated += b.Bids[x].Amount
|
||||
total += b.Bids[x].Amount * b.Bids[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
|
||||
func (b *Base) TotalAsksAmount() (amountCollated, total float64) {
|
||||
for y := range b.Asks {
|
||||
amountCollated += b.Asks[y].Amount
|
||||
total += b.Asks[y].Amount * b.Asks[y].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()
|
||||
func (b *Base) Update(bids, asks []Item) {
|
||||
b.Bids = bids
|
||||
b.Asks = asks
|
||||
b.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 asset.Item) (Base, error) {
|
||||
orderbook, err := GetByExchange(exchange)
|
||||
if err != nil {
|
||||
return Base{}, err
|
||||
}
|
||||
|
||||
if !BaseCurrencyExists(exchange, p.Base) {
|
||||
return Base{}, errors.New(errBaseCurrencyNotFound)
|
||||
}
|
||||
|
||||
if !QuoteCurrencyExists(exchange, p) {
|
||||
return Base{}, errors.New(errQuoteCurrencyNotFound)
|
||||
}
|
||||
|
||||
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
|
||||
// Verify ensures that the orderbook items are correctly sorted
|
||||
// Bids should always go from a high price to a low price and
|
||||
// asks should always go from a low price to a higher price
|
||||
func (b *Base) Verify() {
|
||||
var lastPrice float64
|
||||
var sortBids, sortAsks bool
|
||||
for x := range b.Bids {
|
||||
if lastPrice != 0 && b.Bids[x].Price >= lastPrice {
|
||||
sortBids = true
|
||||
break
|
||||
}
|
||||
lastPrice = b.Bids[x].Price
|
||||
}
|
||||
return nil, errors.New(errExchangeOrderbookNotFound)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
lastPrice = 0
|
||||
for x := range b.Asks {
|
||||
if lastPrice != 0 && b.Asks[x].Price <= lastPrice {
|
||||
sortAsks = true
|
||||
break
|
||||
}
|
||||
lastPrice = b.Asks[x].Price
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
if sortBids {
|
||||
sort.Sort(sort.Reverse(byOBPrice(b.Bids)))
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CreateNewOrderbook creates a new orderbook
|
||||
func CreateNewOrderbook(exchangeName string, orderbookNew *Base, orderbookType asset.Item) *Orderbook {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
orderbook := Orderbook{}
|
||||
orderbook.ExchangeName = exchangeName
|
||||
orderbook.Orderbook = make(map[*currency.Item]map[*currency.Item]map[asset.Item]Base)
|
||||
a := make(map[*currency.Item]map[asset.Item]Base)
|
||||
b := make(map[asset.Item]Base)
|
||||
b[orderbookType] = *orderbookNew
|
||||
a[orderbookNew.Pair.Quote.Item] = b
|
||||
orderbook.Orderbook[orderbookNew.Pair.Base.Item] = a
|
||||
Orderbooks = append(Orderbooks, orderbook)
|
||||
return &orderbook
|
||||
if sortAsks {
|
||||
sort.Sort((byOBPrice(b.Asks)))
|
||||
}
|
||||
}
|
||||
|
||||
// Process processes incoming orderbooks, creating or updating the orderbook
|
||||
// list
|
||||
func (o *Base) Process() error {
|
||||
if o.Pair.IsEmpty() {
|
||||
func (b *Base) Process() error {
|
||||
if b.ExchangeName == "" {
|
||||
return errors.New(errExchangeNameUnset)
|
||||
}
|
||||
|
||||
b.ExchangeName = strings.ToLower(b.ExchangeName)
|
||||
|
||||
if b.Pair.IsEmpty() {
|
||||
return errors.New(errPairNotSet)
|
||||
}
|
||||
|
||||
if o.AssetType == "" {
|
||||
if b.AssetType.String() == "" {
|
||||
return errors.New(errAssetTypeNotSet)
|
||||
}
|
||||
|
||||
if o.LastUpdated.IsZero() {
|
||||
o.LastUpdated = time.Now()
|
||||
if len(b.Asks) == 0 && len(b.Bids) == 0 {
|
||||
return errors.New(errNoOrderbook)
|
||||
}
|
||||
|
||||
o.Verify()
|
||||
|
||||
orderbook, err := GetByExchange(o.ExchangeName)
|
||||
if err != nil {
|
||||
CreateNewOrderbook(o.ExchangeName, o, o.AssetType)
|
||||
return nil
|
||||
if b.LastUpdated.IsZero() {
|
||||
b.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
if BaseCurrencyExists(o.ExchangeName, o.Pair.Base) {
|
||||
m.Lock()
|
||||
a := make(map[asset.Item]Base)
|
||||
a[o.AssetType] = *o
|
||||
orderbook.Orderbook[o.Pair.Base.Item][o.Pair.Quote.Item] = a
|
||||
m.Unlock()
|
||||
return nil
|
||||
}
|
||||
b.Verify()
|
||||
|
||||
m.Lock()
|
||||
a := make(map[*currency.Item]map[asset.Item]Base)
|
||||
b := make(map[asset.Item]Base)
|
||||
b[o.AssetType] = *o
|
||||
a[o.Pair.Quote.Item] = b
|
||||
orderbook.Orderbook[o.Pair.Base.Item] = a
|
||||
m.Unlock()
|
||||
return nil
|
||||
return service.Update(b)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,139 @@
|
||||
package orderbook
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
err := dispatch.Start(1)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cpyMux = service.mux
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
var cpyMux *dispatch.Mux
|
||||
|
||||
func TestSubscribeOrderbook(t *testing.T) {
|
||||
_, err := SubscribeOrderbook("", currency.Pair{}, asset.Item(""))
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
|
||||
b := Base{
|
||||
Pair: p,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
err = b.Process()
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
b.ExchangeName = "SubscribeOBTest"
|
||||
|
||||
err = b.Process()
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
b.Bids = []Item{{}}
|
||||
|
||||
err = b.Process()
|
||||
if err != nil {
|
||||
t.Error("test failed - process error", err)
|
||||
}
|
||||
|
||||
_, err = SubscribeOrderbook("SubscribeOBTest", p, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
// process redundant update
|
||||
err = b.Process()
|
||||
if err != nil {
|
||||
t.Error("test failed - process error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBooks(t *testing.T) {
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
|
||||
b := Base{
|
||||
Pair: p,
|
||||
AssetType: asset.Spot,
|
||||
ExchangeName: "UpdateTest",
|
||||
}
|
||||
|
||||
service.mux = nil
|
||||
|
||||
err := service.Update(&b)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
b.Pair.Base = currency.CYC
|
||||
err = service.Update(&b)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
b.Pair.Quote = currency.ENAU
|
||||
err = service.Update(&b)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
b.AssetType = "unicorns"
|
||||
err = service.Update(&b)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
service.mux = cpyMux
|
||||
}
|
||||
|
||||
func TestSubscribeToExchangeOrderbooks(t *testing.T) {
|
||||
_, err := SubscribeToExchangeOrderbooks("")
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
|
||||
b := Base{
|
||||
Pair: p,
|
||||
AssetType: asset.Spot,
|
||||
ExchangeName: "SubscribeToExchangeOrderbooks",
|
||||
Bids: []Item{{}},
|
||||
}
|
||||
|
||||
err = b.Process()
|
||||
if err != nil {
|
||||
t.Error("test failed:", err)
|
||||
}
|
||||
|
||||
_, err = SubscribeToExchangeOrderbooks("SubscribeToExchangeOrderbooks")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerify(t *testing.T) {
|
||||
t.Parallel()
|
||||
b := Base{
|
||||
@@ -95,13 +218,18 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
func TestGetOrderbook(t *testing.T) {
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
base := &Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
ExchangeName: "Exchange",
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", &base, asset.Spot)
|
||||
err := base.Process()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := Get("Exchange", c, asset.Spot)
|
||||
if err != nil {
|
||||
@@ -128,83 +256,37 @@ func TestGetOrderbook(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbook retrieved non-existent orderbook using invalid second currency")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrderbookByExchange(t *testing.T) {
|
||||
currency := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: currency,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", &base, asset.Spot)
|
||||
|
||||
_, err := GetByExchange("Exchange")
|
||||
base.Pair = newCurrency
|
||||
err = base.Process()
|
||||
if err != nil {
|
||||
t.Fatalf("Test failed. TestGetOrderbookByExchange failed to get orderbook. Error %s",
|
||||
err)
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = GetByExchange("nonexistent")
|
||||
_, err = Get("Exchange", newCurrency, "meowCats")
|
||||
if err == nil {
|
||||
t.Fatal("Test failed. TestGetOrderbookByExchange retrieved non-existent orderbook")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstCurrencyExists(t *testing.T) {
|
||||
c := currency.NewPairFromStrings("BTC", "AUD")
|
||||
base := Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", &base, asset.Spot)
|
||||
|
||||
if !BaseCurrencyExists("Exchange", c.Base) {
|
||||
t.Fatal("Test failed. TestFirstCurrencyExists expected first currency doesn't exist")
|
||||
}
|
||||
|
||||
var item = currency.NewCode("blah")
|
||||
if BaseCurrencyExists("Exchange", item) {
|
||||
t.Fatal("Test failed. TestFirstCurrencyExists unexpected first currency exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecondCurrencyExists(t *testing.T) {
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", &base, asset.Spot)
|
||||
|
||||
if !QuoteCurrencyExists("Exchange", c) {
|
||||
t.Fatal("Test failed. TestSecondCurrencyExists expected first currency doesn't exist")
|
||||
}
|
||||
|
||||
c.Quote = currency.NewCode("blah")
|
||||
if QuoteCurrencyExists("Exchange", c) {
|
||||
t.Fatal("Test failed. TestSecondCurrencyExists unexpected first currency exists")
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNewOrderbook(t *testing.T) {
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
base := &Base{
|
||||
Pair: c,
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
ExchangeName: "testCreateNewOrderbook",
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
CreateNewOrderbook("Exchange", &base, asset.Spot)
|
||||
|
||||
result, err := Get("Exchange", c, asset.Spot)
|
||||
err := base.Process()
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook failed to create new orderbook")
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := Get("testCreateNewOrderbook", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestCreateNewOrderbook failed to create new orderbook", err)
|
||||
}
|
||||
|
||||
if !result.Pair.Equal(c) {
|
||||
@@ -223,12 +305,11 @@ func TestCreateNewOrderbook(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProcessOrderbook(t *testing.T) {
|
||||
Orderbooks = []Orderbook{}
|
||||
c := currency.NewPairFromStrings("BTC", "USD")
|
||||
base := Base{
|
||||
Asks: []Item{{Price: 100, Amount: 10}},
|
||||
Bids: []Item{{Price: 200, Amount: 10}},
|
||||
ExchangeName: "Exchange",
|
||||
ExchangeName: "ProcessOrderbook",
|
||||
}
|
||||
|
||||
// test for empty pair
|
||||
@@ -251,7 +332,7 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("unexpcted result: ", err)
|
||||
}
|
||||
result, err := Get("Exchange", c, asset.Spot)
|
||||
result, err := Get("ProcessOrderbook", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
|
||||
}
|
||||
@@ -266,7 +347,7 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
result, err = Get("Exchange", c, asset.Spot)
|
||||
result, err = Get("ProcessOrderbook", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
@@ -281,7 +362,7 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
result, err = Get("Exchange", c, asset.Spot)
|
||||
result, err = Get("ProcessOrderbook", c, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
@@ -296,7 +377,7 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
t.Error("Test Failed - Process() error", err)
|
||||
}
|
||||
|
||||
result, err = Get("Exchange", c, "monthly")
|
||||
result, err = Get("ProcessOrderbook", c, "monthly")
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
|
||||
}
|
||||
@@ -408,3 +489,17 @@ func TestProcessOrderbook(t *testing.T) {
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestSetNewData(t *testing.T) {
|
||||
err := service.SetNewData(nil)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil ")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAssociations(t *testing.T) {
|
||||
_, err := service.GetAssociations(nil)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil ")
|
||||
}
|
||||
}
|
||||
|
||||
69
exchanges/orderbook/orderbook_types.go
Normal file
69
exchanges/orderbook/orderbook_types.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package orderbook
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// const values for orderbook package
|
||||
const (
|
||||
errExchangeNameUnset = "orderbook exchange name not set"
|
||||
errPairNotSet = "orderbook currency pair not set"
|
||||
errAssetTypeNotSet = "orderbook asset type not set"
|
||||
errNoOrderbook = "orderbook bids and asks are empty"
|
||||
)
|
||||
|
||||
// Vars for the orderbook package
|
||||
var (
|
||||
service *Service
|
||||
)
|
||||
|
||||
func init() {
|
||||
service = new(Service)
|
||||
service.mux = dispatch.GetNewMux()
|
||||
service.Books = make(map[string]map[*currency.Item]map[*currency.Item]map[asset.Item]*Book)
|
||||
service.Exchange = make(map[string]uuid.UUID)
|
||||
}
|
||||
|
||||
// Book defines an orderbook with its links to different dispatch outputs
|
||||
type Book struct {
|
||||
b *Base
|
||||
Main uuid.UUID
|
||||
Assoc []uuid.UUID
|
||||
}
|
||||
|
||||
// Service holds orderbook information for each individual exchange
|
||||
type Service struct {
|
||||
Books map[string]map[*currency.Item]map[*currency.Item]map[asset.Item]*Book
|
||||
Exchange map[string]uuid.UUID
|
||||
mux *dispatch.Mux
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// 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 asset.Item `json:"assetType"`
|
||||
ExchangeName string `json:"exchangeName"`
|
||||
}
|
||||
|
||||
type byOBPrice []Item
|
||||
|
||||
func (a byOBPrice) Len() int { return len(a) }
|
||||
func (a byOBPrice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byOBPrice) Less(i, j int) bool { return a[i].Price < a[j].Price }
|
||||
@@ -329,6 +329,7 @@ func (p *Poloniex) WsProcessOrderbookSnapshot(ob []interface{}, symbol string) e
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.Pair = currency.NewPairFromString(symbol)
|
||||
newOrderBook.ExchangeName = p.GetName()
|
||||
|
||||
return p.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
}
|
||||
|
||||
@@ -3,191 +3,205 @@ package ticker
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"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
|
||||
func init() {
|
||||
service = new(Service)
|
||||
service.Tickers = make(map[string]map[*currency.Item]map[*currency.Item]map[asset.Item]*Ticker)
|
||||
service.Exchange = make(map[string]uuid.UUID)
|
||||
service.mux = dispatch.GetNewMux()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
// SubscribeTicker subcribes to a ticker and returns a communication channel to
|
||||
// stream new ticker updates
|
||||
func SubscribeTicker(exchange string, p currency.Pair, a asset.Item) (dispatch.Pipe, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
|
||||
// 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 ""
|
||||
tick, ok := service.Tickers[exchange][p.Base.Item][p.Quote.Item][a]
|
||||
if !ok {
|
||||
return dispatch.Pipe{}, fmt.Errorf("ticker item not found for %s %s %s",
|
||||
exchange,
|
||||
p,
|
||||
a)
|
||||
}
|
||||
|
||||
return service.mux.Subscribe(tick.Main)
|
||||
}
|
||||
|
||||
// SubscribeToExchangeTickers subcribes to all tickers on an exchange
|
||||
func SubscribeToExchangeTickers(exchange string) (dispatch.Pipe, error) {
|
||||
exchange = strings.ToLower(exchange)
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
id, ok := service.Exchange[exchange]
|
||||
if !ok {
|
||||
return dispatch.Pipe{}, fmt.Errorf("%s exchange tickers not found",
|
||||
exchange)
|
||||
}
|
||||
|
||||
return service.mux.Subscribe(id)
|
||||
}
|
||||
|
||||
// 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
|
||||
exchange = strings.ToLower(exchange)
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
if service.Tickers[exchange] == nil {
|
||||
return Price{}, fmt.Errorf("no tickers for %s exchange", exchange)
|
||||
}
|
||||
|
||||
if !BaseCurrencyExists(exchange, p.Base) {
|
||||
return Price{}, errors.New(errBaseCurrencyNotFound)
|
||||
if service.Tickers[exchange][p.Base.Item] == nil {
|
||||
return Price{}, fmt.Errorf("no tickers associated with base currency %s",
|
||||
p.Base)
|
||||
}
|
||||
|
||||
if !QuoteCurrencyExists(exchange, p) {
|
||||
return Price{}, errors.New(errQuoteCurrencyNotFound)
|
||||
if service.Tickers[exchange][p.Base.Item][p.Quote.Item] == nil {
|
||||
return Price{}, fmt.Errorf("no tickers associated with quote currency %s",
|
||||
p.Quote)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
if service.Tickers[exchange][p.Base.Item][p.Quote.Item][tickerType] == nil {
|
||||
return Price{}, fmt.Errorf("no tickers associated with asset type %s",
|
||||
tickerType)
|
||||
}
|
||||
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
|
||||
return service.Tickers[exchange][p.Base.Item][p.Quote.Item][tickerType].Price, nil
|
||||
}
|
||||
|
||||
// ProcessTicker processes incoming tickers, creating or updating the Tickers
|
||||
// list
|
||||
func ProcessTicker(exchangeName string, tickerNew *Price, assetType asset.Item) error {
|
||||
if exchangeName == "" {
|
||||
return fmt.Errorf(errExchangeNameUnset)
|
||||
}
|
||||
|
||||
tickerNew.ExchangeName = strings.ToLower(exchangeName)
|
||||
|
||||
if tickerNew.Pair.IsEmpty() {
|
||||
return fmt.Errorf("%v %v", exchangeName, errPairNotSet)
|
||||
return fmt.Errorf("%s %s", exchangeName, errPairNotSet)
|
||||
}
|
||||
|
||||
if assetType == "" {
|
||||
return fmt.Errorf("%v %v %v", exchangeName, tickerNew.Pair.String(), errAssetTypeNotSet)
|
||||
return fmt.Errorf("%s %s %s", exchangeName,
|
||||
tickerNew.Pair,
|
||||
errAssetTypeNotSet)
|
||||
}
|
||||
|
||||
tickerNew.AssetType = assetType
|
||||
|
||||
if tickerNew.LastUpdated.IsZero() {
|
||||
tickerNew.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
ticker, err := GetTickerByExchange(exchangeName)
|
||||
return service.Update(tickerNew)
|
||||
}
|
||||
|
||||
// Update updates ticker price
|
||||
func (s *Service) Update(p *Price) error {
|
||||
var ids []uuid.UUID
|
||||
|
||||
s.Lock()
|
||||
switch {
|
||||
case s.Tickers[p.ExchangeName] == nil:
|
||||
s.Tickers[p.ExchangeName] = make(map[*currency.Item]map[*currency.Item]map[asset.Item]*Ticker)
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Ticker)
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
||||
err := s.SetItemID(p)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
case s.Tickers[p.ExchangeName][p.Pair.Base.Item] == nil:
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Ticker)
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
||||
err := s.SetItemID(p)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
case s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] == nil:
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
||||
err := s.SetItemID(p)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
case s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType] == nil:
|
||||
err := s.SetItemID(p)
|
||||
if err != nil {
|
||||
s.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Last = p.Last
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].High = p.High
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Low = p.Low
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Bid = p.Bid
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Ask = p.Ask
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Volume = p.Volume
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].QuoteVolume = p.QuoteVolume
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].PriceATH = p.PriceATH
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Open = p.Open
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Close = p.Close
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].LastUpdated = p.LastUpdated
|
||||
ids = s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Assoc
|
||||
ids = append(ids, s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Main)
|
||||
}
|
||||
s.Unlock()
|
||||
return s.mux.Publish(ids, p)
|
||||
}
|
||||
|
||||
// SetItemID retrieves and sets dispatch mux publish IDs
|
||||
func (s *Service) SetItemID(p *Price) error {
|
||||
if p == nil {
|
||||
return errors.New(errTickerPriceIsNil)
|
||||
}
|
||||
|
||||
ids, err := s.GetAssociations(p)
|
||||
if err != nil {
|
||||
CreateNewTicker(exchangeName, tickerNew, assetType)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
singleID, err := s.mux.GetID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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()
|
||||
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType] = &Ticker{Price: *p,
|
||||
Main: singleID,
|
||||
Assoc: ids}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAssociations links a singular book with it's dispatch associations
|
||||
func (s *Service) GetAssociations(p *Price) ([]uuid.UUID, error) {
|
||||
if p == nil || *p == (Price{}) {
|
||||
return nil, errors.New(errTickerPriceIsNil)
|
||||
}
|
||||
var ids []uuid.UUID
|
||||
exchangeID, ok := s.Exchange[p.ExchangeName]
|
||||
if !ok {
|
||||
var err error
|
||||
exchangeID, err = s.mux.GetID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.Exchange[p.ExchangeName] = exchangeID
|
||||
}
|
||||
|
||||
ids = append(ids, exchangeID)
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
@@ -1,55 +1,94 @@
|
||||
package ticker
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
func TestPriceToString(t *testing.T) {
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
Pair: newPair,
|
||||
Last: 1200,
|
||||
High: 1298,
|
||||
Low: 1148,
|
||||
Bid: 1195,
|
||||
Ask: 1220,
|
||||
Volume: 5,
|
||||
PriceATH: 1337,
|
||||
func TestMain(m *testing.M) {
|
||||
err := dispatch.Start(1)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
newTicker := CreateNewTicker("ANX", &priceStruct, asset.Spot)
|
||||
cpyMux = service.mux
|
||||
|
||||
if newTicker.PriceToString(newPair, "last", asset.Spot) != "1200" {
|
||||
t.Error("Test Failed - ticker PriceToString last value is incorrect")
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
var cpyMux *dispatch.Mux
|
||||
|
||||
func TestSubscribeTicker(t *testing.T) {
|
||||
_, err := SubscribeTicker("", currency.Pair{}, asset.Item(""))
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "high", asset.Spot) != "1298" {
|
||||
t.Error("Test Failed - ticker PriceToString high value is incorrect")
|
||||
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
|
||||
// force error
|
||||
service.mux = nil
|
||||
err = ProcessTicker("subscribetest", &Price{Pair: p}, asset.Spot)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "low", asset.Spot) != "1148" {
|
||||
t.Error("Test Failed - ticker PriceToString low value is incorrect")
|
||||
|
||||
sillyP := p
|
||||
sillyP.Base = currency.GALA_NEO
|
||||
err = ProcessTicker("subscribetest", &Price{Pair: sillyP}, asset.Spot)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "bid", asset.Spot) != "1195" {
|
||||
t.Error("Test Failed - ticker PriceToString bid value is incorrect")
|
||||
|
||||
sillyP.Quote = currency.AAA
|
||||
err = ProcessTicker("subscribetest", &Price{Pair: sillyP}, asset.Spot)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "ask", asset.Spot) != "1220" {
|
||||
t.Error("Test Failed - ticker PriceToString ask value is incorrect")
|
||||
|
||||
err = ProcessTicker("subscribetest", &Price{Pair: sillyP}, "silly")
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "volume", asset.Spot) != "5" {
|
||||
t.Error("Test Failed - ticker PriceToString volume value is incorrect")
|
||||
// reinstate mux
|
||||
service.mux = cpyMux
|
||||
|
||||
err = ProcessTicker("subscribetest", &Price{Pair: p}, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "ath", asset.Spot) != "1337" {
|
||||
t.Error("Test Failed - ticker PriceToString ath value is incorrect")
|
||||
|
||||
_, err = SubscribeTicker("subscribetest", p, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error("cannot subscribe to ticker", err)
|
||||
}
|
||||
if newTicker.PriceToString(newPair, "obtuse", asset.Spot) != "" {
|
||||
t.Error("Test Failed - ticker PriceToString obtuse value is incorrect")
|
||||
}
|
||||
|
||||
func TestSubscribeToExchangeTickers(t *testing.T) {
|
||||
_, err := SubscribeToExchangeTickers("")
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
p := currency.NewPair(currency.BTC, currency.USD)
|
||||
|
||||
err = ProcessTicker("subscribeExchangeTest", &Price{Pair: p}, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = SubscribeToExchangeTickers("subscribeExchangeTest")
|
||||
if err != nil {
|
||||
t.Error("error cannot be nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,142 +150,25 @@ func TestGetTicker(t *testing.T) {
|
||||
if tickerPrice.PriceATH != 9001 {
|
||||
t.Error("Test Failed - ticker tickerPrice.PriceATH value is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTickerByExchange(t *testing.T) {
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
Pair: newPair,
|
||||
Last: 1200,
|
||||
High: 1298,
|
||||
Low: 1148,
|
||||
Bid: 1195,
|
||||
Ask: 1220,
|
||||
Volume: 5,
|
||||
PriceATH: 1337,
|
||||
_, err = GetTicker("bitfinex", newPair, "meowCats")
|
||||
if err == nil {
|
||||
t.Error("Test Failed - Ticker GetTicker error cannot be nil")
|
||||
}
|
||||
|
||||
anxTicker := CreateNewTicker("ANX", &priceStruct, asset.Spot)
|
||||
Tickers = append(Tickers, anxTicker)
|
||||
|
||||
tickerPtr, err := GetTickerByExchange("ANX")
|
||||
err = ProcessTicker("bitfinex", &priceStruct, "meowCats")
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed - GetTickerByExchange init error: %s", err)
|
||||
}
|
||||
if tickerPtr.ExchangeName != "ANX" {
|
||||
t.Error("Test Failed - GetTickerByExchange ExchangeName value is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseCurrencyExists(t *testing.T) {
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
Pair: newPair,
|
||||
Last: 1200,
|
||||
High: 1298,
|
||||
Low: 1148,
|
||||
Bid: 1195,
|
||||
Ask: 1220,
|
||||
Volume: 5,
|
||||
PriceATH: 1337,
|
||||
t.Fatal("Test failed. ProcessTicker error", err)
|
||||
}
|
||||
|
||||
alphaTicker := CreateNewTicker("alphapoint", &priceStruct, asset.Spot)
|
||||
Tickers = append(Tickers, alphaTicker)
|
||||
|
||||
if !BaseCurrencyExists("alphapoint", currency.BTC) {
|
||||
t.Error("Test Failed - BaseCurrencyExists1 value return is incorrect")
|
||||
}
|
||||
if BaseCurrencyExists("alphapoint", currency.NewCode("CATS")) {
|
||||
t.Error("Test Failed - BaseCurrencyExists2 value return is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuoteCurrencyExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
Pair: newPair,
|
||||
Last: 1200,
|
||||
High: 1298,
|
||||
Low: 1148,
|
||||
Bid: 1195,
|
||||
Ask: 1220,
|
||||
Volume: 5,
|
||||
PriceATH: 1337,
|
||||
}
|
||||
|
||||
bitstampTicker := CreateNewTicker("bitstamp", &priceStruct, asset.Spot)
|
||||
Tickers = append(Tickers, bitstampTicker)
|
||||
|
||||
if !QuoteCurrencyExists("bitstamp", newPair) {
|
||||
t.Error("Test Failed - QuoteCurrencyExists1 value return is incorrect")
|
||||
}
|
||||
|
||||
newPair.Quote = currency.NewCode("DOGS")
|
||||
if QuoteCurrencyExists("bitstamp", newPair) {
|
||||
t.Error("Test Failed - QuoteCurrencyExists2 value return is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNewTicker(t *testing.T) {
|
||||
const float64Type = "float64"
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
Pair: newPair,
|
||||
Last: 1200,
|
||||
High: 1298,
|
||||
Low: 1148,
|
||||
Bid: 1195,
|
||||
Ask: 1220,
|
||||
Volume: 5,
|
||||
PriceATH: 1337,
|
||||
}
|
||||
|
||||
newTicker := CreateNewTicker("ANX", &priceStruct, asset.Spot)
|
||||
|
||||
if reflect.ValueOf(newTicker).NumField() != 2 {
|
||||
t.Error("Test Failed - ticker CreateNewTicker struct change/or updated")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.ExchangeName).String() != "string" {
|
||||
t.Error("Test Failed - ticker CreateNewTicker.ExchangeName value is not a string")
|
||||
}
|
||||
if newTicker.ExchangeName != "ANX" {
|
||||
t.Error("Test Failed - ticker CreateNewTicker.ExchangeName value is not ANX")
|
||||
}
|
||||
|
||||
if !newTicker.Price[currency.BTC.Upper().String()][currency.USD.Upper().String()][asset.Spot.String()].Pair.Equal(newPair) {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Pair.Pair().String() value is not expected 'BTCUSD'")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Ask).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Ask value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Bid).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Bid value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Pair).String() != "currency.Pair" {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].CurrencyPair value is not a currency.Pair")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].High).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].High value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Last).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Last value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Low).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Low value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].PriceATH).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].PriceATH value is not a float64")
|
||||
}
|
||||
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][asset.Spot.String()].Volume).String() != float64Type {
|
||||
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Volume value is not a float64")
|
||||
// process update again
|
||||
err = ProcessTicker("bitfinex", &priceStruct, "meowCats")
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. ProcessTicker error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessTicker(t *testing.T) { // non-appending function to tickers
|
||||
Tickers = []Ticker{}
|
||||
exchName := "bitstamp"
|
||||
newPair := currency.NewPairFromStrings("BTC", "USD")
|
||||
priceStruct := Price{
|
||||
@@ -259,8 +181,13 @@ func TestProcessTicker(t *testing.T) { // non-appending function to tickers
|
||||
PriceATH: 1337,
|
||||
}
|
||||
|
||||
err := ProcessTicker("", &priceStruct, asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("empty exchange should throw an err")
|
||||
}
|
||||
|
||||
// test for empty pair
|
||||
err := ProcessTicker(exchName, &priceStruct, asset.Spot)
|
||||
err = ProcessTicker(exchName, &priceStruct, asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("empty pair should throw an err")
|
||||
}
|
||||
@@ -389,5 +316,44 @@ func TestProcessTicker(t *testing.T) { // non-appending function to tickers
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
}
|
||||
|
||||
func TestSetItemID(t *testing.T) {
|
||||
err := service.SetItemID(nil)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
err = service.SetItemID(&Price{})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
p := currency.NewPair(currency.CYC, currency.CYG)
|
||||
|
||||
service.mux = nil
|
||||
err = service.SetItemID(&Price{Pair: p, ExchangeName: "SetItemID"})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
|
||||
service.mux = cpyMux
|
||||
}
|
||||
|
||||
func TestGetAssociation(t *testing.T) {
|
||||
_, err := service.GetAssociations(nil)
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil ")
|
||||
}
|
||||
|
||||
p := currency.NewPair(currency.CYC, currency.CYG)
|
||||
|
||||
service.mux = nil
|
||||
|
||||
_, err = service.GetAssociations(&Price{Pair: p, ExchangeName: "GetAssociation"})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil ")
|
||||
}
|
||||
|
||||
service.mux = cpyMux
|
||||
}
|
||||
|
||||
57
exchanges/ticker/ticker_types.go
Normal file
57
exchanges/ticker/ticker_types.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package ticker
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// const values for the ticker package
|
||||
const (
|
||||
errExchangeNameUnset = "ticker exchange name not set"
|
||||
errPairNotSet = "ticker currency pair not set"
|
||||
errAssetTypeNotSet = "ticker asset type not set"
|
||||
errTickerPriceIsNil = "ticker price is nil"
|
||||
)
|
||||
|
||||
// Vars for the ticker package
|
||||
var (
|
||||
service *Service
|
||||
)
|
||||
|
||||
// Service holds ticker information for each individual exchange
|
||||
type Service struct {
|
||||
Tickers map[string]map[*currency.Item]map[*currency.Item]map[asset.Item]*Ticker
|
||||
Exchange map[string]uuid.UUID
|
||||
mux *dispatch.Mux
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// 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"`
|
||||
ExchangeName string `json:"exchangeName"`
|
||||
AssetType asset.Item `json:"assetType"`
|
||||
LastUpdated time.Time
|
||||
}
|
||||
|
||||
// Ticker struct holds the ticker information for a currency pair and type
|
||||
type Ticker struct {
|
||||
Price
|
||||
Main uuid.UUID
|
||||
Assoc []uuid.UUID
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package wsorderbook
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
@@ -205,6 +206,19 @@ func (w *WebsocketOrderbookLocal) LoadSnapshot(newOrderbook *orderbook.Base) err
|
||||
if len(newOrderbook.Asks) == 0 || len(newOrderbook.Bids) == 0 {
|
||||
return fmt.Errorf("%v snapshot ask and bids are nil", w.exchangeName)
|
||||
}
|
||||
|
||||
if newOrderbook.Pair.IsEmpty() {
|
||||
return errors.New("websocket orderbook pair unset")
|
||||
}
|
||||
|
||||
if newOrderbook.AssetType.String() == "" {
|
||||
return errors.New("websocket orderbook asset type unset")
|
||||
}
|
||||
|
||||
if newOrderbook.ExchangeName == "" {
|
||||
return errors.New("websocket orderbook exchange name unset")
|
||||
}
|
||||
|
||||
w.m.Lock()
|
||||
defer w.m.Unlock()
|
||||
if w.ob == nil {
|
||||
|
||||
@@ -26,6 +26,7 @@ const (
|
||||
|
||||
func createSnapshot() (obl *WebsocketOrderbookLocal, curr currency.Pair, asks, bids []orderbook.Item, err error) {
|
||||
var snapShot1 orderbook.Base
|
||||
snapShot1.ExchangeName = exchangeName
|
||||
curr = currency.NewPairFromString("BTCUSD")
|
||||
asks = []orderbook.Item{
|
||||
{Price: 4000, Amount: 1, ID: 6},
|
||||
@@ -37,19 +38,28 @@ func createSnapshot() (obl *WebsocketOrderbookLocal, curr currency.Pair, asks, b
|
||||
snapShot1.Bids = bids
|
||||
snapShot1.AssetType = asset.Spot
|
||||
snapShot1.Pair = curr
|
||||
obl = &WebsocketOrderbookLocal{}
|
||||
obl = &WebsocketOrderbookLocal{exchangeName: exchangeName}
|
||||
err = obl.LoadSnapshot(&snapShot1)
|
||||
return
|
||||
}
|
||||
|
||||
// BenchmarkBufferPerformance demonstrates buffer more performant than multi process calls
|
||||
// BenchmarkBufferPerformance demonstrates buffer more performant than multi
|
||||
// process calls
|
||||
func BenchmarkBufferPerformance(b *testing.B) {
|
||||
obl, curr, asks, bids, err := createSnapshot()
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.sortBuffer = true
|
||||
cp := currency.NewPairFromString("BTCUSD")
|
||||
// This is to ensure we do not send in zero orderbook info to our main book
|
||||
// in orderbook.go, orderbooks should not be zero even after an update.
|
||||
dummyItem := orderbook.Item{
|
||||
Amount: 1333337,
|
||||
Price: 1337.1337,
|
||||
ID: 1337,
|
||||
}
|
||||
obl.ob[cp][asset.Spot].Bids = append(obl.ob[cp][asset.Spot].Bids, dummyItem)
|
||||
update := &WebsocketOrderbookUpdate{
|
||||
Bids: bids,
|
||||
Asks: asks,
|
||||
@@ -74,7 +84,6 @@ func BenchmarkBufferSortingPerformance(b *testing.B) {
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.sortBuffer = true
|
||||
obl.bufferEnabled = true
|
||||
obl.obBufferLimit = 5
|
||||
@@ -96,13 +105,22 @@ func BenchmarkBufferSortingPerformance(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkNoBufferPerformance demonstrates orderbook process less performant than buffer
|
||||
// BenchmarkNoBufferPerformance demonstrates orderbook process less performant
|
||||
// than buffer
|
||||
func BenchmarkNoBufferPerformance(b *testing.B) {
|
||||
obl, curr, asks, bids, err := createSnapshot()
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
cp := currency.NewPairFromString("BTCUSD")
|
||||
// This is to ensure we do not send in zero orderbook info to our main book
|
||||
// in orderbook.go, orderbooks should not be zero even after an update.
|
||||
dummyItem := orderbook.Item{
|
||||
Amount: 1333337,
|
||||
Price: 1337.1337,
|
||||
ID: 1337,
|
||||
}
|
||||
obl.ob[cp][asset.Spot].Bids = append(obl.ob[cp][asset.Spot].Bids, dummyItem)
|
||||
update := &WebsocketOrderbookUpdate{
|
||||
Bids: bids,
|
||||
Asks: asks,
|
||||
@@ -127,7 +145,6 @@ func TestHittingTheBuffer(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.bufferEnabled = true
|
||||
obl.obBufferLimit = 5
|
||||
for i := 0; i < len(itemArray); i++ {
|
||||
@@ -146,10 +163,12 @@ func TestHittingTheBuffer(t *testing.T) {
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Asks) != 3 {
|
||||
t.Log(obl.ob[curr][asset.Spot])
|
||||
t.Errorf("expected 3 entries, received: %v", len(obl.ob[curr][asset.Spot].Asks))
|
||||
t.Errorf("expected 3 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Asks))
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Bids) != 3 {
|
||||
t.Errorf("expected 3 entries, received: %v", len(obl.ob[curr][asset.Spot].Bids))
|
||||
t.Errorf("expected 3 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Bids))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +178,6 @@ func TestInsertWithIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.bufferEnabled = true
|
||||
obl.updateEntriesByID = true
|
||||
obl.obBufferLimit = 5
|
||||
@@ -179,10 +197,12 @@ func TestInsertWithIDs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Asks) != 6 {
|
||||
t.Errorf("expected 6 entries, received: %v", len(obl.ob[curr][asset.Spot].Asks))
|
||||
t.Errorf("expected 6 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Asks))
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Bids) != 6 {
|
||||
t.Errorf("expected 6 entries, received: %v", len(obl.ob[curr][asset.Spot].Bids))
|
||||
t.Errorf("expected 6 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Bids))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +212,6 @@ func TestSortIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.bufferEnabled = true
|
||||
obl.sortBufferByUpdateIDs = true
|
||||
obl.sortBuffer = true
|
||||
@@ -212,10 +231,12 @@ func TestSortIDs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Asks) != 3 {
|
||||
t.Errorf("expected 6 entries, received: %v", len(obl.ob[curr][asset.Spot].Asks))
|
||||
t.Errorf("expected 3 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Asks))
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Bids) != 3 {
|
||||
t.Errorf("expected 6 entries, received: %v", len(obl.ob[curr][asset.Spot].Bids))
|
||||
t.Errorf("expected 3 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Bids))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +246,16 @@ func TestDeleteWithIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
|
||||
cp := currency.NewPairFromString("BTCUSD")
|
||||
// This is to ensure we do not send in zero orderbook info to our main book
|
||||
// in orderbook.go, orderbooks should not be zero even after an update.
|
||||
dummyItem := orderbook.Item{
|
||||
Amount: 1333337,
|
||||
Price: 1337.1337,
|
||||
ID: 1337,
|
||||
}
|
||||
obl.ob[cp][asset.Spot].Bids = append(obl.ob[cp][asset.Spot].Bids, dummyItem)
|
||||
obl.updateEntriesByID = true
|
||||
for i := 0; i < len(itemArray); i++ {
|
||||
asks := itemArray[i]
|
||||
@@ -243,10 +273,12 @@ func TestDeleteWithIDs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Asks) != 0 {
|
||||
t.Errorf("expected 0 entries, received: %v", len(obl.ob[curr][asset.Spot].Asks))
|
||||
t.Errorf("expected 0 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Asks))
|
||||
}
|
||||
if len(obl.ob[curr][asset.Spot].Bids) != 0 {
|
||||
t.Errorf("expected 0 entries, received: %v", len(obl.ob[curr][asset.Spot].Bids))
|
||||
if len(obl.ob[curr][asset.Spot].Bids) != 1 {
|
||||
t.Errorf("expected 1 entries, received: %v",
|
||||
len(obl.ob[curr][asset.Spot].Bids))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +288,6 @@ func TestUpdateWithIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.updateEntriesByID = true
|
||||
for i := 0; i < len(itemArray); i++ {
|
||||
asks := itemArray[i]
|
||||
@@ -288,11 +319,10 @@ func TestOutOfOrderIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outOFOrderIDs := []int64{2, 1, 5, 3, 4, 6}
|
||||
outOFOrderIDs := []int64{2, 1, 5, 3, 4, 6, 7}
|
||||
if itemArray[0][0].Price != 1000 {
|
||||
t.Errorf("expected sorted price to be 3000, received: %v", itemArray[1][0].Price)
|
||||
}
|
||||
obl.exchangeName = exchangeName
|
||||
obl.bufferEnabled = true
|
||||
obl.sortBuffer = true
|
||||
obl.obBufferLimit = 5
|
||||
@@ -366,7 +396,8 @@ func TestRunUpdateWithoutAnyUpdates(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected an error running update with no snapshot loaded")
|
||||
}
|
||||
if err.Error() != fmt.Sprintf("%v cannot have bids and ask targets both nil", exchangeName) {
|
||||
if err.Error() != fmt.Sprintf("%v cannot have bids and ask targets both nil",
|
||||
exchangeName) {
|
||||
t.Fatal("expected nil asks and bids error")
|
||||
}
|
||||
}
|
||||
@@ -395,6 +426,7 @@ func TestRunSnapshotWithNoData(t *testing.T) {
|
||||
func TestLoadSnapshot(t *testing.T) {
|
||||
var obl WebsocketOrderbookLocal
|
||||
var snapShot1 orderbook.Base
|
||||
snapShot1.ExchangeName = "SnapshotWithOverride"
|
||||
curr := currency.NewPairFromString("BTCUSD")
|
||||
asks := []orderbook.Item{
|
||||
{Price: 4000, Amount: 1, ID: 8},
|
||||
@@ -432,6 +464,7 @@ func TestFlushCache(t *testing.T) {
|
||||
func TestInsertingSnapShots(t *testing.T) {
|
||||
var obl WebsocketOrderbookLocal
|
||||
var snapShot1 orderbook.Base
|
||||
snapShot1.ExchangeName = "WSORDERBOOKTEST1"
|
||||
asks := []orderbook.Item{
|
||||
{Price: 6000, Amount: 1, ID: 1},
|
||||
{Price: 6001, Amount: 0.5, ID: 2},
|
||||
@@ -469,6 +502,7 @@ func TestInsertingSnapShots(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var snapShot2 orderbook.Base
|
||||
snapShot2.ExchangeName = "WSORDERBOOKTEST2"
|
||||
asks = []orderbook.Item{
|
||||
{Price: 51, Amount: 1, ID: 1},
|
||||
{Price: 52, Amount: 0.5, ID: 2},
|
||||
@@ -506,6 +540,7 @@ func TestInsertingSnapShots(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var snapShot3 orderbook.Base
|
||||
snapShot3.ExchangeName = "WSORDERBOOKTEST3"
|
||||
asks = []orderbook.Item{
|
||||
{Price: 511, Amount: 1, ID: 1},
|
||||
{Price: 52, Amount: 0.5, ID: 2},
|
||||
|
||||
@@ -142,6 +142,7 @@ func (z *ZB) WsHandleData() {
|
||||
newOrderBook.Bids = bids
|
||||
newOrderBook.AssetType = asset.Spot
|
||||
newOrderBook.Pair = cPair
|
||||
newOrderBook.ExchangeName = z.GetName()
|
||||
|
||||
err = z.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user