From 5fd30216e53c28bd58434210b9e8d81ca0fdc467 Mon Sep 17 00:00:00 2001 From: fawce Date: Mon, 16 Apr 2012 16:33:53 -0400 Subject: [PATCH] revised protocol to maintain original structure. --- zipline/finance/performance.py | 19 ++----- zipline/finance/trading.py | 4 +- zipline/protocol.py | 98 +++++++++++++--------------------- 3 files changed, 44 insertions(+), 77 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 390ae430..1c6c8946 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -61,8 +61,6 @@ Performance Tracking | | For details look at the comments for | | | :py:meth:`zipline.finance.risk.RiskMetrics.to_dict`| +-----------------+----------------------------------------------------+ - | timestamp | System time evevent occurs in zipilne | - +-----------------+----------------------------------------------------+ Position Tracking @@ -78,14 +76,10 @@ Position Tracking +-----------------+----------------------------------------------------+ | last_sale_price | price at last sale of the security on the exchange | +-----------------+----------------------------------------------------+ - | last_sale_date | datetime of the last trade of the position's | - | | security on the exchange | - +-----------------+----------------------------------------------------+ | transactions | all the transactions that were acrued into this | | | position. | +-----------------+----------------------------------------------------+ - | timestamp | System time event occurs in zipilne | - +-----------------+----------------------------------------------------+ + Performance Period ================== @@ -116,8 +110,7 @@ Performance Period | returns | percentage returns for the entire portfolio over the | | | period | +---------------+------------------------------------------------------+ - | timestamp | System time evevent occurs in zipilne | - +---------------+------------------------------------------------------+ + """ import datetime @@ -227,7 +220,6 @@ class PerformanceTracker(): 'cumulative_perf' : self.cumulative_performance.to_dict(), 'todays_perf' : self.todays_performance.to_dict(), 'cumulative_risk_metrics' : self.cumulative_risk_metrics.to_dict(), - 'timestamp' : datetime.datetime.now(), } def log_order(self, order): @@ -376,9 +368,7 @@ class Position(): 'sid' : self.sid, 'amount' : self.amount, 'cost_basis' : self.cost_basis, - 'last_sale_price' : self.last_sale_price, - 'last_sale_date' : self.last_sale_date, - 'timestamp' : datetime.datetime.now() + 'last_sale_price' : self.last_sale_price } @@ -444,7 +434,7 @@ class PerformancePeriod(): self.max_leverage = 1.1 * self.max_capital_used / self.starting_cash # add transaction to the list of processed transactions - self.processed_transactions.append(txn) + self.processed_transactions.append(txn.as_dict()) def round_to_nearest(self, x, base=5): return int(base * round(float(x)/base)) @@ -476,7 +466,6 @@ class PerformancePeriod(): 'ending_cash' : self.ending_cash, 'portfolio_value': self.ending_cash + self.ending_value, 'positions' : positions, - 'timestamp' : datetime.datetime.now(), 'pnl' : self.pnl, 'returns' : self.returns, 'transactions' : self.processed_transactions, diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 2b088a29..a3bd75b5 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -38,7 +38,7 @@ class TradeSimulationClient(qmsg.Component): self.current_dt = trading_environment.period_start self.last_iteration_dur = datetime.timedelta(seconds=0) self.algorithm = None - self.max_wait = datetime.timedelta(seconds=10) + self.max_wait = datetime.timedelta(seconds=3) self.last_msg_dt = datetime.datetime.utcnow() assert self.trading_environment.frame_index != None @@ -106,7 +106,7 @@ class TradeSimulationClient(qmsg.Component): # drained. Signal the order_source that we're done, and # the done will cascade through the whole zipline. # shutdown the feedback loop to the OrderDataSource - wait_time = self.last_msg_dt - datetime.datetime.utcnow() + wait_time = datetime.datetime.utcnow() - self.last_msg_dt if wait_time > self.max_wait: self.signal_order_done() diff --git a/zipline/protocol.py b/zipline/protocol.py index 17501c05..9908d36b 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -626,71 +626,43 @@ def PERF_FRAME(perf): #TODO: add asserts... - #pull some special fields from the perf for easy access - date = perf['last_close'] + # DATE fields: + # started_at, period_start, period_end, last_close, last_open + # pos.last_sale_date + # txn.dt + + assert isinstance(perf['started_at'], datetime.datetime) + assert isinstance(perf['period_start'], datetime.datetime) + assert isinstance(perf['period_end'], datetime.datetime) + assert isinstance(perf['last_close'], datetime.datetime) + assert isinstance(perf['last_open'], datetime.datetime) + + assert isinstance(perf['todays_perf'], dict) + assert isinstance(perf['cumulative_perf'], dict) + tp = perf['todays_perf'] cp = perf['cumulative_perf'] - risk = perf['cumulative_risk_metrics'] - # aggregate the day's transactions, which are nested in their - # respsective positions. - transactions = [] + assert isinstance(tp['transactions'], list) + assert isinstance(cp['transactions'], list) + + perf['started_at'] = EPOCH(perf['started_at']) + perf['period_start'] = EPOCH(perf['period_start']) + perf['period_end'] = EPOCH(perf['period_end']) + perf['last_close'] = EPOCH(perf['last_close']) + perf['last_open'] = EPOCH(perf['last_open']) + for txn in tp['transactions']: - cur = { - 'date':EPOCH(txn.dt), - 'amount': txn.amount, - 'price': txn.price, - 'sid':txn.sid - } - transactions.append(cur) - - positions = [] - for sid, pos in tp['positions'].iteritems(): - cur = { - 'cost_basis':pos['cost_basis'], - 'sid' :pos['sid'], - 'last_sale' :pos['last_sale_price'], - 'amount' :pos['amount'] - } - positions.append(cur) - - daily_perf = { - 'date' : EPOCH(date), - 'returns' : tp['returns'], - 'pnl' : tp['pnl'], - 'market_value' : tp['ending_value'], - 'portfolio_value' : tp['portfolio_value'], - 'starting_cash' : tp['starting_cash'], - 'ending_cash' : tp['ending_cash'], - 'capital_used' : tp['capital_used'], - 'transactions' : transactions, - 'positions' : positions - } - - cumulative_perf = { - 'alpha' : risk['alpha'], - 'beta' : risk['beta'], - 'sharpe' : risk['sharpe'], - 'volatility' : risk['algo_volatility'], - 'benchmark_volatility' : risk['benchmark_volatility'], - 'benchmark_returns' : risk['benchmark_period_return'], - 'max_drawdown' : risk['max_drawdown'], - 'total_returns' : cp['returns'], - 'pnl' : cp['pnl'], - 'capital_used' : cp['capital_used'] - - } + txn['dt'] = EPOCH(txn['dt']) - # nest the cumulative performance data in the daily. - daily_perf['cumulative'] = cumulative_perf - - result = { - 'started_at' : EPOCH(perf['started_at']), - 'daily' : [daily_perf], - 'percent_complete' : perf['progress'], - } + for txn in cp['transactions']: + txn['dt'] = EPOCH(txn['dt']) + - return msgpack.dumps(tuple(['PERF', result])) + for dr in perf['returns']: + dr['dt'] = EPOCH(dr['dt']) + + return msgpack.dumps(tuple(['PERF', perf])) def RISK_FRAME(risk): @@ -698,7 +670,7 @@ def RISK_FRAME(risk): def PERF_UNFRAME(msg): - prefix, payload = msgpack.loads(msg) + prefix, payload = msgpack.loads(msg, use_list=True) return dict(prefix=prefix, payload=payload) # ----------------------- @@ -730,6 +702,12 @@ def EPOCH(utc_datetime): ms = seconds * 1000 return ms +def UN_EPOCH(ms_since_epoch): + seconds_since_epoch = ms_since_epoch / 1000 + delta = datetime.timedelta(seconds = seconds_since_epoch) + dt = UNIX_EPOCH + delta + return dt + def PACK_DATE(event): """ Packs the datetime property of event into msgpack'able longs.