Open zfsamzfsam opened 7 years ago
这个只是一个行情库而已,对其它回测框架的对接最好还是个人动手吧,毕竟回测框架一大堆,rainx有一些项目是用来对接zipline的,你可以看看
我来打个广告吧。我用这个pytdx以及 @rainx 大神的另外几项目,写了一个对接zipline的项目,最近才开始写的,还在不断的改进,你可以去看看 @zfsamzfsam https://github.com/JaysonAlbert/cn_zipline
我个人觉得zipline框架太大太复杂了,BackTrader/PyAlgoTrade比较小巧简单一些!!! 但zipline的一些模块单独来看还是很有意义的.
rainx大大好像自己有个回测框架的
@JaysonAlbert @zfsamzfsam @yutiansut 我们自己根据zipline1.0.2移植了一份,最初的版本是我来做的,现在交给同事在维护,目前主要是面向学校使用的.. backtrader 和 PyAlgoTrade 我用的比较少,还在摸索中.. 最近感觉自己各个方面的知识需要补充的比较多,主要是计算机科学的深入方面的知识和金融领域的入门知识,现在这个阶段还在学习中... ^_^ , 后面有了更深入的理解之后再下手来做
其实做一个回测系统不是本项目的重点,用rqalpha回测更好,或者改造rqalpha。
https://github.com/yutiansut/quantaxis 已经对接好了的框架
支持:
pytdx与backtrader对接
# author = 'keping.chu' from pytdx.hq import TdxHq_API from backtrader.feeds.pandafeed import PandasData import pandas
def select_market_code(code): code = str(code) if code[0] in ['5', '6', '9'] or code[:3] in ["009", "126", "110", "201", "202", "203", "204"]: return 1 return 0
WEEK = 5 DAY = 9 MONTH = 6 QUARTER = 10 YEAR = 11
class PyTdxPandasData(PandasData):
def __init__(self, host, port, symbol, mapsymbol, cyc=WEEK, period=5):
api = TdxHq_API()
with api.connect(host, port):
if symbol.startswith('399'):
pandasda = api.to_df(api.get_index_bars(cyc, select_market_code(symbol), symbol, 0, period))
else:
pandasda = api.to_df(api.get_security_bars(cyc, select_market_code(symbol), symbol, 0, period))
pandasda.to_csv(symbol+'.csv')
pandasda['datetime'] = pandas.to_datetime(pandasda['datetime'])
self.p.dataname = pandasda
if mapsymbol is None:
self.p.name = symbol
else:
self.p.name = mapsymbol
self.p.volume = 'vol'
self.p.datetime = -1
super(PyTdxPandasData, self).__init__()
class PyTdxPandasDataExt(PandasData):
def __init__(self, symbol, data):
self.p.dataname = data
self.p.name = symbol
self.p.volume = 'vol'
self.p.datetime = -1
super(PyTdxPandasDataExt, self).__init__()
class BatchTDXPandasData:
def __init__(self, symbols, host, port, cyc):
self.host = host
self.port = port
self.cyc = cyc
self.symbols = symbols
def get_data_list(self, size):
data = []
api = TdxHq_API()
with api.connect(self.host, self.port):
for symbol in self.symbols:
pandasda = api.to_df(api.get_security_bars(self.cyc, select_market_code(symbol), symbol, 0, size))
pandasda['datetime'] = pandas.to_datetime(pandasda['datetime'])
data.append(PyTdxPandasDataExt(symbol, pandasda))
return data
def get_data_dict(self, size):
data = {}
api = TdxHq_API()
with api.connect(self.host, self.port):
for symbol in self.symbols:
pandasda = api.to_df(api.get_security_bars(self.cyc, select_market_code(symbol), symbol, 0, size))
pandasda['datetime'] = pandas.to_datetime(pandasda['datetime'])
data[symbol] = PyTdxPandasDataExt(symbol, pandasda)
return data
@spidermachine nice,我后面争取把和一些量化平台关联使用的方法更新到文档里
backtrader https://github.com/mementum/backtrader 成熟度比较高,演进速度很快,文档全,社区还是存在的!!!
PyAlgoTrade/PyAlgoTrade-cn https://github.com/gbeced/pyalgotrade https://github.com/Yam-cn/pyalgotrade-cn 个人感觉PyAlgoTrade比backtrader的成熟度,易用性要差!!!