Example #1
0
def generate_optimum_portfolio():

    #instantiate the objects with the settings
    obj_factory = object_factory(settings)
    companie_extractor = obj_factory.get_companies_extractor()
    cp = obj_factory.get_charts_plotter()
    mcs = obj_factory.get_portfolio_generator()
    fr = obj_factory.get_file_repository()
    metricCalculator = obj_factory.get_metrics_calculator()
    print('1. Get companies')
    companies = companie_extractor.get_companies_list()
    print("hello companies  :", companies)
    price_extractor = obj_factory.get_price_extractor(companies)

    print('2. Get company stock prices')

    end_date = settings.get_end_date()
    start_date = settings.get_start_date(end_date)
    closing_prices = price_extractor.get_prices(settings.PriceEvent,
                                                start_date, end_date)
    print(closing_prices.dtypes)

    closing_prices
    #plot stock prices & save data to a file
    cp.plot_prices(closing_prices)
    fr.save_to_file(closing_prices, 'StockPrices')

    print('3. Calculate Daily Returns')
    returns = settings.DailyAssetsReturnsFunction(closing_prices,
                                                  settings.ReturnType)

    #plot stock prices & save data to a file
    cp.plot_returns(returns)
    fr.save_to_file(returns, 'Returns')

    print('4. Calculate Expected Mean Return & Covariance')
    expected_returns = settings.AssetsExpectedReturnsFunction(returns)

    covariance = settings.AssetsCovarianceFunction(returns)

    #Plot & Save covariance to file
    cp.plot_correlation_matrix(returns)
    fr.save_to_file(covariance, 'Covariances')

    print('5. Use Monte Carlo Simulation')
    #Generate portfolios with allocations
    portfolios_allocations_df = mcs.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)

    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)

    #Plot portfolios, print max sharpe portfolio & save data

    cp.plot_portfolios(portfolio_risk_return_ratio_df)

    max_sharpe_portfolio = metricCalculator.get_max_sharpe_ratio(
        portfolio_risk_return_ratio_df)['Portfolio']
    max_shape_ratio_allocations = portfolios_allocations_df[[
        'Symbol', max_sharpe_portfolio
    ]]
    print("optimum portfolio ", max_shape_ratio_allocations)

    fr.save_to_file(portfolios_allocations_df, 'MonteCarloPortfolios')
    fr.save_to_file(portfolio_risk_return_ratio_df,
                    'MonteCarloPortfolioRatios')

    print('6. Use an optimiser')
    #Generate portfolios
    targets = settings.get_my_targets()
    print('targets', targets)
    optimiser = obj_factory.get_optimiser(targets, len(expected_returns.index))

    portfolios_allocations_df = optimiser.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)
    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)
    #plot efficient frontiers
    cp.plot_efficient_frontier(portfolio_risk_return_ratio_df)
    cp.show_plots()

    #save data
    print('7. Saving Data')
    fr.save_to_file(portfolios_allocations_df, 'OptimisationPortfolios')
    fr.close()
Example #2
0
def generate_optimum_portfolio():
    """ Stepwise generation of optimum portfolios, using the monte carlo
    approach and then a Markowitz optimization approach. """
    # Initialize objects with the settings
    obj_factory = object_factory(settings)
    ce = obj_factory.get_companies_extractor()
    cp = obj_factory.get_charts_plotter()
    mcs = obj_factory.get_portfolio_generator()
    fr = obj_factory.get_file_repository()
    mc = obj_factory.get_metrics_calculator()
    st = obj_factory.get_strategies()

    print('1. Get companies')
    companies = ce.get_companies_list()
    price_extractor = obj_factory.get_price_extractor(companies)

    print('2. Get company stock prices')
    end_date = settings.get_end_date()
    start_date = settings.get_start_date(end_date)
    closing_prices = price_extractor.get_prices(start_date, end_date)
    sma1 = mc.get_sma(closing_prices, settings.SMA1)
    sma2 = mc.get_sma(closing_prices, settings.SMA2)

    # Plot stock prices & save data to a file
    #cp.plot_prices(closing_prices)
    positions = st.strategy_sma(closing_prices, sma1, sma2)
    #cp.plot_sma(sma1,sma2,positions)
    #fr.save_to_file(closing_prices, 'StockPrices')

    print('3. Calculate Daily Returns')
    returns = settings.DailyAssetsReturnsFunction(closing_prices,
                                                  settings.ReturnType)
    # Calculate return of investment
    investments = mc.calculate_investment_return(positions, returns)
    # sdsdsd
    #ols_capm = st.strategy_capm(returns, start_date)
    # Plot stock prices & save data to a file
    cp.plot_investments(investments)
    #cp.plot_returns(returns)
    fr.save_to_file(returns, 'Returns')

    print('4. Calculate Expected Mean Return & Covariance')
    expected_returns = settings.AssetsExpectedReturnsFunction(returns)
    covariance = settings.AssetsCovarianceFunction(returns)

    # Plot & Save covariance to file
    #cp.plot_correlation_matrix(returns)
    fr.save_to_file(covariance, 'Covariances')

    print('5. Use Monte Carlo Simulation')
    # Generate portfolios with allocations
    portfolios_allocations_df = mcs.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)
    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)

    # Plot portfolios, print max sharpe portfolio & save data
    #cp.plot_portfolios(portfolio_risk_return_ratio_df)
    max_sharpe_portfolio = mc.get_max_sharpe_ratio(
        portfolio_risk_return_ratio_df)['Portfolio']
    max_shape_ratio_allocations = portfolios_allocations_df[[
        'Symbol', max_sharpe_portfolio
    ]]
    print(max_shape_ratio_allocations)
    portfolios_allocations_df = portfolios_allocations_df.T
    fr.save_to_file(portfolios_allocations_df, 'MonteCarloPortfolios')
    fr.save_to_file(portfolio_risk_return_ratio_df,
                    'MonteCarloPortfolioRatios')

    print('6. Use an optimiser')
    # Generate portfolios
    targets = settings.get_my_targets()
    optimiser = obj_factory.get_optimiser(targets, len(expected_returns.index))
    portfolios_allocations_df = optimiser.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)
    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)
    max_sharpe_portfolio = mc.get_max_sharpe_ratio(
        portfolio_risk_return_ratio_df)['Portfolio']
    max_shape_ratio_allocations = portfolios_allocations_df[[
        'Symbol', max_sharpe_portfolio
    ]]
    print(max_shape_ratio_allocations)

    # Plot efficient frontiers
    #cp.plot_efficient_frontier(portfolio_risk_return_ratio_df)
    #cp.show_plots()

    # Save data
    print('7. Saving Data')
    portfolios_allocations_df = portfolios_allocations_df.T
    fr.save_to_file(portfolios_allocations_df, 'OptimisationPortfolios')
    fr.close()
