mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-06 05:14:38 +08:00
MAINT: Factored out order arg calculation methods
so callers can use them to construct args for a batch of orders
This commit is contained in:
+39
-24
@@ -1404,6 +1404,12 @@ class TradingAlgorithm(object):
|
||||
if not self._can_order_asset(asset):
|
||||
return None
|
||||
|
||||
amount, style = self._calculate_order(asset, amount,
|
||||
limit_price, stop_price, style)
|
||||
return self.blotter.order(asset, amount, style)
|
||||
|
||||
def _calculate_order(self, asset, amount,
|
||||
limit_price=None, stop_price=None, style=None):
|
||||
# Truncate to the integer share count that's either within .0001 of
|
||||
# amount or closer to zero.
|
||||
# E.g. 3.9999 -> 4.0; 5.5 -> 5.0; -5.5 -> -5.0
|
||||
@@ -1421,7 +1427,7 @@ class TradingAlgorithm(object):
|
||||
style = self.__convert_order_params_for_blotter(limit_price,
|
||||
stop_price,
|
||||
style)
|
||||
return self.blotter.order(asset, amount, style)
|
||||
return amount, style
|
||||
|
||||
def validate_order_params(self,
|
||||
asset,
|
||||
@@ -1744,11 +1750,15 @@ class TradingAlgorithm(object):
|
||||
if not self._can_order_asset(asset):
|
||||
return None
|
||||
|
||||
amount = self._calculate_order_percent_amount(asset, percent)
|
||||
return self.order(asset, amount,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
|
||||
def _calculate_order_percent_amount(self, asset, percent):
|
||||
value = self.portfolio.portfolio_value * percent
|
||||
return self.order_value(asset, value,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
return self._calculate_order_value_amount(asset, value)
|
||||
|
||||
@api_method
|
||||
@disallowed_in_before_trading_start(OrderInBeforeTradingStart())
|
||||
@@ -1810,18 +1820,18 @@ class TradingAlgorithm(object):
|
||||
if not self._can_order_asset(asset):
|
||||
return None
|
||||
|
||||
amount = self._calculate_order_target_amount(asset, target)
|
||||
return self.order(asset, amount,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
|
||||
def _calculate_order_target_amount(self, asset, target):
|
||||
if asset in self.portfolio.positions:
|
||||
current_position = self.portfolio.positions[asset].amount
|
||||
req_shares = target - current_position
|
||||
return self.order(asset, req_shares,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
else:
|
||||
return self.order(asset, target,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
target -= current_position
|
||||
|
||||
return target
|
||||
|
||||
@api_method
|
||||
@disallowed_in_before_trading_start(OrderInBeforeTradingStart())
|
||||
@@ -1885,10 +1895,11 @@ class TradingAlgorithm(object):
|
||||
return None
|
||||
|
||||
target_amount = self._calculate_order_value_amount(asset, target)
|
||||
return self.order_target(asset, target_amount,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
amount = self._calculate_order_target_amount(asset, target_amount)
|
||||
return self.order(asset, amount,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
|
||||
@api_method
|
||||
@disallowed_in_before_trading_start(OrderInBeforeTradingStart())
|
||||
@@ -1947,11 +1958,15 @@ class TradingAlgorithm(object):
|
||||
if not self._can_order_asset(asset):
|
||||
return None
|
||||
|
||||
target_value = self.portfolio.portfolio_value * target
|
||||
return self.order_target_value(asset, target_value,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
amount = self._calculate_order_target_percent_amount(asset, target)
|
||||
return self.order(asset, amount,
|
||||
limit_price=limit_price,
|
||||
stop_price=stop_price,
|
||||
style=style)
|
||||
|
||||
def _calculate_order_target_percent_amount(self, asset, target):
|
||||
target_amount = self._calculate_order_percent_amount(asset, target)
|
||||
return self._calculate_order_target_amount(asset, target_amount)
|
||||
|
||||
@error_keywords(sid='Keyword argument `sid` is no longer supported for '
|
||||
'get_open_orders. Use `asset` instead.')
|
||||
|
||||
@@ -91,7 +91,7 @@ class Blotter(object):
|
||||
"""
|
||||
if amount == 0:
|
||||
# Don't bother placing orders for 0 shares.
|
||||
return
|
||||
return None
|
||||
elif amount > self.max_shares:
|
||||
# Arbitrary limit of 100 billion (US) shares will never be
|
||||
# exceeded except by a buggy algorithm.
|
||||
|
||||
Reference in New Issue
Block a user