diff --git a/zipline/algorithm.py b/zipline/algorithm.py index e863e32d..6450bafc 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -499,6 +499,22 @@ class TradingAlgorithm(object): """ Place an order using the specified parameters. """ + + def round_if_near_integer(a, epsilon=1e-4): + """ + Round a to the nearest integer if that integer is within an epsilon + of a. + """ + if abs(a - round(a)) <= epsilon: + return round(a) + else: + return a + + # 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 + amount = int(round_if_near_integer(amount)) + # Raises a ZiplineError if invalid parameters are detected. self.validate_order_params(sid, amount, diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index 49999655..3415c466 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -88,18 +88,6 @@ class Blotter(object): Stop order: order(sid, amount, StopOrder(price)) StopLimit order: order(sid, amount, StopLimitOrder(price)) """ - # This fixes a bug that if amount is e.g. -27.99999 due to - # floating point madness we actually want to treat it as -28. - def almost_equal_to(a, eps=1e-4): - if abs(a - round(a)) <= eps: - return round(a) - else: - return a - - # Fractional shares are not supported. - amount = int(almost_equal_to(amount)) - - # just validates amount and passes rest on to TransactionSimulator if amount == 0: # Don't bother placing orders for 0 shares. return