-
Notifications
You must be signed in to change notification settings - Fork 790
/
common.go
305 lines (286 loc) · 10.6 KB
/
common.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
package statistics
import (
"errors"
"fmt"
"time"
"github.com/shopspring/decimal"
"github.com/thrasher-corp/gocryptotrader/backtester/common"
"github.com/thrasher-corp/gocryptotrader/backtester/data"
gctmath "github.com/thrasher-corp/gocryptotrader/common/math"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/log"
)
// fSIL shorthand wrapper for FitStringToLimit
func fSIL(str string, limit int) string {
spacer := " "
return common.FitStringToLimit(str, spacer, limit, true)
}
// CalculateBiggestEventDrawdown calculates the biggest drawdown using a slice of DataEvents
func CalculateBiggestEventDrawdown(closePrices []data.Event) (Swing, error) {
if len(closePrices) == 0 {
return Swing{}, fmt.Errorf("%w to calculate drawdowns", errReceivedNoData)
}
var swings []Swing
lowestPrice := closePrices[0].GetLowPrice()
highestPrice := closePrices[0].GetHighPrice()
lowestTime := closePrices[0].GetTime()
highestTime := closePrices[0].GetTime()
interval := closePrices[0].GetInterval()
for i := range closePrices {
currHigh := closePrices[i].GetHighPrice()
currLow := closePrices[i].GetLowPrice()
currTime := closePrices[i].GetTime()
if lowestPrice.GreaterThan(currLow) && !currLow.IsZero() {
lowestPrice = currLow
lowestTime = currTime
}
if highestPrice.LessThan(currHigh) {
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
intervals, err := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, closePrices[i].GetInterval(), 0)
if err != nil {
return Swing{}, fmt.Errorf("cannot calculate max drawdown, date range error: %w", err)
}
if highestPrice.IsPositive() && lowestPrice.IsPositive() {
swings = append(swings, Swing{
Highest: ValueAtTime{
Time: highestTime,
Value: highestPrice,
},
Lowest: ValueAtTime{
Time: lowestTime,
Value: lowestPrice,
},
DrawdownPercent: lowestPrice.Sub(highestPrice).Div(highestPrice).Mul(decimal.NewFromInt(100)),
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
})
}
// reset the drawdown
highestPrice = currHigh
highestTime = currTime
lowestPrice = currLow
lowestTime = currTime
}
}
if (len(swings) > 0 && swings[len(swings)-1].Lowest.Value != closePrices[len(closePrices)-1].GetLowPrice()) || swings == nil {
// need to close out the final drawdown
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
intervals, err := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, closePrices[0].GetInterval(), 0)
if err != nil {
return Swing{}, fmt.Errorf("cannot close out max drawdown calculation: %w", err)
}
drawdownPercent := decimal.Zero
if highestPrice.GreaterThan(decimal.Zero) {
drawdownPercent = lowestPrice.Sub(highestPrice).Div(highestPrice).Mul(decimal.NewFromInt(100))
}
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
swings = append(swings, Swing{
Highest: ValueAtTime{
Time: highestTime,
Value: highestPrice,
},
Lowest: ValueAtTime{
Time: lowestTime,
Value: lowestPrice,
},
DrawdownPercent: drawdownPercent,
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
})
}
var maxDrawdown Swing
if len(swings) > 0 {
maxDrawdown = swings[0]
}
for i := range swings {
if swings[i].DrawdownPercent.LessThan(maxDrawdown.DrawdownPercent) {
maxDrawdown = swings[i]
}
}
return maxDrawdown, nil
}
// CalculateBiggestValueAtTimeDrawdown calculates the biggest drawdown using a slice of ValueAtTimes
func CalculateBiggestValueAtTimeDrawdown(closePrices []ValueAtTime, interval gctkline.Interval) (Swing, error) {
if len(closePrices) == 0 {
return Swing{}, fmt.Errorf("%w to calculate drawdowns", errReceivedNoData)
}
var swings []Swing
lowestPrice := closePrices[0].Value
highestPrice := closePrices[0].Value
lowestTime := closePrices[0].Time
highestTime := closePrices[0].Time
for i := range closePrices {
currHigh := closePrices[i].Value
currLow := closePrices[i].Value
currTime := closePrices[i].Time
if lowestPrice.GreaterThan(currLow) && !currLow.IsZero() {
lowestPrice = currLow
lowestTime = currTime
}
if highestPrice.LessThan(currHigh) && highestPrice.IsPositive() {
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
intervals, err := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, interval, 0)
if err != nil {
return Swing{}, err
}
swings = append(swings, Swing{
Highest: ValueAtTime{
Time: highestTime,
Value: highestPrice,
},
Lowest: ValueAtTime{
Time: lowestTime,
Value: lowestPrice,
},
DrawdownPercent: lowestPrice.Sub(highestPrice).Div(highestPrice).Mul(decimal.NewFromInt(100)),
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
})
// reset the drawdown
highestPrice = currHigh
highestTime = currTime
lowestPrice = currLow
lowestTime = currTime
}
}
if (len(swings) > 0 && !swings[len(swings)-1].Lowest.Value.Equal(closePrices[len(closePrices)-1].Value)) || swings == nil {
// need to close out the final drawdown
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
intervals, err := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, interval, 0)
if err != nil {
log.Errorln(common.CurrencyStatistics, err)
}
drawdownPercent := decimal.Zero
if highestPrice.GreaterThan(decimal.Zero) {
drawdownPercent = lowestPrice.Sub(highestPrice).Div(highestPrice).Mul(decimal.NewFromInt(100))
}
if lowestTime.Equal(highestTime) {
// create distinction if the greatest drawdown occurs within the same candle
lowestTime = lowestTime.Add(interval.Duration() - time.Nanosecond)
}
swings = append(swings, Swing{
Highest: ValueAtTime{
Time: highestTime,
Value: highestPrice,
},
Lowest: ValueAtTime{
Time: lowestTime,
Value: lowestPrice,
},
DrawdownPercent: drawdownPercent,
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
})
}
var maxDrawdown Swing
if len(swings) > 0 {
maxDrawdown = swings[0]
}
for i := range swings {
if swings[i].DrawdownPercent.LessThan(maxDrawdown.DrawdownPercent) {
maxDrawdown = swings[i]
}
}
return maxDrawdown, nil
}
// CalculateRatios creates arithmetic and geometric ratios from funding or currency pair data
func CalculateRatios(benchmarkRates, returnsPerCandle []decimal.Decimal, riskFreeRatePerCandle decimal.Decimal, maxDrawdown *Swing, logMessage string) (arithmeticStats, geometricStats *Ratios, err error) {
var arithmeticBenchmarkAverage, geometricBenchmarkAverage decimal.Decimal
arithmeticBenchmarkAverage, err = gctmath.DecimalArithmeticMean(benchmarkRates)
if err != nil {
return nil, nil, err
}
geometricBenchmarkAverage, err = gctmath.DecimalFinancialGeometricMean(benchmarkRates)
if err != nil {
return nil, nil, err
}
riskFreeRateForPeriod := riskFreeRatePerCandle.Mul(decimal.NewFromInt(int64(len(benchmarkRates))))
var arithmeticReturnsPerCandle, geometricReturnsPerCandle, arithmeticSharpe, arithmeticSortino,
arithmeticInformation, arithmeticCalmar, geomSharpe, geomSortino, geomInformation, geomCalmar decimal.Decimal
arithmeticReturnsPerCandle, err = gctmath.DecimalArithmeticMean(returnsPerCandle)
if err != nil {
return nil, nil, err
}
geometricReturnsPerCandle, err = gctmath.DecimalFinancialGeometricMean(returnsPerCandle)
if err != nil {
return nil, nil, err
}
arithmeticSharpe, err = gctmath.DecimalSharpeRatio(returnsPerCandle, riskFreeRatePerCandle, arithmeticReturnsPerCandle)
if err != nil {
return nil, nil, err
}
arithmeticSortino, err = gctmath.DecimalSortinoRatio(returnsPerCandle, riskFreeRatePerCandle, arithmeticReturnsPerCandle)
if err != nil && !errors.Is(err, gctmath.ErrNoNegativeResults) {
if errors.Is(err, gctmath.ErrInexactConversion) {
log.Warnf(common.Statistics, "%s funding arithmetic sortino ratio %v", logMessage, err)
} else {
return nil, nil, err
}
}
arithmeticInformation, err = gctmath.DecimalInformationRatio(returnsPerCandle, benchmarkRates, arithmeticReturnsPerCandle, arithmeticBenchmarkAverage)
if err != nil {
return nil, nil, err
}
arithmeticCalmar, err = gctmath.DecimalCalmarRatio(maxDrawdown.Highest.Value, maxDrawdown.Lowest.Value, arithmeticReturnsPerCandle, riskFreeRateForPeriod)
if err != nil {
log.Warnf(common.Statistics, "%s funding arithmetic calmar ratio %v", logMessage, err)
}
arithmeticStats = &Ratios{}
if !arithmeticSharpe.IsZero() {
arithmeticStats.SharpeRatio = arithmeticSharpe
}
if !arithmeticSortino.IsZero() {
arithmeticStats.SortinoRatio = arithmeticSortino
}
if !arithmeticInformation.IsZero() {
arithmeticStats.InformationRatio = arithmeticInformation
}
if !arithmeticCalmar.IsZero() {
arithmeticStats.CalmarRatio = arithmeticCalmar
}
geomSharpe, err = gctmath.DecimalSharpeRatio(returnsPerCandle, riskFreeRatePerCandle, geometricReturnsPerCandle)
if err != nil {
return nil, nil, err
}
geomSortino, err = gctmath.DecimalSortinoRatio(returnsPerCandle, riskFreeRatePerCandle, geometricReturnsPerCandle)
if err != nil && !errors.Is(err, gctmath.ErrNoNegativeResults) {
if errors.Is(err, gctmath.ErrInexactConversion) {
log.Warnf(common.Statistics, "%s geometric sortino ratio %v", logMessage, err)
} else {
return nil, nil, err
}
}
geomInformation, err = gctmath.DecimalInformationRatio(returnsPerCandle, benchmarkRates, geometricReturnsPerCandle, geometricBenchmarkAverage)
if err != nil {
return nil, nil, err
}
geomCalmar, err = gctmath.DecimalCalmarRatio(maxDrawdown.Highest.Value, maxDrawdown.Lowest.Value, geometricReturnsPerCandle, riskFreeRateForPeriod)
if err != nil {
log.Warnf(common.Statistics, "%s funding geometric calmar ratio %v", logMessage, err)
}
geometricStats = &Ratios{}
if !arithmeticSharpe.IsZero() {
geometricStats.SharpeRatio = geomSharpe
}
if !arithmeticSortino.IsZero() {
geometricStats.SortinoRatio = geomSortino
}
if !arithmeticInformation.IsZero() {
geometricStats.InformationRatio = geomInformation
}
if !arithmeticCalmar.IsZero() {
geometricStats.CalmarRatio = geomCalmar
}
return arithmeticStats, geometricStats, nil
}