Closed ahxxm closed 8 years ago
... 工作比较忙,不然可以帮帮你 :(
我自己的问题解决了,在雪球找到的,不过似乎不支持带参数的查询,链接类似这样 http://xueqiu.com/S/SZ399606/historical.csv
谢谢 目前已经同时支持python2.x和python 3.x,难道你不能在3下使用? 对于你提到的指数,get_hist_data确实不能获取,因为目前只支持上证和深圳几个重点指数,而get_h_data是可以的,你需要这么使用:ts.get_h_data('399606', index=True)
再次感谢你得关注
前阵子不能,有些print报错 感谢,明天去感受一下这个用法和python3下的安装 如果我想提交代码,是不是应该fork一个,写好代码和测试然后提pull request?
你要更新升级一下tushare 是的,欢迎提交pull request。
pipe.write(text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 180-187: ordinal not in range(128)
help(ts.get_h_data)会有这个问题,是因为代码里有中文吧?
python3
ts.get_index()里name
会显示成utf-8字符串 \u4e0a\u8bc1\u6307\u6570
如果能跟 zipline 整合在一起就大赞了呢~
希望吧,哈哈。需要努力
ts.get_h_data('399606', index=True)执行报错: TypeError: get_h_data() got an unexpected keyword argument 'index'
@laotao 升级一下TuShare
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
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()})
就行了
谢谢指出. 我的运行环境是 python 2.7
AssertionError: 15 columns passed, passed data had 14 columns
,AttributeError: 'list' object has no attribute 'keys'