Zipline-如何将捆绑包DataPortal传递给TradeAlgorithm.run()?

时间:2018-09-09 15:19:51

标签: zipline

我正在尝试通过调用zipline.algorithm.TradeAlgorithm的run()方法来运行Zipline反向测试:

algo = TradingAlgorithm(initialize= CandlestickStrategy.initialize,
                            handle_data= CandlestickStrategy.handle_data,
                            analyze= CandlestickStrategy.analyze,
                            data=None,
                            bundle='quandl')
results = algo.run()

但是我不确定什么或如何传递data参数。我已经摄取了称为“ quandl”的数据包。根据文档,该参数应该接收一个DataPortal实例,但是我不知道如何根据我提取的数据创建其中一个。这样做的最好方法是什么?

本质上,我的目标是创建一个顶级的“仪表板”样式类,该类可以使用存在于单独模块中的不同策略来运行多个反向测试。

完整代码(dashboard.py):

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from mpl_finance import candlestick_ohlc
from datetime import datetime, date, tzinfo, timedelta
from dateutil import parser
import pytz
import numpy as np
import talib
import warnings
import logbook
from logbook import Logger
log = Logger('Algorithm')
from zipline.algorithm import TradingAlgorithm
from zipline.api import order_target_percent, order_target, cancel_order, get_open_orders, get_order, get_datetime, record, symbol
from zipline.data import bundles
from zipline.finance import execution
from CandlestickStrategy import CandlestickStrategy

warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")

# Choosing a security and a time horizon
logbook.StderrHandler().push_application()
start = datetime(2014, 9, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc)

#dataPortal = data_portal.DataPortal(asset_finder, trading_calendar, first_trading_day, e
#bundle = bundles.load('quandl',None,start)
algo = TradingAlgorithm(initialize= CandlestickStrategy.initialize,
                            handle_data= CandlestickStrategy.handle_data,
                            analyze= CandlestickStrategy.analyze,
                            data=None,
                            bundle='quandl')
results = algo.run()

CandleStickStrategy.py:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from mpl_finance import candlestick_ohlc
from zipline.api import order_target_percent, order_target, cancel_order, get_open_orders, get_order, get_datetime, record, symbol
from zipline.finance import execution
from datetime import datetime, date, tzinfo, timedelta
from dateutil import parser
import pytz
import numpy as np
import talib
import warnings

warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")

class CandlestickStrategy:

    def initialize(context):
        print "initializing algorythm..."
        context.i = 0
        context.asset = symbol('AAL')

    def handle_data(context, data):

        try:
            trailing_window = data.history(context.asset, ['open','high','low','close'], 28, '1d')
        except:
            return


    def analyze(context=None, results=None):

        print "Analyze"

希望有人能指出我正确的方向。

谢谢

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。以这种方式手动运行交易算法时,不评估bundle参数。您需要自己创建数据门户。我手动注册了捆绑软件并创建了一个data_portal来运行它:

    bundles.register('yahoo-xetra',
                     csvdir_equities(get_calendar("XETRA"), ["daily"],
                                     '/data/yahoo'),
                     calendar_name='XETRA')

    bundle_data = bundles.load(
        'yahoo-xetra',
    )

    first_trading_day = bundle_data.equity_daily_bar_reader.first_trading_day

    data = DataPortal(
        bundle_data.asset_finder,
        trading_calendar=get_calendar("XETRA"),
        first_trading_day=first_trading_day,
        equity_minute_reader=bundle_data.equity_minute_bar_reader,
        equity_daily_reader=bundle_data.equity_daily_bar_reader,
        adjustment_reader=bundle_data.adjustment_reader,
    )

    Strategy = SimpleAlgorithm(trading_calendar=get_calendar("XETRA"), data_frequency='daily',
                               start=pd.Timestamp('2017-1-1 08:00:00+0200', tz='Europe/Berlin'),
                               end=pd.Timestamp('2018-12-27 08:00:00+0200', tz='Europe/Berlin'),
                               capital_base=10000000,
                               data_portal=data)