Sei sulla pagina 1di 16

30/01/22, 13:15 Untitled

Setup
In [1]:
import numpy as np

import pandas as pd

import yfinance as yf

import warnings

import riskfolio as rp

In [2]:
warnings.filterwarnings("ignore")

start = '2008-01-01'

end = '2022-01-28'

In [3]:
df = pd.read_excel('ticker.xlsx')

df.sort_values(by=['Ticker'], inplace=True)

df

Out[3]: Ticker Classe1 Classe2 Classe3 Nome

Azionario
27 BR50.MI Azioni Azionario EM iShares BRIC 50 UCITS ETF
globale

Azionario
15 CHIP.PA Azioni Azionario tematico Lyxor MSCI Semiconductors ESG
tematico

31 DBX0.DE Fondo/etf Globale Globale Xtrackers - Portfolio UCITS ETF

Azionario Xtrackers MSCI EM Asia Swap UCITS


25 DBX2.DE Azioni Azionario asia
globale ETF 1C

Azionario iShares EURO STOXX Small UCITS


9 DJSC.MI Azioni Azionario europeo
globale ETF

13 EMKT.MI Azioni Azionario EM Globale Lyxor MSCI Emerging Markets

Azionario
8 EUN.MI Azioni Azionario europeo iShares STOXX Europe 50 UCITS ETF
globale

Azionario
7 EXSA.MI Azioni Azionario europeo iShares STOXX Europe 600
globale

iShares Diversified Commodity


1 EXXY.MI Commodities Commodities Commodities
Swap

0 GBS.MI Commodities Oro Oro Gold

Azionario
26 IAPD.MI Azioni Azionario asia iShares Asia Pacific Dividend
globale

Obbligazioni iShares Euro Inflation Linked Govt


20 IBCI.MI Obbligazioni ObbligazioniEUR
inflation Bond

Obbligazioni iShares Euro Corp Bond Large Cap


18 IBCX.MI Obbligazioni ObbligazioniEUR
corporate UCITS

Obbligazioni mid-
24 IEF Obbligazioni ObbligazioniUS iShares 7-10 Year Treasury Bond ETF
term

Azionario
10 IEUX.MI Azioni Azionario europeo iShares MSCI Europe ex-UK UCITS
globale

Azionario
12 IFFF.MI Azioni Azionario EM iShares MSCI AC Far East ex-Japan
globale

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 1/16
30/01/22, 13:15 Untitled

Ticker Classe1 Classe2 Classe3 Nome

Azionario
16 IH2O.MI Azioni Azionario tematico iShares Global Water UCITS
tematico

Azionario Azionario
3 INAA.MI Azioni iShares MSCI North America
americano americano

Azionario
17 INRG.MI Azioni Azionario tematico iShares Global Clean Energy
tematico

Azionario Azionario iShares MSCI North America UCITS


32 IQQN.DE Azioni
americano americano ETF

Azionario Azionario
5 IUSA.MI Azioni iShares S&P 500 UCITS ETF
americano americano

Obbligazioni
19 LQDE.MI Obbligazioni ObbligazioniEUR iShares $ Corp Bond UCITS ETF
corporate

Azionario
29 NRJ.PA Azioni Azionario tematico Lyxor New Energy
tematico

Azionario
30 PRF Azioni Azionario globale Invesco FTSE RAFI US 1000
americano

Obbligazioni long
23 TLT Obbligazioni ObbligazioniUS iShares 20+ Year Treasury Bond ETF
term

Azionario Azionario
6 UST.PA Azioni Lyxor Nasdaq-100
americano americano

2 WLD.MI Azioni Azionario globale Globale Lyxor MSCI World

Obbligazioni long
22 X15E.MI Obbligazioni ObbligazioniEUR Eurozone Government Bond 15-30
term

Obbligazioni Obbligazioni
21 XGIN.MI Obbligazioni Global Inflation-Linked Bond
inflation globali

Azionario
14 XLPE.MI Azioni Azionario tematico Private Equity Swap
tematico

Azionario Azionario
4 XMUS.MI Azioni MSCI USA Swap UCITS ETF
americano americano

Azionario
11 XNIF.MI Azioni Azionario indiano Nifty 50 Swap
globale

28 XSFR.MI Azioni Azionario EM Globale S&P Select Frontier

In [4]:
ticker=df['Ticker'].values.tolist()

ticker

classe1=df['Classe1'].values.tolist()

