ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

edhec_risk_kit_104.py

(1021B)


      1 import pandas as pd
      2 
      3 def drawdown(return_series: pd.Series):
      4     """Takes a time series of asset returns.
      5        returns a DataFrame with columns for
      6        the wealth index, 
      7        the previous peaks, and 
      8        the percentage drawdown
      9     """
     10     wealth_index = 1000*(1+return_series).cumprod()
     11     previous_peaks = wealth_index.cummax()
     12     drawdowns = (wealth_index - previous_peaks)/previous_peaks
     13     return pd.DataFrame({"Wealth": wealth_index, 
     14                          "Previous Peak": previous_peaks, 
     15                          "Drawdown": drawdowns})
     16 
     17 def get_ffme_returns():
     18     """
     19     Load the Fama-French Dataset for the returns of the Top and Bottom Deciles by MarketCap
     20     """
     21     me_m = pd.read_csv("data/Portfolios_Formed_on_ME_monthly_EW.csv",
     22                        header=0, index_col=0, na_values=-99.99)
     23     rets = me_m[['Lo 10', 'Hi 10']]
     24     rets.columns = ['SmallCap', 'LargeCap']
     25     rets = rets/100
     26     rets.index = pd.to_datetime(rets.index, format="%Y%m").to_period('M')
     27     return rets
     28