waditu / tushare

TuShare is a utility for crawling historical data of China stocks
BSD 3-Clause "New" or "Revised" License
12.79k stars 4.28k forks source link

Python3支持 / 指数数据 / 整合进zipline #18

Closed ahxxm closed 8 years ago

ahxxm commented 9 years ago
  1. 理论上把print改一下就支持python3了,不过我没仔细看代码。
  2. 获取不了创业板R(399606)之类的数据,get_hist_data和get_h_data分别报错,AssertionError: 15 columns passed, passed data had 14 columnsAttributeError: 'list' object has no attribute 'keys'
  3. zipline 是Quantopian的一个算法测试框架,目前不支持中国的证券数据……
ahxxm commented 9 years ago

... 工作比较忙,不然可以帮帮你 :(

ahxxm commented 9 years ago

我自己的问题解决了,在雪球找到的,不过似乎不支持带参数的查询,链接类似这样 http://xueqiu.com/S/SZ399606/historical.csv

jimmysoa commented 9 years ago

谢谢 目前已经同时支持python2.x和python 3.x,难道你不能在3下使用? 对于你提到的指数,get_hist_data确实不能获取,因为目前只支持上证和深圳几个重点指数,而get_h_data是可以的,你需要这么使用:ts.get_h_data('399606', index=True)

再次感谢你得关注

ahxxm commented 9 years ago

前阵子不能,有些print报错 感谢,明天去感受一下这个用法和python3下的安装 如果我想提交代码,是不是应该fork一个,写好代码和测试然后提pull request?

jimmysoa commented 9 years ago

你要更新升级一下tushare 是的,欢迎提交pull request。

ahxxm commented 9 years ago
pipe.write(text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 180-187: ordinal not in range(128)

help(ts.get_h_data)会有这个问题,是因为代码里有中文吧?

ahxxm commented 9 years ago

python3 ts.get_index()里name会显示成utf-8字符串 \u4e0a\u8bc1\u6307\u6570

ccoossdddffdd commented 9 years ago

如果能跟 zipline 整合在一起就大赞了呢~

jimmysoa commented 9 years ago

希望吧,哈哈。需要努力

laotao commented 9 years ago

ts.get_h_data('399606', index=True)执行报错: TypeError: get_h_data() got an unexpected keyword argument 'index'

jimmysoa commented 9 years ago

@laotao 升级一下TuShare

zezhou commented 8 years ago

tushare 集成 zipline 只用稍微转换一下 tushare 的数据格式, 使 tushare 的数据格式和 zipline 的格式匹配就可以了。

以 创业板etf为例:

from collections import OrderedDict                                                                                                                                       
import tushare
from pandas import DataFrame, to_datetime
import pytz
from zipline import TradingAlgorithm
from zipline.api import order, record, symbol

SYMBOL = '399006'

def load_bars_from_tushare(stocks, start, end):
    data = OrderedDict()
    for stock in stocks:
        tushare_data = tushare.get_hist_data(stock, start = start , end = end)
        tushare_data.index = to_datetime(tushare_data.index).tz_localize(pytz.utc)
        data[stock] = tushare_data.sort_index()

    close_key = 'close'
    df = DataFrame({key: d[close_key] for key, d in data.iteritems()})
    df.index.name = 'Date'
    return df

# following lines come from zipline example in 'examples/buyapple.py' #

def initialize (context):
    pass

def handle_data(context, data):
    order(symbol(SYMBOL), 10) 
    record(**{SYMBOL: data[symbol(SYMBOL)].price})

def analyze(results=None):
    import matplotlib.pyplot as plt 
    # Plot the portfolio and asset data.
    ax1 = plt.subplot(211)
    results.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('Portfolio value (RMB)')
    ax2 = plt.subplot(212, sharex=ax1)
    results.get(SYMBOL).plot(ax=ax2)
    ax2.set_ylabel('%s price (RMB)' % SYMBOL)

    # Show the plot.
    plt.gcf().set_size_inches(18, 8)
    plt.show()

bars = load_bars_from_tushare( # Using tushare to get data which could be handled by zipline later.
    stocks = [SYMBOL],
    start = "2014-01-01",
    end = "2014-12-31",
)

algo = TradingAlgorithm(
    initialize = initialize,
    handle_data = handle_data,
    identifiers=[SYMBOL]
)

perf = algo.run(bars)
analyze(results = perf)       

主要参考我上面给出代码的 load_bars_from_tushare

ahxxm commented 8 years ago

ls代码df = DataFrame({key: d[close_key] for key, d in data.iteritems()})在我这个python3.4.3里报错,改成df = DataFrame({key: d[close_key] for key, d in dict(data).items()})就行了

zezhou commented 8 years ago

谢谢指出. 我的运行环境是 python 2.7