Skip to content

Instantly share code, notes, and snippets.

@romanmichaelpaolucci
Last active January 23, 2020 22:48
Show Gist options
  • Save romanmichaelpaolucci/fe495c27407748ac2de95f627b700ede to your computer and use it in GitHub Desktop.
Save romanmichaelpaolucci/fe495c27407748ac2de95f627b700ede to your computer and use it in GitHub Desktop.
Portfolio Optimization
class PortfolioOptimization:
"""
Class for optimizing a historic portfolio
"""
def __init__(self, table):
mu = expected_returns.mean_historical_return(table)
S = risk_models.sample_cov(table)
# Optimise for maximal Sharpe ratio
ef = EfficientFrontier(mu, S)
ef.max_sharpe() # Raw weights
self.cleaned_weights = ef.clean_weights()
print(self.cleaned_weights)
ef.portfolio_performance(verbose=True)
latest_prices = discrete_allocation.get_latest_prices(table)
self.allocation, self.leftover = discrete_allocation.portfolio(
self.cleaned_weights, latest_prices, total_portfolio_value=10000 # This value can be adjusted to your actual portfolio value
)
def report_discrete_allocation(self):
print(self.allocation)
print("Funds remaining: ${:.2f}".format(self.leftover))
def get_cleaned_weights(self):
return self.cleaned_weights
@jesusbravo38
Copy link

I've been following your article and I just wonder if something is missing with the following line of code:
expected_returns.mean_historical_return
I don't see where the "expected_returns" object it's been instantiated

@romanmichaelpaolucci
Copy link
Author

Hi Jesus, please check my github for the comprehensive implementation

https://github.com/RomanMichaelPaolucci/Automatic_Portfolio_Optimization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment