Description
Thanks for posting this problem, I am having a similar issue when comparing backtesting.py with TV , commission fees differ significantly. After considerable digging I have a theory that backtesting.py is not computing fees on the Exit Price (at least the math points in that direction). This may be my settings or something deeper. Hopefully @kernc will comment...
I have attached two screen shots one of trade results for TV and the other stats _trades. backtesting.py & TV are using coinbases' stupid high fee of .004% no slippage and the data uploaded in backtesting.py is the same data downloaded from TV chart using 1m time frame.
self.buy(size=1500, sl=(1 - self.stoploss) * self.data.Close, tp=(1+self.takeprofit)*self.data.Close)
bt = Backtest(data, scalp, cash=500, commission=.004,trade_on_close=True, exclusive_orders=True)
My observations:
TV Entry Price is the same as close and reflects no fee. .2166
BT.py Entry Price shows the purchase with fee applied .2166* 1.004 = .217466 which is correct.
TV Exit Price shows .2183
BT.py Exit Price is also .2183
TV shows a profit on this trade of -.06 while BT.py shows a profit of 1.2504
TV math:
- Entry .21660*1500 = 324.9 *.004 = 1.296
- Exit .21830*1500 = 327.45 * .004 = 1.3098
- fees 2.6058
- profit 327.45-324.9-2.6058 = -0.558 or -0.06$
BT.py math:
- Entry .217466 / 1.004 = 0.216599601593625 = close (.2166) - looks good! -
cost .217466*1500 = 326.199 - Exit .2183 / 1.004 = 0.217430278884462 <> close (.2183)
proceeds .2183*1500 = 327.45 - fees 1.299(entry) + 0 (exit)
- profit proceeds-costs or 327.45 - 326.199 = 1.25 - math checks out -
In a nutshell TV is computing fees (entry price * size)* 1.004 where as backtesting.py appears to be computing commision by (Entry price * 1.004)* size. I think both methods are correct, the results are the same. However, TV computes both purchase and sale, BT appears to compute on the purchase only.
Originally posted by @MrDenfish in #734 (comment)