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