Example #3
0
        expected_returns = settings.AssetsExpectedReturnsFunction(returns)
        cp.plot_expected_returns(expected_returns)

        # calculate covariance for efficient edge quantification
        covariance = settings.AssetsCovarianceFunction(returns)

        # visualize daily retunrs correlation
        cp.plot_correlation_scatter(returns)

        # visualize daily change correlation matrix
        cp.plot_correlation_matrix(returns)

    # monte carlo
    with st.spinner(f'...simulating {n_mc} of random portfolio'):
        # use an optimiser
        targets = settings.get_my_targets()
        optimiser = obj_factory.get_optimiser(targets,
                                              len(expected_returns.index))
        portfolios_allocations_df = optimiser.generate_portfolios(
            expected_returns, covariance, settings.RiskFreeRate)
        portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
            portfolios_allocations_df)
        portfolio_risk_return_allc_ratio_df = portfolios_allocation_mapper.map_to_risk_return_allc_ratio(
            portfolios_allocations_df)
        min_risk = mc.get_min_risk(portfolio_risk_return_allc_ratio_df)
        max_sr = mc.get_max_sharpe_ratio(portfolio_risk_return_allc_ratio_df)
        max_return = mc.get_max_return(portfolio_risk_return_allc_ratio_df)
        min_return = mc.get_min_return(portfolio_risk_return_allc_ratio_df)

        portfolios_allocations_df = mcs.generate_portfolios(
            expected_returns, covariance, settings.RiskFreeRate, n_mc)
Example #4
0
def generate_optimum_portfolio():

    #instantiate the objects with the settings
    obj_factory = object_factory(settings)
    ce = obj_factory.get_companies_extractor()
    cp = obj_factory.get_charts_plotter()
    mcs = obj_factory.get_portfolio_generator()
    fr = obj_factory.get_file_repository()
    mc = obj_factory.get_metrics_calculator()

    print('1. Get companies')
    companies = ce.get_companies_list()
    price_extractor = obj_factory.get_price_extractor(companies)
    print('2. Get company stock prices')

    end_date = settings.get_end_date()
    start_date = settings.get_start_date(end_date)
    closing_prices = price_extractor.get_prices(settings.PriceEvent,
                                                start_date, end_date)

    # vytvori graf ceny akcii a da do tabulky
    cp.plot_prices(closing_prices)
    fr.save_to_file(closing_prices, 'StockPrices')

    print('3. Calculate Daily Returns')
    returns = settings.DailyAssetsReturnsFunction(closing_prices,
                                                  settings.ReturnType)
    #graf dennich navratu a da do tabulky
    cp.plot_returns(returns)
    fr.save_to_file(returns, 'Returns')

    print('4. Calculate Expected Mean Return & Covariance')
    expected_returns = settings.AssetsExpectedReturnsFunction(returns)
    covariance = settings.AssetsCovarianceFunction(returns)
    #graf + da do tabulky
    cp.plot_correlation_matrix(returns)
    fr.save_to_file(covariance, 'Covariances')

    print('5. Use Monte Carlo Simulation')

    #generuje portfolia s alokaci
    portfolios_allocations_df = mcs.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)
    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)

    #graf portfolii, vytiskne sharpuv pomer a da do tabulky
    cp.plot_portfolios(portfolio_risk_return_ratio_df)
    max_sharpe_portfolio = mc.get_max_sharpe_ratio(
        portfolio_risk_return_ratio_df)['Portfolio']
    max_shape_ratio_allocations = portfolios_allocations_df[[
        'Symbol', max_sharpe_portfolio
    ]]
    print(max_shape_ratio_allocations)
    fr.save_to_file(portfolios_allocations_df, 'MonteCarloPortfolios')
    fr.save_to_file(portfolio_risk_return_ratio_df,
                    'MonteCarloPortfolioRatios')

    print('6. Use an optimiser')
    #generuje optimalni portfolia
    targets = settings.get_my_targets()
    optimiser = obj_factory.get_optimiser(targets, len(expected_returns.index))
    portfolios_allocations_df = optimiser.generate_portfolios(
        expected_returns, covariance, settings.RiskFreeRate)
    portfolio_risk_return_ratio_df = portfolios_allocation_mapper.map_to_risk_return_ratios(
        portfolios_allocations_df)

    #da do grafu efektivni hranici
    cp.plot_efficient_frontier(portfolio_risk_return_ratio_df)
    cp.show_plots()

    #ulozi tabulku
    print('7. Saving Data')
    fr.save_to_file(portfolios_allocations_df, 'OptimisationPortfolios')
    fr.close()