'verify that the performance period calculates properly for a single short-sale transaction'

def test_short_position(self):

    self.create_environment_stuff(num_days=6)
    trades = factory.create_trade_history(self.asset1, [10, 10, 10, 11, 10, 9], [100, 100, 100, 100, 100, 100], oneday, self.sim_params, trading_calendar=self.trading_calendar)
    trades_1 = trades[:(-2)]
    data_portal = create_data_portal_from_trade_history(self.env.asset_finder, self.trading_calendar, self.instance_tmpdir, self.sim_params, {1: trades})
    txn = create_txn(self.asset1, trades[1].dt, 10.0, (-100))
    pt = perf.PositionTracker(self.sim_params.data_frequency)
    pp = perf.PerformancePeriod(1000.0, self.sim_params.data_frequency)
    pp.position_tracker = pt
    pt.execute_transaction(txn)
    pp.handle_execution(txn)
    pt.sync_last_sale_prices(trades_1[(-1)].dt, False, data_portal)
    pp.calculate_performance()
    self.assertEqual(pp.cash_flow, (((-1) * txn.price) * txn.amount), 'capital    used    should    be    equal    to    the    opposite    of    the    transaction                                                    cost    of    sole    txn    in    test')
    self.assertEqual(len(pp.positions), 1, 'should    be    just    one    position')
    self.assertEqual(pp.positions[1].asset, txn.asset, 'position    should    be    in    security    from    the    transaction')
    self.assertEqual(pp.positions[1].amount, (-100), 'should    have    a    position    of    -100    shares')
    self.assertEqual(pp.positions[1].cost_basis, txn.price, 'should    have    a    cost    basis    of    10')
    self.assertEqual(pp.positions[1].last_sale_price, trades_1[(-1)].price, 'last    sale    should    be    price    of    last    trade')
    self.assertEqual(pp.ending_value, (-1100), 'ending    value    should    be    price    of    last    trade    times    number    of                                                    shares    in    position')
    self.assertEqual(pp.pnl, (-100), 'gain    of    1    on    100    shares    should    be    100')
    trades_2 = trades[(-2):]
    pp.rollover()
    pt.sync_last_sale_prices(trades[(-1)].dt, False, data_portal)
    pp.calculate_performance()
    self.assertEqual(pp.cash_flow, 0, 'capital    used    should    be    zero,    there    were    no    transactions    in                                                    performance    period')
    self.assertEqual(len(pp.positions), 1, 'should    be    just    one    position')
    self.assertEqual(pp.positions[1].asset, txn.asset, 'position    should    be    in    security    from    the    transaction')
    self.assertEqual(pp.positions[1].amount, (-100), 'should    have    a    position    of    -100    shares')
    self.assertEqual(pp.positions[1].cost_basis, txn.price, 'should    have    a    cost    basis    of    10')
    self.assertEqual(pp.positions[1].last_sale_price, trades_2[(-1)].price, 'last    sale    should    be    price    of    last    trade')
    self.assertEqual(pp.ending_value, (-900), 'ending    value    should    be    price    of    last    trade    times    number    of                                                    shares    in    position')
    self.assertEqual(pp.pnl, 200, 'drop    of    2    on    -100    shares    should    be    200')
    ptTotal = perf.PositionTracker(self.sim_params.data_frequency)
    ppTotal = perf.PerformancePeriod(1000.0, self.sim_params.data_frequency)
    ppTotal.position_tracker = pt
    ptTotal.execute_transaction(txn)
    ppTotal.handle_execution(txn)
    ptTotal.sync_last_sale_prices(trades[(-1)].dt, False, data_portal)
    ppTotal.calculate_performance()
    self.assertEqual(ppTotal.cash_flow, (((-1) * txn.price) * txn.amount), 'capital    used    should    be    equal    to    the    opposite    of    the    transaction    cost    of    sole    txn    in    test')
    self.assertEqual(len(ppTotal.positions), 1, 'should    be    just    one    position')
    self.assertEqual(ppTotal.positions[1].asset, txn.asset, 'position    should    be    in    security    from    the    transaction')
    self.assertEqual(ppTotal.positions[1].amount, (-100), 'should    have    a    position    of    -100    shares')
    self.assertEqual(ppTotal.positions[1].cost_basis, txn.price, 'should    have    a    cost    basis    of    10')
    self.assertEqual(ppTotal.positions[1].last_sale_price, trades_2[(-1)].price, 'last    sale    should    be    price    of    last    trade')
    self.assertEqual(ppTotal.ending_value, (-900), 'ending    value    should    be    price    of    last    trade    times    number    of                                                    shares    in    position')
    self.assertEqual(ppTotal.pnl, 100, 'drop    of    1    on    -100    shares    should    be    100')
    check_perf_period(pp, gross_leverage=0.8181, net_leverage=(-0.8181), long_exposure=0.0, longs_count=0, short_exposure=(-900.0), shorts_count=1)
    account = ppTotal.as_account()
    check_account(account, settled_cash=2000.0, equity_with_loan=1100.0, total_positions_value=(-900.0), total_positions_exposure=(-900.0), regt_equity=2000.0, available_funds=2000.0, excess_liquidity=2000.0, cushion=1.8181, leverage=0.8181, net_leverage=(-0.8181), net_liquidation=1100.0)
