quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.61k stars 4.72k forks source link

Working with data older than 2002 #531

Closed scubamut closed 6 years ago

scubamut commented 9 years ago

see code and comment below

scubamut commented 9 years ago

Not sure whether really an issue or intended behavior....

I'm trying to use zipline with data from 1992, but zipline behaves unexpectedly for dates earlier than August 2002.

Here is the test code which reproduces the problem:

from zipline import TradingAlgorithm
from zipline.api import symbol, order_target_percent, get_datetime, schedule_function
from zipline.utils.events import date_rules, time_rules
import pandas as pd

data_path = 'E:\\NOTEBOOKS\\Quantopian\\extended etfs\\'
data = pd.read_pickle(data_path + 'EAAdata.pkl')

print ('START DATE : {}    END DATE : {}\n\n'.format(data.major_axis[0], data.major_axis[-1]))

start = '2001/1/1'
end = '2005/1/1'

data = data[:,start:end,:]

def initialize(context):

    context.active = [symbol('$DBC'), symbol('$EFA'), symbol('$IEF'), symbol('$IYR'), symbol('$TLT'), symbol('$VTI'), symbol('$VWO')]
    context.cash = symbol('$IEF')

    context.bill = symbol('$SHY')
    context.assets = set(context.active + [context.cash, context.bill])

    context.alloc = pd.Series([1./len(context.assets)] * len(context.assets), index=context.assets)

    context.rebalanced = False

    schedule_function(
        rebalance,
        date_rules.month_end(days_offset=0),
        time_rules.market_close(minutes=5)
    )

def handle_data(context, data):   
    if context.rebalanced:
        print('\n\n=====================================\n\nDATE {}\n\nPORTFOLIO HOLDINGS AND VALUE\n\n'.format(get_datetime()))
        positions = [(security,context.portfolio.positions[security].amount) \
                     for security in context.active if context.portfolio.positions[security].amount > 0] 
        print('\nPOSITIONS: {}\nVALUE = {}\n'.format(positions,context.portfolio.portfolio_value))

    context.rebalanced = False

def rebalance(context, data):
    print ('-------------------------\nREBALANCING.....\n\nDATE : {}\n\n{}\n'.format(get_datetime(),context.alloc))
    print ('\nPORTFOLIO VALUE: {}\nHOLDINGS: {}\n' \
           .format(context.portfolio.portfolio_value, context.portfolio.positions))
    for s in context.alloc.index:
        if s in data:
            print ('\nORDERING {} OF {} @PRICE {}\n'.format(context.alloc[s], s, data[s].price))
            order_target_percent(s, context.alloc[s])

    context.rebalanced = True

# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(initialize=initialize, 
                            handle_data=handle_data)

# Run algorithm
results = algo_obj.run(data)

Prior to July 2000, order_target_percent appears to do nothing; it starts working (partly) in July 2000 (doesn't order all the stocks) and only starts working as expected in August 2002.

You can get the data and the output here: data pickle file output

In any event, it would be really useful if zipline was not restricted to data earlier than 2002.

Rgds, Dave

freddiev4 commented 6 years ago

This should work in the most recent version of Zipline. Feel free to open if there are more problems. Also closing b/c this is an older issue.