-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add sharpe function implementation
- Loading branch information
Showing
3 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
package statistics | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
// Sharpe: Calcluates the sharpe ratio of access returns | ||
// | ||
// @param rf (float): Risk-free rate expressed as a yearly (annualized) return | ||
// @param periods (int): Freq. of returns (252/365 for daily, 12 for monthy) | ||
// @param annualize (bool): return annualize sharpe? | ||
// @param smart (bool): return smart sharpe ratio | ||
func Sharpe(returns types.Series, rf float64, periods int, annualize bool, smart bool) { | ||
func Sharpe(returns types.Series, periods int, annualize bool, smart bool) float64 { | ||
data := returns | ||
num := data.Length() | ||
if types.Lowest(data, num) >= 0 && types.Highest(data, num) > 1 { | ||
data = types.PercentageChange(returns) | ||
} | ||
divisor := types.Stdev(data, data.Length(), 1) | ||
if smart { | ||
sum := 0. | ||
coef := math.Abs(types.Correlation(data, types.Shift(data, 1), num-1)) | ||
for i := 1; i < num; i++ { | ||
sum += float64(num-i) / float64(num) * math.Pow(coef, float64(i)) | ||
} | ||
divisor = divisor * math.Sqrt(1.+2.*sum) | ||
} | ||
result := types.Mean(data) / divisor | ||
if annualize { | ||
return result * math.Sqrt(float64(periods)) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package statistics | ||
|
||
import ( | ||
"github.com/c9s/bbgo/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
/* | ||
python | ||
import quantstats as qx | ||
import pandas as pd | ||
print(qx.stats.sharpe(pd.Series([0.01, 0.1, 0.001]), 0, 0, False, False)) | ||
print(qx.stats.sharpe(pd.Series([0.01, 0.1, 0.001]), 0, 252, False, False)) | ||
print(qx.stats.sharpe(pd.Series([0.01, 0.1, 0.001]), 0, 252, True, False)) | ||
*/ | ||
func TestSharpe(t *testing.T) { | ||
var a types.Series = &types.Float64Slice{0.01, 0.1, 0.001} | ||
output := Sharpe(a, 0, false, false) | ||
assert.InDelta(t, output, 0.67586, 0.0001) | ||
output = Sharpe(a, 252, false, false) | ||
assert.InDelta(t, output, 0.67586, 0.0001) | ||
output = Sharpe(a, 252, true, false) | ||
assert.InDelta(t, output, 10.7289, 0.0001) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters