Example #1
0
    def calculating_risk_of_portfolio(self, asset_list, weights_list):
        assets = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        asset_log_returns = np.log(assets / assets.shift(1))
        weights = np.array(weights_list)

        pfolio_var = np.dot(weights.T, np.dot(asset_log_returns.cov() * 250, weights))
        pfolio_vol = (np.dot(weights.T, np.dot(asset_log_returns.cov() * 250, weights))) ** 0.5

        # ---plot the stocks a a probability density function ---
        asset_log_returns.plot.kde()
        plt.show()
        return pfolio_vol, pfolio_var
Example #2
0
    def asset_risks(self, asset_list):
        list_of_asset_annual_stds = []
        list_of_asset_daily_stds = []
        assets = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        asset_log_returns = np.log(assets / assets.shift(1))

        for i, j in zip(asset_log_returns, asset_log_returns):
            print(i+': ' + 'Annual Standard deviation '+ str(asset_log_returns[i].std()*250**0.5), 'Control list vals')
            print(j + ': ' + 'Daily Standard deviation ' + str(asset_log_returns[j].std()), 'Control list vals')
            list_of_asset_annual_stds.append(i+': ' + 'Annual Standard deviation '+ str(asset_log_returns[i].std()*250**0.5))
            list_of_asset_daily_stds.append(j+': ' + 'Daily Standard deviation '+ str(asset_log_returns[j].std()))

        return list_of_asset_annual_stds, list_of_asset_daily_stds
Example #3
0
    def calculate_covariance_and_correlation_between_two_assets(self, asset_list, plot_var):

        assets_data = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        asset_log_returns = np.log(assets_data / assets_data.shift(1))
        asset_1 = asset_list[0]
        asset_2 = asset_list[1]

        cov_val = asset_log_returns[asset_1].cov(asset_log_returns[asset_2])
        corr_val = asset_log_returns[asset_1].corr(asset_log_returns[asset_2])

        if plot_var == 1:
            asset_log_returns.plot.scatter(x=asset_1, y=asset_2, c='DarkBlue')
            plt.show()
            return corr_val, cov_val
        else:
            return corr_val, cov_val
Example #4
0
    def capital_asset_pricing_model(self, asset_list):
        assets = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        sec_returns = np.log(assets / assets.shift(1))

        asset_string = asset_list[0]
        market_string = asset_list[1]

        cov = sec_returns.cov() * 250
        cov_with_market = cov.iloc[0, 1]
        market_var = sec_returns[market_string].var() * 250

        asset_beta = cov_with_market / market_var

        asset_expected_return = 0.025 + asset_beta * 0.05
        Sharpe = (asset_expected_return - 0.025) / (sec_returns[asset_string] * 250 ** 0.5)

        return asset_beta, asset_expected_return, Sharpe
Example #5
0
    def annual_portfolio_info(self, asset_list, weights_list):
        assets = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        asset_log_returns = np.log(assets / assets.shift(1))
        weights = np.array(weights_list)

        #---------Expected portfolio return---------
        port_return = np.sum(weights * asset_log_returns.mean()) * 250
        print(port_return, 'Expected portfolio return')

        # ---------Expected portfolio varience---------
        port_var = np.dot(weights.T, np.dot(asset_log_returns.cov()*250, weights))
        print(port_var, 'Expected portfolio varience')

        #---------Expected portfolio volatility---------
        port_vola = np.sqrt(port_var)
        print(port_vola, 'Expected portfolio volatility')

        return port_return, port_var, port_vola
Example #6
0
    def calculate_systematic_and_non_systematic_risk_of_portfolio(self, asset_list, weights_list):
        assets = Finance_Analytics_Tools.get_multiple_asset_data(self, asset_list)
        asset_log_returns = np.log(assets / assets.shift(1))
        weights = np.array(weights_list)

        asset_1 = asset_list[0]
        asset_2 = asset_list[1]

        # ---Annual Diversifiable Risk---
        # Systematic risk
        # Diversifiable risk = portfolio varaince - weighted annaual variances

        pfolio_var = np.dot(weights.T, np.dot(asset_log_returns.cov() * 250, weights))
        asset_1_annual_variance = asset_log_returns[asset_1].var() * 250
        asset_2_annual_variance = asset_log_returns[asset_2].var() * 250

        systematic_port_risk = pfolio_var - (weights[0] ** 2 * asset_1_annual_variance) - (weights[1] ** 2 * asset_2_annual_variance)

        # ---Annual Un-Diversifiable Risk---
        # Un-Systematic risk
        # Un-Diversifiable risk = portfolio varaince - diversifiable risk
        un_systematic_port_risk = pfolio_var - systematic_port_risk

        return systematic_port_risk, un_systematic_port_risk
