mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-18 11:50:11 +08:00
ENH: Add trading controls to zipline API.
Adds four new methods to the Zipline API that can be used as circuit-breakers to interrupt the execution of an algorithm. The API methods are: `set_max_position_size` `set_max_order_size` `set_max_order_count` `set_long_only` Internally, these methods are implemented by each registering a TradingControl callback object with the TradingAlgorithm. During TradingAlgorithm.__validate_order_params (and thus before any side-effects of the order call occur), each callback's `validate` method is called with information about the order to be placed and the algorithm's current state, raising an exception if the callback detects that an error condition has been breached.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from contextlib import contextmanager
|
||||
from logbook import FileHandler
|
||||
from zipline.finance.blotter import ORDER_STATUS
|
||||
|
||||
@@ -110,3 +111,15 @@ class ExceptionTransform(object):
|
||||
|
||||
def update(self, event):
|
||||
assert False, "An assertion message"
|
||||
|
||||
|
||||
@contextmanager
|
||||
def nullctx():
|
||||
"""
|
||||
Null context manager. Useful for conditionally adding a contextmanager in
|
||||
a single line, e.g.:
|
||||
|
||||
with SomeContextManager() if some_expr else nullcontext:
|
||||
do_stuff()
|
||||
"""
|
||||
yield
|
||||
|
||||
Reference in New Issue
Block a user