-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarkowitzAllocation.py
More file actions
61 lines (46 loc) · 1.57 KB
/
Copy pathmarkowitzAllocation.py
File metadata and controls
61 lines (46 loc) · 1.57 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
markowitzAllocation.py
Created by Luca Camerani at 13/09/2020, University of Milano-Bicocca.
(l.camerani@campus.unimib.it)
All rights reserved.
This file is part of the EcoFin-Library (https://github.com/LucaCamerani/EcoFin-Library),
and is released under the "BSD Open Source License".
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from EcoFin.assetAllocation.markowitzModel import MarkowitzModel
from EcoFin.dataDownload.ticker import Ticker
market = ['AAPL', 'AMZN', 'CSCO', 'MSFT', 'GOOGL']
marketDataFrame = pd.DataFrame(columns=market)
for company in market:
ticker = Ticker(company)
df = ticker.getHistory().tail(260)
marketDataFrame[company] = df['Close']
model = MarkowitzModel(marketDataFrame)
randW = model.randomWeights(10000)
rets = []
stds = []
for r in randW:
performance = model.portfolioPerformance(r)
rets.append(performance.ret)
stds.append(performance.std)
plt.scatter(stds, rets, alpha=.4, linewidths=.1, marker=".", label='Random portfolios')
effWeights = model.computeEfficientFrontier(returnSpace=np.arange(0, 1, .01))
effRets = []
effStds = []
sharpe = []
for w in effWeights:
performance = model.portfolioPerformance(w)
effRets.append(performance.ret)
effStds.append(performance.std)
sharpe.append(performance.SR)
plt.plot(effStds, effRets, color='red', linewidth=2, label='Efficient frontier')
plt.ylabel("Return")
plt.legend()
plt.twinx()
plt.plot(effStds, sharpe, color='orange', linewidth=1, label='Sharpe Ratio')
plt.ylabel("Sharpe Ratio")
plt.legend()
plt.xlabel("Volatility")
plt.show()