'Make sure that after an equity gets delisted, our portfolio holds the
correct number of equities and correct amount of cash.'

@parameter_space(order_size=[10, (-10)], capital_base=[0, 100000], auto_close_lag=[1, 2])
def test_daily_delisted_equities(self, order_size, capital_base, auto_close_lag):

    auto_close_delta = (self.trading_calendar.day * auto_close_lag)
    resources = self.make_data(auto_close_delta, 'daily', capital_base)
    assets = resources.assets
    final_prices = resources.final_prices
    initial_fill_prices = self.prices_on_tick(resources.trade_data_by_sid, 1)
    cost_basis = (sum(initial_fill_prices) * order_size)
    fp0 = final_prices[0]
    fp1 = final_prices[1]
    algo = TradingAlgorithm(initialize=self.default_initialize(), handle_data=self.default_handle_data(assets, order_size), env=resources.env, sim_params=resources.sim_params)
    output = algo.run(resources.data_portal)
    initial_cash = capital_base
    after_fills = (initial_cash - cost_basis)
    after_first_auto_close = (after_fills + (fp0 * order_size))
    after_second_auto_close = (after_first_auto_close + (fp1 * order_size))
    if (auto_close_lag == 1):
        expected_cash = [initial_cash, after_fills, after_fills, after_first_auto_close, after_first_auto_close, after_second_auto_close, after_second_auto_close]
        expected_num_positions = [0, 3, 3, 2, 2, 1, 1]
    elif (auto_close_lag == 2):
        expected_cash = [initial_cash, after_fills, after_fills, after_fills, after_first_auto_close, after_first_auto_close, after_second_auto_close]
        expected_num_positions = [0, 3, 3, 3, 2, 2, 1]
    else:
        self.fail("Don't    know    about    auto_close    lags    other    than    1    or    2.    Add    test    answers    please!")
    self.assertEqual(expected_cash, list(output['ending_cash']))
    expected_cash.insert(3, after_fills)
    self.assertEqual(algo.cash, expected_cash[:(-1)])
    if (order_size > 0):
        self.assertEqual(expected_num_positions, list(output['longs_count']))
        self.assertEqual(([0] * len(self.test_days)), list(output['shorts_count']))
    else:
        self.assertEqual(expected_num_positions, list(output['shorts_count']))
        self.assertEqual(([0] * len(self.test_days)), list(output['longs_count']))
    expected_num_positions.insert(3, 3)
    self.assertEqual(algo.num_positions, expected_num_positions[:(-1)])
    transactions = output['transactions']
    initial_fills = transactions.iloc[1]
    self.assertEqual(len(initial_fills), len(assets))
    last_minute_of_session = self.trading_calendar.session_close(self.test_days[1])
    for (asset, txn) in zip(assets, initial_fills):
        self.assertDictContainsSubset({'amount': order_size, 'commission': None, 'dt': last_minute_of_session, 'price': initial_fill_prices[asset], 'sid': asset}, txn)
        self.assertIsInstance(txn['order_id'], str)
    def transactions_for_date(date):
        return transactions.iloc[self.test_days.get_loc(date)]
    (first_auto_close_transaction,) = transactions_for_date(assets[0].auto_close_date)
    self.assertEqual(first_auto_close_transaction, {'amount': (- order_size), 'commission': 0.0, 'dt': self.trading_calendar.session_close(assets[0].auto_close_date), 'price': fp0, 'sid': assets[0], 'order_id': None})
    (second_auto_close_transaction,) = transactions_for_date(assets[1].auto_close_date)
    self.assertEqual(second_auto_close_transaction, {'amount': (- order_size), 'commission': 0.0, 'dt': self.trading_calendar.session_close(assets[1].auto_close_date), 'price': fp1, 'sid': assets[1], 'order_id': None})
