Locks down the ability to easily override the algo's portfolio.

Starting down the path of making the portfolio completely read-only
with respect to the handle_data in algo.

The portfolio should only be changed during the course of running
the algorithm by the simulator.

This doesn't do a 100% protection, i.e. an algo could use _portfolio,
or the set_attr property, but hoping this helps guides algo writing
to treat the portfolio as read-only.
This commit is contained in:
Eddie Hebert
2012-11-05 13:40:23 -05:00
parent 96ba4c7d6c
commit 086c12ddf8
4 changed files with 45 additions and 2 deletions
+23
View File
@@ -19,6 +19,7 @@ import zipline.utils.simfactory as simfactory
from zipline.test_algorithms import (
ExceptionAlgorithm,
DivByZeroAlgorithm,
SetPortfolioAlgorithm,
)
from zipline.finance.slippage import FixedSlippage
from zipline.transforms.utils import StatefulTransform
@@ -113,3 +114,25 @@ class ExceptionTestCase(TestCase):
self.assertEqual(ctx.exception.message,
'integer division or modulo by zero')
def test_set_portfolio(self):
"""
Are we protected against overwriting an algo's portfolio?
"""
# Simulation
# ----------
self.zipline_test_config['algorithm'] = \
SetPortfolioAlgorithm(
self.zipline_test_config['sid']
)
zipline = simfactory.create_test_zipline(
**self.zipline_test_config
)
with self.assertRaises(AttributeError) as ctx:
output, _ = drain_zipline(self, zipline)
self.assertEqual(ctx.exception.message,
"can't set attribute")