Skip to content

Commit

Permalink
Bugfixes: Bitfinex websocket, ZB market response and portfolio (thras…
Browse files Browse the repository at this point in the history
…her-corp#397)

* bug fix for websocket orderbook processing

* Fix more panics

* fix linter issue

* kick panic can down the road

* temp fix for issue with a 404 returned error as chainz.cryptoid dropped eth support

* Address nits and fixed orderbook updating

* Fix trade data, rm'd event time from struct

* fix time conversion for huobi

* Actually process kline data and fix time stamps

* btse time conversion fix and RM log, as it seems that the gain is reflecting transaction side. Drop ticker fetching support because there does not seem to be support on docs. And added trade fetching support.

* revert huobi println

* Adressed suggestion

* rm unnecessary assignment

* rm unnecessary check and assign

* fix conversion mishap

* fix currency conversion bug

* update websocket logging

* RM websocket type which stops conversion and copy

* fix linter issue, add in unknown side type
  • Loading branch information
shazbert authored and thrasher- committed Dec 19, 2019
1 parent 467d8d9 commit 98a277a
Show file tree
Hide file tree
Showing 30 changed files with 497 additions and 413 deletions.
20 changes: 14 additions & 6 deletions engine/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (p *portfolioManager) Start() error {
Bot.Portfolio = &portfolio.Portfolio
Bot.Portfolio.Seed(Bot.Config.Portfolio)
p.shutdown = make(chan struct{})
portfolio.Verbose = Bot.Settings.Verbose

go p.run()
return nil
}
Expand Down Expand Up @@ -73,13 +75,19 @@ func (p *portfolioManager) processPortfolio() {
pf := portfolio.GetPortfolio()
data := pf.GetPortfolioGroupedCoin()
for key, value := range data {
success := pf.UpdatePortfolio(value, key)
if success {
log.Debugf(log.PortfolioMgr,
"Portfolio manager: Successfully updated address balance for %s address(es) %s\n",
key, value,
)
err := pf.UpdatePortfolio(value, key)
if err != nil {
log.Errorf(log.PortfolioMgr,
"PortfolioWatcher error %s for currency %s\n",
err,
key)
continue
}

log.Debugf(log.PortfolioMgr,
"Portfolio manager: Successfully updated address balance for %s address(es) %s\n",
key,
value)
}
SeedExchangeAccountInfo(GetAllEnabledExchangeAccountInfo().Data)
}
137 changes: 72 additions & 65 deletions engine/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ func printConvertCurrencyFormat(origCurrency currency.Code, origPrice float64) s
)
}

