kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.39k stars 1.05k forks source link

Commission fees differ significantly. #1006

Open MrDenfish opened 1 year ago

MrDenfish commented 1 year ago

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:

BT.py math:

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.

backtesting py TV

Originally posted by @MrDenfish in https://github.com/kernc/backtesting.py/discussions/734#discussioncomment-6200892