classe2=df['Classe2'].tolist()

nome=df['Nome'].tolist()

In [5]:
data = yf.download(ticker, start=start, end=end,period='5d')

data = data.loc[:,('Adj Close', slice(None))]

data.columns=nome

[*********************100%***********************] 33 of 33 completed

In [6]:
Y = data[nome].pct_change().dropna()

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 2/16
30/01/22, 13:15 Untitled

Out[6]: Xtrackers iShares


Xtrackers iShares
iShares MSCI EM EURO Lyxor iShares
Lyxor MSCI - STOXX
BRIC 50 Asia STOXX MSCI STOXX
Semiconductors Portfolio Europe
UCITS Swap Small Emerging Europe
ESG UCITS 50 UCITS
ETF UCITS UCITS Markets 600
ETF ETF
ETF 1C ETF

Date

2008-
-0.004975 0.044888 0.009128 0.010551 -0.013663 0.037471 0.008057 0.000000
11-28

2008-
-0.004167 0.016706 -0.018278 0.004795 -0.004617 0.017813 0.010813 -0.012089
12-01

2008-
-0.063598 0.018779 -0.000314 -0.038637 -0.009278 -0.053108 -0.064186 0.000000
12-02

2008-
0.020554 -0.050691 0.003919 0.006001 -0.006689 0.014659 0.022366 0.000000
12-03

2008-
0.007005 -0.014563 -0.004874 -0.000906 0.034343 0.005025 0.004861 0.000000
12-04

... ... ... ... ... ... ... ... ...

2022-
-0.033874 -0.028179 -0.017778 -0.020798 -0.018892 -0.020506 -0.014586 -0.019271
01-21

2022-
0.000000 -0.049452 -0.023529 0.000000 0.000000 0.000000 0.000000 0.000000
01-24

2022-
0.000000 0.005975 0.005746 0.000000 0.000000 0.000000 0.000000 0.000000
01-25

2022-
0.000000 0.024259 0.016587 0.000000 0.000000 0.000000 0.000000 0.000000
01-26

2022-
0.000000 -0.019101 -0.003444 0.000000 0.000000 0.000000 0.000000 0.000000
01-27

3400 rows × 33 columns

Media Varianza senza vincoli


In [7]:
port = rp.Portfolio(returns=Y)

method_mu='hist'

method_cov='hist'

port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)

