MAINT: Pass order emitted from slippage up to tradesimulation.

Instead of searching through the open orders to find the ones
that match the current transactions, now that simulate returns
the pair of transaction and order for which that transaction
was created for, that order can be used where we previously
searched for a modified order.

This should be a runtime improvement since, but not yet verified
via thorough profiling.
This commit is contained in:
Eddie Hebert
2013-06-20 12:36:49 -04:00
parent e160f9afd8
commit ebe00b83f7
3 changed files with 11 additions and 19 deletions
+1 -3
View File
@@ -362,9 +362,7 @@ class FinanceTestCase(TestCase):
for event in events:
if event.type == DATASOURCE_TYPE.TRADE:
txns, _ = blotter.process_trade(event)
for txn in txns:
for txn, order in blotter.process_trade(event):
transactions.append(txn)
tracker.process_event(txn)
+4 -12
View File
@@ -143,12 +143,12 @@ class Blotter(object):
def process_trade(self, trade_event):
if trade_event.type != zp.DATASOURCE_TYPE.TRADE:
return [], []
raise StopIteration
if zp_math.tolerant_equals(trade_event.volume, 0):
# there are zero volume trade_events bc some stocks trade
# less frequently than once per minute.
return [], []
raise StopIteration
if trade_event.sid in self.open_orders:
orders = self.open_orders[trade_event.sid]
@@ -158,9 +158,7 @@ class Blotter(object):
lambda o: o.dt <= trade_event.dt,
orders)
else:
return [], []
txns = []
raise StopIteration
for order, txn in self.transact(trade_event, current_orders):
order.filled += txn.amount
@@ -168,11 +166,7 @@ class Blotter(object):
# that is filling it.
order.dt = txn.dt
txns.append(txn)
modified_orders = [order for order
in self.open_orders[trade_event.sid]
if order.dt == trade_event.dt]
yield txn, order
# update the open orders for the trade_event's sid
self.open_orders[trade_event.sid] = \
@@ -180,8 +174,6 @@ class Blotter(object):
in self.open_orders[trade_event.sid]
if order.open]
return txns, modified_orders
class Order(object):
def __init__(self, dt, sid, amount, stop=None, limit=None, filled=0,
+6 -4
View File
@@ -12,7 +12,6 @@
# 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 itertools import chain
from logbook import Logger, Processor
import zipline.finance.trading as trading
@@ -120,9 +119,12 @@ class AlgorithmSimulator(object):
if event.type == DATASOURCE_TYPE.BENCHMARK:
self.algo.set_datetime(event.dt)
bm_updated = True
txns, orders = self.algo.blotter.process_trade(event)
for data in chain(txns, orders, [event]):
self.algo.perf_tracker.process_event(data)
process_trade = self.algo.blotter.process_trade
for txn, order in process_trade(event):
self.algo.perf_tracker.process_event(txn)
self.algo.perf_tracker.process_event(order)
self.algo.perf_tracker.process_event(event)
# Update our portfolio.
self.algo.set_portfolio(