diff --git a/zipline/finance/sources.py b/zipline/finance/sources.py index 0aba2186..bfa9e86f 100644 --- a/zipline/finance/sources.py +++ b/zipline/finance/sources.py @@ -90,7 +90,7 @@ class RandomEquityTrades(TradeDataSource): class SpecificEquityTrades(TradeDataSource): """ - Generates a random stream of trades for testing. + Generates a non-random stream of trades for testing. """ def init(self, event_list): diff --git a/zipline/gens/composites.py b/zipline/gens/composites.py index 1b710212..ac24c7ce 100644 --- a/zipline/gens/composites.py +++ b/zipline/gens/composites.py @@ -9,7 +9,7 @@ from zipline.gens.transform def PreTransformLayer(sources): """A generator that takes a list of sources and runs their output through a FeedGen.""" - not_finished = len_ + not_finished = len #NOT DONE while not_finished: diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index 6404c403..619721e4 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -16,11 +16,24 @@ def mock_prices(n, rand = False): return (float(i % 11) for i in xrange(1,n+1)) def mock_volumes(n, rand = False): - """Does the same as mock_prices. Different function name - for readability.""" - return mock_prices(n, rand) + """Utility to generate a set of volumes. By default cycles + through values from 100 to 1000, incrementing by 50. Optional + flag to give random values between 100 and 1000. """ + + if rand: + return (random.randrange(100, 1000) for i in xrange(n)) + else: + return ((i * 50)%900 + 100 for i in xrange(n)) + +def fuzzy_dates(count = 500): + """Add +-10 seconds to each event from a date_gen. Note that + this still guarantees sorting, since the default is minute separation + of events.""" + for date in date_gen(n = count): + yield date + timedelta(seconds = random.randint(-10, 10)) def SpecificEquityTrades(count = 500, sids = [1, 2], event_list = None, filter = None): + """Returns the first n events of event_list if specified. Otherwise generates a sensible stream of events.""" @@ -43,7 +56,25 @@ def SpecificEquityTrades(count = 500, sids = [1, 2], event_list = None, filter = return filtered -if __name__ == "__main__": +def RandomEquityTrades(count = 500, sids = [1,2], filter = None): + dates = fuzzy_dates(500) + prices = mock_prices(500, rand = True) + volumes = mock_volumes(500, rand = True) + sids = cycle(iter(sids)) + + arg_gen = izip(sids, prices, volumes, dates) - import nose.tools; nose.tools.set_trace() - trades = SpecificEquityTrades(filter = [1]) + unfiltered = (create_trade(*args) for args in arg_gen) + + if filter: + filtered = ifilter(lambda event: event.sid in filter, unfiltered) + else: + filtered = unfiltered + return filtered + +if __name__ == "__main__": + rand = RandomEquityTrades() + pass +# x = mock_volumes(500) +# import nose.tools; nose.tools.set_trace() +# trades = SpecificEquityTrades(filter = [1]) diff --git a/zipline/gens/utils.py b/zipline/gens/utils.py index efe81a14..1060cd24 100644 --- a/zipline/gens/utils.py +++ b/zipline/gens/utils.py @@ -17,7 +17,7 @@ def mock_raw_event(sid, dt): return event def date_gen(start = datetime(2012, 6, 6, 0), delta = timedelta(minutes = 1), n = 100): - return (start + i * delta for i in xrange(n)) + return (start + (i * delta) for i in xrange(n)) def alternate(g1, g2): for e1, e2 in izip_longest(g1, g2):