mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 14:09:44 +08:00
fixes for unit tests, back to 50/51 passing.
This commit is contained in:
@@ -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