mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-15 12:15:22 +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:
@@ -109,7 +109,7 @@ class TestAlgorithm(TradingAlgorithm):
|
||||
self.sid_filter = [self.sid]
|
||||
|
||||
def handle_data(self, data):
|
||||
# place an order for 100 shares of sid
|
||||
# place an order for amount shares of sid
|
||||
if self.incr < self.count:
|
||||
self.order(self.sid, self.amount)
|
||||
self.incr += 1
|
||||
@@ -399,7 +399,39 @@ class TestTargetValueAlgorithm(TradingAlgorithm):
|
||||
self.target_shares = np.round(20 / data[0].price)
|
||||
|
||||
|
||||
from zipline.algorithm import TradingAlgorithm
|
||||
############################
|
||||
# TradingControl Test Algos#
|
||||
############################
|
||||
|
||||
|
||||
class SetMaxPositionSizeAlgorithm(TradingAlgorithm):
|
||||
def initialize(self, sid=None, max_shares=None, max_notional=None):
|
||||
self.order_count = 0
|
||||
self.set_max_position_size(sid=sid,
|
||||
max_shares=max_shares,
|
||||
max_notional=max_notional)
|
||||
|
||||
|
||||
class SetMaxOrderSizeAlgorithm(TradingAlgorithm):
|
||||
def initialize(self, sid=None, max_shares=None, max_notional=None):
|
||||
self.order_count = 0
|
||||
self.set_max_order_size(sid=sid,
|
||||
max_shares=max_shares,
|
||||
max_notional=max_notional)
|
||||
|
||||
|
||||
class SetMaxOrderCountAlgorithm(TradingAlgorithm):
|
||||
def initialize(self, count):
|
||||
self.order_count = 0
|
||||
self.set_max_order_count(count)
|
||||
|
||||
|
||||
class SetLongOnlyAlgorithm(TradingAlgorithm):
|
||||
def initialize(self):
|
||||
self.order_count = 0
|
||||
self.set_long_only()
|
||||
|
||||
|
||||
from zipline.transforms import BatchTransform, batch_transform
|
||||
from zipline.transforms import MovingAverage
|
||||
|
||||
|
||||
Reference in New Issue
Block a user