-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio_test.go
336 lines (308 loc) · 7.91 KB
/
portfolio_test.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package portfolio_test
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/portfoliotree/portfolio"
"github.com/portfoliotree/portfolio/returns"
)
func ExampleParse() {
// language=yaml
specYAML := `
---
type: Portfolio
metadata:
name: 60/40
benchmark: BIGPX
spec:
assets: [ACWI, AGG]
policy:
weights: [60, 40]
weights_algorithm: Constant Weights
rebalancing_interval: Quarterly
`
pf, err := portfolio.ParseOneDocument(specYAML)
if err != nil {
panic(err)
}
fmt.Println("Name:", pf.Metadata.Name)
fmt.Println("Alg:", pf.Spec.Policy.WeightsAlgorithm)
// Output:
// Name: 60/40
// Alg: Constant Weights
}
func ExampleOpen() {
portfolios, err := portfolio.ParseSpecificationFile(filepath.Join("examples", "60-40_portfolio.yml"))
if err != nil {
panic(err)
}
pf := portfolios[0]
fmt.Println("Name:", pf.Metadata.Name)
fmt.Println("Alg:", pf.Spec.Policy.WeightsAlgorithm)
// Output:
// Name: 60/40
// Alg: Constant Weights
}
func TestParse(t *testing.T) {
for _, tt := range []struct {
Name string
SpecYAML string
ErrorStringContains string
Portfolio portfolio.Specification
}{
{
Name: "invalid yaml",
SpecYAML: "---}",
ErrorStringContains: "yaml: unmarshal errors",
},
{
Name: "wrong type",
// language=yaml
SpecYAML: `type: Banana`,
ErrorStringContains: "incorrect specification type",
},
{
Name: "the number of assets and policy weights do not match",
// language=yaml
SpecYAML: `{type: Portfolio, spec: {assets: ["a"], policy: {weights: [1, 2]}}}`,
ErrorStringContains: "expected the number of policy weights to be the same as the number of assets",
},
{
Name: "component field is invalid",
// language=yaml
SpecYAML: `{type: Portfolio, spec: {benchmark: {id: []}}}`,
ErrorStringContains: "yaml: unmarshal errors:",
},
{
Name: "empty input",
// language=yaml
SpecYAML: ``,
ErrorStringContains: "exactly one portfolio",
},
{
Name: "empty input",
// language=yaml
SpecYAML: `
{type: Portfolio}
---
{type: Portfolio}`,
ErrorStringContains: "exactly one portfolio",
},
{
Name: "component kind is not correct",
// language=yaml
SpecYAML: `{type: Portfolio, metadata: {benchmark: []}}`,
ErrorStringContains: "wrong YAML type:",
},
} {
t.Run(tt.Name, func(t *testing.T) {
p, err := portfolio.ParseOneDocument(tt.SpecYAML)
if tt.ErrorStringContains == "" {
assert.NoError(t, err)
assert.Equal(t, tt.Portfolio, p)
} else {
assert.ErrorContains(t, err, tt.ErrorStringContains)
}
})
}
}
func ExampleSpecification_Backtest() {
// language=yaml
portfolioSpecYAML := `---
type: Portfolio
metadata:
name: 60/40
benchmark: BIGPX
spec:
assets: [ACWI, AGG]
policy:
weights: [60, 40]
weights_algorithm: ConstantWeights
rebalancing_interval: Quarterly
`
pf, err := portfolio.ParseOneDocument(portfolioSpecYAML)
if err != nil {
panic(err)
}
ctx := context.Background()
assets, err := pf.Spec.AssetReturns(ctx)
if err != nil {
panic(err)
}
result, err := pf.Spec.Backtest(ctx, assets, nil)
if err != nil {
panic(err)
}
portfolioReturns := result.Returns()
fmt.Printf("Annualized Risk: %.2f%%\n", portfolioReturns.AnnualizedRisk()*100)
fmt.Printf("Annualized Return: %.2f%%\n", portfolioReturns.AnnualizedArithmeticReturn()*100)
fmt.Printf("Backtest start date: %s\n", result.ReturnsTable.FirstTime().Format(time.DateOnly))
fmt.Printf("Backtest end date: %s\n", result.ReturnsTable.LastTime().Format(time.DateOnly))
// Output:
// Annualized Risk: 11.46%
// Annualized Return: 5.10%
// Backtest start date: 2008-03-31
// Backtest end date: 2023-06-14
}
func TestPortfolio_Backtest(t *testing.T) {
for _, tt := range []struct {
Name string
PortfolioSpecFilePath string
Portfolio portfolio.Specification
ctx context.Context
ErrorSubstring string
}{
{
Name: "wrong number of weights",
Portfolio: portfolio.Specification{
Assets: []portfolio.Component{{ID: "AAPL"}},
Policy: portfolio.Policy{
Weights: []float64{50, 50},
WeightsAlgorithm: "Constant Weights",
},
},
ctx: context.Background(),
ErrorSubstring: "expected the number of policy weights to be the same as the number of assets",
},
{
Name: "unknown policy algorithm",
Portfolio: portfolio.Specification{
Assets: []portfolio.Component{{ID: "AAPL"}},
Policy: portfolio.Policy{
Weights: []float64{50, 50},
WeightsAlgorithm: "unknown",
},
},
ctx: context.Background(),
ErrorSubstring: `unknown algorithm`,
},
} {
t.Run(tt.Name, func(t *testing.T) {
pf := tt.Portfolio
_, err := pf.Backtest(tt.ctx, returns.NewTable([]returns.List{{}}), nil)
if tt.ErrorSubstring == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.ErrorSubstring)
}
})
}
}
func TestPortfolio_Backtest_custom_function(t *testing.T) {
_, err := (&portfolio.Specification{
Assets: []portfolio.Component{
{ID: "AAPL"},
{ID: "GOOG"},
},
}).Backtest(context.Background(), returns.NewTable([]returns.List{{}}), ErrorAlg{})
assert.EqualError(t, err, "lemon")
}
func Test_Portfolio_Validate(t *testing.T) {
for _, tt := range []struct {
Name string
Portfolio portfolio.Document
ExpectErr bool
}{
{
Name: "okay", Portfolio: portfolio.Document{
Type: "Portfolio",
}, ExpectErr: false,
},
{
Name: "bad asset",
Portfolio: portfolio.Document{
Spec: portfolio.Specification{
Assets: []portfolio.Component{{ID: "_"}},
},
},
ExpectErr: true,
},
{
Name: "benchmark",
Portfolio: portfolio.Document{
Metadata: portfolio.Metadata{
Benchmark: portfolio.Component{ID: "()"},
},
},
ExpectErr: true,
},
} {
t.Run(tt.Name, func(t *testing.T) {
err := tt.Portfolio.Validate()
if tt.ExpectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestPortfolio_RemoveAsset(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var zero portfolio.Specification
require.Error(t, zero.RemoveAsset(0))
})
t.Run("empty", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{},
}
require.Error(t, pf.RemoveAsset(0))
})
t.Run("remove one", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "banana"},
},
}
require.NoError(t, pf.RemoveAsset(0))
require.Len(t, pf.Assets, 0)
})
t.Run("remove one keep first", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "orange"},
{ID: "banana"},
},
}
require.NoError(t, pf.RemoveAsset(1))
require.Equal(t, []portfolio.Component{{ID: "orange"}}, pf.Assets)
})
t.Run("remove one keep last", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "banana"},
{ID: "orange"},
},
}
require.NoError(t, pf.RemoveAsset(0))
require.Equal(t, []portfolio.Component{{ID: "orange"}}, pf.Assets)
})
t.Run("out of bounds", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "banana"},
{ID: "orange"},
},
}
require.Error(t, pf.RemoveAsset(3))
require.Equal(t, []portfolio.Component{{ID: "banana"}, {ID: "orange"}}, pf.Assets)
})
t.Run("negative index of bounds", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "banana"},
{ID: "orange"},
},
}
require.Error(t, pf.RemoveAsset(-1))
})
}
type ErrorAlg struct{}
func (ErrorAlg) Name() string { return "" }
func (ErrorAlg) PolicyWeights(ctx context.Context, today time.Time, assets returns.Table, currentWeights []float64) ([]float64, error) {
return nil, fmt.Errorf("lemon")
}