Files
gocryptotrader/exchanges/orderbook/depth.go
Ryan O'Hara-Reid 5dfbbf84de engine/exchanges: Add exchange currency state subsystem (#774)
* state: Add management system (init)

* linter: fix

* engine: gofmt

* gct: after merge fixup

* documentation: add

* rpc: implement services for testing

* gctcli: gofmt state_management.go

* documentation: reinstate lost information

* state: Add pair check to determine trading operation

* exchanges: add interface for specific state scoped subsystem functionality

* engine/order_man: reduce code footprint using new method

* RPC: implement pair trading request and change exported name to something specific to state

* engine: add tests

* engine: Add to withdraw manager

* documentation: reinstate soxipy in contrib. list

* engine: const fake name

* Glorious: NITERINOS

* merge: fix issues

* engine: csm incorporate service name into log output

* engine: fix linter issues

* gct: fix tests

* currencystate: remove management type

* rpc: fix tests

* backtester: fix tests

* Update engine/currency_state_manager.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update engine/currency_state_manager.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/currencystate/currency_state.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/alert/alert.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/alert/alert.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* config: integrate with config and remove flag delay adjustment

* gctcli: fix issues after name changes

* engine: gofmt manager file

* Update engine/rpcserver.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* engine: Add enable/disable manager functions, add default popoulation for potential assets

* linter: fix

* engine/test: bump subsystem count

* Update engine/currency_state_manager.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/bithumb/bithumb.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits addressed

* alert: fix commenting for its generalized purpose

* glorious: nits

* engine: use standard string in log output

* bitfinex: apply patch, thanks @thrasher-

* bitfinex: fix spelling

* engine/currencystate: Add logs/fix logs

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
2021-09-27 13:33:49 +10:00

284 lines
6.5 KiB
Go

