From e47cb964790a66a0f8be5edea5fe9afab00bf30d Mon Sep 17 00:00:00 2001 From: Stewart Douglas Date: Mon, 29 Feb 2016 10:40:57 -0500 Subject: [PATCH] BUG: Ensure consistent ordering of amounts, prices & multipliers Previously, we have assumed that the `amounts` and `last_sale_prices` lists have the same order as the `value_multipliers`. This is not correct, since to populate the `amounts` and `last_sale_prices` lists we iterate over a `dict` (self.positions). The order of this `dict` can change in arbitrary ways when it is updated, which occurs when we call `update_positions`. Our `value_multipliers` however are stored in an `OrderedDict`, meaning the order of existing key/value pairs is not changed when they are updated. To address this issue, we make sure that `self.positions` subclasses `OrderedDict`. --- zipline/finance/performance/position.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zipline/finance/performance/position.py b/zipline/finance/performance/position.py index d8acec2c..b23b7ecb 100644 --- a/zipline/finance/performance/position.py +++ b/zipline/finance/performance/position.py @@ -33,6 +33,7 @@ Position Tracking from __future__ import division from math import copysign +from collections import OrderedDict from copy import copy @@ -228,7 +229,7 @@ last_sale_price: {last_sale_price}" self.__dict__.update(state) -class positiondict(dict): +class positiondict(OrderedDict): def __missing__(self, key): pos = Position(key)