Using Data Bundle in a Script - zipline 1.0.0

751 views
Skip to first unread message

Peter Chan

unread,
May 23, 2016, 1:32:30 PM5/23/16
to Zipline Python Opensource Backtester
Hi there

Downloaded zipline 1.0.0 and the quantopian-quandl data bundle and looking to test it out from a script. Below snippet is using the old standard yahoo data way, not sure how to amend the code to make it work with the new bundle data.  Anyone knows how?

Thanks

Peter

if __name__ == '__main__':
    from datetime import datetime
    import pytz
    from zipline.algorithm import TradingAlgorithm
    from zipline.utils.factory import load_bars_from_yahoo
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Create and run the algorithm.
    algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data)

    # Set the simulation start and end dates
    start = datetime(2015, 1, 1, 0, 0, 0, 0, pytz.utc)
    end = datetime(2016, 5, 18, 0, 0, 0, 0, pytz.utc)
    
    # Get data
    data = load_bars_from_yahoo(stocks=['IBM', 'GLD', 'XOM', 'AAPL', 'MSFT', 'TLT', 'SHY'],start=start, end=end)
    
    # Run algorithm
    results = algo.run(data)

    analyze(results)

Joe Jevnik

unread,
May 23, 2016, 1:36:23 PM5/23/16
to Peter Chan, Zipline Python Opensource Backtester
To use the new data bundles from within python, see the `run_algorithm` function: http://www.zipline.io/appendix.html#zipline.run_algorithm. For most cases, you should be able to use the command line like: `$ zipline run -f algo.py -s <start> -e <end>`. Regardless of the method for running, you will need to run `$ zipline ingest` once to download the data.

--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cwe...@gmail.com

unread,
May 23, 2016, 3:37:24 PM5/23/16
to Zipline Python Opensource Backtester, epete...@gmail.com
Hi,

I'm running on windows, I'm getting an error when trying to write the asset to file. I have specified yahoo as the data source and registered the bundle followed by ingest.

I'm quite sure I have the proper permissions for the directories.

This is the point where I'm encountering an issue:

adjustment_db_writer = SQLiteAdjustmentWriter(
stack.enter_context(working_file(
adjustment_db_path(name, timestr, environ=environ),
)).path,
BcolzDailyBarReader(daily_bars_path),
bundle.calendar,
overwrite=True,

daily_bars_path is C:\Users\abcd\AppData\Local\Temp\tmp4xznyd

Stacktrace
  File "C:\Anaconda3\lib\site-packages\zipline\data\bundles\core.py", line 358, in ingest
    overwrite=True,
  File "C:\Anaconda3\lib\site-packages\zipline\data\us_equity_pricing.py", line 824, in __init__
    self.conn = sqlite3.connect(conn_or_path)
sqlite3.OperationalError: unable to open database file

Joe Jevnik

unread,
May 23, 2016, 3:43:52 PM5/23/16
to cwe...@gmail.com, Zipline Python Opensource Backtester, Peter Chan

cwengc, could you open an issue on the issue tracker here: https://github.com/quantopian/zipline/issues. In addition to the issue template, could you tell me what the value of `conn_or_path` is right before it is passed to connect?

Peter Chan

unread,
May 27, 2016, 5:24:35 AM5/27/16
to Zipline Python Opensource Backtester, epete...@gmail.com
Hi Joe

Ok I got it to run using the code below in spyder using quantopian- quandl data bundle from 2016-05-24 - also runs from command line.

FYI, tried to verify the result with Quantopian online but got a 10% difference in cumulative return.  But had a better result using the old yahoo data download method, where the difference was about 1%.

Thanks 

Peter

Here is my testing script:

import zipline
from zipline.api import order, record, symbol, set_slippage, slippage, set_commission, commission

def initialize(context):
    context.asset = 'AAPL'
    set_slippage(slippage.FixedSlippage(spread=0.0))
    set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.0))
    pass

def handle_data(context, data):
    order(symbol(context.asset), int(50000/data.current(symbol(context.asset), "price")))
    record(AAPL=data.current(symbol(context.asset), "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.algorithm_period_return.plot(ax=ax1)
    ax1.set_ylabel('Cumulative Return (%)')
    ax2 = plt.subplot(212, sharex=ax1)
    results.AAPL.plot(ax=ax2)
    ax2.set_ylabel('AAPL price (USD)')

    # Show the plot.
    plt.gcf().set_size_inches(18, 8)
    plt.show()
    
if __name__ == '__main__':
    from datetime import datetime
    import pytz
    from zipline.algorithm import TradingAlgorithm
    from zipline.utils.factory import load_from_yahoo

    # Create and run the algorithm.
    algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=1.0e6)

    # Set the simulation start and end dates
    start = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc)
    end = datetime(2014, 11, 1, 0, 0, 0, 0, pytz.utc)

    # Load price data from yahoo.
    #data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end)

    #results = algo.run(data)
    results = zipline.run_algorithm(start=start, end=end, initialize=initialize, capital_base=1.0e6, 
                                    handle_data=handle_data, bundle='quantopian-quandl')
    analyze(results=results)
    print('Portfolio cumulative return: {:.2%}'.format(results.algorithm_period_return.ix[-1]))




On Tuesday, May 24, 2016 at 1:36:23 AM UTC+8, Joe Jevnik wrote:
Reply all
Reply to author
Forward
0 new messages