diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index fdca878d..a96f5d98 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -144,7 +144,7 @@ class PerformanceTracker(object): """ - def __init__(self, trading_environment): + def __init__(self, trading_environment, sid_list): self.trading_environment = trading_environment self.trading_day = datetime.timedelta(hours = 6, minutes = 30) @@ -164,7 +164,6 @@ class PerformanceTracker(object): self.txn_count = 0 self.event_count = 0 self.last_dict = None - self.order_log = [] self.exceeded_max_loss = False self.results_socket = None @@ -198,9 +197,14 @@ class PerformanceTracker(object): keep_transactions = True ) - def set_sids(self, sid_list): for sid in sid_list: self.cumulative_performance.positions[sid] = Position(sid) + self.daily_performance.positions[sid] = Position(sid) + + def update(self, event): + event.perf_message = self.process_event() + event.portfolio = self.get_portfolio + return event def get_portfolio(self): return self.cumulative_performance.as_portfolio() @@ -238,8 +242,6 @@ class PerformanceTracker(object): 'cumulative_risk_metrics' : self.cumulative_risk_metrics.to_dict() } - def log_order(self, order): - self.order_log.append(order) def process_event(self, event): @@ -288,6 +290,8 @@ class PerformanceTracker(object): # calculate progress of test self.progress = self.day_count / self.total_days + # TODO!!!! + # Output results if self.results_socket: msg = zp.PERF_FRAME(self.to_dict()) @@ -584,7 +588,6 @@ class PerformancePeriod(object): return positions - # def get_positions_list(self): positions = [] for sid, pos in self.positions.iteritems(): diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index bf3a5374..9cae6e72 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -10,9 +10,8 @@ log = logbook.Logger('Transaction Simulator') class TransactionSimulator(object): - def __init__(self, style=SIMULATION_STYLE.PARTIAL_VOLUME): - self.open_orders = {} - self.order_count = 0 + def __init__(self, open_orders, style=SIMULATION_STYLE.PARTIAL_VOLUME): + self.open_orders = open_orders self.txn_count = 0 self.trade_window = datetime.timedelta(seconds=30) self.orderTTL = datetime.timedelta(days=1) @@ -27,28 +26,12 @@ class TransactionSimulator(object): elif style == SIMULATION_STYLE.NOOP: self.apply_trade_to_open_orders = self.simulate_noop - def add_open_order(self, event): - # Orders are captured in a buffer by sid. No calculations are done here. - # Amount is explicitly converted to an int. - # Orders of amount zero are ignored. - - self.order_count += 1 - event.amount = int(event.amount) - - if event.amount == 0: - log = "requested to trade zero shares of {sid}".format( - sid=event.sid - ) - log.debug(log) - return - - if not self.open_orders.has_key(event.sid): - self.open_orders[event.sid] = [] - - # set the filled property to zero - event.filled = 0 - self.open_orders[event.sid].append(event) - + def update(self, event): + event.txn = None + if event.type == zp.DATASOURCE_TYPE.TRADE: + event.txn = self.apply_trade_to_open_orders(event) + return event + def simulate_buy_all(self, event): txn = self.create_transaction( event.sid, @@ -81,7 +64,7 @@ class TransactionSimulator(object): txn = self.create_transaction( event.sid, amount, - event.price + 0.10, + event.price + 0.10, # Magic constant? event.dt, direction ) diff --git a/zipline/gens/examples.py b/zipline/gens/examples.py new file mode 100644 index 00000000..d9051b10 --- /dev/null +++ b/zipline/gens/examples.py @@ -0,0 +1,38 @@ +from zipline.gens.composites import + +if __name__ == "__main__": + + filter = [1,2,3,4] + #Set up source a. One hour between events. + args_a = tuple() + kwargs_a = {'sids' : [1,2,3,4], + 'start' : datetime(2012,6,6,0), + 'delta' : timedelta(minutes = ), + 'filter' : filter + } + #Set up source b. One day between events. + args_b = tuple() + kwargs_b = {'sids' : [1,2,3,4], + 'start' : datetime(2012,6,6,0), + 'delta' : timedelta(days = 1), + 'filter' : filter + } + #Set up source c. One minute between events. + args_c = tuple() + kwargs_c = {'sids' : [1,2,3,4], + 'start' : datetime(2012,6,6,0), + 'delta' : timedelta(minutes = 1), + 'filter' : filter + } + + sources = (SpecificEquityTrades,) * 4 + source_args = (args_a, args_b, args_c, args_d) + source_kwargs = (kwargs_a, kwargs_b, kwargs_c, kwargs_d) + + # Generate our expected source_ids. + zip_args = zip(source_args, source_kwargs) + expected_ids = ["SpecificEquityTrades" + hash_args(*args, **kwargs) + for args, kwargs in zip_args] + + # Pipe our sources into sort. + sort_out = date_sorted_sources(sources, source_args, source_kwargs) diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 65523a41..7f0a30eb 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -85,10 +85,12 @@ def trade_simulation_client(stream_in, algo, environment, sim_style): # Creates a TRANSACTION field on the event containing transaction # information if we filled any pending orders on the event's sid. # TRANSACTION is None if we didn't fill any orders. - with_txns = stateful_transform(stream_in, - TransactionSimulator, - open_orders, - style = sim_style) + with_txns = stateful_transform( + stream_in, + TransactionSimulator, + open_orders, + style = sim_style + ) # Pipe the events with transactions to perf. This will remove the @@ -96,10 +98,12 @@ def trade_simulation_client(stream_in, algo, environment, sim_style): # a portfolio object to be passed to the user's algorithm. Also adds # a PERF_MESSAGE field which is usually none, but contains an update # message once per day. - with_portfolio_and_perf_msg = stateful_transform(stream_with_txns, - PerformanceTracker, - trading_environment, - sids) + with_portfolio_and_perf_msg = stateful_transform( + stream_with_txns, + PerformanceTracker, + trading_environment, + sids + ) # Batch the event stream by dt to be processed by the user's algo. # Will also set the PERF_MESSAGE field if the batch contains a perf diff --git a/zipline/gens/zmq_gens.py b/zipline/gens/zmq_gens.py index 524852a7..e60dae2b 100644 --- a/zipline/gens/zmq_gens.py +++ b/zipline/gens/zmq_gens.py @@ -2,15 +2,17 @@ import zmq import zipline.protocol as zp -def gen_from_zmq(poller, unframe): +def gen_from_zmq(poller, unframe, namestring): """ A generator that takes an initialized zmq poller and yields messages from the poller until it gets a zp.CONTROL_PROTOCOL.DONE. """ while True: message = poller.recv() - if message = zp.CONTROL_PROTOCOL.DONE: - yield "DONE" + # Done protocol should now be a message type so that + # done messages can also have source_ids. + if message.type == zp.CONTROL_PROTOCOL.DONE: + yield done_message(message.source_id) break else: yield unframe(message)