我们可以很容易的将美国股票导入到zipline中,但是因为quantopian-quandl没有捆绑非美国股票,因此如何用zipline来回测国内数据是一个需要解决的问题。
参考这个篇文章IMPORTING SOUTH AFRICAN EQUITIES DATA INTO ZIPLINE我们来具体讲一下如何将南非数据导入到zipline中进行回测。
我们根据buyapple.py示例写一个模拟2000-2016年期间购买10个Adcorp股票投资组合,与buyapple.py不同在于使用的是Adcorp股票,其余基本完全相同。
buyadcorp.py
from zipline.api import order, record, symbol
def initialize(context):
pass
def handle_data(context, data):
order(symbol('JSE:ADR'), 10)
record(AAPL=data.current(symbol('JSE:ADR'), 'price'))
# Note: this function can be removed if running
# this algorithm on quantopian.com
def analyze(context=None, 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 (ZAR)')
ax2 = plt.subplot(212, sharex=ax1)
results.AAPL.plot(ax=ax2)
ax2.set_ylabel('AAPL price (ZAR)')
# Show the plot.
plt.gcf().set_size_inches(