-
Notifications
You must be signed in to change notification settings - Fork 122
/
primary_account_manager.go
145 lines (128 loc) · 3.57 KB
/
primary_account_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ib
import (
"fmt"
)
// PrimaryAccountManager tracks the primary IB account's values and portfolio,
// along with all FA sub-accounts. FA accounts may also consider using
// AdvisorAccountManager, although the latter will not report position P&Ls.
type PrimaryAccountManager struct {
AbstractManager
id int64
accountCode []string
unsubscribe string
values map[AccountValueKey]AccountValue
portfolio map[PortfolioValueKey]PortfolioValue
}
// NewPrimaryAccountManager .
func NewPrimaryAccountManager(e *Engine) (*PrimaryAccountManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
p := &PrimaryAccountManager{AbstractManager: *am,
id: UnmatchedReplyID,
values: map[AccountValueKey]AccountValue{},
portfolio: map[PortfolioValueKey]PortfolioValue{},
}
go p.startMainLoop(p.preLoop, p.receive, p.preDestroy)
return p, nil
}
func (p *PrimaryAccountManager) preLoop() error {
p.eng.Subscribe(p.rc, p.id)
// To address if being run under an FA account, request our accounts
// (the 321 warning-level error will be ignored for non-FA accounts)
return p.eng.Send(&RequestManagedAccounts{})
}
func (p *PrimaryAccountManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *AccountDownloadEnd:
finished, err := p.nextAccount()
if err != nil {
return UpdateFalse, err
}
if finished {
return UpdateTrue, nil
}
return UpdateFalse, nil
case *NextValidID:
return UpdateFalse, nil
case *AccountUpdateTime:
return UpdateFalse, nil
case *AccountValue:
t := r.(*AccountValue)
p.values[t.Key] = *t
return UpdateFalse, nil
case *PortfolioValue:
t := r.(*PortfolioValue)
p.portfolio[t.Key] = *t
return UpdateFalse, nil
case *ManagedAccounts:
t := r.(*ManagedAccounts)
if len(t.AccountsList) == 0 {
return UpdateFalse, fmt.Errorf("goib: account manager found no accounts")
}
// Refine the request so we don't block if an FA login
p.accountCode = t.AccountsList
p.nextAccount()
return UpdateFalse, nil
}
return UpdateFalse, fmt.Errorf("Unexpected type %v", r)
}
// nextAccount requests the next FA account, unsubscribing from any previous
// request and returning true if no more accounts are remaining.
func (p *PrimaryAccountManager) nextAccount() (bool, error) {
if p.unsubscribe != "" {
req := &RequestAccountUpdates{}
req.Subscribe = false
req.AccountCode = p.unsubscribe
if err := p.eng.Send(req); err != nil {
return true, err
}
}
next := ""
replace := []string{}
for _, acct := range p.accountCode {
if next == "" {
next = acct
} else {
replace = append(replace, acct)
}
}
p.accountCode = replace
p.unsubscribe = next
if next == "" {
return true, nil
}
req := &RequestAccountUpdates{}
req.Subscribe = true
req.AccountCode = next
if err := p.eng.Send(req); err != nil {
return true, err
}
return false, nil
}
func (p *PrimaryAccountManager) preDestroy() {
p.eng.Unsubscribe(p.rc, p.id)
req := &RequestAccountUpdates{}
req.Subscribe = false
req.AccountCode = p.unsubscribe
p.eng.Send(req)
}
// Values returns the most recent snapshot of account information.
func (p *PrimaryAccountManager) Values() map[AccountValueKey]AccountValue {
p.rwm.RLock()
defer p.rwm.RUnlock()
return p.values
}
// Portfolio returns the most recent snapshot of account portfolio.
func (p *PrimaryAccountManager) Portfolio() map[PortfolioValueKey]PortfolioValue {
p.rwm.RLock()
defer p.rwm.RUnlock()
return p.portfolio
}