예제 #1
0
    def setUp(self):
        ls_asset = ['GOOG']
        self.df_stock_rets = get_price_data(
            ls_asset, end_date=dt.datetime.today(),
            look_back_mths=12).pct_change().fillna(0)

        self.df_benchmark_rets = get_price_data(
            ['^GSPC'], end_date=dt.datetime.today(),
            look_back_mths=12).pct_change().fillna(0)
예제 #2
0
 def __init__(self,
              asset_name_ls,
              rets_hist_length_yrs=10,
              end_date=dt.datetime.today()):
     self.n = len(asset_name_ls)  # number of assets
     self.df_price_data = get_price_data(
         asset_name_ls, end_date, look_back_mths=rets_hist_length_yrs * 12)
def risk_adversion_coefficient(benchmark_ticker, risk_free_rate=0.02, lookback_period_months=48):
    """
    Calculate the risk adversion coefficient

    Args:
        benchmark_ticker: <str> ticker of the benchmark market asset
        risk_free_rate: <float> the annual risk free rate
        lookback_period_months: <int> number of months from today

    Returns:
        <float> risk adversion coefficient

    """
    end_date = dt.datetime.today()
    df_price_data = get_price_data([benchmark_ticker], end_date, look_back_mths=lookback_period_months)

    # resample to annual data
    rets = df_price_data.resample('Y').last().pct_change().apply(lambda x: np.log(1 + x))

    exp_ret = rets.mean()
    var = rets.var()
    return ((exp_ret - risk_free_rate) / var)[0]
예제 #4
0
    # Residual return = Excess return - (Benchmark's excess return * beta).
    df = stock_excess_rets - (benchmark_excess_rets *
                              beta_stock_over_benchmark)
    df.columns = [const.RESIDUAL_RETURN]
    return {
        const.RESIDUAL_RETURN: df,
        const.RESIDUAL_RISK: df.std(),
        const.EXP_RESIDUAL_RETURN: df.mean()
    }


if __name__ == '__main__':
    # Apple
    df_stock_rets = get_price_data(['AAPL'],
                                   end_date=dt.datetime.today(),
                                   look_back_mths=12).pct_change().fillna(0)
    # S&P 500
    df_market_rets = get_price_data(['^GSPC'],
                                    end_date=dt.datetime.today(),
                                    look_back_mths=12).pct_change().fillna(0)
    # iShares U.S. Technology ETF
    df_benchmark_rets = get_price_data(
        ['IYW'], end_date=dt.datetime.today(),
        look_back_mths=12).pct_change().fillna(0)

    # compute beta over the market
    beta_stock = calc_beta(df_stock_rets, df_market_rets)
    beta_benchmark = calc_beta(df_benchmark_rets, df_market_rets)

    # compute beta of the stock over the benchmark
예제 #5
0
def maximum_drawdown(df_price_data):
    """
    Get the maximum drawdown from all given price data

    Args:
        df_price_data: <pd.DataFrame> for an individual stock's price history

    Returns:
        <pd.Series> containing the maximum drawdown experienced during the given price data history
    """
    max_peaks = df_price_data.cummax()
    daily_drawdowns = (df_price_data - max_peaks) / max_peaks
    max_drawdown = daily_drawdowns.cummin()

    # return the last element since this would be the cumulative minimum
    return max_drawdown.iloc[-1]


if __name__ == '__main__':
    ls_assets = ['AAPL', 'NKE', 'GOOGL', 'AMZN']
    df = get_price_data(ls_assets, end_date=dt.datetime.today(), look_back_mths=24).pct_change().fillna(0)

    print(volatility(df))
    print(semi_deviation(df))
    print(gaussian_VaR(df))

    df_price_data = get_price_data(['AAPL'], end_date=dt.datetime.today(), look_back_mths=48)
    print(rolling_maximum_drawdowns(df_price_data=df_price_data))
    print(maximum_drawdown(df_price_data=df_price_data))