本文整理汇总了Python中zipline.finance.performance.PerformanceTracker.transform方法的典型用法代码示例。如果您正苦于以下问题:Python PerformanceTracker.transform方法的具体用法?Python PerformanceTracker.transform怎么用?Python PerformanceTracker.transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipline.finance.performance.PerformanceTracker
的用法示例。
在下文中一共展示了PerformanceTracker.transform方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TradeSimulationClient
# 需要导入模块: from zipline.finance.performance import PerformanceTracker [as 别名]
# 或者: from zipline.finance.performance.PerformanceTracker import transform [as 别名]
class TradeSimulationClient(object):
"""
Generator-style class that takes the expected output of a merge, a
user algorithm, a trading environment, and a simulator slippage as
arguments. Pipes the merge stream through a TransactionSimulator
and a PerformanceTracker, which keep track of the current state of
our algorithm's simulated universe. Results are fed to the user's
algorithm, which directly inserts transactions into the
TransactionSimulator's order book.
TransactionSimulator maintains a dictionary from sids to the
as-yet unfilled orders placed by the user's algorithm. As trade
events arrive, if the algorithm has open orders against the
trade's sid, the simulator will fill orders up to 25% of market
cap. Applied transactions are added to a txn field on the event
and forwarded to PerformanceTracker. The txn field is set to None
on non-trade events and events that do not match any open orders.
PerformanceTracker receives the updated event messages from
TransactionSimulator, maintaining a set of daily and cumulative
performance metrics for the algorithm. The tracker removes the
txn field from each event it receives, replacing it with a
portfolio field to be fed into the user algo. At the end of each
trading day, the PerformanceTracker also generates a daily
performance report, which is appended to event's perf_report
field.
Fully processed events are fed to AlgorithmSimulator, which
batches together events with the same dt field into a single
snapshot to be fed to the algo. The portfolio object is repeatedly
overwritten so that only the most recent snapshot of the universe
is sent to the algo.
"""
def __init__(self, algo, environment):
self.algo = algo
self.environment = environment
self.ordering_client = TransactionSimulator()
self.perf_tracker = PerformanceTracker(self.environment)
self.algo_start = self.environment.first_open
self.algo_sim = AlgorithmSimulator(
self.ordering_client,
self.perf_tracker,
self.algo,
self.algo_start
)
def get_hash(self):
"""
There should only ever be one TSC in the system, so
we don't bother passing args into the hash.
"""
return self.__class__.__name__ + hash_args()
def simulate(self, stream_in):
"""
Main generator work loop.
"""
# Simulate filling any open orders made by the previous run of
# the user's algorithm. Fills the Transaction field on any
# event that results in a filled order.
with_filled_orders = self.ordering_client.transform(stream_in)
# Pipe the events with transactions to perf. This will remove
# the TRANSACTION field added by TransactionSimulator and replace it
# with a portfolio field to be passed to the user's
# algorithm. Also adds a perf_messages field which is usually
# empty, but contains update messages once per day.
with_portfolio = self.perf_tracker.transform(with_filled_orders)
# Pass the messages from perf to the user's algorithm for simulation.
# Events are batched by dt so that the algo handles all events for a
# given timestamp at one one go.
performance_messages = self.algo_sim.transform(with_portfolio)
# The algorithm will yield a daily_results message (as
# calculated by the performance tracker) at the end of each
# day. It will also yield a risk report at the end of the
# simulation.
for message in performance_messages:
yield message