func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.Item, exchangeName string, err error) {
func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.Item, exchangeName, protocol string, err error) {
if err != nil {
log.Errorf(log.Ticker, "Failed to get %s %s ticker. Error: %s\n",
p.String(),
log.Errorf(log.Ticker, "Failed to get %s %s %s %s ticker. Error: %s\n",
exchangeName,
protocol,
p,
assetType,
err)
return
}
Expand All @@ -70,9 +72,10 @@ func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.I
if p.Quote.IsFiatCurrency() &&
p.Quote != Bot.Config.Currency.FiatDisplayCurrency {
origCurrency := p.Quote.Upper()
log.Infof(log.Ticker, "%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
printConvertCurrencyFormat(origCurrency, result.Last),
printConvertCurrencyFormat(origCurrency, result.Ask),
Expand All @@ -83,9 +86,10 @@ func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.I
} else {
if p.Quote.IsFiatCurrency() &&
p.Quote == Bot.Config.Currency.FiatDisplayCurrency {
log.Infof(log.Ticker, "%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
printCurrencyFormat(result.Last),
printCurrencyFormat(result.Ask),
Expand All @@ -94,9 +98,10 @@ func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.I
printCurrencyFormat(result.Low),
result.Volume)
} else {
log.Infof(log.Ticker, "%s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f\n",
log.Infof(log.Ticker, "%s %s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
result.Last,
result.Ask,
Expand All @@ -108,11 +113,12 @@ func printTickerSummary(result *ticker.Price, p currency.Pair, assetType asset.I
}
}

func printOrderbookSummary(result *orderbook.Base, p currency.Pair, assetType asset.Item, exchangeName string, err error) {
func printOrderbookSummary(result *orderbook.Base, p currency.Pair, assetType asset.Item, exchangeName, protocol string, err error) {
if err != nil {
log.Errorf(log.OrderBook, "Failed to get %s %s orderbook of type %s. Error: %s\n",
p,
log.Errorf(log.OrderBook, "Failed to get %s %s %s orderbook of type %s. Error: %s\n",
exchangeName,
protocol,
p,
assetType,
err)
return
Expand All @@ -124,47 +130,50 @@ func printOrderbookSummary(result *orderbook.Base, p currency.Pair, assetType as
if p.Quote.IsFiatCurrency() &&
p.Quote != Bot.Config.Currency.FiatDisplayCurrency {
origCurrency := p.Quote.Upper()
log.Infof(log.OrderBook, "%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s\n",
log.Infof(log.OrderBook, "%s %s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
len(result.Bids),
bidsAmount,
p.Base.String(),
p.Base,
printConvertCurrencyFormat(origCurrency, bidsValue),
len(result.Asks),
asksAmount,
p.Base.String(),
p.Base,
printConvertCurrencyFormat(origCurrency, asksValue),
)
} else {
if p.Quote.IsFiatCurrency() &&
p.Quote == Bot.Config.Currency.FiatDisplayCurrency {
log.Infof(log.OrderBook, "%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s\n",
log.Infof(log.OrderBook, "%s %s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
len(result.Bids),
bidsAmount,
p.Base.String(),
p.Base,
printCurrencyFormat(bidsValue),
len(result.Asks),
asksAmount,
p.Base.String(),
p.Base,
printCurrencyFormat(asksValue),
)
} else {
log.Infof(log.OrderBook, "%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f\n",
log.Infof(log.OrderBook, "%s %s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f\n",
exchangeName,
FormatCurrency(p).String(),
protocol,
FormatCurrency(p),
strings.ToUpper(assetType.String()),
len(result.Bids),
bidsAmount,
p.Base.String(),
p.Base,
bidsValue,
len(result.Asks),
asksAmount,
p.Base.String(),
p.Base,
asksValue,
)
}
Expand Down Expand Up @@ -278,73 +287,71 @@ func WebsocketDataHandler(ws *wshandler.Websocket) {
log.Warnf(log.WebsocketMgr, "routines.go warning - exchange %s websocket not enabled\n",
ws.GetName())
}

default:
log.Info(log.WebsocketMgr, d)
}

case error:
log.Errorf(log.WebsocketMgr, "routines.go exchange %s websocket error - %s", ws.GetName(), data)
case wshandler.TradeData:
// Trade Data
// if Bot.Settings.Verbose {
// log.Println("Websocket trades Updated: ", data.(exchange.TradeData))
// }

case wshandler.TickerData:
// Ticker data
// if Bot.Settings.Verbose {
// log.Println("Websocket Ticker Updated: ", data.(exchange.TickerData))
// }

tickerNew := ticker.Price{
Last: d.Last,
High: d.High,
Low: d.Low,
Bid: d.Bid,
Ask: d.Ask,
Volume: d.Volume,
QuoteVolume: d.QuoteVolume,
PriceATH: d.PriceATH,
Open: d.Open,
Close: d.Close,
Pair: d.Pair,
LastUpdated: d.Timestamp,
// Websocket Trade Data
if Bot.Settings.Verbose {
log.Infof(log.WebsocketMgr, "%s websocket %s %s trade updated %+v\n",
ws.GetName(),
FormatCurrency(d.CurrencyPair),
d.AssetType,
d)
}
case wshandler.FundingData:
// Websocket Funding Data
if Bot.Settings.Verbose {
log.Infof(log.WebsocketMgr, "%s websocket %s %s funding updated %+v\n",
ws.GetName(),
FormatCurrency(d.CurrencyPair),
d.AssetType,
d)
}
case *ticker.Price:
// Websocket Ticker Data
if Bot.Settings.EnableExchangeSyncManager && Bot.ExchangeCurrencyPairManager != nil {
Bot.ExchangeCurrencyPairManager.update(ws.GetName(),
d.Pair, d.AssetType, SyncItemTicker, nil)
d.Pair,
d.AssetType,
SyncItemTicker,
nil)
}
err := ticker.ProcessTicker(ws.GetName(), &tickerNew, d.AssetType)
if err != nil {
log.Errorf(log.WebsocketMgr, "routines.go exchange %s websocket error - %s", ws.GetName(), err)
}
printTickerSummary(&tickerNew, tickerNew.Pair, d.AssetType, ws.GetName(), nil)
err := ticker.ProcessTicker(ws.GetName(), d, d.AssetType)
printTickerSummary(d, d.Pair, d.AssetType, ws.GetName(), "websocket", err)
case wshandler.KlineData:
// Kline data
// Websocket Kline Data
if Bot.Settings.Verbose {
log.Infof(log.WebsocketMgr, "Websocket Kline Updated: %v\n", d)
log.Infof(log.WebsocketMgr, "%s websocket %s %s kline updated %+v\n",
ws.GetName(),
FormatCurrency(d.Pair),
d.AssetType,
d)
}
case wshandler.WebsocketOrderbookUpdate:
// Orderbook data
// Websocket Orderbook Data
result := data.(wshandler.WebsocketOrderbookUpdate)
if Bot.Settings.EnableExchangeSyncManager && Bot.ExchangeCurrencyPairManager != nil {
Bot.ExchangeCurrencyPairManager.update(ws.GetName(),
result.Pair, result.Asset, SyncItemOrderbook, nil)
result.Pair,
result.Asset,
SyncItemOrderbook,
nil)
}
// TO-DO: printOrderbookSummary
//nolint:gocritic

if Bot.Settings.Verbose {
log.Infof(log.WebsocketMgr,
"Websocket %s %s orderbook updated\n",
"%s websocket %s %s orderbook updated\n",
ws.GetName(),
FormatCurrency(result.Pair),
)
d.Asset)
}
default:
if Bot.Settings.Verbose {
log.Warnf(log.WebsocketMgr,
"Websocket %s Unknown type: %v\n",
"%s websocket Unknown type: %+v\n",
ws.GetName(),
d)
}
Expand Down
4 changes: 2 additions & 2 deletions engine/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (e *ExchangeCurrencyPairSyncer) worker() {
} else {
result, err = Bot.Exchanges[x].UpdateTicker(c.Pair, c.AssetType)
}
printTickerSummary(result, c.Pair, c.AssetType, exchangeName, err)
printTickerSummary(result, c.Pair, c.AssetType, exchangeName, "REST", err)
if err == nil {
//nolint:gocritic Bot.CommsRelayer.StageTickerData(exchangeName, c.AssetType, result)
if Bot.Config.RemoteControl.WebsocketRPC.Enabled {
Expand Down Expand Up @@ -453,7 +453,7 @@ func (e *ExchangeCurrencyPairSyncer) worker() {

e.setProcessing(c.Exchange, c.Pair, c.AssetType, SyncItemOrderbook, true)
result, err := Bot.Exchanges[x].UpdateOrderbook(c.Pair, c.AssetType)
printOrderbookSummary(result, c.Pair, c.AssetType, exchangeName, err)
printOrderbookSummary(result, c.Pair, c.AssetType, exchangeName, "REST", err)
if err == nil {
//nolint:gocritic Bot.CommsRelayer.StageOrderbookData(exchangeName, c.AssetType, result)
if Bot.Config.RemoteControl.WebsocketRPC.Enabled {
Expand Down
10 changes: 5 additions & 5 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ type OrderbookItem struct {

// OrderBookData is resp data from orderbook endpoint
type OrderBookData struct {
Code int `json:"code"`
Msg string `json:"msg"`
LastUpdateID int64 `json:"lastUpdateId"`
Bids [][]string `json:"bids"`
Asks [][]string `json:"asks"`
Code int `json:"code"`
Msg string `json:"msg"`
LastUpdateID int64 `json:"lastUpdateId"`
Bids [][2]string `json:"bids"`
Asks [][2]string `json:"asks"`
}

// OrderBook actual structured data that can be used for orderbook
Expand Down
Loading

0 comments on commit 98a277a

Please sign in to comment.