Skip to content

Commit

Permalink
orderbook: Implement initial linked list (thrasher-corp#643)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shazbert authored Apr 23, 2021
1 parent 9973523 commit 7b71870
Show file tree
Hide file tree
Showing 81 changed files with 4,547 additions and 1,593 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
timeout: 2m0s
timeout: 2m30s
issues-exit-code: 1
tests: true
skip-dirs:
Expand Down
6 changes: 3 additions & 3 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ func ({{.Variable}} *{{.CapitalName}}) FetchOrderbook(currency currency.Pair, as
// UpdateOrderbook updates and returns the orderbook for a currency pair
func ({{.Variable}} *{{.CapitalName}}) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
book := &orderbook.Base{
ExchangeName: {{.Variable}}.Name,
Exchange: {{.Variable}}.Name,
Pair: p,
AssetType: assetType,
VerificationBypass: {{.Variable}}.OrderbookVerificationBypass,
Asset: assetType,
VerifyOrderbook: {{.Variable}}.CanVerifyOrderbook,
}

// NOTE: UPDATE ORDERBOOK EXAMPLE
Expand Down
21 changes: 12 additions & 9 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ func validateSettings(b *Engine, s *Settings, flagSet map[string]bool) {
b.Settings.EnableOrderbookSyncing = s.EnableOrderbookSyncing
b.Settings.EnableTradeSyncing = s.EnableTradeSyncing
b.Settings.SyncWorkers = s.SyncWorkers
b.Settings.SyncTimeout = s.SyncTimeout
b.Settings.SyncTimeoutREST = s.SyncTimeoutREST
b.Settings.SyncTimeoutWebsocket = s.SyncTimeoutWebsocket
b.Settings.SyncContinuously = s.SyncContinuously
b.Settings.EnableDepositAddressManager = s.EnableDepositAddressManager
b.Settings.EnableExchangeAutoPairUpdates = s.EnableExchangeAutoPairUpdates
Expand Down Expand Up @@ -305,7 +306,8 @@ func PrintSettings(s *Settings) {
gctlog.Debugf(gctlog.Global, "\t Enable ticker syncing: %v\n", s.EnableTickerSyncing)
gctlog.Debugf(gctlog.Global, "\t Enable orderbook syncing: %v\n", s.EnableOrderbookSyncing)
gctlog.Debugf(gctlog.Global, "\t Enable trade syncing: %v\n", s.EnableTradeSyncing)
gctlog.Debugf(gctlog.Global, "\t Exchange sync timeout: %v\n", s.SyncTimeout)
gctlog.Debugf(gctlog.Global, "\t Exchange REST sync timeout: %v\n", s.SyncTimeoutREST)
gctlog.Debugf(gctlog.Global, "\t Exchange Websocket sync timeout: %v\n", s.SyncTimeoutWebsocket)
gctlog.Debugf(gctlog.Global, "- FOREX SETTINGS:")
gctlog.Debugf(gctlog.Global, "\t Enable currency conveter: %v", s.EnableCurrencyConverter)
gctlog.Debugf(gctlog.Global, "\t Enable currency layer: %v", s.EnableCurrencyLayer)
Expand Down Expand Up @@ -464,13 +466,14 @@ func (bot *Engine) Start() error {

if bot.Settings.EnableExchangeSyncManager {
exchangeSyncCfg := CurrencyPairSyncerConfig{
SyncTicker: bot.Settings.EnableTickerSyncing,
SyncOrderbook: bot.Settings.EnableOrderbookSyncing,
SyncTrades: bot.Settings.EnableTradeSyncing,
SyncContinuously: bot.Settings.SyncContinuously,
NumWorkers: bot.Settings.SyncWorkers,
Verbose: bot.Settings.Verbose,
SyncTimeout: bot.Settings.SyncTimeout,
SyncTicker: bot.Settings.EnableTickerSyncing,
SyncOrderbook: bot.Settings.EnableOrderbookSyncing,
SyncTrades: bot.Settings.EnableTradeSyncing,
SyncContinuously: bot.Settings.SyncContinuously,
NumWorkers: bot.Settings.SyncWorkers,
Verbose: bot.Settings.Verbose,
SyncTimeoutREST: bot.Settings.SyncTimeoutREST,
SyncTimeoutWebsocket: bot.Settings.SyncTimeoutWebsocket,
}

bot.ExchangeCurrencyPairManager, err = NewCurrencyPairSyncer(exchangeSyncCfg)
Expand Down
3 changes: 2 additions & 1 deletion engine/engine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type Settings struct {
EnableTradeSyncing bool
SyncWorkers int
SyncContinuously bool
SyncTimeout time.Duration
SyncTimeoutREST time.Duration
SyncTimeoutWebsocket time.Duration

// Forex settings
EnableCurrencyConverter bool
Expand Down
10 changes: 5 additions & 5 deletions engine/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ func TestProcessOrderbook(t *testing.T) {

// now populate it with a 0 entry
o := orderbook.Base{
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []orderbook.Item{{Amount: 24, Price: 23}},
Asks: []orderbook.Item{{Amount: 24, Price: 23}},
ExchangeName: e.Exchange,
AssetType: e.Asset,
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []orderbook.Item{{Amount: 24, Price: 23}},
Asks: []orderbook.Item{{Amount: 24, Price: 23}},
Exchange: e.Exchange,
Asset: e.Asset,
}
if err := o.Process(); err != nil {
t.Fatal("unexpected result:", err)
Expand Down
8 changes: 4 additions & 4 deletions engine/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ func TestGetSpecificOrderbook(t *testing.T) {
bids = append(bids, orderbook.Item{Price: 1000, Amount: 1})

base := orderbook.Base{
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: bids,
ExchangeName: "Bitstamp",
AssetType: asset.Spot,
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: bids,
Exchange: "Bitstamp",
Asset: asset.Spot,
}

err := base.Process()
Expand Down
14 changes: 7 additions & 7 deletions engine/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ func printOrderbookSummary(result *orderbook.Base, protocol string, bot *Engine,
if err == common.ErrNotYetImplemented {
log.Warnf(log.OrderBook, "Failed to get %s orderbook for %s %s %s. Error: %s\n",
protocol,
result.ExchangeName,
result.Exchange,
result.Pair,
result.AssetType,
result.Asset,
err)
return
}
log.Errorf(log.OrderBook, "Failed to get %s orderbook for %s %s %s. Error: %s\n",
protocol,
result.ExchangeName,
result.Exchange,
result.Pair,
result.AssetType,
result.Asset,
err)
return
}
Expand All @@ -165,10 +165,10 @@ func printOrderbookSummary(result *orderbook.Base, protocol string, bot *Engine,
}

log.Infof(log.OrderBook, book,
result.ExchangeName,
result.Exchange,
protocol,
bot.FormatCurrency(result.Pair),
strings.ToUpper(result.AssetType.String()),
strings.ToUpper(result.Asset.String()),
len(result.Bids),
bidsAmount,
result.Pair.Base,
Expand Down Expand Up @@ -317,7 +317,7 @@ func (bot *Engine) WebsocketDataHandler(exchName string, data interface{}) error
if bot.Settings.EnableExchangeSyncManager && bot.ExchangeCurrencyPairManager != nil {
bot.ExchangeCurrencyPairManager.update(exchName,
d.Pair,
d.AssetType,
d.Asset,
SyncItemOrderbook,
nil)
}
Expand Down
4 changes: 2 additions & 2 deletions engine/routines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func TestHandleData(t *testing.T) {
}

err = b.WebsocketDataHandler(exchName, &orderbook.Base{
ExchangeName: fakePassExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
Exchange: fakePassExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
})
if err != nil {
t.Error(err)
Expand Down
59 changes: 25 additions & 34 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,45 +1672,37 @@ func (s *RPCServer) GetOrderbookStream(r *gctrpc.GetOrderbookStreamRequest, stre
return err
}

pipe, err := orderbook.SubscribeOrderbook(r.Exchange, p, a)
depth, err := orderbook.GetDepth(r.Exchange, p, a)
if err != nil {
return err
}

defer pipe.Release()

for {
data, ok := <-pipe.C
if !ok {
return errDispatchSystem
}

ob := (*data.(*interface{})).(orderbook.Base)
var bids, asks []*gctrpc.OrderbookItem
for i := range ob.Bids {
bids = append(bids, &gctrpc.OrderbookItem{
Amount: ob.Bids[i].Amount,
Price: ob.Bids[i].Price,
Id: ob.Bids[i].ID,
})
}
for i := range ob.Asks {
asks = append(asks, &gctrpc.OrderbookItem{
Amount: ob.Asks[i].Amount,
Price: ob.Asks[i].Price,
Id: ob.Asks[i].ID,
})
base := depth.Retrieve()
bids := make([]*gctrpc.OrderbookItem, len(base.Bids))
for i := range base.Bids {
bids[i] = &gctrpc.OrderbookItem{
Amount: base.Bids[i].Amount,
Price: base.Bids[i].Price,
Id: base.Bids[i].ID}
}
asks := make([]*gctrpc.OrderbookItem, len(base.Asks))
for i := range base.Asks {
asks[i] = &gctrpc.OrderbookItem{
Amount: base.Asks[i].Amount,
Price: base.Asks[i].Price,
Id: base.Asks[i].ID}
}
err := stream.Send(&gctrpc.OrderbookResponse{
Pair: &gctrpc.CurrencyPair{Base: ob.Pair.Base.String(),
Quote: ob.Pair.Quote.String()},
Pair: &gctrpc.CurrencyPair{Base: r.Pair.Base, Quote: r.Pair.Quote},
Bids: bids,
Asks: asks,
AssetType: ob.AssetType.String(),
AssetType: r.AssetType,
})
if err != nil {
return err
}
<-depth.Wait(nil)
}
}

Expand All @@ -1734,27 +1726,26 @@ func (s *RPCServer) GetExchangeOrderbookStream(r *gctrpc.GetExchangeOrderbookStr
}

ob := (*data.(*interface{})).(orderbook.Base)
var bids, asks []*gctrpc.OrderbookItem
bids := make([]*gctrpc.OrderbookItem, len(ob.Bids))
for i := range ob.Bids {
bids = append(bids, &gctrpc.OrderbookItem{
bids[i] = &gctrpc.OrderbookItem{
Amount: ob.Bids[i].Amount,
Price: ob.Bids[i].Price,
Id: ob.Bids[i].ID,
})
Id: ob.Bids[i].ID}
}
asks := make([]*gctrpc.OrderbookItem, len(ob.Asks))
for i := range ob.Asks {
asks = append(asks, &gctrpc.OrderbookItem{
asks[i] = &gctrpc.OrderbookItem{
Amount: ob.Asks[i].Amount,
Price: ob.Asks[i].Price,
Id: ob.Asks[i].ID,
})
Id: ob.Asks[i].ID}
}
err := stream.Send(&gctrpc.OrderbookResponse{
Pair: &gctrpc.CurrencyPair{Base: ob.Pair.Base.String(),
Quote: ob.Pair.Quote.String()},
Bids: bids,
Asks: asks,
AssetType: ob.AssetType.String(),
AssetType: ob.Asset.String(),
})
if err != nil {
return err
Expand Down
Loading

0 comments on commit 7b71870

Please sign in to comment.