I'm trying to get yesterday's closing stock prices for all stocks listed on the BSE using https://www.quandl.com/data/BSE-Bombay-Stock-Exchange as the data source. I have a list of company codes that I can use to pull that data but I need to figure out how to iterate over it correctly.
According to the quandl documentation, I can use quandl.get('BSE/BOM500002', column_index = '4', start_date='2019-03-19', end_date='2019-03-20') to get yesterday's closing price for a stock where BOM500002 would be the company code. If my list of company codes is listed in companyCodes['code'], could you help me figure out how to generate the company code dynamically to get yesterday's closing stock prices for all stocks listed on this exchange?
Bonus Question: How would I list the name of the stock next to the closing price?
Here is way to get date name together with the results:
import quandl
df = pd.DataFrame([("BOM500002", "ABB India Limited"),("BOM500003", "AEGIS")], columns=["Code", "Name"])
results = []
for i, r in df.iterrows():
result = quandl.get('BSE/'+r["Code"], column_index = '4', start_date='2019-03-19', end_date='2019-03-20')
result["Name"] = r["Name"]
results.append(result)
final = pd.concat(results)
Give this a try.
import quandl
quandl.ApiConfig.api_key = 'your quandl code'
stocks = [
'BSE/BOM533171',
'BSE/BOM500002'
]
mydata = quandl.get(stocks, start_date = '2019-03-19', end_date='2019-03-21')
mydata.loc[:,(mydata.columns.str.contains('Close'))].T
Related
I need to get historical stock data of S&P 500 including delisted tickers for backtesting in Python.
I tried to parse wiki (https://en.wikipedia.org/wiki/List_of_S%26P_500_companies), then download data
yf.download(delisted_tickers)
but i can't get big part of data because of that error
No data found, symbol may be delisted
So, are there other methods to get all data?
"Only Premium Plus members can view delisted company data"
Source : https://finance.yahoo.com/news/premium-plus-feature-historical-financial-201155209.html
im pretty sure yfinance only pulls stock data of current stocks, but you can give it a time frame, for example:
# Define the ticker list
import pandas as pd
tickers_list = ['AAPL', 'WMT', 'IBM', 'MU', 'BA', 'AXP']
# Fetch the data
import yfinance as yf
data = yf.download(tickers_list,'2015-1-1')['Adj Close']
# Print first 5 rows of the data
print(data.head())
I parsed a html table for financial transactions and have 3 different lists:
1. DATE
2. TICKER
3. MOTHER COMPANY
I would like to populate a stock prices for stocks from my TICKER list for a maximum possible period
I am new to python and cant figure out how to get the data for the stocks from my TICKER list... Any guidance would be of great help
Many thanks in advance
TICKERS
['OSR', 'NWSA', 'MNK', 'ZTS', 'FNAC', 'WWAV', 'NRZ', 'CST', 'BPY', 'ERA', 'AXLL', 'LMCAD', 'ABBV']
I am trying with a simple code but cant get through:
import yfinance as yf
for ticker in tickers:
data = yf.download(ticker, period="max")
The download function in yfinance accepts a list of tickers separated by spaces.
In order to download the data for all your tickers for a max period simply call it this way.
For example, if you want to download the data for 'OSR', 'NWA' and 'MNK':
import yfinance as yf
tickers = 'OSR NWA MNK'
data = yf.download(tickers, period='max')
You can then access each ticker's data using data[ticker].
If you have your tickers as a list and want to convert to a space-delimited string use join:
ticker_list = ['OSR', 'NWA', 'MNK']
ticker_str = ' '.join(ticker_list)
I'm gathering data from a bunch of ETFs through Yahoo Finance using Pandas-Datareader and I'm getting odd errors with a handful of the tickers even though the data seems available. The code is very simple:
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017,1,1)
for ticker in TICKERS:
f = dr.DataReader(ticker, 'yahoo', start, end)
and works for most of my tickers but not all:
EMLP GDVD (Failed to get data for GDVD) AMZA RFDI ARKK ARKW SECT (Failed to get data for SECT)
EMLP works fine. Datareader produces urls like this url for GDVD even though the historical data for GDVD is available on the website. I see the following error in Chrome using the GDVD url:
{"finance": {"error": {"code": "Unauthorized","description": "Invalid cookie"}}}
Is there a way to get historical prices for these tickers? The full list of failed tickers in case anyone can see a pattern:
['GDVD', 'SECT', 'DWLD', 'CCOR', 'DFNL', 'DUSA', 'AIEQ', 'CACG', 'QSY', 'ACT', 'TAXR', 'TTAI', 'FLIO', 'FMDG', 'VGFO', 'FFSG', 'LRGE', 'YLDE', 'VESH', 'DEMS', 'SQZZ']
Using the yahoo_fin package, I was able to get the data for the tickers you listed. Check out this link: http://theautomatic.net/yahoo_fin-documentation/.
My code looks like this:
from yahoo_fin.stock_info import get_data
tickers = ['GDVD', 'SECT', 'DWLD', 'CCOR', 'DFNL', 'DUSA', 'AIEQ', 'CACG',
'QSY', 'ACT', 'TAXR', 'TTAI', 'FLIO', 'FMDG', 'VGFO', 'FFSG',
'LRGE', 'YLDE', 'VESH', 'DEMS', 'SQZZ']
stocks = {}
for ticker in tickers:
stocks[ticker] = get_data(ticker)
So the data gets stored into a dictionary, where the keys are the tickers, and the values are the data frames containing each stock's data.
Alternatively, you could use a dictionary comprehension, like this:
stocks = {ticker : get_data(ticker) for ticker in tickers}
If you want to collapse all of the data sets into a single data frame, you could use the functools package like this:
from functools import reduce
combined = reduce(lambda x,y: x.append(y), stocks.values())
I'm trying to get the historical stock price data for all these tickers going back to 2014. All of these companies went public in 2014, so it will automatically get them from the day they first traded.
What I would like is for the stocklist list to contain at the end is a list of dataframes/price histories for each company, but separately and not put together.
So stocklist would be data frames/stock histories for each company, i.e. ['LC', 'ZAYO'] etc.
tickers = ['LC', 'ZAYO', 'GPRO', 'ANET', 'GRUB', 'CSLT', 'ONDK', 'QUOT', 'NEWR', 'ATEN']
stocklist = []
for i in tickers:
stock = Share(i)
adj = stock.get_historical('2014-1-1', '2016-12-27')
df = pd.DataFrame(adj)
df = df.set_index('Date')
df['Adj_Close'] = df['Adj_Close'].astype(float, errors='coerce')
price = df.sort()
i = price
stocklist.append(i)
You're not appending to stocklist inside the loop due to bad indentation.
Also, you're messing with the loop variable i needlessly.
This might work, although it's difficult to test since the Share class is not available:
tickers = ['LC', 'ZAYO', 'GPRO', 'ANET', 'GRUB',
'CSLT', 'ONDK', 'QUOT', 'NEWR', 'ATEN']
stocklist = []
for ticker in tickers:
stock = Share(ticker)
adj = stock.get_historical('2014-1-1', '2016-12-27')
df = pd.DataFrame(adj)
df.set_index('Date', inplace=True)
df['Adj_Close'] = df['Adj_Close'].astype(float, errors='coerce')
df.sort_index(inplace=True)
stocklist.append(df)
Changes I made:
use tickers as a variable name instead of list which is the name of a built-in type
set index and sort the dataframe in-place instead of making copies
use DataFrame.sort_index() for sorting since DataFrame.sort() is deprecated
fixed indentation so stocklist is populated inside the loop
removed the unnecessary assignment before stocklist appending
It might also be more useful to collect the dataframes in a dictionary keyed by tickers. So you would initialize stocklist = {} and instead of appending do stocklist[ticker] = df.
I've used:
data = DataReader("yhoo", "yahoo", datetime.datetime(2000, 1, 1),
datetime.datetime.today())
in pandas (python) to get history data of yahoo, but it cannot show today's price (the market has not yet closed) how can I resolve such problem, thanks in advance.
import pandas
import pandas.io.data
import datetime
import urllib2
import csv
YAHOO_TODAY="http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sd1ohgl1vl1"
def get_quote_today(symbol):
response = urllib2.urlopen(YAHOO_TODAY % symbol)
reader = csv.reader(response, delimiter=",", quotechar='"')
for row in reader:
if row[0] == symbol:
return row
## main ##
symbol = "TSLA"
history = pandas.io.data.DataReader(symbol, "yahoo", start="2014/1/1")
print history.tail(2)
today = datetime.date.today()
df = pandas.DataFrame(index=pandas.DatetimeIndex(start=today, end=today, freq="D"),
columns=["Open", "High", "Low", "Close", "Volume", "Adj Close"],
dtype=float)
row = get_quote_today(symbol)
df.ix[0] = map(float, row[2:])
history = history.append(df)
print "today is %s" % today
print history.tail(2)
just to complete perigee's answer, it cost me quite some time to find a way to append the data.
Open High Low Close Volume Adj Close
Date
2014-02-04 180.7 181.60 176.20 178.73 4686300 178.73
2014-02-05 178.3 180.59 169.36 174.42 7268000 174.42
today is 2014-02-06
Open High Low Close Volume Adj Close
2014-02-05 178.30 180.59 169.36 174.420 7268000 174.420
2014-02-06 176.36 180.11 176.00 178.793 5199297 178.793
Find a way to work around, just use urllib to fetch the data with:
http://download.finance.yahoo.com/d/quotes.csv?s=yhoo&f=sd1ohgl1l1v
then add it to dataframe
This code uses the pandas read_csv method to get the new quote from yahoo, and it checks if the new quote is an update from the current date or a new date in order to update the last record in history or append a new record.
If you add a while(true) loop and a sleep around the new_quote section, you can have the code refresh the quote during the day.
It also has duplicate last trade price to fill in the Close and the Adjusted Close, given that intraday close and adj close are always the same value.
import pandas as pd
import pandas.io.data as web
def get_quote_today(symbol):
url="http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=d1t1ohgl1vl1"
new_quote= pd.read_csv(url%symbol,
names=[u'Date',u'time',u'Open', u'High', u'Low',
u'Close', u'Volume', u'Adj Close'])
# generate timestamp:
stamp = pd.to_datetime(new_quote.Date+" "+new_quote.time)
new_quote.index= stamp
return new_quote.iloc[:, 2:]
if __name__ == "__main__":
symbol = "TSLA"
history = web.DataReader(symbol, "yahoo", start="2014/1/1")
print history.tail()
new_quote = get_quote_today(symbol)
if new_quote.index > history.index[-1]:
if new_quote.index[-1].date() == history.index[-1].date():
# if both quotes are for the first date, update history's last record.
history.iloc[-1]= new_quote.iloc[-1]
else:
history=history.append(new_quote)
history.tail()
So from trying this out and looking at the dataframe, it doesn't look too possible. You tell it to go from a specific day until today, yet the dataframe stops at may 31st 2013. This tells me that yahoo probably has not made it available for you to use in the past couple days or somehow pandas is just not picking it up. It is not just missing 1 day, it is missing 3.
If I do the following:
>>> df = DataReader("yhoo", "yahoo", datetime.datetime(2013, 6, 1),datetime.datetime.today())
>>> len(df)
0
it shows me that there simply is no data to pick up in those days so far. If there is some way around this then I cannot figure it out, but it just seems that the data is not available for you yet, which is hard to believe.
The module from pandas doesn't work anymore, because the google and yahoo doens't provide support anymore. So you can create a function to take the data direct from the Google Finance using the url. Here is a part of a code to do this
import csv
import datetime
import re
import codecs
import requests
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
You can wrote a function to get data from Google Finance using the url, you have to indent the parte bellow.
#You have to indent this part
def get_google_finance_intraday(ticker, period=60, days=1, exchange='NASD'):
"""
Retrieve intraday stock data from Google Finance.
Parameters
----------------
ticker : str
Company ticker symbol.
period : int
Interval between stock values in seconds.
i = 60 corresponds to one minute tick data
i = 86400 corresponds to daily data
days : int
Number of days of data to retrieve.
exchange : str
Exchange from which the quotes should be fetched
Returns
---------------
df : pandas.DataFrame
DataFrame containing the opening price, high price, low price,
closing price, and volume. The index contains the times associated with
the retrieved price values.
"""
# build url
url = 'https://finance.google.com/finance/getprices?p={days}d&f=d,o,h,l,c,v&q={ticker}&i={period}&x={exchange}'.format(ticker=ticker, period=period, days=days, exchange=exchange)
page = requests.get(url)
reader = csv.reader(codecs.iterdecode(page.content.splitlines(), "utf-8"))
columns = ['Open', 'High', 'Low', 'Close', 'Volume']
rows = []
times = []
for row in reader:
if re.match('^[a\d]', row[0]):
if row[0].startswith('a'):
start = datetime.datetime.fromtimestamp(int(row[0][1:]))
times.append(start)
else:
times.append(start+datetime.timedelta(seconds=period*int(row[0])))
rows.append(map(float, row[1:]))
if len(rows):
return pd.DataFrame(rows, index=pd.DatetimeIndex(times, name='Date'), columns=columns)
else:
return pd.DataFrame(rows, index=pd.DatetimeIndex(times, name='Date'))
Now you can just call the function with the ticket that you want, in my case AAPL and the result is a pandas DataFrame containing the opening price, high price, low price, closing price, and volume.
ticker = 'AAPL'
period = 60
days = 1
exchange = 'NASD'
df = get_google_finance_intraday(ticker, period=period, days=days)
df
The simplest way to extract Indian stock price data into Python is to use the nsepy library.
In case you do not have the nsepy library do the following:
pip install nsepy
The following code allows you to extract HDFC stock price for 10 years.
from nsepy import get_history
from datetime import date
dfc=get_history(symbol="HDFCBANK",start=date(2015,5,12),end=date(2020,5,18))
This is so far the easiest code I have found.