Open JVillella opened 4 years ago
Looks like google finance API support has been removed from pandas datareader package: https://pydata.github.io/pandas-datareader/stable/index.html (top notice).
Is there any way to switch the data source to iex or something else?
Same problem
Versions
Pyfolio version: 0.9.2 Python version: 3.6.10 Pandas version: 1.0.5 Matplotlib version: 3.2.2
Is there any way to switch the data source to iex or something else?
For now, I manually calculate benchmark returns using a data API of my choice.
Patch it like this:
from pandas_datareader import data as web
import empyrical
import pyfolio as pf
import pandas as pd
import numpy as np
def get_max_drawdown_underwater_f(underwater):
"""
Determines peak, valley, and recovery dates given an 'underwater'
DataFrame.
An underwater DataFrame is a DataFrame that has precomputed
rolling drawdown.
Parameters
----------
underwater : pd.Series
Underwater returns (rolling drawdown) of a strategy.
Returns
-------
peak : datetime
The maximum drawdown's peak.
valley : datetime
The maximum drawdown's valley.
recovery : datetime
The maximum drawdown's recovery.
"""
#valley = np.argmin(underwater) # end of the period
valley = underwater.index[np.argmin(underwater)] # end of the period
# Find first 0
peak = underwater[:valley][underwater[:valley] == 0].index[-1]
# Find last 0
try:
recovery = underwater[valley:][underwater[valley:] == 0].index[0]
except IndexError:
recovery = np.nan # drawdown not recovered
return peak, valley, recovery
def get_symbol_returns_from_yahoo_f(symbol, start=None, end=None):
"""
Wrapper for pandas.io.data.get_data_yahoo().
Retrieves prices for symbol from yahoo and computes returns
based on adjusted closing prices.
Parameters
----------
symbol : str
Symbol name to load, e.g. 'SPY'
start : pandas.Timestamp compatible, optional
Start date of time period to retrieve
end : pandas.Timestamp compatible, optional
End date of time period to retrieve
Returns
-------
pandas.DataFrame
Returns of symbol in requested period.
"""
try:
px = web.get_data_yahoo(symbol, start=start, end=end)
px['date'] = px.index.to_list()
#px['date'] = px['date'].apply(lambda x: pd.Timestamp(x))
#px['date'] = pd.to_datetime(px['date'])
#px['date'] = pd.to_datetime(px['date'], unit='s')
px.set_index('date', drop=False, inplace=True)
#px.index.rename('date',inplace=True)
rets = px[['Adj Close']].pct_change().dropna()
rets.rename(columns={"Adj Close": "adjclose"},inplace=True)
except Exception as e:
warnings.warn(
'Yahoo Finance read failed: {}, falling back to Google'.format(e),
UserWarning)
px = web.get_data_google(symbol, start=start, end=end)
rets = px[['Close']].pct_change().dropna()
rets.index = rets.index.tz_localize("UTC")
rets.columns = [symbol]
return rets
empyrical.utils.get_symbol_returns_from_yahoo = get_symbol_returns_from_yahoo_f
pf.timeseries.get_max_drawdown_underwater = get_max_drawdown_underwater_f
Please fix this issue. Library totally useless!
This looks like a great library, a lot of work was done here. It is so sad that it is completely abandoned.
This looks like a great library, a lot of work was done here. It is so sad that it is completely abandoned.
Truly said bro... Please if anyone can fix and merge... https://hjlabs.in/
I have got it working a bit still it does not count the real answers to all the output values. Please someone help.
seems the issue is datareader source google is called for me, when I want it to call the working yahoo source instead. Quantopian also seems to have gone off the grid :/
Patch it like this:
from pandas_datareader import data as web import empyrical import pyfolio as pf import pandas as pd import numpy as np def get_max_drawdown_underwater_f(underwater): """ Determines peak, valley, and recovery dates given an 'underwater' DataFrame. An underwater DataFrame is a DataFrame that has precomputed rolling drawdown. Parameters ---------- underwater : pd.Series Underwater returns (rolling drawdown) of a strategy. Returns ------- peak : datetime The maximum drawdown's peak. valley : datetime The maximum drawdown's valley. recovery : datetime The maximum drawdown's recovery. """ #valley = np.argmin(underwater) # end of the period valley = underwater.index[np.argmin(underwater)] # end of the period # Find first 0 peak = underwater[:valley][underwater[:valley] == 0].index[-1] # Find last 0 try: recovery = underwater[valley:][underwater[valley:] == 0].index[0] except IndexError: recovery = np.nan # drawdown not recovered return peak, valley, recovery def get_symbol_returns_from_yahoo_f(symbol, start=None, end=None): """ Wrapper for pandas.io.data.get_data_yahoo(). Retrieves prices for symbol from yahoo and computes returns based on adjusted closing prices. Parameters ---------- symbol : str Symbol name to load, e.g. 'SPY' start : pandas.Timestamp compatible, optional Start date of time period to retrieve end : pandas.Timestamp compatible, optional End date of time period to retrieve Returns ------- pandas.DataFrame Returns of symbol in requested period. """ try: px = web.get_data_yahoo(symbol, start=start, end=end) px['date'] = px.index.to_list() #px['date'] = px['date'].apply(lambda x: pd.Timestamp(x)) #px['date'] = pd.to_datetime(px['date']) #px['date'] = pd.to_datetime(px['date'], unit='s') px.set_index('date', drop=False, inplace=True) #px.index.rename('date',inplace=True) rets = px[['Adj Close']].pct_change().dropna() rets.rename(columns={"Adj Close": "adjclose"},inplace=True) except Exception as e: warnings.warn( 'Yahoo Finance read failed: {}, falling back to Google'.format(e), UserWarning) px = web.get_data_google(symbol, start=start, end=end) rets = px[['Close']].pct_change().dropna() rets.index = rets.index.tz_localize("UTC") rets.columns = [symbol] return rets empyrical.utils.get_symbol_returns_from_yahoo = get_symbol_returns_from_yahoo_f pf.timeseries.get_max_drawdown_underwater = get_max_drawdown_underwater_f
This is awesome, Thank you! Anyone knows how to add dates to the charts?
Hey ya'll! Try out: https://github.com/stefan-jansen/pyfolio-reloaded. Looks like a solid, updated fork.
This is the decent successor, https://github.com/ranaroussi/quantstats
Running,
https://github.com/quantopian/pyfolio/blob/4b901f6d73aa02ceb6d04b7d83502e5c6f2e81aa/pyfolio/examples/single_stock_example.ipynb
Fails on,
With,
Versions