Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
help using zipline on non US stocks

Hi,

I've managed to overcome the issue on skipping the US holiday by adding the following - trading_calendar=get_calendar("LSE") -
twice in TradingAlgorithim - see below in bold. Output is now:

2017-01-13 16:30:00+00:00
2017-01-16 16:30:00+00:00
2017-01-17 16:30:00+00:00

But unfortunately I still have the problem when I use pipeline, I get this: ValueError: Mask shape (20, 1) != data shape (21, 1).

if name == 'main':

start = makeTS("2017-01-01"); end = makeTS("2017-1-31")  

# load the bundle  
bundle_data = load('csv2', os.environ, None)  
cal = bundle_data.equity_daily_bar_reader.trading_calendar.all_sessions  
pipeline_loader = USEquityPricingLoader(bundle_data.equity_daily_bar_reader, bundle_data.adjustment_reader)  
choose_loader = make_choose_loader(pipeline_loader)  
env = TradingEnvironment(bm_symbol='^FTSE', exchange_tz='Europe/London',trading_calendar=get_calendar("LSE"),  
                         asset_db_path=parse_sqlite_connstr(bundle_data.asset_finder.engine.url))  

data = DataPortal(  
    env.asset_finder, get_calendar("LSE"),  
    first_trading_day=bundle_data.equity_minute_bar_reader.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,  
)


# the actual running of the backtest happens in the TradingAlgorithm object  
bt_start = time()  
perf = TradingAlgorithm(  
    env=env,  
    get_pipeline_loader=choose_loader,  
    **trading_calendar=get_calendar("LSE"),**  
    sim_params=create_simulation_parameters(  
        start=start,  
        end=end,  
        capital_base=CAPITAL_BASE,  
        **trading_calendar=get_calendar("LSE"),**  
        data_frequency='daily'  
    ),  
    **{  
        'initialize': initialize,  
        'handle_data': handle_data,  
        'before_trading_start': None,  
        'analyze': None,  
    }  
).run(data, overwrite_sim_params=False,)  
bt_end = time()

Hi Guys,

I'm trying to run zipline locally on UK stocks but I haven't figured out how to stop it using US holidays. The date I'm testing with is 16th Jan 2017 (Martin Luther King Day)

My steps have been -

1) Downloaded data from yahoo into a csv file
2) Implemented the steps to ingest custom data as per this link http://www.prokopyshen.com/create-custom-zipline-data-bundle
3) Ran the ingest comment using the LSE calendar

from zipline.data.bundles import register  
from zipline.data.bundles.viacsv import viacsv  
from zipline.utils.calendars import get_calendar  
from zipline.utils.calendars import exchange_calendar_lse

eqSym = {  
    "CPI",  
}

    register(  
   'csv2',    # name this whatever you like  
   viacsv(eqSym),  
    calendar_name='LSE',  
)

4) Implemented the following code - and so far - bundle_data.equity_daily_bar_reader.trading_calendar.all_sessions has returned a UK looking calendar

 bundle_data = load('csv2', os.environ, None)  
 cal = bundle_data.equity_daily_bar_reader.trading_calendar.all_sessions  
 pipeline_loader = USEquityPricingLoader(bundle_data.equity_daily_bar_reader, bundle_data.adjustment_reader)  
 choose_loader = make_choose_loader(pipeline_loader)  
 env = TradingEnvironment(bm_symbol='^FTSE',  
 exchange_tz='Europe/London',asset_db_path=parse_sqlite_connstr(bundle_data.asset_finder.engine.url))

data = DataPortal(  
        env.asset_finder, get_calendar("LSE"),  
        first_trading_day=bundle_data.equity_minute_bar_reader.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,  
    )

5) But when I run it with a very simple test, I can see it skips 16th Jan 2017 which is a US holiday not a UK one by using get_datetime() in handle_data()

2017-01-12 21:00:00+00:00
2017-01-13 21:00:00+00:00
2017-01-17 21:00:00+00:00

6) And if I use pipeline it throws this error
ValueError: Mask shape (20, 1) != data shape (19, 1).

Any help will greatly be appreciated.

Thanks Stuart

1 response

Hi Stuart,

I've just responded to a similar issue on the zipline repo here in regards to fixing the calendar problem you're having.

Thanks,
Freddie

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.