Skip to content

Commit

Permalink
331 simple sharpe ratio in portfolio (#332)
Browse files Browse the repository at this point in the history
* sharpe

* sharpe
  • Loading branch information
tschm authored Jan 23, 2024
1 parent 7d2e98c commit 53c495b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cvx/simulator/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from dataclasses import dataclass
from datetime import datetime
from math import sqrt
from typing import Any

import numpy as np
Expand Down Expand Up @@ -408,6 +409,11 @@ def snapshot(

return fig

def sharpe(self, n=252):
"""Simple Sharpe ratio"""
ts = self.nav.pct_change().dropna()
return ts.mean() / ts.std() * sqrt(n)

@classmethod
def from_cashpos_prices(
cls, prices: pd.DataFrame, cashposition: pd.DataFrame, aum: float
Expand Down
2 changes: 2 additions & 0 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,5 @@ def test_profit_metrics(portfolio):
assert portfolio.profit.std() == pytest.approx(839.8620124674756)
assert portfolio.profit.sum() == pytest.approx(-3492.4000000000033)
assert sharpe(portfolio.profit, n=252) == pytest.approx(-0.10965282385614909)
# profit is replacing NaNs with 0?!
assert portfolio.sharpe() == pytest.approx(-0.10210959124482079)

0 comments on commit 53c495b

Please sign in to comment.