Last week a community member noticed a problem in the implementation of how commissions are charged in simulation. We dug into it, found the underlying bug, fixed it, tested it, and released the fix today.
The impact of the bug is that in many cases, including the default commission model, simulated commissions were larger than they should have been. Real money trading and IB paper trading were unaffected - this bug is only present in simulation.
The specific details of the bug were that the full commission was being applied on each fill, but the commission should be calculated on the order, not the fill. For example, the default commission is $.0075 per share with a minimum of $1. Using that model, if your order for 400 shares was filled in four 100-share fills, your algorithm was charged $1 for each fill, for a total commission of $4. The correct commission should have been $.0075 * 400, or $3. Any algorithm using the default commission model that had partial fills with small share quantities (under 133 shares) incorrectly was charged $1 on those partial fills.
The nature of the bug means that PerTrade
-type commissioned orders in less-liquid stocks were most affected. An affected algorithm would generally see a lower overall return; a backtest run today with the fixed bug will show slightly higher returns as the smaller commissions are applied. Algorithms using PerShare
commissions without a minimum were unaffected. (The different commission models are explained in the documentation).
As a part of the fix, we changed the way the commission charge appears during simulation. Previously, we adjusted the cost of the shares in the trade by the amount of the commission, spread over the shares filled. With this fix, we now apply the commission by deducting it from your cash. This change has the added benefit of making the backtester closer to live trading. In real-money trading, the commission comes back to the algorithm asynchronously and is deducted from the cash. The backtester is now effectively on the same model.
We also made the order's commission available to your algorithm on the order
object. The attached backtest stores the order's id as it is placed, and then checks the status of the order each minute. You can see the commission increase as the order fills over several minutes.
Of course, we're very sorry about this bug. The bug has been there since the beginning - never reported or discovered until now. We're very grateful to our community for feedback like this. Our tools get better every day because they have so many watchful, thoughtful eyes on every aspect.