Exemplo n.º 1
0
    def test__calc_transations_change_existing_multiple(self):
        new_pos = {self.asset1: (3, 2, 3, None), self.asset2: (1, 2, 3, None)}
        old_pos = {self.asset1: (2, 1, 1, None)}  # Old was opened at '2018-01-01'

        (
            transactions,
            pnl_close_total, pnl_execution_total,
            costs_close_total, costs_exec_total,
            costs_potential_close_total, costs_potential_exec_total,
        ) = Account._calc_transactions(pd.Timestamp('2018-01-02'), new_pos, old_pos)

        self.assertEqual(2, len(transactions))

        for t in transactions:
            (
                t_dt,
                t_asset,
                t_position_action,
                t_trans_qty,
                t_close_price,
                t_exec_price,
                t_costs_close,
                t_costs_exec,
                t_pnl_close,
                t_pnl_exec,
                t_ctx,
            ) = t
            if t_asset == self.asset1:
                self.assertEqual(pd.Timestamp('2018-01-02'), t_dt)
                self.assertEqual(self.asset1, t_asset)
                self.assertEqual(1, t_position_action)
                self.assertEqual(1, t_trans_qty)
                self.assertEqual(2, t_close_price)
                self.assertEqual(3, t_exec_price)
                self.assertEqual(-2 * 0.5 * 1, t_costs_close)
                self.assertEqual(-3 * 0.5 * 1, t_costs_exec)
            elif t_asset == self.asset2:
                self.assertEqual(pd.Timestamp('2018-01-02'), t_dt)
                self.assertEqual(self.asset2, t_asset)
                self.assertEqual(1, t_position_action)
                self.assertEqual(1, t_trans_qty)
                self.assertEqual(2, t_close_price)
                self.assertEqual(3, t_exec_price)
                self.assertEqual(-2 * 0.5 * 1, t_costs_close)
                self.assertEqual(-3 * 0.5 * 1, t_costs_exec)

        self.assertEqual(pnl_close_total, 2 + (-2 * 0.5 * 1) + (-2 * 0.5 * 1) )
        self.assertEqual(pnl_execution_total, 4 + (-3 * 0.5 * 1) + (-3 * 0.5 * 1))
        self.assertEqual(costs_close_total, (-2 * 0.5 * 1) + (-2 * 0.5 * 1))
        self.assertEqual(costs_exec_total, (-3 * 0.5 * 1) + (-3 * 0.5 * 1))
        self.assertEqual(costs_potential_close_total, -2 * 0.5 * 4)
        self.assertEqual(costs_potential_exec_total, -3 * 0.5 * 4)
Exemplo n.º 2
0
    def test__calc_transations_change_existing_both_zeros(self):
        new_pos = {self.asset1: (0, 2, 3, None)}
        old_pos = {self.asset1: (0, 1, 1, None)}  # Old was opened at '2018-01-01'

        (
            transactions,
            pnl_close_total, pnl_execution_total,
            costs_close_total, costs_exec_total,
            costs_potential_close_total, costs_potential_exec_total,
        ) = Account._calc_transactions(pd.Timestamp('2018-01-02'), new_pos, old_pos)

        self.assertEqual(0, len(transactions))

        self.assertEqual(pnl_close_total, 0)
        self.assertEqual(pnl_execution_total, 0)
        self.assertEqual(costs_close_total, 0)
        self.assertEqual(costs_exec_total, 0)
        self.assertEqual(costs_potential_close_total, 0)
        self.assertEqual(costs_potential_exec_total, 0)
Exemplo n.º 3
0
    def test__calc_transations_change_existing_decrease(self):
        new_pos = {self.asset1: (1, 2, 3, None)}
        old_pos = {self.asset1: (2, 1, 1, None)}  # Old was opened at '2018-01-01'

        (
            transactions,
            pnl_close_total, pnl_execution_total,
            costs_close_total, costs_exec_total,
            costs_potential_close_total, costs_potential_exec_total,
        ) = Account._calc_transactions(pd.Timestamp('2018-01-02'), new_pos, old_pos)

        self.assertEqual(1, len(transactions))

        (
            t_dt,
            t_asset,
            t_position_action,
            t_trans_qty,
            t_close_price,
            t_exec_price,
            t_costs_close,
            t_costs_exec,
            t_pnl_close,
            t_pnl_exec,
            t_ctx,
        ) = transactions[0]

        self.assertEqual(pd.Timestamp('2018-01-02'), t_dt)
        self.assertEqual(self.asset1, t_asset)
        self.assertEqual(-1, t_position_action)
        self.assertEqual(-1, t_trans_qty)
        self.assertEqual(2, t_close_price)
        self.assertEqual(3, t_exec_price)
        self.assertEqual(-2 * 0.5 * 1, t_costs_close)
        self.assertEqual(-3 * 0.5 * 1, t_costs_exec)
        self.assertEqual(None, t_ctx)

        self.assertEqual(pnl_close_total, 2 + t_costs_close)
        self.assertEqual(pnl_execution_total, 4 + t_costs_exec)
        self.assertEqual(costs_close_total, t_costs_close)
        self.assertEqual(costs_exec_total, t_costs_exec)
        self.assertEqual(costs_potential_close_total, -2 * 0.5 * 1)
        self.assertEqual(costs_potential_exec_total, -3 * 0.5 * 1)
Exemplo n.º 4
0
    def test__calc_transations_new(self):
        new_pos = {self.asset1: (2, 2, 3, None)}
        old_pos = None

        (
            transactions,
            pnl_close_total, pnl_execution_total,
            costs_close_total, costs_exec_total,
            costs_potential_close_total, costs_potential_exec_total,
        ) = Account._calc_transactions(pd.Timestamp('2018-01-02'), new_pos, old_pos)

        self.assertEqual(1, len(transactions))

        (
            t_dt,
            t_asset,
            t_position_action,
            t_curr_qty,
            t_close_price,
            t_exec_price,
            t_costs_close,
            t_costs_exec,
            t_pnl_close,
            t_pnl_exec,
            t_ctx,
        ) = transactions[0]
        self.assertEqual(pd.Timestamp('2018-01-02'), t_dt)
        self.assertEqual(self.asset1, t_asset)
        self.assertEqual(1, t_position_action)
        self.assertEqual(2, t_curr_qty)
        self.assertEqual(2, t_close_price)
        self.assertEqual(3, t_exec_price)
        self.assertEqual(-2 * 0.5 * 2, t_costs_close)
        self.assertEqual(-3 * 0.5 * 2, t_costs_exec)

        self.assertEqual(pnl_close_total, 0 + t_costs_close)
        self.assertEqual(pnl_execution_total, 0 + t_costs_exec)
        self.assertEqual(costs_close_total, t_costs_close)
        self.assertEqual(costs_exec_total, t_costs_exec)
        self.assertEqual(costs_potential_close_total, t_costs_close)
        self.assertEqual(costs_potential_exec_total, t_costs_exec)