forked from thrasher-corp/gocryptotrader
/
orderbook_types.go
73 lines (61 loc) · 1.87 KB
/
orderbook_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package orderbook
import (
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/nbltrust/gocryptotrader/currency"
"github.com/nbltrust/gocryptotrader/dispatch"
"github.com/nbltrust/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
// Contract variables
LiquidationOrders int64
OrderCount 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 }