mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
fix: nan in sortino and sharpe
This commit is contained in:
parent
c73f4018d0
commit
20ee3fdfbb
|
@ -180,6 +180,9 @@ func Sum(a Series, limit ...int) (sum float64) {
|
|||
// otherwise will operate on all elements
|
||||
func Mean(a Series, limit ...int) (mean float64) {
|
||||
l := a.Length()
|
||||
if l == 0 {
|
||||
return 0
|
||||
}
|
||||
if len(limit) > 0 && limit[0] < l {
|
||||
l = limit[0]
|
||||
}
|
||||
|
@ -741,6 +744,9 @@ func PercentageChange(a Series, offset ...int) SeriesExtend {
|
|||
|
||||
func Stdev(a Series, params ...int) float64 {
|
||||
length := a.Length()
|
||||
if length == 0 {
|
||||
return 0
|
||||
}
|
||||
if len(params) > 0 && params[0] < length {
|
||||
length = params[0]
|
||||
}
|
||||
|
|
|
@ -16,6 +16,16 @@ func Sharpe(returns Series, periods int, annualize bool, smart bool) float64 {
|
|||
if smart {
|
||||
divisor *= autocorrPenalty(returns)
|
||||
}
|
||||
if divisor == 0 {
|
||||
mean := Mean(data)
|
||||
if mean > 0 {
|
||||
return math.Inf(1)
|
||||
} else if mean < 0 {
|
||||
return math.Inf(-1)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
result := Mean(data) / divisor
|
||||
if annualize {
|
||||
return result * math.Sqrt(float64(periods))
|
||||
|
|
|
@ -24,6 +24,9 @@ func Sortino(returns Series, riskFreeReturns float64, periods int, annualize boo
|
|||
}
|
||||
|
||||
num := returns.Length()
|
||||
if num == 0 {
|
||||
return 0
|
||||
}
|
||||
var sum = 0.
|
||||
for i := 0; i < num; i++ {
|
||||
exRet := returns.Index(i) - avgRiskFreeReturns
|
||||
|
@ -35,7 +38,15 @@ func Sortino(returns Series, riskFreeReturns float64, periods int, annualize boo
|
|||
if smart {
|
||||
risk *= autocorrPenalty(returns)
|
||||
}
|
||||
|
||||
if risk == 0 {
|
||||
if excessReturn > 0 {
|
||||
return math.Inf(1)
|
||||
} else if excessReturn < 0 {
|
||||
return math.Inf(-1)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
result := excessReturn / risk
|
||||
if annualize {
|
||||
return result * math.Sqrt(float64(periods))
|
||||
|
|
Loading…
Reference in New Issue
Block a user