如何:Python Pandas 获取当前股票数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16903416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:52:11  来源:igfitidea点击:

How To: Python Pandas get current stock data

pythonpandasyahooyahoo-finance

提问by perigee

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.

在 pandas (python) 中获取 yahoo 的历史数据,但无法显示今天的价格(市场尚未关闭)我该如何解决此问题,在此先感谢。

回答by Dyno Fu

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

回答by Ryan Saxe

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.

因此,通过尝试并查看数据框,它看起来不太可能。你告诉它从特定的一天到今天,但数据框在 2013 年 5 月 31 日停止。这告诉我雅虎可能在过去几天里没有让你使用它,或者不知何故Pandas只是没有拿起它. 不只是少了 1 天,而是少了 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.

它告诉我,到目前为止,在那些日子里根本没有数据可以提取。如果有办法解决这个问题,那么我无法弄清楚,但似乎数据还没有提供给您,这令人难以置信。

回答by perigee

Find a way to work around, just use urllib to fetch the data with:

找到一种解决方法,只需使用 urllib 来获取数据:

    http://download.finance.yahoo.com/d/quotes.csv?s=yhoo&f=sd1ohgl1l1v

then add it to dataframe

然后将其添加到数据框

回答by PabTorre

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.

此代码使用pandas read_csv 方法从雅虎获取新报价,并检查新报价是当前日期的更新还是新日期,以便更新历史记录中的最后一条记录或追加新记录。如果您在 new_quote 部分添加一个 while(true) 循环和睡眠,您可以让代码在白天刷新报价。考虑到日内收盘价和调整收盘价始终相同,它还具有重复的最后交易价格以填充收盘价和调整收盘价。

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()

回答by Emanuel Fontelles

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

pandas 的模块不再工作,因为 google 和 yahoo 不再提供支持。因此,您可以创建一个函数,使用 url 直接从 Google Finance 获取数据。这是执行此操作的代码的一部分

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.

您可以编写一个函数来使用 url 从 Google Finance 获取数据,您必须缩进下面的部分。

#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.

现在,您可以使用所需的票证调用该函数,在我的例子中为 AAPL,结果是一个包含开盘价、最高价、最低价、收盘价和交易量的 Pandas DataFrame。

ticker = 'AAPL'
period = 60
days = 1
exchange = 'NASD'

df = get_google_finance_intraday(ticker, period=period, days=days)
df

回答by Benjamin

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:

将印度股票价格数据提取到 Python 中的最简单方法是使用 nsepy 库。如果您没有 nsepy 库,请执行以下操作:

pip install nsepy

The following code allows you to extract HDFC stock price for 10 years.

以下代码允许您提取 10 年的 HDFC 股票价格。

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.

这是迄今为止我找到的最简单的代码。