mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-30 07:01:52 +08:00
modification of the transport protocol -- keeping it close to the export of data from the performance tracker.
This commit is contained in:
@@ -218,7 +218,7 @@ class PerformanceTracker():
|
||||
'capital_base' : self.capital_base,
|
||||
'returns' : returns_list,
|
||||
'cumulative_perf' : self.cumulative_performance.to_dict(),
|
||||
'todays_perf' : self.todays_performance.to_dict(),
|
||||
'daily_perf' : self.todays_performance.to_dict(),
|
||||
'cumulative_risk_metrics' : self.cumulative_risk_metrics.to_dict(),
|
||||
}
|
||||
|
||||
@@ -434,7 +434,7 @@ class PerformancePeriod():
|
||||
self.max_leverage = 1.1 * self.max_capital_used / self.starting_cash
|
||||
|
||||
# add transaction to the list of processed transactions
|
||||
self.processed_transactions.append(txn.as_dict())
|
||||
self.processed_transactions.append(txn)
|
||||
|
||||
def round_to_nearest(self, x, base=5):
|
||||
return int(base * round(float(x)/base))
|
||||
@@ -456,7 +456,8 @@ class PerformancePeriod():
|
||||
Creates a dictionary representing the state of this performance
|
||||
period. See header comments for a detailed description.
|
||||
"""
|
||||
positions = self.get_positions()
|
||||
positions = self.get_positions_list()
|
||||
transactions = [x.as_dict() for x in self.processed_transactions]
|
||||
|
||||
return {
|
||||
'ending_value' : self.ending_value,
|
||||
@@ -468,7 +469,7 @@ class PerformancePeriod():
|
||||
'positions' : positions,
|
||||
'pnl' : self.pnl,
|
||||
'returns' : self.returns,
|
||||
'transactions' : self.processed_transactions,
|
||||
'transactions' : transactions,
|
||||
}
|
||||
|
||||
def to_namedict(self):
|
||||
@@ -502,6 +503,14 @@ class PerformancePeriod():
|
||||
|
||||
return positions
|
||||
|
||||
#
|
||||
def get_positions_list(self):
|
||||
positions = []
|
||||
for sid, pos in self.positions.iteritems():
|
||||
cur = pos.to_dict()
|
||||
positions.append(cur)
|
||||
return positions
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class TradeSimulationClient(qmsg.Component):
|
||||
self.current_dt = trading_environment.period_start
|
||||
self.last_iteration_dur = datetime.timedelta(seconds=0)
|
||||
self.algorithm = None
|
||||
self.max_wait = datetime.timedelta(seconds=3)
|
||||
self.max_wait = datetime.timedelta(seconds=7)
|
||||
self.last_msg_dt = datetime.datetime.utcnow()
|
||||
|
||||
assert self.trading_environment.frame_index != None
|
||||
|
||||
+21
-10
@@ -637,14 +637,16 @@ def PERF_FRAME(perf):
|
||||
assert isinstance(perf['last_close'], datetime.datetime)
|
||||
assert isinstance(perf['last_open'], datetime.datetime)
|
||||
|
||||
assert isinstance(perf['todays_perf'], dict)
|
||||
assert isinstance(perf['daily_perf'], dict)
|
||||
assert isinstance(perf['cumulative_perf'], dict)
|
||||
|
||||
tp = perf['todays_perf']
|
||||
tp = perf['daily_perf']
|
||||
cp = perf['cumulative_perf']
|
||||
|
||||
assert isinstance(tp['transactions'], list)
|
||||
assert isinstance(cp['transactions'], list)
|
||||
assert isinstance(tp['positions'], list)
|
||||
assert isinstance(cp['positions'], list)
|
||||
|
||||
perf['started_at'] = EPOCH(perf['started_at'])
|
||||
perf['period_start'] = EPOCH(perf['period_start'])
|
||||
@@ -652,18 +654,27 @@ def PERF_FRAME(perf):
|
||||
perf['last_close'] = EPOCH(perf['last_close'])
|
||||
perf['last_open'] = EPOCH(perf['last_open'])
|
||||
|
||||
for txn in tp['transactions']:
|
||||
txn['dt'] = EPOCH(txn['dt'])
|
||||
|
||||
for txn in cp['transactions']:
|
||||
txn['dt'] = EPOCH(txn['dt'])
|
||||
|
||||
|
||||
tp['transactions'] = convert_transactions(tp['transactions'])
|
||||
cp['transactions'] = convert_transactions(cp['transactions'])
|
||||
|
||||
returns = []
|
||||
for dr in perf['returns']:
|
||||
dr['dt'] = EPOCH(dr['dt'])
|
||||
updated = {}
|
||||
updated['returns'] = dr['returns']
|
||||
updated['date'] = EPOCH(dr['dt'])
|
||||
returns.append(updated)
|
||||
|
||||
perf['returns'] = returns
|
||||
|
||||
return msgpack.dumps(tuple(['PERF', perf]))
|
||||
|
||||
def convert_transactions(transactions):
|
||||
results = []
|
||||
for txn in transactions:
|
||||
txn['date'] = EPOCH(txn['dt'])
|
||||
del(txn['dt'])
|
||||
results.append(txn)
|
||||
return results
|
||||
|
||||
def RISK_FRAME(risk):
|
||||
return msgpack.dumps(tuple(['RISK', risk]))
|
||||
|
||||
@@ -76,7 +76,39 @@ class TestAlgorithm():
|
||||
self.incr += 1
|
||||
|
||||
def get_sid_filter(self):
|
||||
return [self.sid]
|
||||
return [self.sid]
|
||||
|
||||
#
|
||||
class HeavyBuyAlgorithm():
|
||||
"""
|
||||
This algorithm will send a specified number of orders, to allow unit tests
|
||||
to verify the orders sent/received, transactions created, and positions
|
||||
at the close of a simulation.
|
||||
"""
|
||||
|
||||
def __init__(self, sid, amount):
|
||||
self.sid = sid
|
||||
self.amount = amount
|
||||
self.incr = 0
|
||||
self.done = False
|
||||
self.order = None
|
||||
self.frame_count = 0
|
||||
self.portfolio = None
|
||||
|
||||
def set_order(self, order_callable):
|
||||
self.order = order_callable
|
||||
|
||||
def set_portfolio(self, portfolio):
|
||||
self.portfolio = portfolio
|
||||
|
||||
def handle_frame(self, frame):
|
||||
self.frame_count += 1
|
||||
#place an order for 100 shares of sid
|
||||
self.order(self.sid, self.amount)
|
||||
self.incr += 1
|
||||
|
||||
def get_sid_filter(self):
|
||||
return [self.sid]
|
||||
|
||||
class NoopAlgorithm(object):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user