mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 06:13:40 +08:00
fixes for unit tests, back to 50/51 passing.
This commit is contained in:
@@ -26,9 +26,7 @@ class ExceptionTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.zipline_test_config = {
|
||||
'allocator' : allocator,
|
||||
'sid' : 133,
|
||||
'devel' : False,
|
||||
'results_socket_uri' : allocator.lease(1)[0],
|
||||
'simulation_style' : SIMULATION_STYLE.FIXED_SLIPPAGE
|
||||
}
|
||||
|
||||
+8
-17
@@ -11,7 +11,6 @@ from collections import defaultdict
|
||||
from nose.tools import timed
|
||||
|
||||
import zipline.utils.factory as factory
|
||||
import zipline.protocol as zp
|
||||
|
||||
from zipline.test_algorithms import TestAlgorithm
|
||||
from zipline.finance.trading import TradingEnvironment
|
||||
@@ -19,10 +18,9 @@ from zipline.core.devsimulator import AddressAllocator
|
||||
from zipline.lines import SimulatedTrading
|
||||
from zipline.finance.performance import PerformanceTracker
|
||||
from zipline.utils.protocol_utils import ndict
|
||||
from zipline.finance.trading import TransactionSimulator, SIMULATION_STYLE
|
||||
from zipline.finance.trading import TransactionSimulator
|
||||
from zipline.utils.test_utils import \
|
||||
drain_zipline, \
|
||||
check, \
|
||||
setup_logger, \
|
||||
teardown_logger,\
|
||||
assert_single_position
|
||||
@@ -39,10 +37,8 @@ class FinanceTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.zipline_test_config = {
|
||||
'allocator' : allocator,
|
||||
'sid' : 133,
|
||||
#'devel' : True,
|
||||
'results_socket' : allocator.lease(1)[0]
|
||||
'sid' : 133,
|
||||
'results_socket_uri' : allocator.lease(1)[0]
|
||||
}
|
||||
self.ctx = zmq.Context()
|
||||
|
||||
@@ -60,7 +56,7 @@ class FinanceTestCase(TestCase):
|
||||
trading_environment
|
||||
)
|
||||
prev = None
|
||||
for trade in trade_source.event_list:
|
||||
for trade in trade_source:
|
||||
if prev:
|
||||
self.assertTrue(trade.dt > prev.dt)
|
||||
prev = trade
|
||||
@@ -123,7 +119,6 @@ class FinanceTestCase(TestCase):
|
||||
self.zipline_test_config['order_count'] = 100
|
||||
self.zipline_test_config['trade_count'] = 200
|
||||
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
|
||||
|
||||
assert_single_position(self, zipline)
|
||||
|
||||
#@timed(DEFAULT_TIMEOUT)
|
||||
@@ -148,9 +143,6 @@ class FinanceTestCase(TestCase):
|
||||
)
|
||||
output, transaction_count = drain_zipline(self, zipline)
|
||||
|
||||
self.assertTrue(zipline.sim.ready())
|
||||
self.assertFalse(zipline.sim.exception)
|
||||
|
||||
#check that the algorithm received no events
|
||||
self.assertEqual(
|
||||
0,
|
||||
@@ -301,12 +293,12 @@ class FinanceTestCase(TestCase):
|
||||
# if present, expect transaction amounts to match orders exactly.
|
||||
complete_fill = params.get('complete_fill')
|
||||
|
||||
sid = 1
|
||||
trading_environment = factory.create_trading_environment()
|
||||
trade_sim = TransactionSimulator()
|
||||
trade_sim = TransactionSimulator([sid])
|
||||
price = [10.1] * trade_count
|
||||
volume = [100] * trade_count
|
||||
start_date = trading_environment.first_open
|
||||
sid = 1
|
||||
|
||||
generated_trades = factory.create_trade_history(
|
||||
sid,
|
||||
@@ -330,7 +322,7 @@ class FinanceTestCase(TestCase):
|
||||
'dt' : order_date
|
||||
})
|
||||
|
||||
trade_sim.add_open_order(order)
|
||||
trade_sim.place_order(order)
|
||||
|
||||
order_date = order_date + order_interval
|
||||
# move after market orders to just after market next
|
||||
@@ -353,14 +345,13 @@ class FinanceTestCase(TestCase):
|
||||
self.assertEqual(order.amount, order_amount * alternator**i)
|
||||
|
||||
|
||||
tracker = PerformanceTracker(trading_environment)
|
||||
tracker = PerformanceTracker(trading_environment, [sid])
|
||||
|
||||
# this approximates the loop inside TradingSimulationClient
|
||||
transactions = []
|
||||
for trade in generated_trades:
|
||||
if trade_delay:
|
||||
trade.dt = trade.dt + trade_delay
|
||||
|
||||
txn = trade_sim.apply_trade_to_open_orders(trade)
|
||||
if txn:
|
||||
transactions.append(txn)
|
||||
|
||||
@@ -14,13 +14,15 @@ class TestMonitor(TestCase):
|
||||
def test_init(self):
|
||||
pub_socket = 'tcp://127.0.0.1:5000'
|
||||
route_socket = 'tcp://127.0.0.1:5001'
|
||||
exception_socket = 'tcp://127.0.0.1:5002'
|
||||
|
||||
mon = Monitor(pub_socket, route_socket)
|
||||
mon = Monitor(pub_socket, route_socket, exception_socket)
|
||||
mon.manage([])
|
||||
|
||||
def test_init_topology(self):
|
||||
pub_socket = 'tcp://127.0.0.1:5000'
|
||||
route_socket = 'tcp://127.0.0.1:5001'
|
||||
exception_socket = 'tcp://127.0.0.1:5002'
|
||||
|
||||
mon = Monitor(pub_socket, route_socket, )
|
||||
mon = Monitor(pub_socket, route_socket, exception_socket)
|
||||
mon.manage([ 'a', 'b', 'c', 'd' ])
|
||||
|
||||
@@ -543,7 +543,10 @@ shares in position"
|
||||
self.trading_environment.capital_base = 1000.0
|
||||
self.trading_environment.frame_index = ['sid', 'volume', 'dt', \
|
||||
'price', 'changed']
|
||||
perf_tracker = perf.PerformanceTracker(self.trading_environment)
|
||||
perf_tracker = perf.PerformanceTracker(
|
||||
self.trading_environment,
|
||||
[sid, sid2]
|
||||
)
|
||||
|
||||
for event in trade_history:
|
||||
#create a transaction for all but
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
"""
|
||||
Test the FRAME/UNFRAME functions in the sequence expected from ziplines.
|
||||
"""
|
||||
import pytz
|
||||
|
||||
from unittest2 import TestCase
|
||||
from datetime import datetime, timedelta
|
||||
from collections import defaultdict
|
||||
@@ -10,10 +8,8 @@ from collections import defaultdict
|
||||
from nose.tools import timed
|
||||
|
||||
import zipline.utils.factory as factory
|
||||
from zipline.utils import logger
|
||||
import zipline.protocol as zp
|
||||
|
||||
from zipline.finance.sources import SpecificEquityTrades
|
||||
|
||||
DEFAULT_TIMEOUT = 5 # seconds
|
||||
|
||||
|
||||
+10
-11
@@ -37,7 +37,7 @@ class ZiplineWithTransformsTestCase(TestCase):
|
||||
zipline = SimulatedTrading.create_test_zipline(
|
||||
**self.zipline_test_config
|
||||
)
|
||||
vwap = VWAPTransform("vwap_10", daycount=10)
|
||||
vwap = VWAP("vwap_10", daycount=10)
|
||||
zipline.add_transform(vwap)
|
||||
|
||||
zipline.simulate(blocking=True)
|
||||
@@ -49,7 +49,7 @@ class FinanceTransformsTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.trading_environment = factory.create_trading_environment()
|
||||
setup_logger(self, '/var/log/qexec/qexec.log')
|
||||
setup_logger(self)
|
||||
|
||||
trade_history = factory.create_trade_history(
|
||||
133,
|
||||
@@ -74,11 +74,11 @@ class FinanceTransformsTestCase(TestCase):
|
||||
((10.0 * 100) + (10.0 * 100)) / (200.0),
|
||||
((10.0 * 100) + (10.0 * 100) + (11.0 * 100)) / (300.0),
|
||||
# First event should get droppped here.
|
||||
((10.0 * 100) + (11.0 * 100) + (11.0 * 300)) / (500.0)]
|
||||
((10.0 * 100) + (11.0 * 100) + (11.0 * 300)) / (500.0)]
|
||||
|
||||
# Output should match the expected.
|
||||
assert tnfm_vals == expected
|
||||
|
||||
|
||||
|
||||
def test_returns(self):
|
||||
trade_history = factory.create_trade_history(
|
||||
@@ -98,13 +98,13 @@ class FinanceTransformsTestCase(TestCase):
|
||||
|
||||
|
||||
def test_moving_average(self):
|
||||
|
||||
|
||||
mavg = StatefulTransform(
|
||||
MovingAverage,
|
||||
timedelta(days = 2),
|
||||
MovingAverage,
|
||||
timedelta(days = 2),
|
||||
['price', 'volume']
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
transformed = list(mavg.transform(self.source))
|
||||
# Output values.
|
||||
tnfm_prices = [message.tnfm_value.price for message in transformed]
|
||||
@@ -120,7 +120,6 @@ class FinanceTransformsTestCase(TestCase):
|
||||
((100.0 + 100.0 + 100.0) / 3.0),
|
||||
# First event should get dropped here.
|
||||
((100.0 + 100.0 + 300.0) / 3.0)]
|
||||
|
||||
|
||||
assert tnfm_prices == expected_prices
|
||||
assert tnfm_volumes == expected_volumes
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
from collections import defaultdict
|
||||
from zipline.transforms.base import BaseTransform
|
||||
|
||||
class Returns(object):
|
||||
"""
|
||||
Class that maintains a dictionary from sids to the event
|
||||
representing the most recent closing price.
|
||||
"""
|
||||
def __init__(self, days == 1):
|
||||
def __init__(self, days = 1):
|
||||
self.days = days
|
||||
self.mapping = defaultdict(self._create)
|
||||
|
||||
|
||||
def update(self, event):
|
||||
"""
|
||||
Update and return the calculated returns for this event's sid.
|
||||
@@ -18,7 +17,7 @@ class Returns(object):
|
||||
return sid_returns
|
||||
|
||||
def _create(self):
|
||||
return ReturnsFromPriorClose(days)
|
||||
return ReturnsFromPriorClose(self.days)
|
||||
|
||||
class ReturnsFromPriorClose(object):
|
||||
"""
|
||||
|
||||
@@ -31,6 +31,8 @@ class TransactionSimulator(object):
|
||||
self.open_orders[sid] = []
|
||||
|
||||
def place_order(self, order):
|
||||
# initialized filled field.
|
||||
order.filled = 0
|
||||
self.open_orders[order.sid].append(order)
|
||||
|
||||
def update(self, event):
|
||||
@@ -39,7 +41,7 @@ class TransactionSimulator(object):
|
||||
if event.type == zp.DATASOURCE_TYPE.TRADE:
|
||||
event.TRANSACTION = self.apply_trade_to_open_orders(event)
|
||||
return event
|
||||
|
||||
|
||||
def simulate_buy_all(self, event):
|
||||
txn = self.create_transaction(
|
||||
event.sid,
|
||||
|
||||
+6
-1
@@ -162,8 +162,13 @@ class SimulatedTrading(object):
|
||||
finally:
|
||||
self.close()
|
||||
|
||||
def signal_done(self):
|
||||
# notify monitor we're done
|
||||
done_frame = zp.DONE_FRAME('succes')
|
||||
self.results_socket.send(done_frame)
|
||||
|
||||
def close(self):
|
||||
log.info("Closing Simulation")
|
||||
log.info("Closing Simulation: {id}".format(id=self.sim_id))
|
||||
|
||||
def cancel(self):
|
||||
if self.proc and self.proc.is_alive():
|
||||
|
||||
@@ -570,6 +570,12 @@ def CANCEL_FRAME(date):
|
||||
|
||||
return BT_UPDATE_FRAME('CANCEL', result)
|
||||
|
||||
def DONE_FRAME(msg):
|
||||
assert isinstance(msg, basestring), \
|
||||
"Done message must be a string."
|
||||
|
||||
return BT_UPDATE_FRAME('DONE', msg)
|
||||
|
||||
|
||||
def BT_UPDATE_FRAME(prefix, payload):
|
||||
"""
|
||||
|
||||
@@ -12,7 +12,7 @@ from datetime import datetime, timedelta
|
||||
import zipline.finance.risk as risk
|
||||
import zipline.protocol as zp
|
||||
|
||||
from zipline.finance.sources import RandomEquityTrades
|
||||
from zipline.gens.tradegens import RandomEquityTrades
|
||||
from zipline.gens.tradegens import SpecificEquityTrades
|
||||
from zipline.gens.utils import create_trade
|
||||
from zipline.finance.trading import TradingEnvironment
|
||||
|
||||
+11
-14
@@ -76,7 +76,7 @@ def drain_zipline(test, zipline):
|
||||
time.sleep(1)
|
||||
|
||||
# start the simulation
|
||||
zipline.simulate(blocking=True)
|
||||
zipline.simulate(blocking=False)
|
||||
output, transaction_count = drain_receiver(test.receiver)
|
||||
# some processes will exit after the message stream is
|
||||
# finished. We block here to avoid collisions with subsequent
|
||||
@@ -96,16 +96,15 @@ def drain_receiver(receiver):
|
||||
transaction_count = 0
|
||||
while True:
|
||||
msg = receiver.recv()
|
||||
if msg == str(zp.CONTROL_PROTOCOL.DONE):
|
||||
update = zp.BT_UPDATE_UNFRAME(msg)
|
||||
output.append(update)
|
||||
if update['prefix'] == 'PERF':
|
||||
transaction_count += \
|
||||
len(update['payload']['daily_perf']['transactions'])
|
||||
elif update['prefix'] == 'EXCEPTION':
|
||||
break
|
||||
elif update['prefix'] == 'DONE':
|
||||
break
|
||||
else:
|
||||
update = zp.BT_UPDATE_UNFRAME(msg)
|
||||
output.append(update)
|
||||
if update['prefix'] == 'PERF':
|
||||
transaction_count += \
|
||||
len(update['payload']['daily_perf']['transactions'])
|
||||
elif update['prefix'] == 'EXCEPTION':
|
||||
break
|
||||
|
||||
receiver.close()
|
||||
del receiver
|
||||
@@ -116,9 +115,6 @@ def drain_receiver(receiver):
|
||||
def assert_single_position(test, zipline):
|
||||
output, transaction_count = drain_zipline(test, zipline)
|
||||
|
||||
test.assertTrue(zipline.sim.ready())
|
||||
test.assertFalse(zipline.sim.exception)
|
||||
|
||||
test.assertEqual(
|
||||
test.zipline_test_config['order_count'],
|
||||
transaction_count
|
||||
@@ -127,7 +123,8 @@ def assert_single_position(test, zipline):
|
||||
# the final message is the risk report, the second to
|
||||
# last is the final day's results. Positions is a list of
|
||||
# dicts.
|
||||
closing_positions = output[-2]['payload']['daily_perf']['positions']
|
||||
perfs = [x for x in output if x['prefix'] == 'PERF']
|
||||
closing_positions = perfs[-2]['payload']['daily_perf']['positions']
|
||||
|
||||
test.assertEqual(
|
||||
len(closing_positions),
|
||||
|
||||
Reference in New Issue
Block a user