How To: Python Pandas get current stock data
Asked Answered
A

6

9

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.

Amytal answered 3/6, 2013 at 18:30 Comment(0)
O
8
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
Ozone answered 6/2, 2014 at 20:32 Comment(0)
A
2

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

Amytal answered 3/6, 2013 at 20:57 Comment(0)
Y
2

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()
Youngyoungblood answered 5/9, 2015 at 5:3 Comment(0)
T
1

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.

Taipan answered 3/6, 2013 at 18:49 Comment(2)
Since in R, the quantmod package can resolve this problem by adding a column in time series data.Amytal
You can add columns to pandas easily, but you don't have the data from yahoo to fill it...I'm actually surprised that pandas doesn't make it go all the way to the date and fill those rows with NANTaipan
K
0

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
Kickapoo answered 27/4, 2018 at 17:52 Comment(0)
H
0

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.

Heathheathberry answered 19/5, 2020 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.