Skip to content

Instantly share code, notes, and snippets.

@xoelop
Created February 14, 2019 07:45
Show Gist options
  • Save xoelop/6ce27b73a3bc6048e555838216f074b1 to your computer and use it in GitHub Desktop.
Save xoelop/6ce27b73a3bc6048e555838216f074b1 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
def ratio(ret1: pd.Series,
ret2: pd.Series = 0,
ratio: str = 'sharpe',
log: bool = True) -> float:
"""log: if True, convert ret1 and ret2 to log returns"""
if log:
ret1 = np.log1p(ret1)
ret2 = np.log1p(ret2)
ret = ret1 - ret2
if ratio == 'sortino':
neg_ret = ret[ret<0]
ratio = ret.mean() / neg_ret.std()
elif ratio == 'sharpe':
ratio = ret.mean() / ret.std()
ratio = ratio * np.sqrt(365)
return ratio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment