forked from gofinance/ib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrument_manager.go
89 lines (78 loc) · 1.6 KB
/
instrument_manager.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ib
// InstrumentManager .
type InstrumentManager struct {
AbstractManager
id int64
c Contract
last float64
bid float64
ask float64
}
// NewInstrumentManager .
func NewInstrumentManager(e *Engine, c Contract) (*InstrumentManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
m := &InstrumentManager{
AbstractManager: *am,
c: c,
}
go m.StartMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
}
func (i *InstrumentManager) preLoop() error {
i.id = i.eng.NextRequestID()
req := &RequestMarketData{Contract: i.c}
req.SetID(i.id)
i.eng.Subscribe(i.rc, i.id)
return i.eng.Send(req)
}
func (i *InstrumentManager) preDestroy() {
i.eng.Unsubscribe(i.rc, i.id)
req := &CancelMarketData{}
req.SetID(i.id)
i.eng.Send(req)
}
func (i *InstrumentManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *TickPrice:
r := r.(*TickPrice)
switch r.Type {
case TickLast:
i.last = r.Price
case TickBid:
i.bid = r.Price
case TickAsk:
i.ask = r.Price
}
}
if i.last <= 0 && (i.bid <= 0 || i.ask <= 0) {
return UpdateFalse, nil
}
return UpdateTrue, nil
}
// Bid .
func (i *InstrumentManager) Bid() float64 {
i.rwm.RLock()
defer i.rwm.RUnlock()
return i.bid
}
// Ask .
func (i *InstrumentManager) Ask() float64 {
i.rwm.RLock()
defer i.rwm.RUnlock()
return i.ask
}
// Last .
func (i *InstrumentManager) Last() float64 {
i.rwm.RLock()
defer i.rwm.RUnlock()
return i.last
}