mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-01 11:59:14 +08:00
Extended the algorithm protocol to include portfolio.
This commit is contained in:
@@ -115,6 +115,14 @@ class TradeSimulationClient(qmsg.Component):
|
||||
|
||||
|
||||
def run_algorithm(self):
|
||||
"""
|
||||
As per the algorithm protocol:
|
||||
|
||||
- Set the current portfolio for the algorithm as per protocol.
|
||||
- Construct frame based on backlog of events, send to algorithm.
|
||||
"""
|
||||
current_portfolio = self.perf.get_portfolio()
|
||||
self.algorithm.set_portfolio(current_portfolio)
|
||||
frame = self.get_frame()
|
||||
if len(frame) > 0:
|
||||
self.algorithm.handle_frame(frame)
|
||||
|
||||
+13
-13
@@ -177,20 +177,20 @@ class SimulatedTrading(object):
|
||||
@staticmethod
|
||||
def create_test_zipline(**config):
|
||||
"""
|
||||
:param config: A configuration object that is a dict with::
|
||||
:param config: A configuration object that is a dict with:
|
||||
|
||||
- environment - a \
|
||||
:py:class:`zipline.finance.trading.TradingEnvironment`
|
||||
- allocator - a :py:class:`zipline.simulator.AddressAllocator`
|
||||
- sid - an integer, which will be used as the security ID.
|
||||
- order_count - the number of orders the test algo will place,
|
||||
defaults to 100
|
||||
- trade_count - the number of trades to simulate, defaults to 100
|
||||
- simulator_class - optional parameter that provides an alternative
|
||||
subclass of ComponentHost to hold the whole zipline. Defaults to
|
||||
:py:class:`zipline.simulator.Simulator`
|
||||
- algorithm - optional parameter providing an algorithm. defaults
|
||||
to :py:class:`zipline.test.algorithms.TestAlgorithm`
|
||||
- environment - a \
|
||||
:py:class:`zipline.finance.trading.TradingEnvironment`
|
||||
- allocator - a :py:class:`zipline.simulator.AddressAllocator`
|
||||
- sid - an integer, which will be used as the security ID.
|
||||
- order_count - the number of orders the test algo will place,
|
||||
defaults to 100
|
||||
- trade_count - the number of trades to simulate, defaults to 100
|
||||
- simulator_class - optional parameter that provides an alternative
|
||||
subclass of ComponentHost to hold the whole zipline. Defaults to
|
||||
:py:class:`zipline.simulator.Simulator`
|
||||
- algorithm - optional parameter providing an algorithm. defaults
|
||||
to :py:class:`zipline.test.algorithms.TestAlgorithm`
|
||||
"""
|
||||
assert isinstance(config, dict)
|
||||
|
||||
|
||||
+34
-18
@@ -7,30 +7,39 @@ For a class to be passed as a trading algorithm to the
|
||||
it must follow an implementation protocol. Examples of this algorithm protocol
|
||||
are provided below.
|
||||
|
||||
The algorithm must expose methods::
|
||||
The algorithm must expose methods:
|
||||
|
||||
- get_sid_filter: method that takes no args, and returns a list
|
||||
of valid sids. List must have a length between 1 and 10. If None is returned
|
||||
the filter will block all events.
|
||||
|
||||
- handle_frame: method that accepts a :py:class:`pandas.Dataframe` of the
|
||||
current state of the simulation universe. An example frame:
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| | SID(133) | SID(134) | SID(135) |
|
||||
+=================+==============+=====================================+
|
||||
| price | $10.10 | $22.50 | $13.37 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| volume | 10,000 | 5,000 | 50,000 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| mvg_avg_30 | $9.97 | $22.61 | $13.37 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| dt | 6/30/2012 | 6/30/2011 | 6/29/2012 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
current state of the simulation universe. An example frame::
|
||||
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| | SID(133) | SID(134) | SID(135) |
|
||||
+=================+==============+=====================================+
|
||||
| price | $10.10 | $22.50 | $13.37 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| volume | 10,000 | 5,000 | 50,000 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| mvg_avg_30 | $9.97 | $22.61 | $13.37 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| dt | 6/30/2012 | 6/30/2011 | 6/29/2012 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
|
||||
The algorithm must also expose settable properties:
|
||||
- order: property which can be set equal to the order method of
|
||||
trading_client. An algorithm can then place orders with a valid
|
||||
SID and a number of shares::
|
||||
self.order(SID(133), share_count)
|
||||
- set_order: method that accepts a callable. Will be set as the value of the
|
||||
order method of trading_client. An algorithm can then place orders with a
|
||||
valid SID and a number of shares::
|
||||
|
||||
self.order(SID(133), share_count)
|
||||
|
||||
- set_performance: property which can be set equal to the
|
||||
cumulative_trading_performance property of the trading_client. An
|
||||
algorithm can then check position information with the
|
||||
Portfolio object::
|
||||
|
||||
self.Portfolio[SID(133)]['cost_basis']
|
||||
|
||||
"""
|
||||
|
||||
@@ -51,10 +60,14 @@ class TestAlgorithm():
|
||||
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:133
|
||||
@@ -73,6 +86,9 @@ class NoopAlgorithm(object):
|
||||
def set_order(self, order_callable):
|
||||
pass
|
||||
|
||||
def set_portfolio(self, portfolio):
|
||||
pass
|
||||
|
||||
def handle_frame(self, frame):
|
||||
pass
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ class FinanceTestCase(TestCase):
|
||||
|
||||
@timed(DEFAULT_TIMEOUT)
|
||||
def test_orders(self):
|
||||
|
||||
# Simulation
|
||||
# ----------
|
||||
zipline = SimulatedTrading.create_test_zipline(**self.zipline_test_config)
|
||||
|
||||
Reference in New Issue
Block a user