Esempio n. 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()
Esempio n. 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()
Esempio n. 3
0
    # call functions
    cp = obj_factory.get_charts_plotter(
    )  # get stock charts over given time horizon
    mc = obj_factory.get_metrics_calculator()
    mcs = obj_factory.get_portfolio_generator()

    # get companies list by dataframe
    companies = settings.get_companies_list(ticker)

    # initialize price extractor
    price_extractor = obj_factory.get_price_extractor(companies)

    # get company stock prices
    end_date = settings.get_end_date()
    start_date = settings.get_start_date(end_date, n_yrs)

    with st.spinner('...getting company info'):
        closing_prices = price_extractor.get_prices(settings.PriceEvent,
                                                    start_date, end_date)

        # plot stock charts
        st.header('Exploretory Data Analysis')
        st.write('  ')
        with st.beta_expander('About Analysis'):
            st.write('Review historical stock performance as a start')
            st.write(
                'Then translate to daily percent change to see magnitude of daily stock fluctuations'
            )
            st.write(
                'Normalize stock perfomance starting as $1, then apply daily changes with all time horizon cumulated to predict how much $ changes did $1 stock at start has been made at last in the horizon'
Esempio n. 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()