-
Notifications
You must be signed in to change notification settings - Fork 390
/
PyPortfOpt.py
36 lines (27 loc) · 1.07 KB
/
PyPortfOpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
import pandas_datareader as pdr
import yfinance as yf
import datetime
stocks = ["AAPL","TSLA", "AMZN", "WMT", "AMD"]
n = 100000 # total port. value
start = datetime.datetime(2019,1,16)
end = datetime.datetime(2020,1,16)
df = pdr.DataReader(stocks, 'yahoo', start=start, end=end)['Close']
# Calculate expected returns and sample covariance
mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df)
# Optimise for maximal Sharpe ratio
ef = EfficientFrontier(mu, S)
raw_weights = ef.max_sharpe()
cleaned_weights = ef.clean_weights()
print(cleaned_weights)
ef.portfolio_performance(verbose=True)
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
latest_prices = get_latest_prices(df)
da = DiscreteAllocation(cleaned_weights, latest_prices, total_portfolio_value=n)
allocation, leftover = da.lp_portfolio()
print("Discrete allocation:", allocation)
print("Funds remaining: ${:.2f}".format(leftover))