From 3f801ef387a0ebeb11ffdcf8ed375899acb1405f Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Thu, 23 Aug 2012 17:38:48 -0400 Subject: [PATCH] Debugging. --- tests/test_optimize.py | 18 ++++++++++++------ zipline/gens/tradesimulation.py | 10 +++++----- zipline/optimize/algorithms.py | 12 +++++++++--- zipline/optimize/factory.py | 8 ++------ zipline/utils/factory.py | 9 ++++++--- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index 83dca6a9..0b237981 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -50,8 +50,7 @@ class TestUpDown(TestCase): algo, config = create_predictable_zipline( self.zipline_test_config, - offset=0, - simulate=False + offset=0 ) #extract arguments @@ -74,7 +73,9 @@ class TestUpDown(TestCase): "Minimum price does not equal expected maximum price." ) - algo.run(config['trade_source']) + stats = algo.run(config['trade_source']) + + self.assertTrue(len(stats) != 0) orders = np.asarray(algo.orders) max_order_idx = np.where(orders==orders.max())[0] @@ -93,6 +94,8 @@ class TestUpDown(TestCase): "Algorithm did not sell when price was going to increase." ) + from nose.tools import set_trace; set_trace() + # @skip def test_concavity_of_returns(self): """verify concave relationship between free parameter and @@ -110,12 +113,15 @@ class TestUpDown(TestCase): compound_returns = np.empty(len(test_offsets)) ziplines = [] for i, offset in enumerate(test_offsets): - zipline, config = create_predictable_zipline( + algo, config = create_predictable_zipline( self.zipline_test_config, offset=offset, ) - ziplines.append(zipline) - compound_returns[i] = zipline.get_cumulative_performance()['returns'] + results = algo.run(config['trade_source']) + ziplines.append(algo) + + compound_returns[i] = results.returns.sum() + self.assertTrue(np.all(compound_returns[supposed_max] > compound_returns[np.logical_not(supposed_max)]), "Maximum compound returns are not where they are supposed to be." diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 98aa727f..8ad54053 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -109,12 +109,12 @@ class TradeSimulationClient(object): yield message class AlgorithmSimulator(object): - + def __init__(self, order_book, algo, algo_start): - + # ========== # Algo Setup # ========== @@ -205,7 +205,7 @@ class AlgorithmSimulator(object): # simulator so that it can fill the placed order when it # receives its next message. self.order_book.place_order(order) - + def transform(self, stream_in): """ Main generator work loop. @@ -266,7 +266,7 @@ class AlgorithmSimulator(object): del event['perf_message'] self.update_universe(event) - + # Send the current state of the universe to the user's algo. self.simulate_snapshot(date) @@ -289,7 +289,7 @@ class AlgorithmSimulator(object): # Needs to be set so that we inject the proper date into algo # log/print lines. self.snapshot_dt = date - + start_tic = datetime.now() with self.heartbeat_monitor: self.algo.handle_data(self.universe) diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index 727e43db..30feaf59 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -73,7 +73,7 @@ class TradingAlgorithm(object): assert hasattr(self, 'source'), 'source not set.' assert hasattr(self, 'sids'), "sids not set." - environment = create_trading_environment() + environment = create_trading_environment(start=self.data.index[0], end=self.data.index[-1]) # Create transforms by wrapping them into StatefulTransforms transforms = [] @@ -118,11 +118,16 @@ class TradingAlgorithm(object): def run(self, data, compute_risk_metrics=False): self.source = DataFrameSource(data, sids=self.sids) - + self.data = data self._setup(compute_risk_metrics=compute_risk_metrics) # drain simulated_trading - perfs = list(self.simulated_trading) + perfs = [] + for perf in self.simulated_trading: + from nose.tools import set_trace; set_trace() + perfs.append(perf) + + #perfs = list(self.simulated_trading) daily_stats = self._create_daily_stats(perfs) return daily_stats @@ -187,3 +192,4 @@ class BuySellAlgorithmNew(TradingAlgorithm): self.frame_count += 1 self.incr += 1 + from nose.tools import set_trace; set_trace() diff --git a/zipline/optimize/factory.py b/zipline/optimize/factory.py index 85ee2c34..319ee0dd 100644 --- a/zipline/optimize/factory.py +++ b/zipline/optimize/factory.py @@ -42,7 +42,7 @@ def create_updown_trade_source(sid, trade_count, trading_environment, base_price price = base_price-amplitude/2. cur = trading_environment.first_open - one_day = timedelta(minutes = 1)#days = 1) + one_day = timedelta(days = 1) #create iterator to cycle through up and down phases change = cycle([1,-1]) @@ -60,7 +60,7 @@ def create_updown_trade_source(sid, trade_count, trading_environment, base_price return df -def create_predictable_zipline(config, offset=0, simulate=True): +def create_predictable_zipline(config, offset=0): """Create a test zipline object as specified by config. The zipline will use the UpDown tradesource which is perfectly predictable. @@ -118,10 +118,6 @@ def create_predictable_zipline(config, offset=0, simulate=True): config['trade_source'] = source config['environment'] = trading_environment config['simulation_style'] = SIMULATION_STYLE.FIXED_SLIPPAGE - config['devel'] = True - - if simulate: - algorithm.run() return algorithm, config diff --git a/zipline/utils/factory.py b/zipline/utils/factory.py index cf2168fb..8b2565c4 100644 --- a/zipline/utils/factory.py +++ b/zipline/utils/factory.py @@ -55,12 +55,15 @@ def load_market_data(): return bm_returns, tr_curves -def create_trading_environment(year=2006): +def create_trading_environment(year=2006, start=None, end=None): """Construct a complete environment with reasonable defaults""" benchmark_returns, treasury_curves = load_market_data() - start = datetime(year, 1, 1, tzinfo=pytz.utc) - end = datetime(year, 12, 31, tzinfo=pytz.utc) + if start is None: + start = datetime(year, 1, 1, tzinfo=pytz.utc) + if end is None: + end = datetime(year, 12, 31, tzinfo=pytz.utc) + trading_environment = TradingEnvironment( benchmark_returns, treasury_curves,