本文整理汇总了Python中pandas_datareader.data.get_data_yahoo方法的典型用法代码示例。如果您正苦于以下问题:Python data.get_data_yahoo方法的具体用法?Python data.get_data_yahoo怎么用?Python data.get_data_yahoo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas_datareader.data
的用法示例。
在下文中一共展示了data.get_data_yahoo方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_sp500
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_sp500(start_date, end_date):
"""
Gets sp500 price data
:param start_date: starting date for sp500 prices
:type start_date: string of date "Y-m-d"
:param end_date: end date for sp500 prices
:type end_date: string of date "Y-m-d"
:return: sp500_data.csv
"""
i = 1
try:
sp500_all_data = pdr.get_data_yahoo("SPY", start_date, end_date)
except ValueError:
print("ValueError, trying again")
i += 1
if i < 5:
time.sleep(10)
get_stock_data(start_date, end_date)
else:
print("Tried 5 times, Yahoo error. Trying after 2 minutes")
time.sleep(120)
get_stock_data(start_date, end_date)
sp500_data = sp500_all_data["Adj Close"]
sp500_data.to_csv("sp500_data.csv")
示例2: get_index_data
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_index_data(self, index='SP500'):
"""
Get historical OHLC data from Yahoo! Finance for the chosen index
for given date range.
Parameter:
- index: String representing the index you want data for,
supported indices include:
- 'SP500' for S&P 500,
- 'DOW' for Dow Jones Industrial Average,
- 'NASDAQ' for NASDAQ Composite Index
Check the `available_tickers` property for more.
Returns:
A pandas dataframe with the index data.
"""
if index not in self.available_tickers:
raise ValueError(
'Index not supported. '
f"Available tickers are: {', '.join(self.available_tickers)}"
)
return web.get_data_yahoo(
self.get_index_ticker(index), self.start, self.end
)
示例3: get_ticker_data
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_ticker_data(self, ticker):
"""
Get historical OHLC data for given date range and ticker.
Tries to get from Investors Exchange (IEX), but falls back
to Yahoo! Finance if IEX doesn't have it.
Parameter:
- ticker: The stock symbol to lookup as a string.
Returns:
A pandas dataframe with the stock data.
"""
try:
data = web.DataReader(ticker, 'iex', self.start, self.end)
data.index = pd.to_datetime(data.index)
except:
data = web.get_data_yahoo(
ticker, self.start, self.end
)
return data
示例4: fetch_yahoo_data
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def fetch_yahoo_data(ticker, start_date, end_date, fname, max_attempt, check_exist):
if (os.path.exists(fname) == True) and check_exist:
print("file exist")
else:
# remove exist file
if os.path.exists(fname):
os.remove(fname)
for attempt in range(max_attempt):
time.sleep(2)
try:
dat = data.get_data_yahoo(''.join("{}".format(
ticker)), start=start_date, end=end_date)
dat.to_csv(fname)
except Exception as e:
if attempt < max_attempt - 1:
print('Attempt {}: {}'.format(attempt + 1, str(e)))
else:
raise
else:
break
开发者ID:jason887,项目名称:Using-Deep-Learning-Neural-Networks-and-Candlestick-Chart-Representation-to-Predict-Stock-Market,代码行数:22,代码来源:get_data.py
示例5: yf
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def yf(ticker, field, start=None, end=None, mrefresh=False):
if field is None:
field = 'Adj Close'
tmp = pdata.get_data_yahoo(ticker, start=start, end=end)
if tmp is None:
raise ValueError('failed to retrieve data for %s:%s' % (ticker, field))
if field:
return tmp[field]
else:
return tmp
示例6: get_data
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_data(name, start, end):
data = web.get_data_yahoo(name, start, end)
del data['Adj Close'] # we don't need Adj Close
data['Return'] = (data['Close'] - data['Open']) / data['Open']
return data
示例7: __init__
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def __init__(self, tk, start, end):
self.tk = tk
self.start = start
self.end = end
all_data = pdr.get_data_yahoo(self.tk, start=self.start, end=self.end)
self.stock_data = pd.DataFrame(all_data['Adj Close'], columns=["Adj Close"])
self.stock_data["log"] = np.log(self.stock_data)-np.log(self.stock_data.shift(1))
示例8: get_stock_data
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_stock_data(ticker, start_date, end_date):
"""
Gets historical stock data of given tickers between dates
:param ticker: company, or companies whose data is to fetched
:type ticker: string or list of strings
:param start_date: starting date for stock prices
:type start_date: string of date "YYYY-mm-dd"
:param end_date: end date for stock prices
:type end_date: string of date "YYYY-mm-dd"
:return: stock_data.csv
"""
i = 1
try:
all_data = pdr.get_data_yahoo(ticker, start_date, end_date)
except ValueError:
print("ValueError, trying again")
i += 1
if i < 5:
time.sleep(10)
get_stock_data(ticker, start_date, end_date)
else:
print("Tried 5 times, Yahoo error. Trying after 2 minutes")
time.sleep(120)
get_stock_data(ticker, start_date, end_date)
stock_data = all_data["Adj Close"]
stock_data.to_csv("stock_prices.csv")
示例9: back_test
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def back_test(strategy, seq_len, ticker, start_date, end_date, dim):
"""
A simple back test for a given date period
:param strategy: the chosen strategy. Note to have already formed the model, and fitted with training data.
:param seq_len: length of the days used for prediction
:param ticker: company ticker
:param start_date: starting date
:type start_date: "YYYY-mm-dd"
:param end_date: ending date
:type end_date: "YYYY-mm-dd"
:param dim: dimension required for strategy: 3dim for LSTM and 2dim for MLP
:type dim: tuple
:return: Percentage errors array that gives the errors for every test in the given date range
"""
data = pdr.get_data_yahoo(ticker, start_date, end_date)
stock_data = data["Adj Close"]
errors = []
for i in range((len(stock_data)//10)*10 - seq_len - 1):
x = np.array(stock_data.iloc[i: i + seq_len, 1]).reshape(dim) / 200
y = np.array(stock_data.iloc[i + seq_len + 1, 1]) / 200
predict = strategy.predict(x)
while predict == 0:
predict = strategy.predict(x)
error = (predict - y) / 100
errors.append(error)
total_error = np.array(errors)
print(f"Average error = {total_error.mean()}")
# If you want to see the full error list then print the following statement
# print(errors)
示例10: get_symbol_returns_from_yahoo
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def get_symbol_returns_from_yahoo(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'] = pd.to_datetime(px['date'])
px.set_index('date', drop=False, inplace=True)
rets = px[['adjclose']].pct_change().dropna()
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
示例11: build_stock_dataset
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def build_stock_dataset(start=START_DATE, end=END_DATE):
"""
Creates the dataset containing all stock prices
:returns: stock_prices.csv
"""
statspath = "intraQuarter/_KeyStats/"
ticker_list = os.listdir(statspath)
# Required on macOS
if ".DS_Store" in ticker_list:
os.remove(f"{statspath}/.DS_Store")
ticker_list.remove(".DS_Store")
# Get all Adjusted Close prices for all the tickers in our list,
# between START_DATE and END_DATE
all_data = pdr.get_data_yahoo(ticker_list, start, end)
stock_data = all_data["Adj Close"]
# Remove any columns that hold no data, and print their tickers.
stock_data.dropna(how="all", axis=1, inplace=True)
missing_tickers = [
ticker for ticker in ticker_list if ticker.upper() not in stock_data.columns
]
print(f"{len(missing_tickers)} tickers are missing: \n {missing_tickers} ")
# If there are only some missing datapoints, forward fill.
stock_data.ffill(inplace=True)
stock_data.to_csv("stock_prices.csv")
示例12: build_sp500_dataset
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def build_sp500_dataset(start=START_DATE, end=END_DATE):
"""
Creates the dataset containing S&P500 prices
:returns: sp500_index.csv
"""
index_data = pdr.get_data_yahoo("SPY", start=START_DATE, end=END_DATE)
index_data.to_csv("sp500_index.csv")
示例13: build_dataset_iteratively
# 需要导入模块: from pandas_datareader import data [as 别名]
# 或者: from pandas_datareader.data import get_data_yahoo [as 别名]
def build_dataset_iteratively(
idx_start, idx_end, date_start=START_DATE, date_end=END_DATE
):
"""
This is an alternative iterative solution to building the stock dataset, which may be necessary if the
tickerlist is too big.
Instead of downloading all at once, we download ticker by ticker and append to a dataframe.
This will download data for tickerlist[idx_start:idx_end], which makes this method suitable
for chunking data.
:param idx_start: (int) the starting index of the tickerlist
:param idx_end: (int) the end index of the tickerlist
"""
statspath = "intraQuarter/_KeyStats/"
ticker_list = os.listdir(statspath)
df = pd.DataFrame()
for ticker in ticker_list:
ticker = ticker.upper()
stock_ohlc = pdr.get_data_yahoo(ticker, start=date_start, end=date_end)
if stock_ohlc.empty:
print(f"No data for {ticker}")
continue
adj_close = stock_ohlc["Adj Close"].rename(ticker)
df = pd.concat([df, adj_close], axis=1)
df.to_csv("stock_prices.csv")