mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 07:26:48 +00:00
* Exchanges: Initial implementation after rebase of depth (WIP) * orderbook/buffer: convert and couple orderbook interaction functionality from buffer to orderbook linked list - Use single point reference for orderbook depth * buffer/orderbook: conversion continued (WIP) * exchange: buffer/linkedlist handover (WIP) * Added some tests for yesterday * linkedList: added more testing and trying to figure out broken things * Started tying everything in * continuous integration and testing * orderbook: expanded tests * go mod tidy * Add in different synchornisation levels for protocols Add in timer for the streaming system to reduce updates to datahandler Add in more test code as I integrate more exchanges * Depth: Add tests, add length check to call linked list updating, add in constructor. Linked List: Improve tests, add in checks for zero liquidity on books. Node: Added in cleaner POC, add in contructor. Buffer: Fixed tests, checked benchmarks. * orderbook: reinstate dispatch calls * Addr glorious & madcozbad nits * fix functionality and add tests * Address linterinos * remove label * expanded comment * fix races and and bitmex test * reinstate go routine for alerting changes * rm line :D * fix more tests * Addr glorious nits * rm glorious field * depth: defer unlock to stop deadlock * orderbook: remove unused vars * buffer: fix test to what it should be * nits: madcosbad addr * nits: glorious nits * linkedlist: remove unused params * orderbook: shift time call to outside of push to inline, add in case for update inster price for zero liquidity, nits * orderbook: nits addressed * engine: change stream -> websocket convention and remove unused function * nits: glorious nits * Websocket Buffer: Add verbosity switch * linked list: Add comment * linked list: fix spelling * nits: glorious nits * orderbook: Adds in test and explicit time type with constructor, fix nits * linter * spelling: removed the dere fence * depth: Update alerting mechanism to a more battle tested state * depth: spelling * nits: glorious nits * linked list: match cases * buffer: fix linter issue * golangci: increase timeout by 30 seconds * nodes: update atomic checks * spelling: fix * node: add in commentary * exchanges/syncer: add function to switch over to REST when websocket functionality is not available for a specific asset type * linter: exchange linter issues * syncer: Add in warning * nits: glorious nits * AssetWebsocketSupport: unexport map * Nits: Adrr * rm letter * exchanges: Orderbook verification change for naming, deprecate checksum bypass as it has the potential to obfuscate errors that are at the tail end of the book, add in verification for websocket stream updates * general: fix spelling remove breakpoint * nits: fix more glorious nits until more are found * orderbook: fix tests * orderbook: fix wait tests and add in more checks * nits: addr * orderbook: remove dispatch reference * linkedlist: consolidate bid/ask functions * linked lisdt: remove words * fix spelling
360 lines
10 KiB
Go
360 lines
10 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/stats"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
func printCurrencyFormat(price float64, displayCurrency currency.Code) string {
|
|
displaySymbol, err := currency.GetSymbolByCurrencyName(displayCurrency)
|
|
if err != nil {
|
|
log.Errorf(log.Global, "Failed to get display symbol: %s\n", err)
|
|
}
|
|
|
|
return fmt.Sprintf("%s%.8f", displaySymbol, price)
|
|
}
|
|
|
|
func printConvertCurrencyFormat(origCurrency currency.Code, origPrice float64, displayCurrency currency.Code) string {
|
|
conv, err := currency.ConvertCurrency(origPrice,
|
|
origCurrency,
|
|
displayCurrency)
|
|
if err != nil {
|
|
log.Errorf(log.Global, "Failed to convert currency: %s\n", err)
|
|
}
|
|
|
|
displaySymbol, err := currency.GetSymbolByCurrencyName(displayCurrency)
|
|
if err != nil {
|
|
log.Errorf(log.Global, "Failed to get display symbol: %s\n", err)
|
|
}
|
|
|
|
origSymbol, err := currency.GetSymbolByCurrencyName(origCurrency)
|
|
if err != nil {
|
|
log.Errorf(log.Global, "Failed to get original currency symbol for %s: %s\n",
|
|
origCurrency,
|
|
err)
|
|
}
|
|
|
|
return fmt.Sprintf("%s%.2f %s (%s%.2f %s)",
|
|
displaySymbol,
|
|
conv,
|
|
displayCurrency,
|
|
origSymbol,
|
|
origPrice,
|
|
origCurrency,
|
|
)
|
|
}
|
|
|
|
func printTickerSummary(result *ticker.Price, protocol string, err error) {
|
|
if err != nil {
|
|
if err == common.ErrNotYetImplemented {
|
|
log.Warnf(log.Ticker, "Failed to get %s ticker. Error: %s\n",
|
|
protocol,
|
|
err)
|
|
return
|
|
}
|
|
log.Errorf(log.Ticker, "Failed to get %s ticker. Error: %s\n",
|
|
protocol,
|
|
err)
|
|
return
|
|
}
|
|
|
|
stats.Add(result.ExchangeName, result.Pair, result.AssetType, result.Last, result.Volume)
|
|
if result.Pair.Quote.IsFiatCurrency() &&
|
|
Bot != nil &&
|
|
result.Pair.Quote != Bot.Config.Currency.FiatDisplayCurrency {
|
|
origCurrency := result.Pair.Quote.Upper()
|
|
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
|
|
result.ExchangeName,
|
|
protocol,
|
|
Bot.FormatCurrency(result.Pair),
|
|
strings.ToUpper(result.AssetType.String()),
|
|
printConvertCurrencyFormat(origCurrency, result.Last, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printConvertCurrencyFormat(origCurrency, result.Ask, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printConvertCurrencyFormat(origCurrency, result.Bid, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printConvertCurrencyFormat(origCurrency, result.High, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printConvertCurrencyFormat(origCurrency, result.Low, Bot.Config.Currency.FiatDisplayCurrency),
|
|
result.Volume)
|
|
} else {
|
|
if result.Pair.Quote.IsFiatCurrency() &&
|
|
Bot != nil &&
|
|
result.Pair.Quote == Bot.Config.Currency.FiatDisplayCurrency {
|
|
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
|
|
result.ExchangeName,
|
|
protocol,
|
|
Bot.FormatCurrency(result.Pair),
|
|
strings.ToUpper(result.AssetType.String()),
|
|
printCurrencyFormat(result.Last, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printCurrencyFormat(result.Ask, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printCurrencyFormat(result.Bid, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printCurrencyFormat(result.High, Bot.Config.Currency.FiatDisplayCurrency),
|
|
printCurrencyFormat(result.Low, Bot.Config.Currency.FiatDisplayCurrency),
|
|
result.Volume)
|
|
} else {
|
|
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f\n",
|
|
result.ExchangeName,
|
|
protocol,
|
|
Bot.FormatCurrency(result.Pair),
|
|
strings.ToUpper(result.AssetType.String()),
|
|
result.Last,
|
|
result.Ask,
|
|
result.Bid,
|
|
result.High,
|
|
result.Low,
|
|
result.Volume)
|
|
}
|
|
}
|
|
}
|
|
|
|
const (
|
|
book = "%s %s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s\n"
|
|
)
|
|
|
|
func printOrderbookSummary(result *orderbook.Base, protocol string, bot *Engine, err error) {
|
|
if err != nil {
|
|
if result == nil {
|
|
log.Errorf(log.OrderBook, "Failed to get %s orderbook. Error: %s\n",
|
|
protocol,
|
|
err)
|
|
return
|
|
}
|
|
if err == common.ErrNotYetImplemented {
|
|
log.Warnf(log.OrderBook, "Failed to get %s orderbook for %s %s %s. Error: %s\n",
|
|
protocol,
|
|
result.Exchange,
|
|
result.Pair,
|
|
result.Asset,
|
|
err)
|
|
return
|
|
}
|
|
log.Errorf(log.OrderBook, "Failed to get %s orderbook for %s %s %s. Error: %s\n",
|
|
protocol,
|
|
result.Exchange,
|
|
result.Pair,
|
|
result.Asset,
|
|
err)
|
|
return
|
|
}
|
|
|
|
bidsAmount, bidsValue := result.TotalBidsAmount()
|
|
asksAmount, asksValue := result.TotalAsksAmount()
|
|
|
|
var bidValueResult, askValueResult string
|
|
switch {
|
|
case result.Pair.Quote.IsFiatCurrency() && bot != nil && result.Pair.Quote != bot.Config.Currency.FiatDisplayCurrency:
|
|
origCurrency := result.Pair.Quote.Upper()
|
|
bidValueResult = printConvertCurrencyFormat(origCurrency, bidsValue, bot.Config.Currency.FiatDisplayCurrency)
|
|
askValueResult = printConvertCurrencyFormat(origCurrency, asksValue, bot.Config.Currency.FiatDisplayCurrency)
|
|
case result.Pair.Quote.IsFiatCurrency() && bot != nil && result.Pair.Quote == bot.Config.Currency.FiatDisplayCurrency:
|
|
bidValueResult = printCurrencyFormat(bidsValue, bot.Config.Currency.FiatDisplayCurrency)
|
|
askValueResult = printCurrencyFormat(asksValue, bot.Config.Currency.FiatDisplayCurrency)
|
|
default:
|
|
bidValueResult = strconv.FormatFloat(bidsValue, 'f', -1, 64)
|
|
askValueResult = strconv.FormatFloat(asksValue, 'f', -1, 64)
|
|
}
|
|
|
|
log.Infof(log.OrderBook, book,
|
|
result.Exchange,
|
|
protocol,
|
|
bot.FormatCurrency(result.Pair),
|
|
strings.ToUpper(result.Asset.String()),
|
|
len(result.Bids),
|
|
bidsAmount,
|
|
result.Pair.Base,
|
|
bidValueResult,
|
|
len(result.Asks),
|
|
asksAmount,
|
|
result.Pair.Base,
|
|
askValueResult,
|
|
)
|
|
}
|
|
|
|
func relayWebsocketEvent(result interface{}, event, assetType, exchangeName string) {
|
|
evt := WebsocketEvent{
|
|
Data: result,
|
|
Event: event,
|
|
AssetType: assetType,
|
|
Exchange: exchangeName,
|
|
}
|
|
err := BroadcastWebsocketMessage(evt)
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "Failed to broadcast websocket event %v. Error: %s\n",
|
|
event, err)
|
|
}
|
|
}
|
|
|
|
// WebsocketRoutine Initial routine management system for websocket
|
|
func (bot *Engine) WebsocketRoutine() {
|
|
if bot.Settings.Verbose {
|
|
log.Debugln(log.WebsocketMgr, "Connecting exchange websocket services...")
|
|
}
|
|
|
|
exchanges := bot.GetExchanges()
|
|
for i := range exchanges {
|
|
go func(i int) {
|
|
if exchanges[i].SupportsWebsocket() {
|
|
if bot.Settings.Verbose {
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Exchange %s websocket support: Yes Enabled: %v\n",
|
|
exchanges[i].GetName(),
|
|
common.IsEnabled(exchanges[i].IsWebsocketEnabled()),
|
|
)
|
|
}
|
|
|
|
ws, err := exchanges[i].GetWebsocket()
|
|
if err != nil {
|
|
log.Errorf(
|
|
log.WebsocketMgr,
|
|
"Exchange %s GetWebsocket error: %s\n",
|
|
exchanges[i].GetName(),
|
|
err,
|
|
)
|
|
return
|
|
}
|
|
|
|
// Exchange sync manager might have already started ws
|
|
// service or is in the process of connecting, so check
|
|
if ws.IsConnected() || ws.IsConnecting() {
|
|
return
|
|
}
|
|
|
|
// Data handler routine
|
|
go bot.WebsocketDataReceiver(ws)
|
|
|
|
if ws.IsEnabled() {
|
|
err = ws.Connect()
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "%v\n", err)
|
|
}
|
|
err = ws.FlushChannels()
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "Failed to subscribe: %v\n", err)
|
|
}
|
|
}
|
|
} else if bot.Settings.Verbose {
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Exchange %s websocket support: No\n",
|
|
exchanges[i].GetName(),
|
|
)
|
|
}
|
|
}(i)
|
|
}
|
|
}
|
|
|
|
var shutdowner = make(chan struct{}, 1)
|
|
var wg sync.WaitGroup
|
|
|
|
// WebsocketDataReceiver handles websocket data coming from a websocket feed
|
|
// associated with an exchange
|
|
func (bot *Engine) WebsocketDataReceiver(ws *stream.Websocket) {
|
|
wg.Add(1)
|
|
defer wg.Done()
|
|
|
|
for {
|
|
select {
|
|
case <-shutdowner:
|
|
return
|
|
case data := <-ws.ToRoutine:
|
|
err := bot.WebsocketDataHandler(ws.GetName(), data)
|
|
if err != nil {
|
|
log.Error(log.WebsocketMgr, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// WebsocketDataHandler is a central point for exchange websocket implementations to send
|
|
// processed data. WebsocketDataHandler will then pass that to an appropriate handler
|
|
func (bot *Engine) WebsocketDataHandler(exchName string, data interface{}) error {
|
|
if data == nil {
|
|
return fmt.Errorf("routines.go - exchange %s nil data sent to websocket",
|
|
exchName)
|
|
}
|
|
|
|
switch d := data.(type) {
|
|
case string:
|
|
log.Info(log.WebsocketMgr, d)
|
|
case error:
|
|
return fmt.Errorf("routines.go exchange %s websocket error - %s", exchName, data)
|
|
case stream.FundingData:
|
|
if bot.Settings.Verbose {
|
|
log.Infof(log.WebsocketMgr, "%s websocket %s %s funding updated %+v",
|
|
exchName,
|
|
bot.FormatCurrency(d.CurrencyPair),
|
|
d.AssetType,
|
|
d)
|
|
}
|
|
case *ticker.Price:
|
|
if bot.Settings.EnableExchangeSyncManager && bot.ExchangeCurrencyPairManager != nil {
|
|
bot.ExchangeCurrencyPairManager.update(exchName,
|
|
d.Pair,
|
|
d.AssetType,
|
|
SyncItemTicker,
|
|
nil)
|
|
}
|
|
err := ticker.ProcessTicker(d)
|
|
printTickerSummary(d, "websocket", err)
|
|
case stream.KlineData:
|
|
if bot.Settings.Verbose {
|
|
log.Infof(log.WebsocketMgr, "%s websocket %s %s kline updated %+v",
|
|
exchName,
|
|
bot.FormatCurrency(d.Pair),
|
|
d.AssetType,
|
|
d)
|
|
}
|
|
case *orderbook.Base:
|
|
if bot.Settings.EnableExchangeSyncManager && bot.ExchangeCurrencyPairManager != nil {
|
|
bot.ExchangeCurrencyPairManager.update(exchName,
|
|
d.Pair,
|
|
d.Asset,
|
|
SyncItemOrderbook,
|
|
nil)
|
|
}
|
|
printOrderbookSummary(d, "websocket", bot, nil)
|
|
case *order.Detail:
|
|
if !bot.OrderManager.orderStore.exists(d) {
|
|
err := bot.OrderManager.orderStore.Add(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
od, err := bot.OrderManager.orderStore.GetByExchangeAndID(d.Exchange, d.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
od.UpdateOrderFromDetail(d)
|
|
}
|
|
case *order.Cancel:
|
|
return bot.OrderManager.Cancel(d)
|
|
case *order.Modify:
|
|
od, err := bot.OrderManager.orderStore.GetByExchangeAndID(d.Exchange, d.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
od.UpdateOrderFromModify(d)
|
|
case order.ClassificationError:
|
|
return errors.New(d.Error())
|
|
case stream.UnhandledMessageWarning:
|
|
log.Warn(log.WebsocketMgr, d.Message)
|
|
default:
|
|
if bot.Settings.Verbose {
|
|
log.Warnf(log.WebsocketMgr,
|
|
"%s websocket Unknown type: %+v",
|
|
exchName,
|
|
d)
|
|
}
|
|
}
|
|
return nil
|
|
}
|