w_sharpe1 = port.optimization(model='Classic', rm='MV', obj='Sharpe', rf=0.00006, l=

In [8]:
ax = rp.plot_pie(w=w_sharpe1, title='Sharpe Mean Variance', others=0.05, nrow=25, cm
height=6, width=10, ax=None)

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 3/16
30/01/22, 13:15 Untitled

In [9]:
points = 50

model='Classic' # Could be Classic (historical), BL (Black Litterman) or FM (Factor


rm = 'MV' # Risk measure used, this time will be variance

obj = 'Sharpe' # Objective function, could be MinRisk, MaxRet, Utility or Sharpe

hist = True # Use historical scenarios for risk measures that depend on scenarios

rf = 0.00006 # Risk free rate

l = 0

frontier = port.efficient_frontier(model=model, rm=rm, points=points, rf=rf, hist=hi


label = 'Max Risk Adjusted Return Portfolio' # Title of point

mu = port.mu # Expected returns

cov = port.cov # Covariance matrix

returns = port.returns # Returns of the assets

ax = rp.plot_frontier(w_frontier=frontier, mu=mu, cov=cov, returns=returns, rm=rm,

rf=rf, alpha=0.05, cmap='viridis', w=w_sharpe1, label=label,

marker='*', s=16, c='r', height=6, width=10, ax=None)

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 4/16
30/01/22, 13:15 Untitled

cVar
In [10]:
port = rp.Portfolio(returns=Y)

method_mu='hist'

method_cov='hist'

port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)

w_cvar1 = port.optimization(model='Classic', rm='CVaR', obj='Sharpe', rf=0.00006, l=

In [11]:
ax = rp.plot_pie(w=w_cvar1, title='Sharpe CVaR', others=0.03, nrow=25, cmap = "tab20
height=6, width=10, ax=None)

Constraints
In [12]:
df1 = pd.read_excel('ticker.xlsx')

df1 = df1.sort_values(by=['Ticker'])

constraints= pd.read_excel('const.xlsx',na_filter=False)

In [13]:
df1= df1[ [ col for col in df1.columns if col != 'Ticker' ] + ['Ticker']]

df1= df1[ ['Nome'] + [ col for col in df1.columns if col != 'Nome' ]]

df1.drop('Ticker', axis=1, inplace=True)

df1.rename(columns={'Nome': 'Assets'}, inplace=True)

In [14]:
constraints

Out[14]: Type Relative


Disabled Type Set Position Sign Weight Relative Factor
Relative Set

Lyxor Nasdaq-
0 False Assets <= 0.00
100

1 False Classes Classe3 ObbligazioniUS <= 0.30

2 False Classes Classe3 Commodities >= 0.02

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 5/16
30/01/22, 13:15 Untitled

Type Relative
Disabled Type Set Position Sign Weight Relative Factor
Relative Set

Azionario
3 False Classes Classe2 >= 0.05
globale

Azionario
4 False Classes Classe2 >= 0.05
tematico

In [15]:
A, B = rp.assets_constraints(constraints, df1)

In [16]:
df1

Out[16]: Assets Classe1 Classe2 Classe3

27 iShares BRIC 50 UCITS ETF Azioni Azionario EM Azionario globale

15 Lyxor MSCI Semiconductors ESG Azioni Azionario tematico Azionario tematico

31 Xtrackers - Portfolio UCITS ETF Fondo/etf Globale Globale

Xtrackers MSCI EM Asia Swap UCITS ETF


25 Azioni Azionario asia Azionario globale
1C

9 iShares EURO STOXX Small UCITS ETF Azioni Azionario europeo Azionario globale

13 Lyxor MSCI Emerging Markets Azioni Azionario EM Globale

8 iShares STOXX Europe 50 UCITS ETF Azioni Azionario europeo Azionario globale

7 iShares STOXX Europe 600 Azioni Azionario europeo Azionario globale

1 iShares Diversified Commodity Swap Commodities Commodities Commodities

0 Gold Commodities Oro Oro

26 iShares Asia Pacific Dividend Azioni Azionario asia Azionario globale

20 iShares Euro Inflation Linked Govt Bond Obbligazioni Obbligazioni inflation ObbligazioniEUR

Obbligazioni
18 iShares Euro Corp Bond Large Cap UCITS Obbligazioni ObbligazioniEUR
corporate

Obbligazioni mid-
24 iShares 7-10 Year Treasury Bond ETF Obbligazioni ObbligazioniUS
term

10 iShares MSCI Europe ex-UK UCITS Azioni Azionario europeo Azionario globale

12 iShares MSCI AC Far East ex-Japan Azioni Azionario EM Azionario globale

16 iShares Global Water UCITS Azioni Azionario tematico Azionario tematico

Azionario
3 iShares MSCI North America Azioni Azionario americano
americano

17 iShares Global Clean Energy Azioni Azionario tematico Azionario tematico

Azionario
32 iShares MSCI North America UCITS ETF Azioni Azionario americano
americano

Azionario
5 iShares S&P 500 UCITS ETF Azioni Azionario americano
americano

Obbligazioni
19 iShares $ Corp Bond UCITS ETF Obbligazioni ObbligazioniEUR
corporate

29 Lyxor New Energy Azioni Azionario tematico Azionario tematico

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 6/16
30/01/22, 13:15 Untitled

Assets Classe1 Classe2 Classe3

Azionario
30 Invesco FTSE RAFI US 1000 Azioni Azionario globale
americano

Obbligazioni long
23 iShares 20+ Year Treasury Bond ETF Obbligazioni ObbligazioniUS
term

Azionario
6 Lyxor Nasdaq-100 Azioni Azionario americano
americano

2 Lyxor MSCI World Azioni Azionario globale Globale

Obbligazioni long
22 Eurozone Government Bond 15-30 Obbligazioni ObbligazioniEUR
term

Obbligazioni
21 Global Inflation-Linked Bond Obbligazioni Obbligazioni inflation
globali

14 Private Equity Swap Azioni Azionario tematico Azionario tematico

Azionario
4 MSCI USA Swap UCITS ETF Azioni Azionario americano
americano

11 Nifty 50 Swap Azioni Azionario indiano Azionario globale

28 S&P Select Frontier Azioni Azionario EM Globale

In [19]:
port.ainequality = A

port.binequality = B

model = 'Classic'

rm = 'CVaR'

obj = 'Sharpe'

rf = 0

w_personale = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)

In [20]:
ax = rp.plot_pie(w=w_personale, title='Constrained CVaR', others=0.05, nrow=25, cmap
height=6, width=10, ax=None)

In [22]:
constraints= pd.read_excel('prudente.xlsx',na_filter=False)

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 7/16
30/01/22, 13:15 Untitled

port = rp.Portfolio(returns=Y)

method_mu='hist'

method_cov='hist'

port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)

