Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Fix Sharpe ratio calculation. #219

Merged
merged 1 commit into from
Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions pyfolio/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,12 @@ def test_annual_volatility(self, returns, period, expected):
)

@parameterized.expand([
(simple_rets, 'calendar', 0.8624740045072119),
(simple_rets, 'compound', 1.3297007080039505)
(simple_rets, 1.2333396776895436)
])
def test_sharpe(self, returns, returns_style, expected):
def test_sharpe(self, returns, expected):
self.assertAlmostEqual(
timeseries.sharpe_ratio(
returns,
returns_style=returns_style),
returns),
expected, DECIMAL_PLACES)

@parameterized.expand([
Expand Down
12 changes: 5 additions & 7 deletions pyfolio/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def downside_risk(returns, required_return=0, period=DAILY):
return dside_risk


def sharpe_ratio(returns, returns_style='compound', period=DAILY):
def sharpe_ratio(returns, risk_free=0, period=DAILY):
"""
Determines the Sharpe ratio of a strategy.

Expand All @@ -476,13 +476,11 @@ def sharpe_ratio(returns, returns_style='compound', period=DAILY):
See https://en.wikipedia.org/wiki/Sharpe_ratio for more details.
"""

numer = annual_return(returns, style=returns_style, period=period)
denom = annual_volatility(returns, period=period)
returns_risk_adj = returns - risk_free

if denom > 0.0:
return numer / denom
else:
return np.nan
return np.mean(returns_risk_adj) / \
np.std(returns_risk_adj) * \
np.sqrt(ANNUALIZATION_FACTORS[period])


def stability_of_timeseries(returns):
Expand Down