Skip to content

Incorrect commission calculation #1273

Open
@esaesa

Description

@esaesa

Expected behavior

It appears the commission is being added directly to the per-unit adjusted price in:

adjusted_price_plus_commission = adjusted_price + self._commission(order.size, price)

Python
adjusted_price_plus_commission = adjusted_price + self._commission(order.size, price)
If self._commission returns a total commission for the entire order (not per unit), this addition is not correct, since it adds the entire commission fee to each unit price. The commission should either be distributed per unit before adding, or added as a lump sum after computing the total order cost.

Expected behavior: Commission should be correctly applied to the order total, not to each unit, unless self._commission returns a per-unit commission.

Suggested fix: Change the calculation so that the commission is only added once per order, or ensure that self._commission returns a per-unit fee if this logic is intended.

Code sample

bt = Backtest(
        data,
        DCAStrategy,
        cash=1000,
        commission=0.0005,
        trade_on_close=True,
        finalize_trades=True,
        margin=1/1,
    )

Actual behavior

Image

Image

Additional info, steps to reproduce, full crash traceback, screenshots

No response

Software versions

  • backtesting.__version__: 0.6.4
  • pandas.__version__: 2.2.3
  • numpy.__version__: 2.1.3
  • bokeh.__version__: 3.7.0
  • OS: Win 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions