From 7412cc97a0a7e05d238bc3d99a77804b9bab6c8b Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 25 Oct 2013 16:14:11 -0400 Subject: [PATCH] MAINT: Use bitwise flags to help order cases easier to follow. Instead of nesting order direction and related stop and limit logic, derive a bitwise mask from the combination of order configurations and use the mask as a 'switch'. --- zipline/finance/slippage.py | 72 +++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index 4ff0226f..0224a5ce 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -23,6 +23,11 @@ from functools import partial from zipline.protocol import DATASOURCE_TYPE import zipline.utils.math_utils as zp_math +SELL = 0 +BUY = 1 +STOP = 1 << 1 +LIMIT = 1 << 2 + def check_order_triggers(order, event): """ @@ -43,38 +48,43 @@ def check_order_triggers(order, event): stop_reached = False limit_reached = False sl_stop_reached = False - if order.stop is not None: - if order.limit is not None: - if order.amount > 0: - # This is a BUY STOP LIMIT order - if event.price >= order.stop: - sl_stop_reached = True - if event.price <= order.limit: - limit_reached = True - else: - # This is a SELL STOP LIMIT order - if event.price <= order.stop: - sl_stop_reached = True - if event.price >= order.limit: - limit_reached = True - else: - if order.amount > 0: - # This is a BUY STOP order - if event.price >= order.stop: - stop_reached = True - else: - # This is a SELL STOP order - if event.price <= order.stop: - stop_reached = True + + order_type = 0 + + if order.amount > 0: + order_type |= BUY else: - if order.amount > 0: - # This is BUY LIMIT order - if event.price <= order.limit: - limit_reached = True - else: - # This is a SELL LIMIT order - if event.price >= order.limit: - limit_reached = True + order_type |= SELL + + if order.stop is not None: + order_type |= STOP + + if order.limit is not None: + order_type |= LIMIT + + if order_type == BUY | STOP | LIMIT: + if event.price >= order.stop: + sl_stop_reached = True + if event.price <= order.limit: + limit_reached = True + elif order_type == SELL | STOP | LIMIT: + if event.price <= order.stop: + sl_stop_reached = True + if event.price >= order.limit: + limit_reached = True + elif order_type == BUY | STOP: + if event.price >= order.stop: + stop_reached = True + elif order_type == SELL | STOP: + if event.price <= order.stop: + stop_reached = True + elif order_type == BUY | LIMIT: + if event.price <= order.limit: + limit_reached = True + elif order_type == SELL | LIMIT: + # This is a SELL LIMIT order + if event.price >= order.limit: + limit_reached = True return (stop_reached, limit_reached, sl_stop_reached)