From 243c0348f3a04b697d5ce84983c244d7a8cd8183 Mon Sep 17 00:00:00 2001 From: David Edwards Date: Mon, 2 Jun 2014 13:29:36 -0700 Subject: [PATCH] MAINT: Refactored target order methods Refactored the target order methods to separate their logic from the other order methods. This makes order_target the only target method that calls order() directly. order_target_value is passed to order_target instead of order_value. order_target_percent is passed to order_target_value rather than order_value. This simplified the code and decouples the logic of target orders from the other order methods. This allows the target order methods to be developed independently from order_value and order_percent. --- zipline/algorithm.py | 45 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index d67e4ab8..79479337 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -706,7 +706,8 @@ class TradingAlgorithm(object): @api_method def order_target_value(self, sid, target, - limit_price=None, stop_price=None, style=None): + limit_price=None, stop_price=None, + style=None, include_open_orders=False): """ Place an order to adjust a position to a target value. If the position doesn't already exist, this is equivalent to placing a new @@ -714,20 +715,18 @@ class TradingAlgorithm(object): order for the difference between the target value and the current value. """ - if sid in self.portfolio.positions: - current_position = self.portfolio.positions[sid].amount - current_price = self.trading_client.current_data[sid].price - current_value = current_position * current_price - req_value = target - current_value - return self.order_value(sid, req_value, - limit_price=limit_price, - stop_price=stop_price, - style=style) - else: - return self.order_value(sid, target, - limit_price=limit_price, - stop_price=stop_price, - style=style) + last_price = self.trading_client.current_data[sid].price + if np.allclose(last_price, 0): + # Don't place an order + if self.logger: + zero_message = "Price of 0 for {psid}; can't infer value" + self.logger.debug(zero_message.format(psid=sid)) + return + target_amount = target / last_price + return self.order_target(sid, target_amount, + limit_price=limit_price, + stop_price=stop_price, + style=style) @api_method def order_target_percent(self, sid, target, @@ -741,19 +740,11 @@ class TradingAlgorithm(object): Note that target must expressed as a decimal (0.50 means 50\%). """ - if sid in self.portfolio.positions: - current_position = self.portfolio.positions[sid].amount - current_price = self.trading_client.current_data[sid].price - current_value = current_position * current_price - else: - current_value = 0 target_value = self.portfolio.portfolio_value * target - - req_value = target_value - current_value - return self.order_value(sid, req_value, - limit_price=limit_price, - stop_price=stop_price, - style=style) + return self.order_target_value(sid, target_value, + limit_price=limit_price, + stop_price=stop_price, + style=style) @api_method def get_open_orders(self, sid=None):