From eadf9d27e8107ab75299023cec2d2b0379deb9d3 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 21 Apr 2014 17:01:35 -0400 Subject: [PATCH] BUG: Fix a bug causing us to discard very low limit prices. Fixes an issue where very low limit prices were being rounded to 0.0 and effectively resulting in market orders. Adds an explicit check to test for this behavior. --- zipline/finance/blotter.py | 8 ++++---- zipline/test_algorithms.py | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index 227d95cf..b434cfaa 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -296,10 +296,10 @@ class Order(object): self.amount = int(self.amount / ratio) - if self.limit: + if self.limit is not None: self.limit = round(self.limit * ratio, 2) - if self.stop: + if self.stop is not None: self.stop = round(self.stop * ratio, 2) @property @@ -324,10 +324,10 @@ class Order(object): For a stop order, True IFF stop_reached. For a limit order, True IFF limit_reached. """ - if self.stop and not self.stop_reached: + if self.stop is not None and not self.stop_reached: return False - if self.limit and not self.limit_reached: + if self.limit is not None and not self.limit_reached: return False return True diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index ca624594..31f8818d 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -424,6 +424,12 @@ class AmbitiousStopLimitAlgorithm(TradingAlgorithm): # prevent trigger). self.order(self.sid, -100, limit_price=1, stop_price=1) + ################### + # Rounding Checks # + ################### + self.order(self.sid, 100, limit_price=.00000001) + self.order(self.sid, -100, stop_price=.00000001) + ########################################## # Algorithm using simple batch transforms