Does anyone know why often the contract chains (as obtained from data.current_chain
) don't return all the contracts that they should? For example, from 2007-03-09 to 2007-03-16 the ES
chain returns a single contract. And even outside that returns only two contracts.
I was trying to roll the ES
root symbol as in this sample, but obviously that code breaks if data.current_chain
isn't returning at least the present contract and the following contract.
Algorithm:
from zipline.api import continuous_future, schedule_function
from zipline.api import time_rules, date_rules, record
from zipline.api import order_target_value, order_target_percent
from zipline.utils.calendars import get_calendar
import numpy as np
import scipy as sp
import pandas as pd
def initialize(context):
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=30))
def my_rebalance(context, data):
f = continuous_future("ES", offset=0, roll='volume', adjustment='mul')
chain = data.current_chain(f)
log.info("current_chain: {}".format(chain))
for c in chain:
log.info("{} will be closed on date {}".format(c, c.auto_close_date))
Logs:
2007-03-06 12:00 my_rebalance:20 INFO current_chain: [Future(1034200703 [ESH07]), Future(1034200706 [ESM07])]
2007-03-06 12:00 my_rebalance:23 INFO Future(1034200703 [ESH07]) will be closed on date 2007-03-14 00:00:00+00:00
2007-03-06 12:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-07 12:00 my_rebalance:20 INFO current_chain: [Future(1034200703 [ESH07]), Future(1034200706 [ESM07])]
2007-03-07 12:00 my_rebalance:23 INFO Future(1034200703 [ESH07]) will be closed on date 2007-03-14 00:00:00+00:00
2007-03-07 12:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-08 12:00 my_rebalance:20 INFO current_chain: [Future(1034200703 [ESH07]), Future(1034200706 [ESM07])]
2007-03-08 12:00 my_rebalance:23 INFO Future(1034200703 [ESH07]) will be closed on date 2007-03-14 00:00:00+00:00
2007-03-08 12:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-09 12:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-09 12:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-12 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-12 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-13 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-13 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-14 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-14 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-15 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-15 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-16 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07])]
2007-03-16 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-19 11:00 my_rebalance:20 INFO current_chain: [Future(1034200706 [ESM07]), Future(1034200709 [ESU07])]
2007-03-19 11:00 my_rebalance:23 INFO Future(1034200706 [ESM07]) will be closed on date 2007-06-13 00:00:00+00:00
2007-03-19 11:00 my_rebalance:23 INFO Future(1034200709 [ESU07]) will be closed on date 2007-09-19 00:00:00+00:00