-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockscenario_test.go
153 lines (119 loc) · 3.69 KB
/
stockscenario_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
package portfolio
import (
"fmt"
"math"
"testing"
)
func TestAddStock(t *testing.T) {
sc := NewStockScenario("1900-01-01", "3000-01-01")
agg, err := NewStock("AGG")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if err = sc.AddStock(agg, 2); err == nil {
t.Error("missed error pct > 1")
}
if err = sc.AddStock(agg, 0); err == nil {
t.Error("missed error pct <= 0")
}
if err = sc.AddStock(agg, 1); err != nil {
t.Errorf("unexpected error: %v", err)
}
if sc.StartDate != "2003-09-29" || sc.EndDate != "2021-06-08" {
t.Errorf("start/end dates not set to stock start/end dates: %s %s", sc.StartDate, sc.EndDate)
}
if len(sc.Stocks) != 1 || sc.Stocks[0] != agg {
t.Error("stock agg not added correctly")
}
if len(sc.PctHolding) != 1 || sc.PctHolding[0] != 1 {
t.Error("pct holding not set correctly")
}
}
func TestCalcResults(t *testing.T) {
agg, err := NewStock("AGG")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
sc := NewStockScenario("1900-01-01", "3000-01-01")
if err = sc.AddStock(agg, 1); err != nil {
t.Errorf("unexpected error: %v", err)
}
if err = sc.CalcResults(10000); err != nil {
t.Errorf("unexpected error: %v", err)
}
sc = NewStockScenario("202x-01-01", "2020-12-31")
sc.AddStock(agg, 1)
if err = sc.CalcResults(10000); err == nil {
t.Error("didn't catch invalid start date error")
}
// NOTE: invalid end date must still be < "2021-06-08"
// the highest date in AGG stock history, or it will be changed to that date.
sc = NewStockScenario("2020-01-01", "2020x-12-31")
sc.AddStock(agg, 1)
if err = sc.CalcResults(10000); err == nil {
t.Error("didn't catch invalid end date error")
}
sc = NewStockScenario("2021-01-01", "2020-12-31")
sc.AddStock(agg, 1)
if err = sc.CalcResults(10000); err == nil {
t.Error("didn't catch start date after end date error")
}
sc = NewStockScenario("2020-01-01", "2020-12-31")
sc.AddStock(agg, 1)
if err = sc.CalcResults(10000); err != nil {
t.Errorf("unexpected .Run error: %v", err)
}
if len(sc.Results) != 254 {
t.Errorf("invalid .Results len: %d", len(sc.Results))
}
if cap(sc.Results) != 366 {
t.Errorf("invalid .Results capacity: %d", cap(sc.Results))
}
}
func TestString(t *testing.T) {
fxaix, _ := NewStock("FXAIX")
sc := NewStockScenario("2020-01-01", "2021-01-01")
sc.AddStock(fxaix, 1)
sc.CalcResults(10000)
// TODO: make a better test of String()
fmt.Println(len(sc.String()))
// output: 16902
if len(sc.String()) != 14062 {
t.Errorf("invalid .Results capacity: %d", cap(sc.Results))
}
}
func TestCalcResults_Part02(t *testing.T) {
stockTickers := []string{"FXAIX", "FXNAX", "VDADX"}
years := []string{"2016", "2017", "2018", "2019", "2020"}
expectedResult := [][]float64{
{11.97, 21.81, -4.40, 31.47, 18.40},
{2.51, 3.49, 0.03, 8.48, 7.80},
{11.79, 22.22, -2.03, 29.68, 15.46},
}
stocks := []*Stock{}
// read stock history files
for _, stockTicker := range stockTickers {
stock, err := NewStock(stockTicker)
if err != nil {
t.Errorf("unexpected error reading stock: %v", err)
}
stocks = append(stocks, stock)
}
for i, year := range years {
for j, stock := range stocks {
sc := NewStockScenario(year+"-01-01", year+"-12-31")
sc.AddStock(stock, 1)
sc.CalcResults(10000)
expectPctChg := expectedResult[j][i]
actualPctChg := sc.PctChange * 100
actualGeomean := sc.GeomeanPctChg * 100
actualStdDev := sc.StdDev * 100
if math.Abs(actualPctChg-expectPctChg) > .021 {
t.Errorf("stock %s, year %s expected %.2f%% change, got %.4f%% change",
stock.Ticker, year, expectPctChg, actualPctChg)
}
t.Errorf("%s %s %.5f%% days: %d, stdDev: %.5f%%",
stock.Ticker, year, actualGeomean, len(sc.Results)-1, actualStdDev)
}
}
}