-
Notifications
You must be signed in to change notification settings - Fork 122
/
chain_manager.go
80 lines (71 loc) · 1.61 KB
/
chain_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
package ib
import (
"fmt"
"time"
)
// ChainManager .
type ChainManager struct {
AbstractManager
id int64
c Contract
chains OptionChains
}
// NewChainManager .
func NewChainManager(e *Engine, c Contract) (*ChainManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
m := &ChainManager{
AbstractManager: *am,
c: c,
chains: OptionChains{},
}
go m.startMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
}
func (c *ChainManager) preLoop() error {
c.id = c.eng.NextRequestID()
req := &RequestContractData{Contract: c.c}
req.Contract.SecurityType = "OPT"
req.Contract.LocalSymbol = ""
req.SetID(c.id)
c.eng.Subscribe(c.rc, c.id)
return c.eng.Send(req)
}
func (c *ChainManager) preDestroy() {
c.eng.Unsubscribe(c.rc, c.id)
}
func (c *ChainManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *ContractData:
r := r.(*ContractData)
expiry, err := time.Parse("20060102", r.Contract.Summary.Expiry)
if err != nil {
return UpdateFalse, err
}
if _, ok := c.chains[expiry]; !ok {
c.chains[expiry] = &OptionChain{
Expiry: expiry,
Strikes: map[float64]*OptionStrike{},
}
}
c.chains[expiry].update(r)
return UpdateFalse, nil
case *ContractDataEnd:
return UpdateFinish, nil
}
return UpdateFalse, fmt.Errorf("Unexpected type %v", r)
}
// Chains .
func (c *ChainManager) Chains() map[time.Time]*OptionChain {
c.rwm.RLock()
defer c.rwm.RUnlock()
return c.chains
}