mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-19 11:22:06 +08:00
generator random equity trades
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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:
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user