Debugging.

This commit is contained in:
Thomas Wiecki
2012-08-23 17:38:48 -04:00
parent 0b589ee39c
commit 3f801ef387
5 changed files with 34 additions and 23 deletions
+12 -6
View File
@@ -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."
+5 -5
View File
@@ -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)
+9 -3
View File
@@ -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()
+2 -6
View File
@@ -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
+6 -3
View File
@@ -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,