BUG: Prevent unintended keys from appearing in data bar.

The defaultdict behavior was allowing both algo code and
TradingAlgorithm wrappers to add unintended keys.

Remove use of defaultdict in favor of a dictionary that explicitly
adds the values in tradesimulation, otherwise allow a KeyError
if the bar is indexed with a sid that doesn't exist.

Also, when iterating over the keys in the data bar, only return
those keys that have pricing data.
This commit is contained in:
Eddie Hebert
2013-08-01 16:07:52 -04:00
parent 963324723c
commit 73eb3f12f5
2 changed files with 13 additions and 5 deletions
+9 -2
View File
@@ -15,7 +15,11 @@
from logbook import Logger, Processor
import zipline.finance.trading as trading
from zipline.protocol import BarData, DATASOURCE_TYPE
from zipline.protocol import (
BarData,
SIDData,
DATASOURCE_TYPE
)
from zipline.gens.utils import hash_args
log = Logger('Trade Simulation')
@@ -198,5 +202,8 @@ class AlgorithmSimulator(object):
Update the universe with new event information.
"""
# Update our knowledge of this event's sid
sid_data = self.current_data[event.sid]
if event.sid in self.current_data:
sid_data = self.current_data[event.sid]
else:
sid_data = self.current_data[event.sid] = SIDData()
sid_data.__dict__.update(event.__dict__)
+4 -3
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 collections import defaultdict
import datetime
from . utils.protocol_utils import Enum
@@ -135,7 +134,7 @@ class BarData(object):
"""
def __init__(self):
self._data = defaultdict(SIDData)
self._data = {}
self._contains_override = None
def __contains__(self, name):
@@ -164,7 +163,9 @@ class BarData(object):
del self._data[name]
def __iter__(self):
return self._data.iterkeys()
for sid, data in self._data.iteritems():
if len(data):
yield sid
def keys(self):
return self._data.keys()