-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket.go
66 lines (57 loc) · 1.34 KB
/
market.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
package bittrex
import (
"sync"
)
type Market struct {
sync.Mutex
MarketName string
MarketAsset string
BaseAsset string
Bids BookRowsDescending
Asks BookRowsAscending
Ready bool
LastNonce int
StreamClient *StreamClient
OnError func(error)
OnUpdate func(*Market, MarketUpdate, bool)
}
func NewMarket(baseAsset string, marketAsset string, client *StreamClient) *Market {
market := Market{
BaseAsset: baseAsset,
MarketAsset: marketAsset,
MarketName: baseAsset + "-" + marketAsset,
StreamClient: client,
OnError: func(err error) { panic(err) }, //default error handler
OnUpdate: func(m *Market, mu MarketUpdate, s bool) {}, //default after-update hook
}
client.Markets[market.MarketName] = &market
return &market
}
func (m *Market) Subscribe() error {
err := m.subscribeToDeltas()
if err != nil {
return err
}
err = m.queryBook()
return err
}
func (m *Market) GetBid(depth int) BookRow {
m.Lock()
defer m.Unlock()
row := BookRow{}
if len(m.Bids) > depth {
row.Price = m.Bids[depth].Price
row.Quantity = m.Bids[depth].Quantity
}
return row
}
func (m *Market) GetAsk(depth int) BookRow {
m.Lock()
defer m.Unlock()
row := BookRow{}
if len(m.Asks) > depth {
row.Price = m.Asks[depth].Price
row.Quantity = m.Asks[depth].Quantity
}
return row
}