Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active September 21, 2023 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantra-go-algo/21d8d623c0640e9e1e79ca0ba2cae468 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/21d8d623c0640e9e1e79ca0ba2cae468 to your computer and use it in GitHub Desktop.
# Define the ticker list
tickers_list = ['AAPL', 'AMZN', 'MSFT', 'WMT']
# Import pandas and create a placeholder for the data
import pandas as pd
data = pd.DataFrame(columns=tickers_list)
# Fetch the data
import yfinance as yf
for ticker in tickers_list:
data[ticker] = yf.download(ticker, period='5y',)['Adj Close']
# Compute the returns of individual stocks and then compute the daily mean returns.
# The mean return is the daily portfolio returns with the above four stocks.
data = data.pct_change().dropna().mean(axis=1)
# Import Pyfolio
import pyfolio as pf
# Get the full tear sheet
pf.create_full_tear_sheet(data)
@Pmica
Copy link

Pmica commented Sep 13, 2023

I have an error when running this code, please see below. Any idea why this is ?


AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_27500\3084592832.py in ?()
17 # Import Pyfolio
18 import pyfolio as pf
19
20 # Get the full tear sheet
---> 21 pf.create_simple_tear_sheet(data)

~\AppData\Local\anaconda3\Lib\site-packages\pyfolio\plotting.py in ?(*args, **kwargs)
48 def call_w_context(*args, **kwargs):
49 set_context = kwargs.pop('set_context', True)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)

~\AppData\Local\anaconda3\Lib\site-packages\pyfolio\tears.py in ?(returns, positions, transactions, benchmark_rets, slippage, estimate_intraday, live_start_date, turnover_denom, header_rows)
355
356 if live_start_date is not None:
357 live_start_date = ep.utils.get_utc_timestamp(live_start_date)
358
--> 359 plotting.show_perf_stats(returns,
360 benchmark_rets,
361 positions=positions,
362 transactions=transactions,

~\AppData\Local\anaconda3\Lib\site-packages\pyfolio\plotting.py in ?(returns, factor_returns, positions, transactions, turnover_denom, live_start_date, bootstrap, header_rows)
644 APPROX_BDAYS_PER_MONTH)
645 perf_stats = pd.DataFrame(perf_stats_all, columns=['Backtest'])
646
647 for column in perf_stats.columns:
--> 648 for stat, value in perf_stats[column].iteritems():
649 if stat in STAT_FUNCS_PCT:
650 perf_stats.loc[stat, column] = str(np.round(value * 100,
651 1)) + '%'

~\AppData\Local\anaconda3\Lib\site-packages\pandas\core\generic.py in ?(self, name)
5985 and name not in self._accessors
5986 and self._info_axis._can_hold_identifiers_and_holds_name(name)
5987 ):
5988 return self[name]
-> 5989 return object.getattribute(self, name)

AttributeError: 'Series' object has no attribute 'iteritems'

@quantra-go-algo
Copy link
Author

Hi,

The error is due to some issue with the pyfolio library. We have modified the code and replaced pyfolio with pyfolio-reloaded. You can remove the existing version of pyfolio from your system, install pyfolio-reloaded and then rerun the code. It should run fine. The steps to remove the existing pyfolio library have been provided in the previous code cell in the blog.

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