def get_volatility(input_df, params=None): """function to calculate annualized volatility of a trading strategy""" if params is None: raise ValueError("Please set the corresponding Interval parameter" "{interval:Ct.INTERVAL.MONTH|Ct.INTERVAL.DAY}") if Ct.neg_volatility_key() not in params.keys(): params[Ct.neg_volatility_key()] = False reference_days = KPI.get_reference_days(params) negative = params[Ct.neg_volatility_key()] df = input_df.copy() df.columns = df.columns.droplevel(1) result_df = pd.DataFrame() # Whole volatility was calculated if negative is False: result_df[Volatility.kpi_name] = (df.std() * np.sqrt(reference_days)) else: df_neg = df.where(df < 0, 0) result_df[Volatility.kpi_name] = (df_neg.std() * np.sqrt(reference_days)) return result_df
def get_sortino(input_df, params=None): if params is None: params = {} if "rf" not in params.keys(): # USA: risk free rate params = {"rf": 0.0144} rf = params["rf"] "function to calculate Sortino ratio ; rf is the risk free rate" cagr = CAGR.get_cagr(input_df, params) vol_params = params vol_params[Ct.neg_volatility_key()] = True neg_vol = Volatility.get_volatility(input_df, vol_params) result_df = pd.DataFrame() result_df[Sortino.kpi_name] = (cagr.loc[:, Ct.cagr_key()] - rf) / neg_vol.loc[:, Ct.volatility_key()] result_df.rename(index={0: Sortino.kpi_name}, inplace=True) return result_df