From 778da204681b72f41e38d154d371f431adcd9946 Mon Sep 17 00:00:00 2001 From: twiecki Date: Wed, 5 Mar 2014 17:58:03 -0500 Subject: [PATCH] BUG: Fix floating point error in order(). --- zipline/finance/blotter.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index a4fd034b..3d9053a9 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -99,8 +99,16 @@ class Blotter(object): StopLimit order: order(sid, amount, limit_price, stop_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(amount) + amount = int(almost_equal_to(amount)) # just validates amount and passes rest on to TransactionSimulator # Tell the user if they try to buy 0 shares of something.