From 3bb03e0f8cae141f2ec13bf24df341be4064dc56 Mon Sep 17 00:00:00 2001 From: fawce Date: Mon, 14 May 2012 16:49:05 -0400 Subject: [PATCH] converting to handle_data nomenclature. --- zipline/finance/trading.py | 26 ++++++++------------------ zipline/test/algorithms.py | 10 +++++----- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 1910aff5..4e512e2a 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -41,8 +41,7 @@ class TradeSimulationClient(qmsg.Component): self.last_msg_dt = datetime.datetime.utcnow() self.txn_sim = TransactionSimulator(sim_style) - assert self.trading_environment.frame_index != None - self.event_frame = ndict() + self.event_data = ndict() self.perf = perf.PerformanceTracker(self.trading_environment) @property @@ -147,13 +146,13 @@ class TradeSimulationClient(qmsg.Component): As per the algorithm protocol: - Set the current portfolio for the algorithm as per protocol. - - Construct frame based on backlog of events, send to algorithm. + - Construct data based on backlog of events, send to algorithm. """ current_portfolio = self.perf.get_portfolio() self.algorithm.set_portfolio(current_portfolio) - frame = self.get_frame() - if len(frame) > 0: - self.algorithm.handle_frame(frame) + data = self.get_data() + if len(data) > 0: + self.algorithm.handle_data(data) def connect_order(self): return self.connect_push_socket(self.addresses['order_address']) @@ -176,11 +175,11 @@ class TradeSimulationClient(qmsg.Component): self.event_queue = [] self.event_queue.append(event) - def get_frame(self): + def get_data(self): for event in self.event_queue: - self.event_frame[event['sid']] = event + self.event_data[event['sid']] = event self.event_queue = [] - return self.event_frame + return self.event_data class TransactionSimulator(object): @@ -370,7 +369,6 @@ class TradingEnvironment(object): self.trading_day_map = {} self.treasury_curves = treasury_curves self.benchmark_returns = benchmark_returns - self.frame_index = ['sid', 'volume', 'dt', 'price', 'changed'] self.period_start = period_start self.period_end = period_end self.capital_base = capital_base @@ -471,14 +469,6 @@ class TradingEnvironment(object): return self.trading_day_map[date].returns else: return 0.0 - - def add_to_frame(self, name): - """ - Add an entry to the frame index. - :param name: new index entry name. Used by TradingSimulationClient - to - """ - self.frame_index.append(name) diff --git a/zipline/test/algorithms.py b/zipline/test/algorithms.py index b3743709..1c1e71c3 100644 --- a/zipline/test/algorithms.py +++ b/zipline/test/algorithms.py @@ -16,8 +16,8 @@ The algorithm must expose methods: of valid sids. List must have a length between 1 and 10. If None is returned the filter will block all events. - - handle_frame: method that accepts a :py:class:`pandas.Dataframe` of the - current state of the simulation universe. An example frame:: + - handle_data: method that accepts a :py:class:`zipline.protocol_utils.ndict` + of the current state of the simulation universe. An example data ndict:: +-----------------+--------------+----------------+--------------------+ | | SID(133) | SID(134) | SID(135) | @@ -74,7 +74,7 @@ class TestAlgorithm(): def set_portfolio(self, portfolio): self.portfolio = portfolio - def handle_frame(self, frame): + def handle_data(self, data): self.frame_count += 1 #place an order for 100 shares of sid if self.incr < self.count: @@ -110,7 +110,7 @@ class HeavyBuyAlgorithm(): def set_portfolio(self, portfolio): self.portfolio = portfolio - def handle_frame(self, frame): + def handle_data(self, data): self.frame_count += 1 #place an order for 100 shares of sid self.order(self.sid, self.amount) @@ -133,7 +133,7 @@ class NoopAlgorithm(object): def set_portfolio(self, portfolio): pass - def handle_frame(self, frame): + def handle_data(self, data): pass def get_sid_filter(self):