-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.go
387 lines (319 loc) · 9.24 KB
/
statistic.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package backtest
import (
"errors"
"fmt"
"math"
"net/http"
"time"
"github.com/shopspring/decimal"
"github.com/wcharczuk/go-chart"
"gonum.org/v1/gonum/stat"
)
// StatisticHandler is a basic statistic interface
type StatisticHandler interface {
EventTracker
TransactionTracker
StatisticPrinter
Reseter
StatisticUpdater
Resulter
}
// EventTracker is responsible for all event tracking during a backtest
type EventTracker interface {
TrackEvent(EventHandler)
Events() []EventHandler
}
// TransactionTracker is responsible for all transaction tracking during a backtest
type TransactionTracker interface {
TrackTransaction(FillEvent)
Transactions() []FillEvent
}
// StatisticPrinter handles printing of the statistics to screen
type StatisticPrinter interface {
PrintResult()
}
// StatisticUpdater handles the updateing of the statistics
type StatisticUpdater interface {
Update(DataEventHandler, PortfolioHandler)
}
// Resulter bundles all methods which return the results of the backtest
type Resulter interface {
TotalEquityReturn() (float64, error)
MaxDrawdown() float64
MaxDrawdownTime() time.Time
MaxDrawdownDuration() time.Duration
SharpRatio(float64) float64
SortinoRatio(float64) float64
}
// Statistic is a basic test statistic, which holds simple lists of historic events
type Statistic struct {
eventHistory []EventHandler
transactionHistory []FillEvent
equity []equityPoint
high equityPoint
low equityPoint
initialBuy float64 // TODO: this only handles one currency, needs to support multiple
}
type equityPoint struct {
timestamp time.Time
equity float64
equityReturn float64
drawdown float64
buyAndHoldValue float64
}
// Update the complete statistics to a given data event.
func (s *Statistic) Update(d DataEventHandler, p PortfolioHandler) {
if s.initialBuy == 0 {
s.initialBuy = p.InitialCash() / d.LatestPrice()
}
// create new equity point based on current data timestamp and portfolio value
e := equityPoint{}
e.timestamp = d.GetTime()
e.equity = p.Value()
// Record buy and hold value
e.buyAndHoldValue = s.initialBuy * d.LatestPrice()
// calc equity return for current equity point
if len(s.equity) > 0 {
e = s.calcEquityReturn(e)
}
// calc drawdown for current equity point
if len(s.equity) > 0 {
e = s.calcDrawdown(e)
}
// set high and low equity point
if e.equity >= s.high.equity {
s.high = e
}
if e.equity <= s.low.equity {
s.low = e
}
// append new quity point
s.equity = append(s.equity, e)
}
// TrackEvent tracks an event
func (s *Statistic) TrackEvent(e EventHandler) {
s.eventHistory = append(s.eventHistory, e)
}
// Events returns the complete events history
func (s Statistic) Events() []EventHandler {
return s.eventHistory
}
// TrackTransaction tracks a transaction aka a fill event
func (s *Statistic) TrackTransaction(f FillEvent) {
s.transactionHistory = append(s.transactionHistory, f)
}
// Transactions returns the complete events history
func (s Statistic) Transactions() []FillEvent {
return s.transactionHistory
}
// Reset the statistic to a clean state
func (s *Statistic) Reset() {
s.eventHistory = nil
s.transactionHistory = nil
s.equity = nil
s.high = equityPoint{}
s.low = equityPoint{}
}
// PrintResult prints the backtest statistics to the screen
func (s Statistic) PrintResult() {
fmt.Println("Printing backtest results:")
fmt.Printf("Counted %d total events.\n", len(s.Events()))
fmt.Printf("Counted %d total transactions:\n", len(s.Transactions()))
for k, v := range s.Transactions() {
fmt.Printf("%d. Transaction: %v Action: %s Price: %f Qty: %f\n", k+1, v.GetTime().Format("2006-01-02 03:04 PM"), v.GetDirection(), v.GetPrice(), v.GetQty())
}
}
// TotalEquityReturn calculates the the total return on the first and last equity point
func (s Statistic) TotalEquityReturn() (r float64, err error) {
firstEquityPoint, ok := s.firstEquityPoint()
if !ok {
return r, errors.New("could not calculate totalEquityReturn, no equity points found")
}
firstEquity := decimal.NewFromFloat(firstEquityPoint.equity)
lastEquityPoint, _ := s.lastEquityPoint()
// if !ok {
// return r, errors.New("could not calculate totalEquityReturn, no last equity point")
// }
lastEquity := decimal.NewFromFloat(lastEquityPoint.equity)
totalEquityReturn := lastEquity.Sub(firstEquity).Div(firstEquity)
total, _ := totalEquityReturn.Round(DP).Float64()
return total, nil
}
// MaxDrawdown returns the maximum draw down value in percent.
func (s Statistic) MaxDrawdown() float64 {
_, ep := s.maxDrawdownPoint()
return ep.drawdown
}
// MaxDrawdownTime returns the time of the maximum draw down value.
func (s Statistic) MaxDrawdownTime() time.Time {
_, ep := s.maxDrawdownPoint()
return ep.timestamp
}
// MaxDrawdownDuration returns the maximum draw down value in percent
func (s Statistic) MaxDrawdownDuration() (d time.Duration) {
i, ep := s.maxDrawdownPoint()
if len(s.equity) == 0 {
return d
}
// walk the equity slice up to find a higher equity point
maxPoint := equityPoint{}
for index := i; index >= 0; index-- {
if s.equity[index].equity > maxPoint.equity {
maxPoint = s.equity[index]
}
}
d = ep.timestamp.Sub(maxPoint.timestamp)
return d
}
func (s *Statistic) GraphResult(res http.ResponseWriter, req *http.Request) {
var xv []time.Time
var yv1 []float64
var yv2 []float64
var maxY float64
var minY float64
for _, e := range s.equity {
xv = append(xv, e.timestamp)
yv1 = append(yv1, e.equity)
yv2 = append(yv2, e.buyAndHoldValue)
maxY = math.Max(math.Max(maxY, e.equity), e.buyAndHoldValue)
if minY == 0 {
minY = maxY
}
minY = math.Min(math.Min(minY, e.equity), e.buyAndHoldValue)
}
fmt.Println(maxY, minY)
priceSeries := chart.TimeSeries{
Name: "SPY",
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultColor(0),
},
XValues: xv,
YValues: yv1,
}
comparisonSeries := chart.TimeSeries{
Name: "SPC",
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultColor(1),
},
XValues: xv,
YValues: yv2,
}
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.Style{Show: true},
TickPosition: chart.TickPositionBetweenTicks,
},
YAxis: chart.YAxis{
Style: chart.Style{Show: true},
Range: &chart.ContinuousRange{
Max: maxY + (maxY-minY)/10,
Min: minY - (maxY-minY)/10,
},
},
Series: []chart.Series{
priceSeries,
comparisonSeries,
},
}
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
// SharpRatio returns the Sharp ratio compared to a risk free benchmark return.
func (s *Statistic) SharpRatio(riskfree float64) float64 {
var equityReturns = make([]float64, len(s.equity))
for i, v := range s.equity {
equityReturns[i] = v.equityReturn
}
mean, stddev := stat.MeanStdDev(equityReturns, nil)
sharp := (mean - riskfree) / stddev
return sharp
}
// SortinoRatio returns the Sortino ratio compared to a risk free benchmark return.
func (s *Statistic) SortinoRatio(riskfree float64) float64 {
var equityReturns = make([]float64, len(s.equity))
for i, v := range s.equity {
equityReturns[i] = v.equityReturn
}
mean := stat.Mean(equityReturns, nil)
// sortino uses the stddev of only the negativ returns
var negReturns = []float64{}
for _, v := range equityReturns {
if v < 0 {
negReturns = append(negReturns, v)
}
}
stdDev := stat.StdDev(negReturns, nil)
sortino := (mean - riskfree) / stdDev
return sortino
}
func (s Statistic) ViewEquityHistory() {
fmt.Println(s.equity)
}
// returns the first equityPoint
func (s Statistic) firstEquityPoint() (ep equityPoint, ok bool) {
if len(s.equity) <= 0 {
return ep, false
}
ep = s.equity[0]
return ep, true
}
// returns the last equityPoint
func (s Statistic) lastEquityPoint() (ep equityPoint, ok bool) {
if len(s.equity) <= 0 {
return ep, false
}
ep = s.equity[len(s.equity)-1]
return ep, true
}
// calculates the equity return of an equity point relativ to the last equity point
func (s Statistic) calcEquityReturn(e equityPoint) equityPoint {
last, ok := s.lastEquityPoint()
// no equity point before the current
if !ok {
e.equityReturn = 0
return e
}
lastEquity := decimal.NewFromFloat(last.equity)
currentEquity := decimal.NewFromFloat(e.equity)
// last equity point has 0 equity
if lastEquity.Equal(decimal.Zero) {
e.equityReturn = 1
return e
}
equityReturn := currentEquity.Sub(lastEquity).Div(lastEquity)
e.equityReturn, _ = equityReturn.Round(DP).Float64()
return e
}
// calculates the drawdown of an equity point relativ to the latest high of the statistic handler
func (s Statistic) calcDrawdown(e equityPoint) equityPoint {
if s.high.equity == 0 {
e.drawdown = 0
return e
}
lastHigh := decimal.NewFromFloat(s.high.equity)
equity := decimal.NewFromFloat(e.equity)
if equity.GreaterThanOrEqual(lastHigh) {
e.drawdown = 0
return e
}
drawdown := equity.Sub(lastHigh).Div(lastHigh)
e.drawdown, _ = drawdown.Round(DP).Float64()
return e
}
// returns the equity point with the maximum drawdown
func (s Statistic) maxDrawdownPoint() (i int, ep equityPoint) {
if len(s.equity) == 0 {
return 0, ep
}
var maxDrawdown = 0.0
var index = 0
for i, ep := range s.equity {
if ep.drawdown < maxDrawdown {
maxDrawdown = ep.drawdown
index = i
}
}
return index, s.equity[index]
}