[Python] 取得金融市场数据

博客首页 » Python 取得金融市场数据

发布于 06 May 2016 15:58
标签 blog
有几种方法取得国际市场的金融数据。

本来Yahoo的YQL是非常好的工具,但是pm.finance是需要授权的。
现在只剩下Yahoo Finance的ichart还是可以得到的,只是不知道能维持到什么时候。
http://ichart.finance.yahoo.com/table.csv?s=AAPL
http://ichart.finance.yahoo.com/table.csv?s=000816.SZ

Google Finance的信息质量很好,是实时的,但是历史数据居然对于中国市场不开放下载。
纳斯达克之类的美国市场是没有问题的。
https://www.google.com/finance/historical?q=AAPL&startdate=2016-05-01&enddate=2016-05-31&output=csv

比如,这样是没有问题的
https://www.google.com/finance/historical?q=SHE:000816&startdate=2016-05-01&enddate=2016-05-31
一旦加上output=csv,就会有问题
https://www.google.com/finance/historical?q=SHE:000816&startdate=2016-05-01&enddate=2016-05-31&output=csv

对于Google Finance的实时数据,可以这样得到。
https://finance.google.com/finance/info?client=ig&q=SHE:000816

写成程序就是这样

import urllib2  # works fine with Python 2.7.9 (not 3.4.+)
import json
import time
 
def fetchPreMarket(symbol, exchange):
    link = "https://finance.google.com/finance/info?client=ig&q="
    url = link+"%s:%s" % (exchange, symbol)
    u = urllib2.urlopen(url)
    content = u.read()
    data = json.loads(content[3:])
    info = data[0]
    t = str(info["elt"])    # time stamp
    l = float(info["l"])    # close price (previous trading day)
    p = float(info["el"])   # stock price in pre-market (after-hours)
    return (t,l,p)
 
p0 = 0
while True:
    t, l, p = fetchPreMarket("000816","SHE")
    if(p!=p0):
        p0 = p
        print("%s\t%.2f\t%.2f\t%+.2f\t%+.2f%%" % (t, l, p, p-l,
                                                 (p/l-1)*100.))
    time.sleep(60)

Reference:
http://www.quantatrisk.com/2015/05/07/hacking-google-finance-in-pre-market-trading-python/
https://victorliew.quora.com/3-Ways-to-programatically-download-stuff-from-Yahoo-Financials


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Python 取得金融市场数据

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment