diff --git a/cvx/simulator/portfolio.py b/cvx/simulator/portfolio.py index 039189c0..38af14c4 100644 --- a/cvx/simulator/portfolio.py +++ b/cvx/simulator/portfolio.py @@ -15,6 +15,7 @@ from dataclasses import dataclass from datetime import datetime +from math import sqrt from typing import Any import numpy as np @@ -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 diff --git a/tests/test_portfolio.py b/tests/test_portfolio.py index 092e2bf2..d296bed1 100644 --- a/tests/test_portfolio.py +++ b/tests/test_portfolio.py @@ -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)