Created
July 31, 2012 22:16
-
-
Save ssanderson/3221139 to your computer and use it in GitHub Desktop.
tentative top-level design for TSC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from zipline.gens import stateful_transform | |
from zipline.finance.trading import TransactionSimulator | |
from zipline.finance.performance import PerformanceTracker | |
def trade_simulation_client(stream_in, algo, environment, sim_style): | |
#============ | |
# Algo Setup | |
#============ | |
# Initialize txn_sim's dictionary of orders here so that we can | |
# reference it from within the user's algorithm. | |
sids = algo.get_sid_filter() | |
open_orders = {} | |
for sid in sids: | |
open_orders[sids] = [] | |
# Closure to pass into the user's algo to allow placing orders | |
# into the txn_sim's dict of open orders. | |
def order(self, sid, amount): | |
order = zp.ndict({ | |
'dt' : self.current_dt, | |
'sid' : sid, | |
'amount' : amount | |
'filled' : 0 | |
}) | |
open_orders[sid].append(event) | |
algo.set_order(order) | |
algo.set_logger(Logger("Algolog")) | |
# Pipe the in stream into the transaction simulator. | |
# Creates a TRANSACTION field on the event containing transaction | |
# information if we filled any pending orders on the event's sid. | |
# TRANSACTION is None if we didn't fill any orders. | |
with_txns = stateful_transform(stream_in, | |
TransactionSimulator, | |
open_orders, | |
style = sim_style) | |
# Pipe the events with transactions to perf. This will remove the | |
# TRANSACTION field added by TransactionSimulator and replace it with | |
# a portfolio object to be passed to the user's algorithm. Also adds | |
# a PERF_MESSAGE field which is usually none, but contains an update | |
# message once per day. | |
with_portfolio_and_perf_msg = stateful_transform(stream_with_txns, | |
PerformanceTracker, | |
trading_environment) | |
# Batch the event stream by dt to be processed by the user's algo. | |
# Will also set the PERF_MESSAGE field if the batch contains a perf | |
# message. | |
batches = batcher(with_portfolio_and_perf_msg) | |
for batch in batches: | |
algo.handle_data(batch.data, batch.context) | |
if batch.perf_message: | |
yield perf_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment