Esempio n. 1
0
    def efficient_portfolios(self, plot = True):
        max_sharpe = self.max_sharpe_ratio()
        min_vol = self.min_volatility()

        res_max = pf_valuation(weights=max_sharpe['x'], data=self._data,
                               risk_free_rate=self._risk_free_rate, freq=self._freq)

        returns_max = res_max.get('Returns')
        volatility_max = res_max.get('Volatility')

        res_min = pf_valuation(weights=min_vol['x'], data=self._data,
                               risk_free_rate=self._risk_free_rate, freq=self._freq)

        returns_min = res_min.get('Returns')
        volatility_min = res_min.get('Volatility')

        target = np.linspace(returns_min, returns_max, 50)
        self._efficient_portfolios = self.efficient_frontier(returns_range=target)

        if plot:
            plt.plot([p['fun'] for p in self._efficient_portfolios], target, 'k-x',
                     label='efficient frontier', color = 'g')

        return self._efficient_portfolios
Esempio n. 2
0
 def _pf_return(weights):
     ret = pf_valuation(weights=weights, data=self._data,
                        risk_free_rate=self._risk_free_rate,
                        freq=self._freq).get('Returns')
     return ret
Esempio n. 3
0
 def _neg_sortino_ratio(self, weights, data, risk_free_rate, freq=252):
     sharp = pf_valuation(weights=weights, data=data, risk_free_rate=risk_free_rate, freq=freq).get('Sortino')
     return -sharp
Esempio n. 4
0
pd.set_option('display.width', 150)

start_date = datetime.datetime(2018, 11, 20)
end_date = datetime.datetime(2020, 11, 20)
path = (Path.cwd() / '..' / "cache" / "moex.csv").resolve()
data = pd.read_csv(path, index_col=0, parse_dates=['TRADEDATE'])

pf1_data = pd.DataFrame(
    data,
    columns=['AFKS', 'APTK', 'LNZL', 'MAGEP', 'MRKS', 'PLZL', 'ROLO', 'SELG'])

pf1 = portfolio(data=pf1_data, risk_free_rate=0.01, freq=252)
pf1.mc(100000)
pf1.plot_portfolios()
pf1.print_results()
msr1 = pf1.max_sharpe_ratio()
print(np.round(msr1['x'], decimals=4))
pf1.efficient_portfolios()
# #
ef1 = efficient_frontier(data=pf1_data)
msr1 = ef1.max_sharpe_ratio()
mv1 = ef1.min_volatility()
#
res1 = pf_valuation(msr1['x'], data=pf1_data)
print(res1)
print(np.round(msr1['x'], decimals=4))

ef1.efficient_portfolios()

plt.show()
Esempio n. 5
0
# tickers = ['BABA', 'MA', 'AAPL', 'BA', 'TWTR', \
#            'MU', 'T', 'F', 'BIDU', 'BIIB', 'XOM', \
#            'DIS', 'PFE', 'BMY']

#tickers = ['AKRN', 'PIKK', 'PLZL', 'SELG']

data = download(source=Source.YFINANCE,
                tickers=tickers,
                start_date=start_date,
                end_date=end_date)
data = clean_data(data)
#data.to_csv(path)
#
#
pf = portfolio(data=data, risk_free_rate=0.01, freq=252)
pf.mc()
pf.plot_portfolios()
pf.print_results()
#
ef = sharp_efficient_frontier(data=data)
msr = ef.max_sharpe_ratio()
mv = ef.min_volatility()
#
res = pf_valuation(msr['x'], data=data)
print(res)
print(np.round(msr['x'], decimals=4))

ef.efficient_portfolios()

plt.show()
Esempio n. 6
0
                            freq=252,
                            num_portfolios=10000)
pf_sortino14_17.plot_portfolios()
pf_sortino14_17.print_results()
pf_proposed_sortino14_17 = pf_sortino14_17.max_sortino_port

print('=' * 80)
print('Sortino portfolio 17-20')
df_weights_sortino = pd.DataFrame(pf_proposed_sortino14_17,
                                  index=pf_stocks_sortino)
sortino_data17_20 = download(source=Source.YFINANCE,
                             tickers=pf_stocks_sortino,
                             start_date=start_date17_20,
                             end_date=end_date17_20)
sortino_data17_20 = clean_data(sortino_data17_20)
pf_val1 = pf_valuation(weights=df_weights_sortino, data=sortino_data14_17)
pf_val2 = pf_valuation(weights=df_weights_sortino, data=sortino_data17_20)

stocks_yearly_returns = mean_returns(sortino_data17_20)
returns = pd.concat([df_weights_sortino, stocks_yearly_returns],
                    keys=['Weights', 'Yearly returns'],
                    join='inner',
                    axis=1)
returns.columns = returns.columns.droplevel(1)
returns['Weighted return'] = returns['Weights'] * returns['Yearly returns']
ret17_20 = returns['Weighted return'].sum()
print(f'Yearly returns - 17 - 20 :{ret17_20}')

print(pf_val1)
print(pf_val2)