首页 > 编程知识 正文

从0学量化投资 在mbp上用pip安装zipline

时间:2023-05-06 15:47:06 阅读:227908 作者:3367

很久前搭建过一套zipline+A股分析单机版,后来换了电脑,系统升级后,又重做,结果发现很多之前解决过的问题又需要google,很是麻烦,想必大家也会有类似问题,特此记录。

zipline最新版本 1.4.0 在最新的苹果笔记本系统里面是可以正常使用的,但在安装时有几个关键点需要解决。

1,首先补充必要运行依赖库 

brew install freetype pkg-config gcc openssl hdf5

2,zipline 1.4.0 正常使用仅限于python版本 2.7.X, 3.5.X, 3.6.X, 因此如果使用python 3的同学,必须要配合virtualenv来解决目前python3已经到了3.8.X问题。

3,如果想无脑安装,建议使用Conda或Anaconda安装zipline,因为这里面包含到依赖库到版本不存在兼容问题,但我一直嫌弃这两个安装包太大,启动好使,因此一直没用过,不确认会有啥问题,看官方文档和用户区讨论,应该是好使但。当然,还有一种更省事的办法,就是用docker,官方文档有说明,不再展开。

4,pip安装首先要确保pip为最新版本,在python2和python3共存的系统里,为了确保安装时不会相互干扰,建议使用

python2 -m pip install ...  或  python3 -m pip install ...

5,安装jupyter: pip install --user jupyter

 查看ipython内核版本: jupyter kernelspec list

安装ipython内核: python2 -m pip install ipykernel

6, 安装zipline之前,一定要手工先把几个关键的依赖包事先依次装好:bcolz==1.0.0, numpy, pandas, tornado==4.5.3, zipline

其中bcolz必须指定版本,否则会导致zipline安装时,出现编译bcolz库时失败,tornado指定版本,则是为了解决jupyter notebook启动出错问题

7,国内使用pip时,如果不用vpn,会很不稳定,建议使用镜像

pip config set global.index-url 'https://pypi.tuna.tsinghua.edu.cn/simple'

8,拉取交易基础数据首先要申请QUANDL_API_KEY,建议放入.zshrc启动脚本里面,将此作为环境变量

9, 量化模拟测试, 保存如下量化算法为t1.py,

执行

zipline ingestpython -m zipline run -f t1.py --start 2018-1-1 --end 2018-2-1 -o dma.pickle --no-benchmark # t1.py : to test ziplinefrom zipline.api import order_target, record, symboldef initialize(context): context.i = 0 context.asset = symbol('AAPL')def handle_data(context, data): # Skip first 300 days to get full windows context.i += 1 if context.i < 300: return # Compute averages # data.history() has to be called with the same params # from above and returns a pandas dataframe. short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean() long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean() # Trading logic if short_mavg > long_mavg: # order_target orders as many shares as needed to # achieve the desired number of shares. order_target(context.asset, 100) elif short_mavg < long_mavg: order_target(context.asset, 0) # Save values for later inspection record(AAPL=data.current(context.asset, 'price'), short_mavg=short_mavg, long_mavg=long_mavg)

出现错误:

/Users/zfluo/Library/Python/2.7/lib/python/site-packages/empyrical/stats.py:711: RuntimeWarning: invalid value encountered in divide  out=out,/Users/zfluo/Library/Python/2.7/lib/python/site-packages/empyrical/stats.py:797: RuntimeWarning: invalid value encountered in divide  np.divide(average_annual_return, annualized_downside_risk, out=out)[2020-09-23 05:39:49.139801] INFO: zipline.finance.metrics.tracker: Simulated 22 trading daysfirst open: 2018-01-02 14:31:00+00:00last close: 2018-02-01 21:00:00+00:00Fatal Python error: Acquisition count is -1 (line 22761)[1]    40885 abort   

 

原因:Cython版本不对,解决方法,重装Cython后,重装zipline

pip install Cython --install-option="--no-cython-compile"pip uninstall ziplinepip install zipline

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。