Repository: quantopian/zipline
File: tests/test_algorithm.py (Python)

Code before:
        )

        results = algo.run(self.data_portal)

        # On 1/07, portfolio value is the same as without split
        self.assertEqual(results.port_value.iloc[0], 10000)
        self.assertAlmostEqual(results.port_value.iloc[1],
                               10000 + 780 - 392 - 0, places=2)


class TestAlgoScript(WithLogger,
                     WithDataPortal,
                     WithSimParams,
                     ZiplineTestCase):
    START_DATE = pd.Timestamp('2006-01-03', tz='utc')
    END_DATE = pd.Timestamp('2006-12-31', tz='utc')
    DATA_PORTAL_USE_MINUTE_DATA = False
    EQUITY_DAILY_BAR_LOOKBACK_DAYS = 5  # max history window length

    STRING_TYPE_NAMES = [s.__name__ for s in string_types]
    STRING_TYPE_NAMES_STRING = ', '.join(STRING_TYPE_NAMES)
    ASSET_TYPE_NAME = Asset.__name__
    CONTINUOUS_FUTURE_NAME = ContinuousFuture.__name__
    ASSET_OR_STRING_TYPE_NAMES = ', '.join([ASSET_TYPE_NAME] +
                                           STRING_TYPE_NAMES)
    ASSET_OR_STRING_OR_CF_TYPE_NAMES = ', '.join([ASSET_TYPE_NAME,
                                                  CONTINUOUS_FUTURE_NAME] +
                                                 STRING_TYPE_NAMES)
    ARG_TYPE_TEST_CASES = (
        ('history__assets', (bad_type_history_assets,
                             ASSET_OR_STRING_OR_CF_TYPE_NAMES,
                             True)),
        ('history__fields', (bad_type_history_fields,
                             STRING_TYPE_NAMES_STRING,
                             True)),
        ('history__bar_count', (bad_type_history_bar_count, 'int', False)),
        ('history__frequency', (bad_type_history_frequency,
                                STRING_TYPE_NAMES_STRING,
                                False)),
        ('current__assets', (bad_type_current_assets,
                             ASSET_OR_STRING_OR_CF_TYPE_NAMES,
                             True)),
        ('current__fields', (bad_type_current_fields,
                             STRING_TYPE_NAMES_STRING,
                             True)),
        ('is_stale__assets', (bad_type_is_stale_assets, 'Asset', True)),
        ('can_trade__assets', (bad_type_can_trade_assets, 'Asset', True)),
        ('history_kwarg__assets',
         (bad_type_history_assets_kwarg,
          ASSET_OR_STRING_OR_CF_TYPE_NAMES,
          True)),

Review comment (bug):
I was getting this failure `'10387.99899997449 != 10388 within 7 places'` without adding `places=2`  I'm not sure if there is a better way to do this.

Code after:
            record(port_value=context.account.equity_with_loan)

        def handle_data(context, data):
            if not context.ordered:
                order(sid(1), 1)
                context.ordered = True
            context.hd_account = context.account
        """)

        algo = TradingAlgorithm(
            script=algo_code,
            data_frequency="minute",
            sim_params=self.sim_params,
            env=self.env
        )

        results = algo.run(self.data_portal)

        # Starting portfolio value is 10000. Order for the asset fills on the
        # second bar of 1/06, where the price is 391, and costs the default
        # commission of 0. On 1/07, the price is 780, and the increase in
        # portfolio value is 780-392-0
        self.assertEqual(results.port_value.iloc[0], 10000)
        self.assertAlmostEqual(results.port_value.iloc[1],
                               10000 + 780 - 392 - 0,
                               places=2)

    def test_portfolio_bts_with_overnight_split(self):
        algo_code = dedent("""
        from zipline.api import order, sid, record
        def initialize(context):
            context.ordered = False
            context.hd_portfolio = context.portfolio
        def before_trading_start(context, data):
            bts_portfolio = context.portfolio
            # Assert that the portfolio in BTS is the same as the last
            # portfolio in handle_data, except for the positions
            for k in bts_portfolio.__dict__:
                if k != 'positions':
                    assert (context.hd_portfolio.__dict__[k]
                            == bts_portfolio.__dict__[k])
            record(pos_value=bts_portfolio.positions_value)
            record(pos_amount=bts_portfolio.positions[sid(3)].amount)
            record(
                last_sale_price=bts_portfolio.positions[sid(3)].last_sale_price
            )
        def handle_data(context, data):
            if not context.ordered:
                order(sid(3), 1)
                context.ordered = True
            context.hd_portfolio = context.portfolio