From 23076ae7f12229f89a3217fcedb8e73c1c65f565 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 11 Oct 2012 13:42:53 -0400 Subject: [PATCH] Allows for collapsed orders by changing the current order filter. Changes our filter so that instead of just checking for the current day, we ensure that orders are before or on the current event time. This adds a delay, (defaulting to one minute), to the order so that we avoid filling an order exactly when it is placed. --- tests/test_finance.py | 14 ++++++++++++++ zipline/finance/slippage.py | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/test_finance.py b/tests/test_finance.py index 094aeb80..28718a6a 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -197,6 +197,20 @@ class FinanceTestCase(TestCase): } self.transaction_sim(**params2) + # Runs the collapsed trades over daily trade intervals. + # Ensuring that our delay works for daily intervals as well. + params3 = { + 'trade_count': 6, + 'trade_amount': 100, + 'trade_interval': timedelta(days=1), + 'order_count': 24, + 'order_amount': 1, + 'order_interval': timedelta(minutes=1), + 'expected_txn_count': 1, + 'expected_txn_volume': 24 * 1 + } + self.transaction_sim(**params3) + @timed(DEFAULT_TIMEOUT) def test_alternating_long_short(self): # create a scenario where we alternate buys and sells diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index abf7f713..49d539ab 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from datetime import timedelta import pytz import math @@ -55,10 +56,12 @@ class VolumeShareSlippage(object): def __init__(self, volume_limit=.25, - price_impact=0.1): + price_impact=0.1, + delay=timedelta(minutes=1)): self.volume_limit = volume_limit self.price_impact = price_impact + self.delay = delay def simulate(self, event, open_orders): @@ -72,7 +75,7 @@ class VolumeShareSlippage(object): orders = sorted(orders, key=lambda o: o.dt) # Only use orders for the current day or before current_orders = filter( - lambda o: o.dt.toordinal() <= event.dt.toordinal(), + lambda o: o.dt + self.delay <= event.dt, orders) else: return None