package orderbook
import (
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/exchanges/alert"
"github.com/thrasher-corp/gocryptotrader/log"
)
// Depth defines a linked list of orderbook items
type Depth struct {
asks
bids
// unexported stack of nodes
stack *stack
alert.Notice
mux *dispatch.Mux
id uuid.UUID
options
m sync.Mutex
}
// NewDepth returns a new depth item
func newDepth(id uuid.UUID) *Depth {
return &Depth{
stack: newStack(),
id: id,
mux: service.Mux,
}
}
// Publish alerts any subscribed routines using a dispatch mux
func (d *Depth) Publish() {
err := d.mux.Publish([]uuid.UUID{d.id}, d.Retrieve())
if err != nil {
log.Errorf(log.ExchangeSys, "Cannot publish orderbook update to mux %v", err)
}
}
// GetAskLength returns length of asks
func (d *Depth) GetAskLength() int {
d.m.Lock()
defer d.m.Unlock()
return d.asks.length
}
// GetBidLength returns length of bids
func (d *Depth) GetBidLength() int {
d.m.Lock()
defer d.m.Unlock()
return d.bids.length
}
// Retrieve returns the orderbook base a copy of the underlying linked list
// spread
func (d *Depth) Retrieve() *Base {
d.m.Lock()
defer d.m.Unlock()
return &Base{
Bids: d.bids.retrieve(),
Asks: d.asks.retrieve(),
Exchange: d.exchange,
Asset: d.asset,
Pair: d.pair,
LastUpdated: d.lastUpdated,
LastUpdateID: d.lastUpdateID,
PriceDuplication: d.priceDuplication,
IsFundingRate: d.isFundingRate,
VerifyOrderbook: d.VerifyOrderbook,
}
}
// TotalBidAmounts returns the total amount of bids and the total orderbook
// bids value
func (d *Depth) TotalBidAmounts() (liquidity, value float64) {
d.m.Lock()
defer d.m.Unlock()
return d.bids.amount()
}
// TotalAskAmounts returns the total amount of asks and the total orderbook
// asks value
func (d *Depth) TotalAskAmounts() (liquidity, value float64) {
d.m.Lock()
defer d.m.Unlock()
return d.asks.amount()
}
// LoadSnapshot flushes the bids and asks with a snapshot
func (d *Depth) LoadSnapshot(bids, asks []Item, lastUpdateID int64, lastUpdated time.Time, updateByREST bool) {
d.m.Lock()
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.restSnapshot = updateByREST
d.bids.load(bids, d.stack)
d.asks.load(asks, d.stack)
d.Alert()
d.m.Unlock()
}
// Flush flushes the bid and ask depths
func (d *Depth) Flush() {
d.m.Lock()
d.lastUpdateID = 0
d.lastUpdated = time.Time{}
d.bids.load(nil, d.stack)
d.asks.load(nil, d.stack)
d.Alert()
d.m.Unlock()
}
// UpdateBidAskByPrice updates the bid and ask spread by supplied updates, this
// will trim total length of depth level to a specified supplied number
func (d *Depth) UpdateBidAskByPrice(bidUpdts, askUpdts Items, maxDepth int, lastUpdateID int64, lastUpdated time.Time) {
if len(bidUpdts) == 0 && len(askUpdts) == 0 {
return
}
d.m.Lock()
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
tn := getNow()
if len(bidUpdts) != 0 {
d.bids.updateInsertByPrice(bidUpdts, d.stack, maxDepth, tn)
}
if len(askUpdts) != 0 {
d.asks.updateInsertByPrice(askUpdts, d.stack, maxDepth, tn)
}
d.Alert()
d.m.Unlock()
}
// UpdateBidAskByID amends details by ID
func (d *Depth) UpdateBidAskByID(bidUpdts, askUpdts Items, lastUpdateID int64, lastUpdated time.Time) error {
if len(bidUpdts) == 0 && len(askUpdts) == 0 {
return nil
}
d.m.Lock()
defer d.m.Unlock()
if len(bidUpdts) != 0 {
err := d.bids.updateByID(bidUpdts)
if err != nil {
return err
}
}
if len(askUpdts) != 0 {
err := d.asks.updateByID(askUpdts)
if err != nil {
return err
}
}
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.Alert()
return nil
}
// DeleteBidAskByID deletes a price level by ID
func (d *Depth) DeleteBidAskByID(bidUpdts, askUpdts Items, bypassErr bool, lastUpdateID int64, lastUpdated time.Time) error {
if len(bidUpdts) == 0 && len(askUpdts) == 0 {
return nil
}
d.m.Lock()
defer d.m.Unlock()
if len(bidUpdts) != 0 {
err := d.bids.deleteByID(bidUpdts, d.stack, bypassErr)
if err != nil {
return err
}
}
if len(askUpdts) != 0 {
err := d.asks.deleteByID(askUpdts, d.stack, bypassErr)
if err != nil {
return err
}
}
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.Alert()
return nil
}
// InsertBidAskByID inserts new updates
func (d *Depth) InsertBidAskByID(bidUpdts, askUpdts Items, lastUpdateID int64, lastUpdated time.Time) error {
if len(bidUpdts) == 0 && len(askUpdts) == 0 {
return nil
}
d.m.Lock()
defer d.m.Unlock()
if len(bidUpdts) != 0 {
err := d.bids.insertUpdates(bidUpdts, d.stack)
if err != nil {
return err
}
}
if len(askUpdts) != 0 {
err := d.asks.insertUpdates(askUpdts, d.stack)
if err != nil {
return err
}
}
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.Alert()
return nil
}
// UpdateInsertByID updates or inserts by ID at current price level.
func (d *Depth) UpdateInsertByID(bidUpdts, askUpdts Items, lastUpdateID int64, lastUpdated time.Time) error {
if len(bidUpdts) == 0 && len(askUpdts) == 0 {
return nil
}
d.m.Lock()
defer d.m.Unlock()
if len(bidUpdts) != 0 {
err := d.bids.updateInsertByID(bidUpdts, d.stack)
if err != nil {
return err
}
}
if len(askUpdts) != 0 {
err := d.asks.updateInsertByID(askUpdts, d.stack)
if err != nil {
return err
}
}
d.Alert()
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
return nil
}
// AssignOptions assigns the initial options for the depth instance
func (d *Depth) AssignOptions(b *Base) {
d.m.Lock()
d.options = options{
exchange: b.Exchange,
pair: b.Pair,
asset: b.Asset,
lastUpdated: b.LastUpdated,
lastUpdateID: b.LastUpdateID,
priceDuplication: b.PriceDuplication,
isFundingRate: b.IsFundingRate,
VerifyOrderbook: b.VerifyOrderbook,
restSnapshot: b.RestSnapshot,
idAligned: b.IDAlignment,
}
d.m.Unlock()
}
// GetName returns name of exchange
func (d *Depth) GetName() string {
d.m.Lock()
defer d.m.Unlock()
return d.exchange
}
// IsRestSnapshot returns if the depth item was updated via REST
func (d *Depth) IsRestSnapshot() bool {
d.m.Lock()
defer d.m.Unlock()
return d.restSnapshot
}
// LastUpdateID returns the last Update ID
func (d *Depth) LastUpdateID() int64 {
d.m.Lock()
defer d.m.Unlock()
return d.lastUpdateID
}
// IsFundingRate returns if the depth is a funding rate
func (d *Depth) IsFundingRate() bool {
d.m.Lock()
defer d.m.Unlock()
return d.isFundingRate
}