Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bug Fix: Commission Implementation

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.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

7 responses

Thank you. I noticed a $73 commission a couple of weeks ago on a small cap stock that took awhile to fill, and hadn't got back to figuring out what was going on. That explains things. I guess I should go back and try the backtest again...

Hi Dan,

The help says:

The default commission model is PerShare, at 7.5 cents per share and $1 minimum cost per trade. The first fill will incur at least the minimum commission (if there is one), and subsequent fills will incur additional commission.

You might want to change the language to be consistent with IB (https://www.interactivebrokers.com/en/index.php?f=1590&p=stocks1), and your statement above:

...the commission should be calculated on the order, not the fill.

The help should say:

$1 minimum cost per order

Also, the help says:

if there is one

But in the prior sentence, you state that there is a minimum of $1, so why are you saying "if there is one"?

Also, is it correct that IB charges no commission if there is no fill whatsoever? Presumably no commission is applied, if there is no fill.

So, perhaps the help should read:

The PerShare commission model allows the cost per share and the minimum cost per order to be set (the default commission model is PerShare, at 7.5 cents per share and $1 minimum cost per order). The first fill will incur at least the minimum commission (if there is one), and subsequent fills will incur additional commission at the per share cost. Orders that go completely unfilled will incur no commission.

Also, I see that IB has a maximum commission per order, of 0.5% of trade value. Did you consider incorporating this, too?

I don't know if this is due to the bug fix, but right now it feels like it takes much (5-10x) longer to run code. Also, when in debug mode, the mini repl window will now more often break and exit.

Hi Toan:

Nothing in our instrumentation shows any runtime slowdown. Feel free to contact us via the "Contact Support" dropdown in the Learn & Support part of our navbar and we'd be happy to look at any code you have.

thanks,
Jean

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

The help at https://www.quantopian.com/help#ide-commission says

your backtest defaults to $0.0075 per share with a $1 minimum cost per trade

and, two paragraphs below,

The default commission model is PerShare, at 7.5 cents per share and $1 minimum cost per trade.

So which is it, 7.5 cents or 3/4 of a cent per share?

Straight from IB website transaction cost would be $0.0075 per share with $1 minimum on US API directed orders. Therefore, it would be 3/4 of a cent per share.

https://www.interactivebrokers.com/en/index.php?f=1590&p=stocks1 (North American, 'Exceptions' dropdown)

Connor

@Grant and @André - yikes, we had an error in the documentation. It is fixed. The right answer is $.0075 per share. I also updated some of the ambiguity around the usage of the word trade and order.

The default commission isn't an exact representation of IB's commission model - also IB's commission model varies from account to account, so there is no true representation available. The default commission model is a general model that we think is useful to apply.