Resolved conflicts

This commit is contained in:
Thomas Wiecki
2012-08-23 10:54:02 -04:00
27 changed files with 920 additions and 517 deletions
+53
View File
@@ -0,0 +1,53 @@
import os
from signal import signal, SIGHUP, SIGINT
import time
from types import FrameType
import unittest
from zipline.utils.delayed_signals import delayed_signals
class DelayedSignals(unittest.TestCase):
def handler(self, signum, frame):
print "Got signal " + str(signum)
self.got[signum] = time.time()
self.assertTrue(isinstance(frame, FrameType))
def setUp(self):
signal(SIGHUP, self.handler)
signal(SIGINT, self.handler)
def reset(self):
self.got = {}
def test_delayed_signals(self):
self.reset()
with delayed_signals([SIGHUP]):
os.kill(os.getpid(), SIGHUP)
time.sleep(2)
self.assertTrue(self.got[SIGHUP])
self.assertTrue(time.time() - self.got[SIGHUP] < 2)
def test_immediate_signals(self):
self.reset()
os.kill(os.getpid(), SIGHUP)
time.sleep(2)
self.assertTrue(self.got[SIGHUP])
self.assertTrue(time.time() - self.got[SIGHUP] > 1)
def test_multiple_signals(self):
self.reset()
with delayed_signals([SIGHUP, SIGINT]):
os.kill(os.getpid(), SIGINT)
self.assertFalse(SIGHUP in self.got)
self.assertTrue(SIGINT in self.got)
@delayed_signals([SIGHUP])
def kill_and_sleep(self):
os.kill(os.getpid(), SIGHUP)
time.sleep(2)
def test_decorator(self):
self.reset()
self.kill_and_sleep()
self.assertTrue(SIGHUP in self.got)
self.assertTrue(time.time() - self.got[SIGHUP] < 2)
+47 -1
View File
@@ -3,11 +3,14 @@ import zmq
from unittest2 import TestCase
from collections import defaultdict
from zipline.test_algorithms import ExceptionAlgorithm, DivByZeroAlgorithm
from zipline.test_algorithms import ExceptionAlgorithm, DivByZeroAlgorithm, \
InitializeTimeoutAlgorithm, TooMuchProcessingAlgorithm
from zipline.finance.trading import SIMULATION_STYLE
from zipline.core.devsimulator import AddressAllocator
from zipline.lines import SimulatedTrading
from zipline.gens.transform import StatefulTransform
from zipline.gens.tradesimulation import HEARTBEAT_INTERVAL, \
MAX_HEARTBEAT_INTERVALS
from zipline.utils.test_utils import \
drain_zipline, \
@@ -143,3 +146,46 @@ class ExceptionTestCase(TestCase):
# make sure our path shortening is working
self.assertEqual(payload['stack'][0]['filename'], '/zipline/lines.py')
self.assertEqual(payload['stack'][-1]['filename'], '/zipline/test_algorithms.py')
def test_initialize_timeout(self):
self.zipline_test_config['algorithm'] = \
InitializeTimeoutAlgorithm(
self.zipline_test_config['sid']
)
zipline = SimulatedTrading.create_test_zipline(
**self.zipline_test_config
)
output, _ = drain_zipline(self, zipline)
self.assertEqual(output[-1]['prefix'], 'EXCEPTION')
payload = output[-1]['payload']
self.assertEqual(payload['name'],'Timeout')
self.assertEqual(payload['message'], 'Call to initialize timed out')
def test_heartbeat(self):
self.zipline_test_config['algorithm'] = \
TooMuchProcessingAlgorithm(
self.zipline_test_config['sid']
)
zipline = SimulatedTrading.create_test_zipline(
**self.zipline_test_config
)
output, _ = drain_zipline(self, zipline)
# There should be a message for each hearbeat, plus a message
# for the final timeout.
assert len(output) == MAX_HEARTBEAT_INTERVALS + 1
# Assert that everything but the last message is a heartbeat log.
for message in output[0:-1]:
assert message['prefix'] == 'LOG'
assert message['payload']['func_name'] == 'log_heartbeats'
# Assert that the last message is a timeout exception.
self.assertEqual(output[-1]['prefix'], 'EXCEPTION')
payload = output[-1]['payload']
self.assertEqual(payload['name'],'Timeout')
self.assertEqual(payload['message'], 'Too much time spent in handle_data call')
-31
View File
@@ -20,7 +20,6 @@ from zipline.finance.performance import PerformanceTracker
from zipline.utils.protocol_utils import ndict
from zipline.finance.trading import TransactionSimulator
from zipline.utils.test_utils import \
drain_zipline, \
setup_logger, \
teardown_logger,\
assert_single_position
@@ -121,36 +120,6 @@ class FinanceTestCase(TestCase):
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
assert_single_position(self, zipline)
#@timed(DEFAULT_TIMEOUT)
def test_sid_filter(self):
# Ensure the algorithm's filter prevents events from arriving.
# create a test algorithm whose filter will not match any of the
# trade events sourced inside the zipline.
order_amount = 100
order_count = 100
no_match_sid = 222
test_algo = TestAlgorithm(
no_match_sid,
order_amount,
order_count
)
self.zipline_test_config['trade_count'] = 200
self.zipline_test_config['algorithm'] = test_algo
zipline = SimulatedTrading.create_test_zipline(
**self.zipline_test_config
)
output, transaction_count = drain_zipline(self, zipline)
#check that the algorithm received no events
self.assertEqual(
0,
transaction_count,
"The algorithm should not receive any events due to filtering."
)
# TODO: write tests for short sales
# TODO: write a test to do massive buying or shorting.
+40
View File
@@ -1,7 +1,15 @@
import logging
import logbook
import uuid
import zmq
from zipline import ndict
from zipline.utils.logger import configure_logging, tail
from zipline.utils.log_utils import ZeroMQLogHandler
from zipline.utils.test_utils import create_receiver, drain_receiver
from unittest2 import TestCase
@@ -20,3 +28,35 @@ class LoggerTestCase(TestCase):
last_line = tail(logfile, window=1)
logged_msg = last_line.split(" - ")[1]
self.assertEqual(test_msg, logged_msg)
def test_zmq_handler(self):
socket_addr = 'tcp://127.0.0.1:10000'
ctx = zmq.Context()
socket_push = ctx.socket(zmq.PUSH)
socket_push.connect(socket_addr)
recv = create_receiver(socket_addr, ctx)
zmq_out = ZeroMQLogHandler(
socket = socket_push,
filter = lambda r, h: r.channel in ['test zmq logger'],
context=ctx,
#bubble=False
)
log = logbook.Logger('test zmq logger')
x = ndict({})
x.a = 1
ex = example(133)
with zmq_out.threadbound():
log.info(ex.num)
output, _ = drain_receiver(recv, count=1)
self.assertEqual(output[-1]['prefix'], 'LOG')
self.assertTrue(isinstance(output[-1]['payload']['msg'], basestring))
class example(object):
def __init__(self, num):
self.num = num
+49 -1
View File
@@ -1,4 +1,5 @@
import pytz
import numpy
from datetime import timedelta, datetime
from collections import defaultdict
@@ -15,6 +16,7 @@ from zipline.gens.tradegens import SpecificEquityTrades
from zipline.gens.transform import StatefulTransform, EventWindow
from zipline.gens.vwap import VWAP
from zipline.gens.mavg import MovingAverage
from zipline.gens.stddev import MovingStandardDev
from zipline.gens.returns import Returns
import zipline.utils.factory as factory
@@ -70,6 +72,7 @@ class EventWindowTestCase(TestCase):
delta = timedelta(minutes = 5),
days = None
)
now = utcnow()
# 15 dates, increasing in 1 minute increments.
@@ -99,6 +102,7 @@ class EventWindowTestCase(TestCase):
delta = None,
days = 1
)
dates = ([self.pre_open]*3)
dates += ([self.mid_day]*3)
dates += ([self.post_close]*3)
@@ -239,11 +243,12 @@ class FinanceTransformsTestCase(TestCase):
fields = ['price', 'volume'],
delta = timedelta(days = 2),
)
transformed = list(mavg.transform(self.source))
# Output values.
tnfm_prices = [message.tnfm_value.price for message in transformed]
tnfm_volumes = [message.tnfm_value.volume for message in transformed]
# "Hand-calculated" values
expected_prices = [
((10.0) / 1.0),
@@ -264,3 +269,46 @@ class FinanceTransformsTestCase(TestCase):
assert tnfm_prices == expected_prices
assert tnfm_volumes == expected_volumes
def test_moving_stddev(self):
trade_history = factory.create_trade_history(
133,
[10.0, 15.0, 13.0, 12.0],
[100, 100, 100, 100],
timedelta(hours = 1),
self.trading_environment
)
stddev = StatefulTransform(
MovingStandardDev,
market_aware = False,
delta = timedelta(minutes = 150),
)
self.source = SpecificEquityTrades(event_list=trade_history)
transformed = list(stddev.transform(self.source))
vals = [message.tnfm_value for message in transformed]
expected = [
None,
numpy.std([10.0, 15.0], ddof = 1),
numpy.std([10.0, 15.0, 13.0], ddof = 1),
numpy.std([15.0, 13.0, 12.0], ddof = 1),
]
# numpy has odd rounding behavior, cf.
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html
for v1, v2 in zip(vals, expected):
if v1 == None:
assert v2 == None
continue
assert round(v1, 5) == round(v2, 5)