Skip to content

Commit

Permalink
BUG: When increasing the length dynamically, the rolling panel was
Browse files Browse the repository at this point in the history
getting filled with the wrong datetimes and causing errors.

Updates the logic for addressing missing datetimes and adds unit tests
for the 2 main cases (no missing datetimes, and some missing datetimes).
  • Loading branch information
llllllllll committed Nov 10, 2014
1 parent ca59abc commit 5a149d5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
53 changes: 53 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,59 @@ def handle_data(context, data):
' HistorySpec',
)

@parameterized.expand([
('zero', 1),
('non-zero', 2),
])
def test_history_grow_length_date_buf_delta(self, name, incr):
"""
Tests growing the length of a digest panel with different date_buf
deltas.
When incr=1, you will not be missing any dates, so there is nothing to
fill in and the date_buf delta is zero.
Anything greater that 0 will peek back in time and need to be filled
in.
"""
algo_text = dedent(
"""\
from zipline.api import history
def initialize(context):
context.bar_count = 1
def handle_data(context, data):
prices = history(context.bar_count, '1d', 'price')
context.test_case.assertEqual(len(prices), context.bar_count)
context.bar_count += {incr}
"""
).format(incr=incr)
start = pd.Timestamp('2007-04-05', tz='UTC')
end = pd.Timestamp('2007-04-10', tz='UTC')

sim_params = SimulationParameters(
period_start=start,
period_end=end,
capital_base=float("1.0e5"),
data_frequency='minute',
emission_rate='daily'
)

test_algo = TradingAlgorithm(
script=algo_text,
data_frequency='minute',
sim_params=sim_params
)
test_algo.test_case = self

source = RandomWalkSource(start=start, end=end)

self.assertIsNone(test_algo.history_container)
test_algo.run(source)


class TestHistoryContainerResize(TestCase):
@parameterized.expand(
Expand Down
12 changes: 8 additions & 4 deletions zipline/history/history_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,19 @@ def _resize_panel(self, panel, size, dt, freq, env=None):
old_cap = panel.cap
panel.resize(size)

delta = (old_cap - oldest_idx) - panel._oldest_frame_idx
# The number of days that we need to fill in.
delta = abs(oldest_idx - panel._oldest_frame_idx)
if not delta:
# We do not need to fill in any dates, jump out now.
return

# Backfill the missing dates of the new current window.
# Construct the missing dates.
missing_dts = self._create_window_date_buf(
delta, freq.unit_str, freq.data_frequency, oldest_dt,
)

# Fill the dates in between the new oldest index and adjusted oldest
# index.
# Fill the dates in between the new oldest index and adjusted
# oldest index.
where = slice(panel._oldest_frame_idx, -(old_cap - oldest_idx))
panel.date_buf[where] = missing_dts

Expand Down

0 comments on commit 5a149d5

Please sign in to comment.