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

Code before:
        # Test per trade model for futures.
        model = PerFutureTrade(cost=10)
        self.verify_per_trade_commissions(
            model, expected_commission=10, sid=1000,
        )

        # Test per trade model with custom costs per future symbol.
        model = PerFutureTrade(cost={'CL': 5, 'FV': 10})
        self.verify_per_trade_commissions(
            model, expected_commission=5, sid=1000,
        )
        self.verify_per_trade_commissions(
            model, expected_commission=10, sid=1001,
        )

    def test_per_share_no_minimum(self):
        model = PerShare(cost=0.0075, min_trade_cost=None)

        fill_amounts = [230, 170, 100]
        order, txns = self.generate_order_and_txns(
            sid=1, order_amount=500, fill_amounts=fill_amounts
        )
        expected_commissions = [1.725, 1.275, 0.75]

        # make sure each commission is pro-rated
        for fill_amount, expected_commission, txn in \
                zip(fill_amounts, expected_commissions, txns):

            commission = model.calculate(order, txn)
            self.assertAlmostEqual(expected_commission, commission)
            order.filled += fill_amount
            order.commission += commission

    def test_per_share_shrinking_position(self):
        model = PerShare(cost=0.0075, min_trade_cost=None)

        fill_amounts = [-230, -170, -100]
        order, txns = self.generate_order_and_txns(
            sid=1, order_amount=-500, fill_amounts=fill_amounts
        )
        expected_commissions = [1.725, 1.275, 0.75]

        # make sure each commission is positive and pro-rated
        for fill_amount, expected_commission, txn in \
                zip(fill_amounts, expected_commissions, txns):

            commission = model.calculate(order, txn)
            self.assertAlmostEqual(expected_commission, commission)
            order.filled += fill_amount
            order.commission += commission


Review comment (suggestion):
Small style recommendation.

The following would avoid a line break:

```
        for fill_amount, expected_commission, txn in zip(
            fill_amounts,
            expected_commissions,
            txns
        ):
```


Code after:
        # Test per trade model for futures.
        model = PerFutureTrade(cost=10)
        self.verify_per_trade_commissions(
            model, expected_commission=10, sid=1000,
        )

        # Test per trade model with custom costs per future symbol.
        model = PerFutureTrade(cost={'CL': 5, 'FV': 10})
        self.verify_per_trade_commissions(
            model, expected_commission=5, sid=1000,
        )
        self.verify_per_trade_commissions(
            model, expected_commission=10, sid=1001,
        )

    def test_per_share_no_minimum(self):
        model = PerShare(cost=0.0075, min_trade_cost=None)

        fill_amounts = [230, 170, 100]
        order, txns = self.generate_order_and_txns(
            sid=1, order_amount=500, fill_amounts=fill_amounts
        )
        expected_commissions = [1.725, 1.275, 0.75]

        # make sure each commission is pro-rated
        for fill_amount, expected_commission, txn in zip(
            fill_amounts,
            expected_commissions,
            txns,
        ):

            commission = model.calculate(order, txn)
            self.assertAlmostEqual(expected_commission, commission)
            order.filled += fill_amount
            order.commission += commission

    def test_per_share_shrinking_position(self):
        model = PerShare(cost=0.0075, min_trade_cost=None)

        fill_amounts = [-230, -170, -100]
        order, txns = self.generate_order_and_txns(
            sid=1, order_amount=-500, fill_amounts=fill_amounts
        )
        expected_commissions = [1.725, 1.275, 0.75]

        # make sure each commission is positive and pro-rated
        for fill_amount, expected_commission, txn in \
                zip(fill_amounts, expected_commissions, txns):

            commission = model.calculate(order, txn)
            self.assertAlmostEqual(expected_commission, commission)