Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Paying broker's trade fees... code snippet...

Hey guys...

So I wanted to encapsulate the payment of trade fees and track them to get an idea of how much I'm losing to commissions.

Attached is the Sample Algorithm provided within this forum + a small helper function which handles the payments of fees.

The nominal fee (avg_trade_fee) is declared in the initializer, along with a total fees counter (total_trade_fees) to keep track of how much profit I've lost to fees.

I have noticed that under some conditions, I get total_trade_fees numbers which are not even denominators of the avg_trade_fee. Not sure why, but my first guess is that it's due chopping a given trade in two pieces between days. The log output shows a consistent trade payment.

Next step is to include a small function which checks that an equity trade is large enough to make a net profit over the trade fee. Something to the effect of:

if (trade_is_profitable) = true then (commit order).

Comments / suggestions are greatly welcomed!

Thanks,
-mc

4 responses

Michael,

There is a way to take out commission fees using set_commission (https://www.quantopian.com/help#ide-commission), yet this is also a great way to record the commissions. Looking forward to see further improvements on this!

  • Seong
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.

Seong / community:

I took a look at the set_commission module. Thanks for the heads up on that. I find it interesting, but I think I'm either missing something, or it's lacking a bit of functionality and/or transparency. Documentation seemed a bit light for that particular module (otherwise, documentation is pretty solid).

I initialize as per "set_commission(commission.PerTrade(cost=5.00))"... after that, I don't see anyway to retrieve the commission as a float, ie "commission.cost" such that I could do math on it such as "total_fees += commission.cost".

I also haven't seen that the commission is taken out of my cash on hand. Should I expect this? Is the commission "paid" in some other manner, perhaps within the order itself? I've tried to identify the commission by taking the cash on hand before the order, then after the order, but I keep getting a zero sum when I compare it to the price of the equity at the time of the order.

See the attached backtest... In the pay_trade_fees, I've annotated how I've tried to find the commission. One thought is that I'm retrieving a reference to the cash on hand, such that when I go to view it, I'm actually taking x = cash(t=1) - cash(t=1) rather than x = cash(t=1) - cash(t=0), so that would be poor programming on my part. If that's not the issue, then I'm stumped.

NOTE: the attached algo appears to be very slow... backtests about 1/5th the speed of V1.0 of this algo (first post in this thread). Originally had some print commands to watch it all happen, but those had to come out when she froze each time at approx 27% backtest. Next step is to look at a data_handler clock to make sure we can do all our calcs w/in the 1-minute intervals...

Thanks,
-mc

Hello Michael,

The backtest might not be frozen - try to refresh the page i.e. F5 in Firefox. Also, on the backtest page have a look at the icon to the right of 'Share Results' where under 'Backtest Details' you can see a start and finish time. If the backtest has finished but shows no finish time then refresh again.

P.

Hello Michael,

Commission is reflected in the price under 'Transactions'. I changed your algo to daily mode and logged the daily price and the first order:

2008-01-07 PRINT 177.58  
2008-01-07 PRINT Event({'status': 0, 'created': datetime.datetime(2008, 1, 7, 0, 0,  
tzinfo=<UTC>), 'limit_reached': False, 'stop': None, 'stop_reached': False,  
'amount': -1, 'limit': None, 'sid': Security(24, symbol=u'AAPL',
security_name=u'APPLE INC', start_date=datetime.datetime(1993, 1, 4, 5, 0,  
tzinfo=<UTC>), end_date=datetime.datetime(2013, 10, 14, 5, 0, tzinfo=<UTC>),  
first_traded=None), 'dt': datetime.datetime(2008, 1, 7, 0, 0, tzinfo=<UTC>),  
'id': '14f52dcf416447e5bebf07a888abaec1', 'filled': 0})
2008-01-08 PRINT 171.23  
2008-01-08 PRINT Event({'status': 1, 'created': datetime.datetime(2008, 1, 7, 0, 0,  
tzinfo=<UTC>), 'limit_reached': False, 'stop': None, 'stop_reached': False,  
'amount': -1, 'limit': None, 'sid': Security(24, symbol=u'AAPL',
security_name=u'APPLE INC', start_date=datetime.datetime(1993, 1, 4, 5, 0,  
tzinfo=<UTC>), end_date=datetime.datetime(2013, 10, 14, 5, 0, tzinfo=<UTC>),  
first_traded=None), 'dt': datetime.datetime(2008, 1, 8, 0, 0, tzinfo=<UTC>),  
'id': '14f52dcf416447e5bebf07a888abaec1', 'filled': -1})

The transaction shows as:

2008-01-08 00:00:00     AAPL    SELL    -1  $171.20     ($171.20)  

P.