-
Notifications
You must be signed in to change notification settings - Fork 122
/
advisor_account_manager.go
173 lines (156 loc) · 5.42 KB
/
advisor_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ib
import (
"bytes"
)
// AccountSummary Enum
const (
AccountSummaryTagAccountType = "AccountType"
AccountSummaryTagNetLiquidation = "NetLiquidation"
AccountSummaryTagTotalCashValue = "TotalCashValue"
AccountSummaryTagSettledCash = "SettledCash"
AccountSummaryTagAccruedCash = "AccruedCash"
AccountSummaryTagBuyingPower = "BuyingPower"
AccountSummaryTagEquityWithLoanValue = "EquityWithLoanValue"
AccountSummaryTagPreviousEquityWithLoanValue = "PreviousEquityWithLoanValue"
AccountSummaryTagGrossPositionValue = "GrossPositionValue"
AccountSummaryTagRegTEquity = "RegTEquity"
AccountSummaryTagRegTMargin = "RegTMargin"
AccountSummaryTagSMA = "SMA"
AccountSummaryTagInitMarginReq = "InitMarginReq"
AccountSummaryTagMaintMarginReq = "MaintMarginReq"
AccountSummaryTagAvailableFunds = "AvailableFunds"
AccountSummaryTagExcessLiquidity = "ExcessLiquidity"
AccountSummaryTagCushion = "Cushion"
AccountSummaryTagFullInitMarginReq = "FullInitMarginReq"
AccountSummaryTagFullMaintMarginReq = "FullMaintMarginReq"
AccountSummaryTagFullAvailableFunds = "FullAvailableFunds"
AccountSummaryTagFullExcessLiquidity = "FullExcessLiquidity"
AccountSummaryTagLookAheadNextChange = "LookAheadNextChange"
AccountSummaryTagLookAheadInitMarginReq = "LookAheadInitMarginReq"
AccountSummaryTagLookAheadMaintMarginReq = "LookAheadMaintMarginReq"
AccountSummaryTagLookAheadAvailableFunds = "LookAheadAvailableFunds"
AccountSummaryTagLookAheadExcessLiquidity = "LookAheadExcessLiquidity"
AccountSummaryTagHighestSeverity = "HighestSeverity"
AccountSummaryTagDayTradesRemaining = "DayTradesRemaining"
AccountSummaryTagLeverage = "Leverage"
)
var allTags = [...]string{
AccountSummaryTagAccountType,
AccountSummaryTagNetLiquidation,
AccountSummaryTagTotalCashValue,
AccountSummaryTagSettledCash,
AccountSummaryTagAccruedCash,
AccountSummaryTagBuyingPower,
AccountSummaryTagEquityWithLoanValue,
AccountSummaryTagPreviousEquityWithLoanValue,
AccountSummaryTagGrossPositionValue,
AccountSummaryTagRegTEquity,
AccountSummaryTagRegTMargin,
AccountSummaryTagSMA,
AccountSummaryTagInitMarginReq,
AccountSummaryTagMaintMarginReq,
AccountSummaryTagAvailableFunds,
AccountSummaryTagExcessLiquidity,
AccountSummaryTagCushion,
AccountSummaryTagFullInitMarginReq,
AccountSummaryTagFullMaintMarginReq,
AccountSummaryTagFullAvailableFunds,
AccountSummaryTagFullExcessLiquidity,
AccountSummaryTagLookAheadNextChange,
AccountSummaryTagLookAheadInitMarginReq,
AccountSummaryTagLookAheadMaintMarginReq,
AccountSummaryTagLookAheadAvailableFunds,
AccountSummaryTagLookAheadExcessLiquidity,
AccountSummaryTagHighestSeverity,
AccountSummaryTagDayTradesRemaining,
AccountSummaryTagLeverage,
}
// AdvisorAccountManager tracks advisor-managed account values and portfolios.
// It cannot be used with a non-FA account (use PrimaryAccountManager instead).
type AdvisorAccountManager struct {
AbstractManager
id int64
endMsgs int
values map[AccountSummaryKey]AccountSummary
portfolio map[PositionKey]Position
}
// NewAdvisorAccountManager creates a new AdvisorAccountManager
func NewAdvisorAccountManager(e *Engine) (*AdvisorAccountManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
a := &AdvisorAccountManager{AbstractManager: *am,
id: UnmatchedReplyID,
values: map[AccountSummaryKey]AccountSummary{},
portfolio: map[PositionKey]Position{},
}
go a.startMainLoop(a.preLoop, a.receive, a.preDestroy)
return a, nil
}
func (a *AdvisorAccountManager) preLoop() error {
a.id = a.eng.NextRequestID()
a.eng.Subscribe(a.rc, a.id)
var tags bytes.Buffer
for _, tag := range allTags {
tags.WriteString(tag)
tags.WriteString(",")
}
reqAs := &RequestAccountSummary{}
reqAs.SetID(a.id)
reqAs.Group = "All"
reqAs.Tags = tags.String()
if err := a.eng.Send(reqAs); err != nil {
return err
}
a.eng.Subscribe(a.rc, UnmatchedReplyID)
reqPos := &RequestPositions{}
return a.eng.Send(reqPos)
}
func (a *AdvisorAccountManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *AccountSummary:
t := r.(*AccountSummary)
a.values[t.Key] = *t
return UpdateFalse, nil
case *Position:
t := r.(*Position)
a.portfolio[t.Key] = *t
return UpdateFalse, nil
case *AccountSummaryEnd:
a.endMsgs++
case *PositionEnd:
a.endMsgs++
}
if a.endMsgs == 2 {
return UpdateFinish, nil
}
return UpdateFalse, nil
}
func (a *AdvisorAccountManager) preDestroy() {
a.eng.Unsubscribe(a.rc, a.id)
a.eng.Unsubscribe(a.rc, UnmatchedReplyID)
canAs := &CancelAccountSummary{}
canAs.SetID(a.id)
a.eng.Send(canAs)
canPos := &CancelPositions{}
a.eng.Send(canPos)
}
// Values returns the most recent snapshot of account information.
func (a *AdvisorAccountManager) Values() map[AccountSummaryKey]AccountSummary {
a.rwm.RLock()
defer a.rwm.RUnlock()
return a.values
}
// Portfolio returns the most recent snapshot of account portfolio.
func (a *AdvisorAccountManager) Portfolio() map[PositionKey]Position {
a.rwm.RLock()
defer a.rwm.RUnlock()
return a.portfolio
}