Skip to content

Commit

Permalink
Merge 75f1001 into f8e84ec
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainKanuk committed Jun 30, 2014
2 parents f8e84ec + 75f1001 commit 5d2ae1f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tests/test_algorithm.py
Expand Up @@ -98,6 +98,12 @@ def test_record_incr(self):

np.testing.assert_array_equal(output['incr'].values,
range(1, len(output) + 1))
np.testing.assert_array_equal(output['name'].values,
range(1, len(output) + 1))
np.testing.assert_array_equal(output['name2'].values,
[2] * len(output))
np.testing.assert_array_equal(output['name3'].values,
range(1, len(output) + 1))


class TestMiscellaneousAPI(TestCase):
Expand Down
13 changes: 10 additions & 3 deletions zipline/algorithm.py
Expand Up @@ -21,7 +21,7 @@

from datetime import datetime

from itertools import groupby
from itertools import groupby, chain
from six.moves import filter
from six import iteritems, exec_
from operator import attrgetter
Expand Down Expand Up @@ -467,11 +467,18 @@ def add_transform(self, transform_class, tag, *args, **kwargs):
'kwargs': kwargs}

@api_method
def record(self, **kwargs):
def record(self, *args, **kwargs):
"""
Track and record local variable (i.e. attributes) each day.
"""
for name, value in kwargs.items():
# Make 2 objects both referencing the same iterator
args = [iter(args)] * 2
# Zip now takes one from L1 and one from L2 and returns a tuple
# Because they point to the same iterator, the call of next on L1
# Will advance the pointer in L2, resulting in zip returning
# (a,b) (c,d) (e,f) rather than (a,a) (b,b) (c,c) etc.
positionals = zip(*args)
for name, value in chain(positionals, iteritems(kwargs)):
self._recorded_vars[name] = value

@api_method
Expand Down
3 changes: 3 additions & 0 deletions zipline/test_algorithms.py
Expand Up @@ -233,6 +233,9 @@ def initialize(self):
def handle_data(self, data):
self.incr += 1
self.record(incr=self.incr)
name = 'name'
self.record(name, self.incr)
record(name, self.incr, 'name2', 2, name3=self.incr)


class TestOrderAlgorithm(TradingAlgorithm):
Expand Down

0 comments on commit 5d2ae1f

Please sign in to comment.