zipline

Zipline - How to pass bundle DataPortal to TradeAlgorithm.run()?


I am trying to run a Zipline back test by calling the run() method of zipline.algorithm.TradeAlgorithm:

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

But I'm not sure what or how to pass the data parameter. I have already ingested the data bundle which is called 'quandl'. According to the docs, that parameter should receive a DataPortal instance, but I don't know how to create one of those based on the data I have ingested. What is the best way of doing this/is this necessary?

Essentially my goal is to create a top level 'dashboard' style class which can run multiple back tests using different strategies which exist in separate modules.

Full code (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"

Hopefully someone can point me in the right direction.

Thanks


Solution

  • I faced the same issue. When running the trading algorithm manually this way the bundle argument is not evaluated. You need to create the data portal yourself. I manually registered the bundle and created a data_portal to run it:

        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)