From c199a0d9560cf40dd038e67fb082bba9ce93aba3 Mon Sep 17 00:00:00 2001 From: Thomas Wiecki Date: Mon, 25 Nov 2013 17:38:30 -0500 Subject: [PATCH] STY: Move order_value from blotter to TradingAlgorithm. --- zipline/algorithm.py | 25 ++++++++++++++++++++++--- zipline/finance/blotter.py | 25 ------------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 7d19fc3f..970de59a 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -382,10 +382,29 @@ class TradingAlgorithm(object): return self.blotter.order(sid, amount, limit_price, stop_price) def order_value(self, sid, value, limit_price=None, stop_price=None): + """ + Place an order by desired value rather than desired number of shares. + If the requested sid is found in the universe, the requested value is + divided by its price to imply the number of shares to transact. + + value > 0 :: Buy/Cover + value < 0 :: Sell/Short + Market order: order(sid, value) + Limit order: order(sid, value, limit_price) + Stop order: order(sid, value, None, stop_price) + StopLimit order: order(sid, value, limit_price, stop_price) + """ last_price = self.trading_client.current_data[sid].price - return self.blotter.order_value(sid, value, last_price, - limit_price=limit_price, - stop_price=stop_price) + if np.allclose(last_price, 0): + zero_message = "Price of 0 for {psid}; can't infer value".format( + psid=sid + ) + log.debug(zero_message) + # Don't place any order + return + else: + amount = value / last_price + return self.order(sid, amount, limit_price, stop_price) @property def recorded_vars(self): diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index 242799a1..76707d12 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -136,31 +136,6 @@ class Blotter(object): return order.id - def order_value(self, sid, value, last_price, - limit_price=None, stop_price=None): - """ - Place an order by desired value rather than desired number of shares. - If the requested sid is found in the universe, the requested value is - divided by its price to imply the number of shares to transact. - - value > 0 :: Buy/Cover - value < 0 :: Sell/Short - Market order: order(sid, value) - Limit order: order(sid, value, limit_price) - Stop order: order(sid, value, None, stop_price) - StopLimit order: order(sid, value, limit_price, stop_price) - """ - if np.allclose(last_price, 0): - zero_message = "Price of 0 for {psid}; can't infer value".format( - psid=sid - ) - log.debug(zero_message) - # Don't place any order - return - else: - amount = value / last_price - return self.order(sid, amount, limit_price, stop_price) - def cancel(self, order_id): if order_id not in self.orders: return