A, B = rp.assets_constraints(constraints, df1)

port.ainequality = A

port.binequality = B

model = 'Classic'

rm = 'CVaR'

obj = 'Utility'

rf = 0

w_prudente = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=0.05, hist=hist


ax = rp.plot_pie(w=w_prudente, title='PRUDENTE', others=0.002, nrow=10, cmap = "tab2
height=10, width=20, ax=None)

In [57]:
constraints= pd.read_excel('moderato.xlsx',na_filter=False)

port = rp.Portfolio(returns=Y)

method_mu='hist'

method_cov='hist'

port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)

A, B = rp.assets_constraints(constraints, df1)

port.ainequality = A

port.binequality = B

model = 'Classic'

rm = 'CVaR'

obj = 'Sharpe'

rf = 0

w_moderato = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)


ax = rp.plot_pie(w=w_moderato, title='BILANCIATO globale', others=0.01, nrow=10, cma
height=10, width=20, ax=None)

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 8/16
30/01/22, 13:15 Untitled

In [58]:
constraints= pd.read_excel('aggressivo.xlsx',na_filter=False)

port = rp.Portfolio(returns=Y)

method_mu='hist'

method_cov='hist'

port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)

A, B = rp.assets_constraints(constraints, df1)

port.ainequality = A

port.binequality = B

model = 'Classic'

rm = 'CVaR'

obj = 'Sharpe'

rf = 0

w_aggressivo = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=0.025, hist=h


ax = rp.plot_pie(w=w_aggressivo, title='Aggressivo', others=0.001, nrow=10, cmap = "
height=10, width=20, ax=None)

Backtesting
localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 9/16
30/01/22, 13:15 Untitled

In [59]: import bt

In [60]:
def varname(variable):

for name in list(globals().keys()):

expression = f'id({name})'

if id(variable) == eval(expression):

return name

In [61]:
rebalance = bt.algos.RunIfOutOfBounds(0.1)

rebalAlgo = bt.algos.Rebalance()

In [62]:
z = pd.read_csv('rayd.csv', sep=';')

z=z.dropna()

rayd=z.T

In [63]:
w_tot= [w_cvar1,w_sharpe1, w_const1,rayd, w_prudente, w_moderato, w_aggressivo]

In [64]:
i=0

a={}

for x in w_tot:

weight= bt.algos.WeighSpecified(**x.T)

var = bt.Strategy('static',[rebalance,weight,rebalAlgo])

