Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Feature request: get_commission

There should be a way for an algorithm to know the type and amount of commission charged - something like get_commission(). This is necessary to calculate (or estimate, since I don't know the price I'll end up paying - for now, let's disregard this) the number of shares I can afford with a given amount of cash.

A naïve approach is to calculate the number of shares to buy as

p=data[sid].price  
n=int(cash/p)  

But that will incur transaction costs, and if I don't have enough cash left over, will put me in the red, and, if I can't or don't want to borrow, will necessitate selling of some shares, which will incur further commissions. If the price per share is less than the commission amount, this will continue indefinitely, or until I go bankrupt or the algorithm crashes.

A workaround is to set the commission amount manually:

cps=0.03 # commission per share: USD 0.03  
p=data[sid].price  
n=int(cash/(p+cps))  

This will fall apart if default commissions change, I change them myself (for example, to see how this algorithm would perform with a different broker), someone else clones my algorithm and changes them, and the hardwired constant 0.03 is not changed accordingly.

Another workaround would be to create and set a variable (Python has no constants) to store the commission amount, then set the commission:

cps=0.03 # commission per share: USD 0.03  
set_commission(commission.PerShare(cps))  
p=data[sid].price  
n=int(cash/(p+cps))  

This will guarantee that cps reflects the commission per share amount. But it cannot be used in the contest - Quantopian rejects the line, even if it's commented out!

I would like to be able to write

cpt, cps=get_commission() # commission per trade, per share  
p=data[sid].price  
n=int((cash-cpt)/(p+cps))  
if n>0:  
    order(sid, n)  
    # log, freeze n*(p+cps)+cpt cash so you don't try to spend it, etc.  

Since the Quantopian platform already knows the commissions, this feature should only cost writing a few lines of code and running regression tests. This being a simple getter, the tests should pass the first time. I hope I have sufficiently demonstrated the need.

3 responses

The above describes "regular" commissions, such as those normally charged for stocks and ETFs. But mutual funds (and some ETFs - for example, TD Ameritrade has about 100 commission-free ETFs) are normally commission-free, though there may be limitations like frequent-trading fees or minimum holding periods.

For this reason, get_commission(sid) should return the per-trade and per-share commission charged for buying or selling security sid.

Hello André - Thanks for the idea and the thought you've put into it.

I actually think that the issue you raised in the first paragraph is very important, and I don't think you should disregard it. What you're trying to do here is predict the future. You're trying to predict the future price and the future commission down to the penny. There's no way to do that repeatedly. You need to manage that imprecision through other methods: obtaining a margin account, maintaining a cash buffer, smaller order batches, things like that.

Also, commissions often aren't fully known beforehand. Different exchanges have different fees, plus liquidity charges may or may not be applied. We're talking about fractions of a penny, but it makes the whole exercise very difficult.

I would suggest that you'll get more reward for your time making smart buy/sell decisions than predicting to-the-penny execution prices.

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.

@Dan - I'm disregarding the uncertainty of the actual net price here and addressing it in the other one requesting get_slippage.

For simplicity, I am also disregarding additional fees here - frequent trading, odd lot, etc. If these matter, the user should be able to account for them or avoid them in his/her code.

If the trade may be executed by one or more of several exchanges, get_commission() should return the maximum. (And perhaps get_commission (exchange=...) could be made available, especially when the user requests a specific exchange in the orders.) This would lead to the number of shares the user can afford to buy being underestimated, but, since the purpose is to avoid ordering too many, it would just mean erring on the safe side.

I chose to allocate my time to these low-level details first, so I don't have to wrack my brains wondering what went wrong in a complicated algorithm when there are too many high- and low-level unknowns. Bottom-up software development, if you will.