From 1576447c764ebcb3bdc7825ac2b22ece164be0f1 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Fri, 10 Aug 2012 11:08:11 -0400 Subject: [PATCH 01/10] Refactored optimize factory. Enabled optimize unittests. --- tests/test_optimize.py | 8 ++++---- zipline/gens/examples.py | 6 +++--- zipline/optimize/factory.py | 5 ++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index c16454b1..0b132b8f 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -7,7 +7,7 @@ import numpy as np from zipline.core.devsimulator import AddressAllocator # TODO: refactor the factory to use generators -# from zipline.optimize.factory import create_predictable_zipline +from zipline.optimize.factory import create_predictable_zipline DEFAULT_TIMEOUT = 15 # seconds EXTENDED_TIMEOUT = 90 @@ -38,7 +38,7 @@ class TestUpDown(TestCase): def tearDown(self): teardown_logger(self) - @skip +# @skip @timed(DEFAULT_TIMEOUT) def test_source_and_orders(self): """verify that UpDownSource is having the correct @@ -94,7 +94,7 @@ class TestUpDown(TestCase): "Algorithm did not sell when price was going to increase." ) - @skip +# @skip def test_concavity_of_returns(self): """verify concave relationship between free parameter and returns in certain region around the max. Moreover, @@ -136,7 +136,7 @@ class TestUpDown(TestCase): idx[0] -= 1 idx[1] += 1 - @skip +# @skip def test_optimize(self): """verify that gradient descent (Powell's method) can find the optimal free parameter under which the BuySellAlgorithm produces diff --git a/zipline/gens/examples.py b/zipline/gens/examples.py index f3a0dd0b..21d9cde4 100644 --- a/zipline/gens/examples.py +++ b/zipline/gens/examples.py @@ -9,10 +9,10 @@ from itertools import izip from zipline.utils.factory import create_trading_environment from zipline.test_algorithms import TestAlgorithm -from zipline.gens.composites import SourceBundle, TransformBundle, \ - date_sorted_sources, merged_transforms, sequential_transforms +from zipline.gens.composites import date_sorted_sources, merged_transforms, sequential_transforms from zipline.gens.tradegens import SpecificEquityTrades -from zipline.gens.transform import MovingAverage, Passthrough, StatefulTransform +from zipline.gens.mavg import MovingAverage +from zipline.gens.transform import Passthrough, StatefulTransform from zipline.gens.tradesimulation import TradeSimulationClient as tsc import zipline.protocol as zp diff --git a/zipline/optimize/factory.py b/zipline/optimize/factory.py index cb0de069..3a7fdfff 100644 --- a/zipline/optimize/factory.py +++ b/zipline/optimize/factory.py @@ -4,12 +4,11 @@ Factory functions to prepare useful data for optimize tests. Author: Thomas V. Wiecki (thomas.wiecki@gmail.com), 2012 """ from datetime import timedelta -import pandas as pd import zipline.protocol as zp from zipline.utils.factory import get_next_trading_dt, create_trading_environment -from zipline.finance.sources import SpecificEquityTrades +from zipline.gens.tradegens import SpecificEquityTrades from zipline.optimize.algorithms import BuySellAlgorithm from zipline.lines import SimulatedTrading from zipline.finance.trading import SIMULATION_STYLE @@ -66,7 +65,7 @@ def create_updown_trade_source(sid, trade_count, trading_environment, base_price trading_environment.period_end = cur - source = SpecificEquityTrades(events) + source = SpecificEquityTrades(event_list=events) return source From d88b1756726f790dd0c932b74c997a081b88e7fc Mon Sep 17 00:00:00 2001 From: fawce Date: Fri, 10 Aug 2012 11:55:30 -0400 Subject: [PATCH 02/10] SimulatedTrading is now a generator. --- zipline/lines.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/zipline/lines.py b/zipline/lines.py index 9bc8b3ac..2a59190f 100644 --- a/zipline/lines.py +++ b/zipline/lines.py @@ -244,10 +244,17 @@ class SimulatedTrading(object): else: return [] + def __iter__(self): + return self + + def next(self): + return self.gen.next() + @staticmethod def create_test_zipline(**config): """ - :param config: A configuration object that is a dict with: + :param config: A configuration object that is a dict with + (all optional): - environment - a \ :py:class:`zipline.finance.trading.TradingEnvironment` @@ -269,7 +276,7 @@ class SimulatedTrading(object): of StatefulTransform objects. """ assert isinstance(config, dict) - sid = config['sid'] + sid = config.get('sid', 133) #-------------------- # Trading Environment From 51f01c0f1f15eeb8b684ffff064e2bf0f975010f Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Fri, 10 Aug 2012 17:23:08 -0400 Subject: [PATCH 03/10] ENH: Implemented new Zipline interface. Implemented Algorithm base class. Implemented example algorithm. Implemented example.py code. --- zipline/gens/composites.py | 23 ++++++++ zipline/gens/tradegens.py | 3 +- zipline/gens/transform.py | 6 +- zipline/lines.py | 104 +++++++++++++++++++++++++++++++++ zipline/optimize/algorithms.py | 57 ++++++++++++++++++ zipline/optimize/example.py | 19 ++++++ zipline/test_algorithms.py | 16 +++++ 7 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 zipline/optimize/example.py diff --git a/zipline/gens/composites.py b/zipline/gens/composites.py index b3fa7576..4a74b8f6 100644 --- a/zipline/gens/composites.py +++ b/zipline/gens/composites.py @@ -88,6 +88,29 @@ def sequential_transforms(stream_in, *transforms): dt_aliased = alias_dt(stream_out) return add_done(dt_aliased) +def sequential_transforms_dict(stream_in, transforms): + """ + Apply each transform in transforms sequentially to each event in stream_in. + Each transform application will add a new entry indexed to the transform's + hash string. + """ + + assert isinstance(transforms, dict) + + for tnfm in transforms.itervalues(): + tnfm.forward_all = False + tnfm.update_in_place = False + tnfm.append_value = True + + # Recursively apply all transforms to the stream. + stream_out = reduce(lambda stream, tnfm: tnfm.transform(stream), + transforms, + stream_in) + + dt_aliased = alias_dt(stream_out) + return add_done(dt_aliased) + + def alias_dt(stream_in): """ Alias the dt field to datetime on each message. diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index 2e8f6bea..fad8bc29 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -97,8 +97,9 @@ class SpecificEquityTrades(object): return self.__class__.__name__ + "-" + self.arg_string def create_fresh_generator(self): - if self.event_list: + for event in self.event_list: + event['source_id'] = self.get_hash() unfiltered = (event for event in self.event_list) # Set up iterators for each expected field. diff --git a/zipline/gens/transform.py b/zipline/gens/transform.py index 651c337d..099fc1eb 100644 --- a/zipline/gens/transform.py +++ b/zipline/gens/transform.py @@ -93,10 +93,10 @@ class StatefulTransform(object): #TODO: refactor this to avoid unnecessary copying. assert_sort_unframe_protocol(message) - message_copy = deepcopy(message) - + #message_copy = deepcopy(message) + message_copy = message # Same shared pointer issue here as above. - tnfm_value = self.state.update(deepcopy(message_copy)) + tnfm_value = self.state.update(message_copy) # FORWARDER flag means we want to keep all original # values, plus append tnfm_id and tnfm_value. Used for diff --git a/zipline/lines.py b/zipline/lines.py index 2a59190f..c05c5c1c 100644 --- a/zipline/lines.py +++ b/zipline/lines.py @@ -63,6 +63,11 @@ import sys import zmq import os from signal import SIGHUP, SIGINT +import datetime +import pytz +import pandas as pd +import numpy as np + import multiprocessing from setproctitle import setproctitle @@ -70,6 +75,10 @@ from zipline.test_algorithms import TestAlgorithm from zipline.finance.trading import SIMULATION_STYLE from zipline.utils.log_utils import ZeroMQLogHandler, stdout_only_pipe from zipline.utils import factory +from zipline.utils.factory import create_trading_environment +from zipline.gens.tradegens import SpecificEquityTrades +from zipline import ndict +from zipline.protocol import DATASOURCE_TYPE from zipline.test_algorithms import TestAlgorithm @@ -358,3 +367,98 @@ class SimulatedTrading(object): #------------------- return sim + + +def create_sp_source(start_dt=None, end_dt=None): + if start_dt is None: + start_dt = datetime.datetime(2002, 1, 1, tzinfo=pytz.utc) + if end_dt is None: + end_dt = datetime.datetime(2008, 1, 1, tzinfo=pytz.utc) + + sp_events, _ = factory.load_market_data() + sp_transformed = [] + for event in sp_events: + transformed = ndict(event.to_dict()) + if (transformed.dt < start_dt) or (transformed.dt > end_dt): + continue + transformed['sid'] = 0 + transformed['price'] = transformed['returns'] + transformed['type'] = DATASOURCE_TYPE.TRADE + sp_transformed.append(transformed) + + source = SpecificEquityTrades(event_list=sp_transformed) + + return source + +class Zipline(object): + def __init__(self, **kwargs): + algorithm = kwargs.get('algorithm', TestAlgorithm) + source_descrs = kwargs.get('sources', ['S&P']) + if isinstance(source_descrs, str): + source_descrs = [source_descrs] + + sources = [] + for source_descr in source_descrs: + if isinstance(source_descr, str): + if source_descr == 'S&P': + source = create_sp_source() + else: + raise NotImplementedError, "Source with name {source_descr} not known.".format(source_descr=source_descr) + else: + source = source_descr + + sources.append(source) + + environment = kwargs.get('environment', create_trading_environment()) + + try: + transform_descrs = kwargs.get('transforms', algorithm.registered_transforms) + except: + print "Couldn't load any registered_transforms." + transform_descrs = {} + + # Create transforms by wrapping them into StatefulTransforms + transforms = [] + for namestring, trans_descr in transform_descrs.iteritems(): + sf = StatefulTransform( + trans_descr['class'], + *trans_descr['args'], + **trans_descr['kwargs'] + ) + sf.namestring = namestring + + transforms.append(sf) + + results_socket_uri = None + context = None + sim_id = None + style = SIMULATION_STYLE.FIXED_SLIPPAGE + + self.simulated_trading = SimulatedTrading( + sources, + transforms, + algorithm, + environment, + style, + results_socket_uri, + context, + sim_id) + + + def run(self): + # drain simulated_trading + perfs = [perf for perf in self.simulated_trading] + + # create daily stats dataframe + daily_perfs = [] + cum_perfs = [] + for perf in perfs: + if 'daily_perf' in perf: + daily_perfs.append(perf['daily_perf']) + else: + cum_perfs.append(perf) + + daily_dts = [np.datetime64(perf['period_close'], utc=True) for perf in daily_perfs] + daily_stats = pd.DataFrame(daily_perfs, index=daily_dts) + + return daily_stats diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index c4e3fb9d..119c0f76 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -1,3 +1,6 @@ +from zipline.gens.mavg import MovingAverage +from datetime import datetime, timedelta + class BuySellAlgorithm(object): """Algorithm that buys and sells alternatingly. The amount for each order can be specified. In addition, an offset that will @@ -46,3 +49,57 @@ class BuySellAlgorithm(object): def get_sid_filter(self): return [self.sid] + +# Algorithm base class, user algorithms inherit from this as they +# don't want to have to copy and know about set_order and +# set_portfolio +class Algorithm(object): + def set_order(self, order_callable): + self.order = order_callable + + def get_sid_filter(self): + return [self.sid] + + def set_logger(self, logger): + self.logger = logger + + def initialize(self): + pass + + def add_transform(self, transform_class, tag, *args, **kwargs): + if not hasattr(self, 'registered_transforms'): + self.registered_transforms = {} + + self.registered_transforms[tag] = {'class': transform_class, + 'args': args, + 'kwargs': kwargs} + + +# Inherits from Algorithm base class +class DMA(Algorithm): + """Dual Moving Average algorithm. + """ + + def __init__(self, sid, amount, short_window=20, long_window=40): + self.sid = sid + self.amount = amount + self.done = False + self.order = None + self.frame_count = 0 + self.portfolio = None + self.orders = [] + self.market_entered = False + self.prices = [] + self.events = 0 + self.add_transform(MovingAverage, 'short_mavg', ['price'], market_aware=False, delta=timedelta(days=short_window)) + self.add_transform(MovingAverage, 'long_mavg', ['price'], market_aware=False, delta=timedelta(days=long_window)) + + def handle_data(self, data): + self.events += 1 + # access transforms via their user-defined tag + if (data[self.sid].short_mavg > data[self.sid].long_mavg) and not self.market_entered: + self.order(self.sid, 100) + self.market_entered = True + elif (data[self.sid].short_mavg < data[self.sid].long_mavg) and self.market_entered: + self.order(self.sid, -100) + self.market_entered = False diff --git a/zipline/optimize/example.py b/zipline/optimize/example.py new file mode 100644 index 00000000..0119161b --- /dev/null +++ b/zipline/optimize/example.py @@ -0,0 +1,19 @@ +from zipline.lines import Zipline +from zipline.optimize.algorithms import DMA +import pandas as pd +import matplotlib.pyplot as plt +import cProfile + +def run(): + myalgo = DMA(sid=0, amount=100) + zp = Zipline(algorithm=myalgo, sources='S&P') + stats = zp.run() + print stats + return stats + + +#cProfile.run('run()') + +stats = run() +stats.returns.plot() +plt.show() \ No newline at end of file diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index a7881fa8..723c5fb9 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -46,6 +46,22 @@ The algorithm must expose methods: """ +# Algorithm base class, user algorithms inherit from this as they +# don't want to have to copy and know about set_order and +# set_portfolio +class Algorithm(object): + def set_order(self, order_callable): + self.order = order_callable + + def get_sid_filter(self): + return [self.sid] + + def add_transform(self, transform_class, tag, **kwargs): + if not hasattr(self, 'registered_transforms'): + self.registered_transforms = {} + + self.registered_transforms[tag] = transform_class(**kwargs) + class TestAlgorithm(): """ From 3884b15eb6414e0f5b39a4b7ad2cb441028255b3 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Sun, 12 Aug 2012 20:24:34 -0400 Subject: [PATCH 04/10] ENH: Changed algorithm class to provide .run() method. Testing of multiple param combos of DMA. --- zipline/finance/performance.py | 15 +++-- zipline/gens/composites.py | 2 +- zipline/gens/tradegens.py | 62 +++++++++++++++++++ zipline/optimize/algorithms.py | 110 +++++++++++++++++++++++---------- zipline/optimize/example.py | 71 ++++++++++++++++++--- 5 files changed, 212 insertions(+), 48 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 9fee208b..a1fbf65c 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -167,6 +167,8 @@ class PerformanceTracker(object): self.last_dict = None self.exceeded_max_loss = False + self.compute_risk_metrics = True + self.results_socket = None self.results_addr = None @@ -297,12 +299,13 @@ class PerformanceTracker(object): self.returns.append(todays_return_obj) #calculate risk metrics for cumulative performance - self.cumulative_risk_metrics = risk.RiskMetrics( - start_date=self.period_start, - end_date=self.market_close.replace(hour=0, minute=0, second=0), - returns=self.returns, - trading_environment=self.trading_environment - ) + if self.compute_risk_metrics: + self.cumulative_risk_metrics = risk.RiskMetrics( + start_date=self.period_start, + end_date=self.market_close.replace(hour=0, minute=0, second=0), + returns=self.returns, + trading_environment=self.trading_environment + ) # increment the day counter before we move markers forward. self.day_count += 1.0 diff --git a/zipline/gens/composites.py b/zipline/gens/composites.py index 4a74b8f6..0a99fc0f 100644 --- a/zipline/gens/composites.py +++ b/zipline/gens/composites.py @@ -16,7 +16,7 @@ def date_sorted_sources(*sources): for source in sources: assert iter(source), "Source %s not iterable" % source - assert source.__class__.__dict__.has_key('get_hash'), "No get_hash" + assert hasattr(source, 'get_hash'), "No get_hash" # Get name hashes to pass to date_sort. names = [source.get_hash() for source in sources] diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index fad8bc29..5c22ef93 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -4,6 +4,11 @@ and zipline development """ import random import pytz +from copy import copy + +import pandas as pd +from zipline import ndict +from zipline.protocol import DATASOURCE_TYPE from itertools import chain, cycle, ifilter, izip from datetime import datetime, timedelta @@ -132,6 +137,63 @@ class SpecificEquityTrades(object): return filtered +class DataFrameSource(SpecificEquityTrades): + """ + Yields all events in event_list that match the given sid_filter. + If no event_list is specified, generates an internal stream of events + to filter. Returns all events if filter is None. + + Configuration options: + + count : integer representing number of trades + sids : list of values representing simulated internal sids + start : start date + delta : timedelta between internal events + filter : filter to remove the sids + """ + + def __init__(self, data, **kwargs): + assert isinstance(data.index, pd.tseries.index.DatetimeIndex) + + self.data = data + # Unpack config dictionary with default values. + self.count = kwargs.get('count', 500) + self.sids = kwargs.get('sids', [1, 2]) + self.start = kwargs.get('start', datetime(1957, 1, 1, 0, tzinfo = pytz.utc)) + self.end = kwargs.get('end', datetime(2010, 1, 1, tzinfo=pytz.utc)) + self.delta = kwargs.get('delta', timedelta(days = 1)) + + # Default to None for event_list and filter. + self.filter = kwargs.get('filter') + + # Hash_value for downstream sorting. + self.arg_string = hash_args(data, **kwargs) + + self.generator = self.create_fresh_generator() + + def create_fresh_generator(self): + def _generator(df=self.data): + for dt, series in df.iterrows(): + dt = dt.tz_localize('UTC') + if (self.start > dt) or (dt < self.end): + continue + event = {'dt': dt, + 'source_id': self.get_hash(), + 'type': DATASOURCE_TYPE.TRADE + } + + for sid, price in series.iterkv(): + event = copy(event) + event['sid'] = 0 + event['price'] = price + + yield ndict(event) + + + # Return the filtered event stream. + return _generator() + + # !!!!!!! Deprecated for now !!!!!!!!! def RandomEquityTrades(object): diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index 119c0f76..85dac494 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -1,5 +1,16 @@ +import pandas as pd +import numpy as np + from zipline.gens.mavg import MovingAverage from datetime import datetime, timedelta +from zipline.finance.trading import SIMULATION_STYLE +from zipline.utils import factory +from zipline.gens.tradegens import SpecificEquityTrades, DataFrameSource +from zipline.protocol import DATASOURCE_TYPE +from zipline import ndict +from zipline.utils.factory import create_trading_environment +from zipline.gens.transform import StatefulTransform +from zipline.lines import SimulatedTrading class BuySellAlgorithm(object): """Algorithm that buys and sells alternatingly. The amount for @@ -53,12 +64,77 @@ class BuySellAlgorithm(object): # Algorithm base class, user algorithms inherit from this as they # don't want to have to copy and know about set_order and # set_portfolio -class Algorithm(object): +class TradingAlgorithm(object): + def _setup(self, compute_risk_metrics=False): + assert hasattr(self, 'source'), 'source not set.' + assert hasattr(self, 'sids'), "sids not set." + + environment = create_trading_environment() + + # Create transforms by wrapping them into StatefulTransforms + transforms = [] + for namestring, trans_descr in self.registered_transforms.iteritems(): + sf = StatefulTransform( + trans_descr['class'], + *trans_descr['args'], + **trans_descr['kwargs'] + ) + sf.namestring = namestring + + transforms.append(sf) + + results_socket_uri = None + context = None + sim_id = None + style = SIMULATION_STYLE.FIXED_SLIPPAGE + + self.simulated_trading = SimulatedTrading( + [self.source], + transforms, + self, + environment, + style, + results_socket_uri, + context, + sim_id) + + #self.simulated_trading.trading_client.performance_tracker.compute_risk_metrics = compute_risk_metrics + + + def _create_daily_stats(self, perfs): + # create daily stats dataframe + daily_perfs = [] + cum_perfs = [] + for perf in perfs: + if 'daily_perf' in perf: + daily_perfs.append(perf['daily_perf']) + else: + cum_perfs.append(perf) + + daily_dts = [np.datetime64(perf['period_close'], utc=True) for perf in daily_perfs] + daily_stats = pd.DataFrame(daily_perfs, index=daily_dts) + + return daily_stats + + def run(self, data, compute_risk_metrics=False): + self.source = DataFrameSource(data, sids=self.sids) + + self._setup(compute_risk_metrics=compute_risk_metrics) + + # drain simulated_trading + perfs = [perf for perf in self.simulated_trading] + + daily_stats = self._create_daily_stats(perfs) + return daily_stats + + def set_portfolio(self, portfolio): + self.portfolio = portfolio + def set_order(self, order_callable): self.order = order_callable def get_sid_filter(self): - return [self.sid] + return self.sids def set_logger(self, logger): self.logger = logger @@ -73,33 +149,3 @@ class Algorithm(object): self.registered_transforms[tag] = {'class': transform_class, 'args': args, 'kwargs': kwargs} - - -# Inherits from Algorithm base class -class DMA(Algorithm): - """Dual Moving Average algorithm. - """ - - def __init__(self, sid, amount, short_window=20, long_window=40): - self.sid = sid - self.amount = amount - self.done = False - self.order = None - self.frame_count = 0 - self.portfolio = None - self.orders = [] - self.market_entered = False - self.prices = [] - self.events = 0 - self.add_transform(MovingAverage, 'short_mavg', ['price'], market_aware=False, delta=timedelta(days=short_window)) - self.add_transform(MovingAverage, 'long_mavg', ['price'], market_aware=False, delta=timedelta(days=long_window)) - - def handle_data(self, data): - self.events += 1 - # access transforms via their user-defined tag - if (data[self.sid].short_mavg > data[self.sid].long_mavg) and not self.market_entered: - self.order(self.sid, 100) - self.market_entered = True - elif (data[self.sid].short_mavg < data[self.sid].long_mavg) and self.market_entered: - self.order(self.sid, -100) - self.market_entered = False diff --git a/zipline/optimize/example.py b/zipline/optimize/example.py index 0119161b..7c0e58b4 100644 --- a/zipline/optimize/example.py +++ b/zipline/optimize/example.py @@ -1,19 +1,72 @@ from zipline.lines import Zipline -from zipline.optimize.algorithms import DMA import pandas as pd +import numpy as np +#from mpl_toolkits.mplot3d import Axes3D + import matplotlib.pyplot as plt import cProfile +from zipline.gens.mavg import MovingAverage +from zipline.optimize.algorithms import TradingAlgorithm +from datetime import timedelta -def run(): - myalgo = DMA(sid=0, amount=100) - zp = Zipline(algorithm=myalgo, sources='S&P') - stats = zp.run() - print stats +from mpi4py_map import map + +# Inherits from Algorithm base class +class DMA(TradingAlgorithm): + """Dual Moving Average algorithm. + """ + def __init__(self, sid, amount=100, short_window=20, long_window=40): + self.sids = [sid] + self.amount = amount + self.done = False + self.order = None + self.frame_count = 0 + self.portfolio = None + self.orders = [] + self.market_entered = False + self.prices = [] + self.events = 0 + + self.add_transform(MovingAverage, 'short_mavg', ['price'], + market_aware=False, + delta=timedelta(days=int(short_window))) + + self.add_transform(MovingAverage, 'long_mavg', ['price'], + market_aware=False, + delta=timedelta(days=int(long_window))) + + def handle_data(self, data): + self.events += 1 + sid = self.sids[0] + # access transforms via their user-defined tag + if (data[sid].short_mavg > data[sid].long_mavg) and not self.market_entered: + self.order(sid, 100) + self.market_entered = True + elif (data[sid].short_mavg < data[sid].long_mavg) and self.market_entered: + self.order(sid, -100) + self.market_entered = False + + +def run((short_window, long_window)): + data = pd.DataFrame.from_csv('SP500.csv') + myalgo = DMA(sid=0, amount=100, short_window=short_window, long_window=long_window) + stats = myalgo.run(data, compute_risk_metrics=False) + stats['sw'] = short_window + stats['lw'] = long_window return stats +sws, lws = np.mgrid[50:80:5, 100:140:5] -#cProfile.run('run()') +stats_all = map(run, zip(sws.flatten(), lws.flatten())) -stats = run() -stats.returns.plot() +# for sw, lw in zip(sws.flatten(), lws.flatten()): +# stats = run(short_window=sw, long_window=lw) +# stats_all.append(stats) + +stats = pd.concat(stats_all) +returns = stats.groupby(['sw', 'lw']).sum() +plt.contourf(sws, lws, returns.returns.reshape(sws.shape)) +plt.xlabel('Short window length') +plt.ylabel('Long window length') +plt.savefig('DMA_contour.png') plt.show() \ No newline at end of file From 7f1c5100b5858c03e377d964954cef86aa0aeb7d Mon Sep 17 00:00:00 2001 From: fawce Date: Fri, 10 Aug 2012 11:55:30 -0400 Subject: [PATCH 05/10] SimulatedTrading is iterable agian. --- zipline/lines.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/zipline/lines.py b/zipline/lines.py index b1463e38..e861706c 100644 --- a/zipline/lines.py +++ b/zipline/lines.py @@ -68,15 +68,16 @@ from setproctitle import setproctitle from zipline.test_algorithms import TestAlgorithm from zipline.finance.trading import SIMULATION_STYLE -from zipline.utils.log_utils import ZeroMQLogHandler +from zipline.utils.log_utils import ZeroMQLogHandler, stdout_only_pipe from zipline.utils import factory -from zipline.gens.composites import ( - date_sorted_sources, - sequential_transforms -) +from zipline.test_algorithms import TestAlgorithm + +from zipline.gens.composites import \ + date_sorted_sources, merged_transforms, sequential_transforms +from zipline.gens.transform import Passthrough, StatefulTransform from zipline.gens.tradesimulation import TradeSimulationClient as tsc -from logbook import Logger +from logbook import Logger, NestedSetup, Processor import zipline.protocol as zp @@ -171,8 +172,6 @@ class SimulatedTrading(object): def close(self): log.info("Closing Simulation: {id}".format(id=self.sim_id)) - if self.results_socket: - self.results_socket.close() if self.proc and self.send_sighup: ppid = os.getppid() if self.success: @@ -245,6 +244,12 @@ class SimulatedTrading(object): else: return [] + def __iter__(self): + return self + + def next(self): + return self.gen.next() + @staticmethod def create_test_zipline(**config): """ @@ -319,13 +324,11 @@ class SimulatedTrading(object): trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( - sid_list, + sids, trade_count, - trading_environment, - concurrent=concurrent_trades + trading_environment ) - #------------------- # Transforms #------------------- From ace0b25d315ccc5513f58bafba56f9fe0d0a446e Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Thu, 23 Aug 2012 14:32:22 -0400 Subject: [PATCH 06/10] Dummy. --- tests/test_optimize.py | 4 ++-- zipline/optimize/algorithms.py | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index 0b132b8f..806f2dc3 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -57,7 +57,7 @@ class TestUpDown(TestCase): base_price = self.zipline_test_config['base_price'] amplitude = self.zipline_test_config['amplitude'] - prices = np.array([event.price for event in config['trade_source'].event_list]) + prices = config['trade_source'][0].values max_price_idx = np.where(prices==prices.max())[0] min_price_idx = np.where(prices==prices.min())[0] self.assertTrue(np.all(max_price_idx % 2 == 1), @@ -73,7 +73,7 @@ class TestUpDown(TestCase): "Minimum price does not equal expected maximum price." ) - zipline.simulate(blocking=True) + zipline.run(config['trade_source']) algo = config['algorithm'] diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index 85dac494..b901bc4c 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -10,7 +10,7 @@ from zipline.protocol import DATASOURCE_TYPE from zipline import ndict from zipline.utils.factory import create_trading_environment from zipline.gens.transform import StatefulTransform -from zipline.lines import SimulatedTrading +from zipline.lines import SimulatedTradingLite class BuySellAlgorithm(object): """Algorithm that buys and sells alternatingly. The amount for @@ -46,6 +46,9 @@ class BuySellAlgorithm(object): def set_portfolio(self, portfolio): self.portfolio = portfolio + def set_logger(self, logger): + self.logger = logger + def handle_data(self, frame): order_size = self.buy_or_sell * (self.amount - (self.offset**2)) self.order(self.sid, order_size) @@ -83,21 +86,15 @@ class TradingAlgorithm(object): transforms.append(sf) - results_socket_uri = None - context = None - sim_id = None + style = SIMULATION_STYLE.FIXED_SLIPPAGE - self.simulated_trading = SimulatedTrading( + self.simulated_trading = SimulatedTradingLite( [self.source], transforms, self, environment, - style, - results_socket_uri, - context, - sim_id) - + style) #self.simulated_trading.trading_client.performance_tracker.compute_risk_metrics = compute_risk_metrics @@ -122,7 +119,7 @@ class TradingAlgorithm(object): self._setup(compute_risk_metrics=compute_risk_metrics) # drain simulated_trading - perfs = [perf for perf in self.simulated_trading] + perfs = list(self.simulated_trading) daily_stats = self._create_daily_stats(perfs) return daily_stats From 7491e1f88ea4f3c3cc585f5352b9363bf3a046e5 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Thu, 23 Aug 2012 14:33:25 -0400 Subject: [PATCH 07/10] Resolved internal conflict due to stashing. --- tests/test_optimize.py | 1 + zipline/gens/tradegens.py | 4 +++ zipline/optimize/algorithms.py | 61 +++++++++++++++++++++++++++------- zipline/optimize/factory.py | 44 ++++++++++-------------- 4 files changed, 71 insertions(+), 39 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index 806f2dc3..52e27bb8 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -47,6 +47,7 @@ class TestUpDown(TestCase): UpDownSource and BuySellAlgorithm interact correctly." """ + zipline, config = create_predictable_zipline( self.zipline_test_config, offset=0, diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index 30d575aa..5716f374 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -7,7 +7,11 @@ import pytz from itertools import chain, cycle, ifilter, izip, repeat from datetime import datetime, timedelta +import pandas as pd +from copy import copy +from zipline.protocol import DATASOURCE_TYPE +from zipline.utils import ndict from zipline.gens.utils import hash_args, create_trade def date_gen(start = datetime(2006, 6, 6, 12, tzinfo=pytz.utc), diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index b901bc4c..dc80ef5a 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -46,9 +46,6 @@ class BuySellAlgorithm(object): def set_portfolio(self, portfolio): self.portfolio = portfolio - def set_logger(self, logger): - self.logger = logger - def handle_data(self, frame): order_size = self.buy_or_sell * (self.amount - (self.offset**2)) self.order(self.sid, order_size) @@ -76,15 +73,16 @@ class TradingAlgorithm(object): # Create transforms by wrapping them into StatefulTransforms transforms = [] - for namestring, trans_descr in self.registered_transforms.iteritems(): - sf = StatefulTransform( - trans_descr['class'], - *trans_descr['args'], - **trans_descr['kwargs'] - ) - sf.namestring = namestring + if hasattr(self, 'registered_transforms'): + for namestring, trans_descr in self.registered_transforms.iteritems(): + sf = StatefulTransform( + trans_descr['class'], + *trans_descr['args'], + **trans_descr['kwargs'] + ) + sf.namestring = namestring - transforms.append(sf) + transforms.append(sf) style = SIMULATION_STYLE.FIXED_SLIPPAGE @@ -95,6 +93,7 @@ class TradingAlgorithm(object): self, environment, style) + #self.simulated_trading.trading_client.performance_tracker.compute_risk_metrics = compute_risk_metrics @@ -119,7 +118,7 @@ class TradingAlgorithm(object): self._setup(compute_risk_metrics=compute_risk_metrics) # drain simulated_trading - perfs = list(self.simulated_trading) + perfs = [perf for perf in self.simulated_trading] daily_stats = self._create_daily_stats(perfs) return daily_stats @@ -146,3 +145,41 @@ class TradingAlgorithm(object): self.registered_transforms[tag] = {'class': transform_class, 'args': args, 'kwargs': kwargs} + + +class BuySellAlgorithmNew(TradingAlgorithm): + """Algorithm that buys and sells alternatingly. The amount for + each order can be specified. In addition, an offset that will + quadratically reduce the amount that will be bought can be + specified. + + This algorithm is used to test the parameter optimization + framework. If combined with the UpDown trade source, an offset of + 0 will produce maximum returns. + + """ + + def __init__(self, sids, amount, offset): + self.sids = sids + self.amount = amount + self.incr = 0 + self.done = False + self.order = None + self.frame_count = 0 + self.portfolio = None + self.buy_or_sell = -1 + self.offset = offset + self.orders = [] + self.prices = [] + + def handle_data(self, data): + order_size = self.buy_or_sell * (self.amount - (self.offset**2)) + self.order(self.sid, order_size) + + #sell next time around. + self.buy_or_sell *= -1 + + self.orders.append(order_size) + + self.frame_count += 1 + self.incr += 1 diff --git a/zipline/optimize/factory.py b/zipline/optimize/factory.py index ff91dd35..85ee2c34 100644 --- a/zipline/optimize/factory.py +++ b/zipline/optimize/factory.py @@ -1,21 +1,22 @@ + """ Factory functions to prepare useful data for optimize tests. Author: Thomas V. Wiecki (thomas.wiecki@gmail.com), 2012 """ from datetime import timedelta +import pandas as pd +from copy import copy +from itertools import cycle import zipline.protocol as zp from zipline.utils.factory import get_next_trading_dt, create_trading_environment -from zipline.gens.tradegens import SpecificEquityTrades -from zipline.optimize.algorithms import BuySellAlgorithm +from zipline.gens.tradegens import SpecificEquityTrades, DataFrameSource +from zipline.optimize.algorithms import BuySellAlgorithmNew from zipline.lines import SimulatedTrading from zipline.finance.trading import SIMULATION_STYLE -from copy import copy -from itertools import cycle - def create_updown_trade_source(sid, trade_count, trading_environment, base_price, amplitude): """Create the updown trade source. This source emits events with the price going up and down by the same amount in each @@ -38,8 +39,6 @@ def create_updown_trade_source(sid, trade_count, trading_environment, base_price source : SpecificEquityTrades The trade source emitting up down events. """ - volume = 1000 - events = [] price = base_price-amplitude/2. cur = trading_environment.first_open @@ -47,27 +46,18 @@ def create_updown_trade_source(sid, trade_count, trading_environment, base_price #create iterator to cycle through up and down phases change = cycle([1,-1]) - + prices = [] + dts = [] for i in xrange(trade_count + 2): cur = get_next_trading_dt(cur, one_day, trading_environment) - - event = zp.ndict({ - "type" : zp.DATASOURCE_TYPE.TRADE, - "sid" : sid, - "price" : price, - "volume" : volume, - "dt" : cur, - }) - - events.append(event) + dts.append(cur) + prices.append(price) price += change.next()*amplitude - trading_environment.period_end = cur + df = pd.DataFrame(index=dts, data=prices, columns=[0]) - source = SpecificEquityTrades(event_list=events) - - return source + return df def create_predictable_zipline(config, offset=0, simulate=True): @@ -121,7 +111,7 @@ def create_predictable_zipline(config, offset=0, simulate=True): amplitude) if 'algorithm' not in config: - config['algorithm'] = BuySellAlgorithm(sid, 100, offset) + algorithm = BuySellAlgorithmNew(sid, 100, offset) config['order_count'] = trade_count - 1 config['trade_count'] = trade_count @@ -130,9 +120,9 @@ def create_predictable_zipline(config, offset=0, simulate=True): config['simulation_style'] = SIMULATION_STYLE.FIXED_SLIPPAGE config['devel'] = True - zipline = SimulatedTrading.create_test_zipline(**config) - if simulate: - zipline.drain_zipline(blocking=True) + algorithm.run() + + return algorithm, config + - return zipline, config From 0b589ee39c372ab1e0a62b1381ef1a1b5b73f12d Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Thu, 23 Aug 2012 16:03:02 -0400 Subject: [PATCH 08/10] Fixes to the updown algorithm. First unittest passes. --- tests/test_optimize.py | 8 +++----- zipline/gens/tradegens.py | 5 ++--- zipline/lines.py | 13 ++++++++++--- zipline/optimize/algorithms.py | 10 +++++++--- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index 52e27bb8..83dca6a9 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -27,7 +27,7 @@ class TestUpDown(TestCase): def setUp(self): self.zipline_test_config = { 'allocator' : allocator, - 'sid' : 133, + 'sid' : [0], 'trade_count' : 5, 'amplitude' : 30, 'base_price' : 50 @@ -48,7 +48,7 @@ class TestUpDown(TestCase): """ - zipline, config = create_predictable_zipline( + algo, config = create_predictable_zipline( self.zipline_test_config, offset=0, simulate=False @@ -74,9 +74,7 @@ class TestUpDown(TestCase): "Minimum price does not equal expected maximum price." ) - zipline.run(config['trade_source']) - - algo = config['algorithm'] + algo.run(config['trade_source']) orders = np.asarray(algo.orders) max_order_idx = np.where(orders==orders.max())[0] diff --git a/zipline/gens/tradegens.py b/zipline/gens/tradegens.py index 5716f374..09685cba 100644 --- a/zipline/gens/tradegens.py +++ b/zipline/gens/tradegens.py @@ -182,7 +182,7 @@ class DataFrameSource(SpecificEquityTrades): self.data = data # Unpack config dictionary with default values. self.count = kwargs.get('count', 500) - self.sids = kwargs.get('sids', [1, 2]) + self.sids = kwargs.get('sids', [0]) self.start = kwargs.get('start', datetime(1957, 1, 1, 0, tzinfo = pytz.utc)) self.end = kwargs.get('end', datetime(2010, 1, 1, tzinfo=pytz.utc)) self.delta = kwargs.get('delta', timedelta(days = 1)) @@ -198,8 +198,7 @@ class DataFrameSource(SpecificEquityTrades): def create_fresh_generator(self): def _generator(df=self.data): for dt, series in df.iterrows(): - dt = dt.tz_localize('UTC') - if (self.start > dt) or (dt < self.end): + if (dt < self.start) or (dt > self.end): continue event = {'dt': dt, 'source_id': self.get_hash(), diff --git a/zipline/lines.py b/zipline/lines.py index e4e4af8c..9f2a8153 100644 --- a/zipline/lines.py +++ b/zipline/lines.py @@ -179,7 +179,7 @@ class SimulatedTrading(object): else: log.warning("Sending SIGINT") os.kill(ppid, SIGINT) - + def handle_exception(self, exc): if isinstance(exc, CancelSignal): # signal from monitor of an orderly shutdown, @@ -206,7 +206,7 @@ class SimulatedTrading(object): exc_type.__name__, exc_value.message ) - + self.results_socket.send(msg) except: log.exception("Exception while reporting simulation exception.") @@ -380,6 +380,13 @@ class SimulatedTradingLite(object): self.with_tnfms = sequential_transforms(self.date_sorted, *self.transforms) self.trading_client = tsc(algorithm, environment, style) self.gen = self.trading_client.simulate(self.with_tnfms) - + + def get_results(self): return self.gen + + def __iter__(self): + return self + + def next(self): + return self.gen.next() diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index dc80ef5a..727e43db 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -10,8 +10,12 @@ from zipline.protocol import DATASOURCE_TYPE from zipline import ndict from zipline.utils.factory import create_trading_environment from zipline.gens.transform import StatefulTransform -from zipline.lines import SimulatedTradingLite +from zipline.lines import SimulatedTradingLite, SimulatedTrading +from logbook import Logger + + +logger = Logger('Algo') class BuySellAlgorithm(object): """Algorithm that buys and sells alternatingly. The amount for each order can be specified. In addition, an offset that will @@ -118,7 +122,7 @@ class TradingAlgorithm(object): self._setup(compute_risk_metrics=compute_risk_metrics) # drain simulated_trading - perfs = [perf for perf in self.simulated_trading] + perfs = list(self.simulated_trading) daily_stats = self._create_daily_stats(perfs) return daily_stats @@ -174,7 +178,7 @@ class BuySellAlgorithmNew(TradingAlgorithm): def handle_data(self, data): order_size = self.buy_or_sell * (self.amount - (self.offset**2)) - self.order(self.sid, order_size) + self.order(self.sids[0], order_size) #sell next time around. self.buy_or_sell *= -1 From 3f801ef387a0ebeb11ffdcf8ed375899acb1405f Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Thu, 23 Aug 2012 17:38:48 -0400 Subject: [PATCH 09/10] 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, From 5fcaed3644f4fe48e2681f52aa5602b35ba532c7 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Wed, 29 Aug 2012 09:02:48 -0400 Subject: [PATCH 10/10] Added logging statements. --- zipline/gens/tradesimulation.py | 2 ++ zipline/optimize/algorithms.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 8ad54053..2f60c7e0 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -190,6 +190,8 @@ class AlgorithmSimulator(object): 'filled' : 0 }) + log.debug(order) + # Tell the user if they try to buy 0 shares of something. if order.amount == 0: zero_message = "Requested to trade zero shares of {sid}".format( diff --git a/zipline/optimize/algorithms.py b/zipline/optimize/algorithms.py index 30feaf59..cc438fd1 100644 --- a/zipline/optimize/algorithms.py +++ b/zipline/optimize/algorithms.py @@ -14,8 +14,8 @@ from zipline.lines import SimulatedTradingLite, SimulatedTrading from logbook import Logger - logger = Logger('Algo') + class BuySellAlgorithm(object): """Algorithm that buys and sells alternatingly. The amount for each order can be specified. In addition, an offset that will @@ -184,6 +184,7 @@ class BuySellAlgorithmNew(TradingAlgorithm): def handle_data(self, data): order_size = self.buy_or_sell * (self.amount - (self.offset**2)) self.order(self.sids[0], order_size) + logger.debug("ordering" + str(order_size)) #sell next time around. self.buy_or_sell *= -1 @@ -192,4 +193,4 @@ class BuySellAlgorithmNew(TradingAlgorithm): self.frame_count += 1 self.incr += 1 - from nose.tools import set_trace; set_trace() +