ENH: Add max leverage account guard

This commit is contained in:
Brian Fink
2015-04-10 15:19:54 -04:00
parent 2f895bddcd
commit 8baf52fe01
19 changed files with 47518 additions and 166768 deletions
+37
View File
@@ -35,6 +35,7 @@ from zipline.errors import (
OverrideCommissionPostInit,
OverrideSlippagePostInit,
RegisterTradingControlPostInit,
RegisterAccountControlPostInit,
UnsupportedCommissionModel,
UnsupportedOrderParameters,
UnsupportedSlippageModel,
@@ -48,6 +49,7 @@ from zipline.finance.controls import (
MaxOrderCount,
MaxOrderSize,
MaxPositionSize,
MaxLeverage,
RestrictedListOrder
)
from zipline.finance.execution import (
@@ -144,6 +146,9 @@ class TradingAlgorithm(object):
# List of trading controls to be used to validate orders.
self.trading_controls = []
# List of account controls to be checked on each bar.
self.account_controls = []
self._recorded_vars = {}
self.namespace = kwargs.get('namespace', {})
@@ -271,6 +276,11 @@ class TradingAlgorithm(object):
self._handle_data(self, data)
# Unlike trading controls which remain constant unless placing an
# order, account controls can change each bar. Thus, must check
# every bar no matter if the algorithm places an order or not.
self.validate_account_controls()
def analyze(self, perf):
if self._analyze is None:
return
@@ -1001,6 +1011,33 @@ class TradingAlgorithm(object):
)
return self.history_container.get_history(history_spec, self.datetime)
####################
# Account Controls #
####################
def register_account_control(self, control):
"""
Register a new AccountControl to be checked on each bar.
"""
if self.initialized:
raise RegisterAccountControlPostInit()
self.account_controls.append(control)
def validate_account_controls(self):
for control in self.account_controls:
control.validate(self.updated_portfolio(),
self.updated_account(),
self.get_datetime(),
self.trading_client.current_data)
@api_method
def set_max_leverage(self, max_leverage=None):
"""
Set a limit on the maximum leverage of the algorithm.
"""
control = MaxLeverage(max_leverage)
self.register_account_control(control)
####################
# Trading Controls #
####################