/
profile.go
167 lines (132 loc) · 4.1 KB
/
profile.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
package portfolio
import (
"errors"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// Profile contains multiple portfolios
type Profile struct {
Name string
Cash float64
CostBasis float64
Market *Market
Portfolios []*Portfolio
TargetAllocation map[string]float64
MergedPortfolio *Portfolio
Status *ProfileStatus
}
// ProfileStatus defines the real-time status of the entire portfolio
type ProfileStatus struct {
Value float64
RegularMarketChange float64
RegularMarketChangePercent float64
Unrealized float64
UnrealizedPercent float64
Allocation map[string]float64
}
type profileConfig struct {
Cash cashConfig `yaml:"cash"`
Portfolios []portfolioConfig `yaml:"portfolios"`
}
type cashConfig struct {
Value float64 `yaml:"value"`
TargetAllocation float64 `yaml:"allocation"`
}
// NewProfile returns an empty profile
func NewProfile(name string) *Profile {
return &Profile{
Name: name,
Market: NewMarket(),
Portfolios: make([]*Portfolio, 0),
TargetAllocation: make(map[string]float64),
}
}
// Load loads a profile from the given file
func (profile *Profile) Load(name string) error {
file, err := ioutil.ReadFile(name)
if err != nil {
return err
}
profileConfig := profileConfig{}
err = yaml.Unmarshal(file, &profileConfig)
if err != nil {
return err
}
profile.Cash = profileConfig.Cash.Value
profile.CostBasis = profileConfig.Cash.Value
profile.TargetAllocation["cash"] = profileConfig.Cash.TargetAllocation
totalAllocation := profileConfig.Cash.TargetAllocation
for _, portfolioConfig := range profileConfig.Portfolios {
portfolio := NewPortfolio()
err = portfolio.Load(portfolioConfig)
if err != nil {
return err
}
profile.Portfolios = append(profile.Portfolios, portfolio)
profile.TargetAllocation[portfolio.Name] = portfolioConfig.TargetAllocation
totalAllocation += portfolioConfig.TargetAllocation
profile.CostBasis += portfolio.CostBasis
}
if totalAllocation != 100.0 && totalAllocation != 0.0 {
return errors.New("Total allocation should be either 0% (ignored) or 100%")
}
profile.MergedPortfolio = profile.mergePortfolios()
return nil
}
// Refresh computes the current status of the entire profile and its portfolios
func (profile *Profile) Refresh() error {
for _, portfolio := range profile.Portfolios {
err := portfolio.Refresh()
if err != nil {
return err
}
}
err := profile.MergedPortfolio.Refresh()
if err != nil {
return err
}
profile.RefreshStatus()
return nil
}
// RefreshStatus computes the current status of the entire profile
func (profile *Profile) RefreshStatus() {
status := ProfileStatus{
Allocation: make(map[string]float64),
}
status.Value = profile.Cash
for _, portfolio := range profile.Portfolios {
status.Value += portfolio.Status.Value
status.RegularMarketChange += portfolio.Status.RegularMarketChange
status.Unrealized += portfolio.Status.Unrealized
}
previousValue := status.Value - status.RegularMarketChange
status.RegularMarketChangePercent = 0
if previousValue != 0 {
status.RegularMarketChangePercent = (status.RegularMarketChange / previousValue) * 100
}
status.UnrealizedPercent = 0
if profile.CostBasis != 0 {
status.UnrealizedPercent = (status.Unrealized / profile.CostBasis) * 100
}
for _, portfolio := range profile.Portfolios {
status.Allocation[portfolio.Name] = 0
if status.Value != 0 {
status.Allocation[portfolio.Name] = (portfolio.Status.Value / status.Value) * 100
}
}
status.Allocation["cash"] = (profile.Cash / status.Value) * 100
profile.Status = &status
}
// MergePortfolios merges all portfolios in the profile into a single portfolio
func (profile *Profile) mergePortfolios() *Portfolio {
portfolio := NewPortfolio()
portfolio.Name = profile.Name
for _, port := range profile.Portfolios {
portfolio.CostBasis += port.CostBasis
portfolio.Symbols = append(portfolio.Symbols, port.Symbols...)
for symbol, holding := range port.Holdings {
portfolio.Holdings[symbol] = holding
}
}
return portfolio
}