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.