Example #7
0
def main():


    D1 = Finance_Investment_Risks('yahoo', '2010-1-1', '2017-03-24')
    assets = ['PG', 'BEI.DE']
    w_list = [0.5, 0.5]
    D1.asset_risks(assets)
    a,b = D1.calculate_covariance_and_correlation_between_two_assets(assets, 1)
    c,d = D1.calculating_risk_of_portfolio(assets, w_list)
    D1.Markowitz_portfolio_optimization_efficient_frontier(assets)


    D1 = Finance_Analytics_Tools('yahoo', '2012-1-1', '2013-1-1')
    data_frame = D1.get_asset_data('AAPL')
    log_rets = D1.log_rets(data_frame)
    my_tuple = (0.00, 0.01)
    prob_0 = D1.normal_dist_model(log_rets, 0.005,'return between in interval',1, my_tuple)
    prob_1 = D1.normal_dist_model(log_rets, 0.005,'return greater or equal',0)


    D1 = Finance_Analytics_Tools('yahoo', '1995-1-1', '2020-1-25')
    data_frame = D1.get_asset_data('PG')

    average_daily_returns = D1.average_daily_returns(data_frame, 1)
    average_annual_returns = D1.average_annual_returns(data_frame, 1)
    log_daily_returns = D1.log_daily_returns(data_frame, 0)

    portfolio = ['PG', 'MSFT', 'F', 'GE']
    weights = np.array([0.25, 0.25, 0.25, 0.25])
    D2 = Finance_Analytics_Tools('yahoo', '1995-1-1', '2020-1-25')
    portfolio_return_historic = D2.historic_return_of_investment_portfolio(portfolio, weights, 1)
    print(portfolio_return_historic)


    indices = ['^GSPC', '^IXIC', '^GDAXI']
    stocks = ['PG']
    D1 = Finance_Analytics_Tools('yahoo', '1995-1-1', '2020-1-25')
    ind_rets = D1.historic_return_of_indices(indices, 2, stocks)
    print(ind_rets)


    FNN = Finance_Neural_Nets('AAPL', 'yahoo', '2012-01-01', '2020-2-3')
    FNN.window = 60
    FNN.batch_size = 64
    FNN.epoch = 20
    FNN.data_column = 'Adj Close'
    x_train, y_train, x_train.shape, training_data_len, scaler, scaled_data, data, dataset = FNN.prepare_data()
    #model = FNN.neural_net_RNN_model(x_train)
    model = FNN.neural_net_LSTM_model(x_train)
    FNN.train_neural_net(model, x_train, y_train, training_data_len, scaler, scaled_data, data, dataset)


    FMLM = Finance_Machine_Learning_Models('TSLA', 'yahoo', '2000-1-1', '2020-2-4')
    FMLM.forecast_out = 1
    FMLM.data_column = 'Adj Close'
    x_train, x_test, y_train, y_test, df = FMLM.prepare_data_set()
    svm_var, svm_conf = FMLM.forecast_with_SVM(x_train, x_test, y_train, y_test, df)
    linreg_var, linreg_conf = FMLM.forecast_with_linear_reg(x_train, x_test, y_train, y_test, df)
    y_hat = FMLM.linear_reg_for_market(1200)


    #FTSAP = Finance_Trading_Strategy_And_Plot('TSLA', 'yahoo', '2000-1-1', '2020-1-1','Adj Close', 20, 50)
    FTSAP = Finance_Trading_Strategy_And_Plot('TSLA', 'yahoo', '2000-1-1', '2020-1-1', 'Close', 30, 90)
    FTSAP.moving_average_trading_strategy()
    FTSAP.resample = '10D'
    FTSAP.moving_average_trading_strategy_candlestick_plot()
    FTSAP.moving_average_trading_strategy_with_buy_and_sell_signals()


    FMLM = Finance_Machine_Learning_Models()
    econ_data = FMLM.prepare_data_set_multiple_reg()
    regression_model, X_test, y_test, X, Y = FMLM.multiple_regression_model(econ_data)
    y_hat, predicted_vals = FMLM.predict_with_multiple_linear_model(regression_model, X_test, 5)
    FMLM.evaluate_multiple_regression_model(X, Y, y_test, y_hat)