backtest = bt.Backtest(

var,

data.dropna(),

integer_positions=False

r= bt.run(backtest)

i=i+1

if i==1:

s=r.stats

s=s.rename(columns={'static':varname(x)})

w_tot=pd.DataFrame(x)

else:

stats=r.stats

s[varname(x)]=stats['static']

a[varname(x)]=r.backtest_list[0].strategy.values

In [65]:
s

Out[65]: w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_ag

2008-11- 2008-11- 2008-11- 2008-11-


2008-11-30 2008-11-30 20
start 30 30 30 30
00:00:00 00:00:00
00:00:00 00:00:00 00:00:00 00:00:00

2022-01- 2022-01- 2022-01- 2022-01-


2022-01-21 2022-01-21 20
end 21 21 21 21
00:00:00 00:00:00
00:00:00 00:00:00 00:00:00 00:00:00

rf 0.0 0.0 0.0 0.0 0.0 0.0

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 10/16
30/01/22, 13:15 Untitled

w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_ag

total_return 2.144624 2.513417 1.726687 1.677839 1.702342 1.672219

cagr 0.091093 0.100339 0.079317 0.077834 0.078581 0.077661

max_drawdown -0.121633 -0.136201 -0.12828 -0.124002 -0.137186 -0.138296 -

calmar 0.748919 0.736702 0.618312 0.627679 0.572806 0.561557

mtd -0.032363 -0.035368 -0.02795 -0.030615 -0.021478 -0.027246 -

three_month -0.002401 -0.001857 -0.002352 0.014027 -0.00109 -0.002464

six_month 0.001456 0.005003 0.003453 0.021643 0.003842 0.004106

ytd -0.032363 -0.035368 -0.02795 -0.030615 -0.021478 -0.027246 -

one_year 0.044489 0.056005 0.049665 0.07681 0.039967 0.047499

three_year 0.112726 0.125764 0.100667 0.121001 0.095563 0.096905

five_year 0.080568 0.090326 0.068165 0.080619 0.06515 0.065716

ten_year 0.080021 0.08986 0.068123 0.069447 0.064214 0.066843

incep 0.091093 0.100339 0.079317 0.077834 0.078581 0.077661

daily_sharpe 1.966897 1.94069 1.841103 1.20472 1.746522 1.730886

daily_sortino 3.317355 3.236447 3.035891 2.041118 2.83886 2.826937

daily_mean 0.131684 0.144758 0.115289 0.115907 0.114464 0.113187

daily_vol 0.06695 0.074591 0.06262 0.096211 0.065538 0.065393

daily_skew -0.169382 -0.234477 -0.423598 0.162252 -0.582862 -0.470523

daily_kurt 9.845991 9.703609 12.199434 7.724538 12.96247 12.859226

best_day 0.034402 0.03733 0.030803 0.059901 0.02996 0.032193

worst_day -0.035612 -0.039344 -0.034904 -0.039147 -0.039018 -0.03837 -

monthly_sharpe 1.617374 1.599549 1.486191 1.120765 1.400272 1.412008

monthly_sortino 3.614823 3.529984 3.109981 2.545767 2.895224 2.867777

monthly_mean 0.08882 0.097657 0.077781 0.077389 0.077246 0.07633

monthly_vol 0.054916 0.061053 0.052336 0.06905 0.055165 0.054058

monthly_skew -0.083145 -0.094949 -0.149116 0.358207 -0.122187 -0.196742 -

monthly_kurt 0.275171 0.238638 0.704549 0.788746 1.12157 0.793847

best_month 0.049174 0.053587 0.046319 0.078355 0.053752 0.04861

worst_month -0.037026 -0.036963 -0.037619 -0.041203 -0.046584 -0.040305

yearly_sharpe 1.353888 1.364742 1.255822 0.880498 1.182673 1.235879

yearly_sortino 9.944035 10.064092 9.179719 4.910869 9.802529 8.12231

yearly_mean 0.085766 0.094882 0.074206 0.075347 0.073407 0.072449

yearly_vol 0.063348 0.069524 0.059089 0.085573 0.062069 0.058621

yearly_skew -0.308885 -0.259794 -0.418927 0.097305 -0.290507 -0.358365 -

yearly_kurt -0.686157 -0.539371 -1.001213 -1.3052 -1.245971 -0.761155 -

best_year 0.175923 0.196211 0.153104 0.206497 0.150443 0.157436

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 11/16
30/01/22, 13:15 Untitled

w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_ag

worst_year -0.032363 -0.035368 -0.02795 -0.050373 -0.021478 -0.027246 -

avg_drawdown -0.007344 -0.008214 -0.006986 -0.012941 -0.007868 -0.00778 -

avg_drawdown_days 18.353234 17.690821 19.502591 33.370968 21.96 20.950276

avg_up_month 0.01526 0.016765 0.013468 0.018336 0.013584 0.013561

avg_down_month -0.010079 -0.011632 -0.010528 -0.012453 -0.01151 -0.011171

win_year_perc 0.857143 0.857143 0.857143 0.714286 0.785714 0.857143

twelve_month_win_perc 0.966216 0.972973 0.945946 0.837838 0.925676 0.945946

In [66]:
wealth=pd.DataFrame.from_dict(a)

In [67]:
wealth

Out[67]: w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_agg

2008-
1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.0000
11-30

2008-
1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.0000
12-01

2008-
9.964175e+05 9.949578e+05 1.003242e+06 9.797872e+05 1.000663e+06 9.978744e+05 9.8386
12-02

2008-
1.007656e+06 1.007094e+06 1.010001e+06 9.850978e+05 1.009595e+06 1.008028e+06 9.9947
12-03

2008-
1.014509e+06 1.013938e+06 1.014442e+06 1.000277e+06 1.015306e+06 1.014837e+06 1.0102
12-04

... ... ... ... ... ... ...

2022-
3.179693e+06 3.554511e+06 2.757186e+06 2.675647e+06 2.725522e+06 2.701050e+06 4.2593
01-14

2022-
3.160042e+06 3.532198e+06 2.741207e+06 2.661191e+06 2.711661e+06 2.684599e+06 4.2303
01-18

2022-
3.153983e+06 3.526102e+06 2.733788e+06 2.669247e+06 2.708908e+06 2.679852e+06 4.2263
01-19

2022-
3.160083e+06 3.534126e+06 2.737264e+06 2.684444e+06 2.713226e+06 2.682720e+06 4.2397
01-20

2022-
3.144624e+06 3.513417e+06 2.726687e+06 2.677839e+06 2.702342e+06 2.672219e+06 4.2039
01-21

2232 rows × 7 columns

In [68]:
wealth.plot(figsize=(25,15))

Out[68]: <AxesSubplot:>

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 12/16
30/01/22, 13:15 Untitled

df['Cumulative'] = df.Profit.cumsum().round(2)
df['HighValue'] = df['Cumulative'].cummax()

df['Drawdown'] = df['Cumulative'] - df['HighVal']

In [69]:
#w_name= [w_cvar1,w_sharpe1, w_const1, w_const2,w_worst,rayd

In [70]:
w_tot= [w_cvar1,w_sharpe1, w_const1,rayd, w_prudente, w_moderato, w_aggressivo]

cs=pd.concat(w_tot, axis=1)

In [71]:
cs.to_csv('output1.csv',sep=";")

In [72]:
wealth

Out[72]: w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_agg

2008-
1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.0000
11-30

2008-
1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.000000e+06 1.0000
12-01

2008-
9.964175e+05 9.949578e+05 1.003242e+06 9.797872e+05 1.000663e+06 9.978744e+05 9.8386
12-02

2008-
1.007656e+06 1.007094e+06 1.010001e+06 9.850978e+05 1.009595e+06 1.008028e+06 9.9947
12-03

2008-
1.014509e+06 1.013938e+06 1.014442e+06 1.000277e+06 1.015306e+06 1.014837e+06 1.0102
12-04

... ... ... ... ... ... ...

2022-
3.179693e+06 3.554511e+06 2.757186e+06 2.675647e+06 2.725522e+06 2.701050e+06 4.2593
01-14

2022-
3.160042e+06 3.532198e+06 2.741207e+06 2.661191e+06 2.711661e+06 2.684599e+06 4.2303
01-18

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 13/16
30/01/22, 13:15 Untitled

w_cvar1 w_sharpe1 w_const1 rayd w_prudente w_moderato w_agg

2022-
3.153983e+06 3.526102e+06 2.733788e+06 2.669247e+06 2.708908e+06 2.679852e+06 4.2263
01-19

2022-
3.160083e+06 3.534126e+06 2.737264e+06 2.684444e+06 2.713226e+06 2.682720e+06 4.2397
01-20

2022-
3.144624e+06 3.513417e+06 2.726687e+06 2.677839e+06 2.702342e+06 2.672219e+06 4.2039
01-21

2232 rows × 7 columns

In [73]:
wealth_max=wealth.cummax()

In [74]:
dd= wealth - wealth_max

In [75]:
dd.plot(figsize=(20,10))

Out[75]: <AxesSubplot:>

In [76]:
dd['2020':'06/2020'].plot(figsize=(20,10), title='Max Drawdown during Covid19 Outbre

Out[76]: <AxesSubplot:title={'center':'Max Drawdown during Covid19 Outbreak'}>

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 14/16
30/01/22, 13:15 Untitled

In [77]:
dd['2013':'2015'].plot(figsize=(20,10), title='Max Drawdown during Euro Crisis')

Out[77]: <AxesSubplot:title={'center':'Max Drawdown during Euro Crisis'}>

In [78]:
dd['2008':'2009'].plot(figsize=(20,10), title='Max Drawdown during Subrime Crisis')

Out[78]: <AxesSubplot:title={'center':'Max Drawdown during Subrime Crisis'}>

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 15/16
30/01/22, 13:15 Untitled

In [79]:
s.to_csv('Stats1.csv',sep=";")

In [80]:
dd.to_csv('drawdown.csv',sep=";")

In [81]:
wealth.to_csv('wealth.csv',sep=";")

In [ ]:

In [ ]:

localhost:8888/lab/tree/Desktop/Lazy/Untitled.ipynb 16/16

Potrebbero piacerti anche