I have a list of booleans that correspond to buy and sell signals that I would like to backtest. To achieve this, I calculated the return ret
of a security and when the signal is False
I modify the corresponding return to 0
(corresponds to a cash position), and when the signal is True
I kept the return. The result is a Pandas series like this:
> signal
2018-01-01 00:00:00+00:00 NaN
2018-01-01 00:05:00+00:00 True
2018-01-01 00:10:00+00:00 False
2018-01-01 00:15:00+00:00 False
2018-01-01 00:20:00+00:00 True
...
> ret
2018-01-01 00:00:00+00:00 NaN
2018-01-01 00:05:00+00:00 -0.003664
2018-01-01 00:10:00+00:00 -0.002735
2018-01-01 00:15:00+00:00 -0.005104
2018-01-01 00:20:00+00:00 0.000366
...
> ret_backtest = ret.loc[signal[~signal].index] = 0
> ret_backtest
2018-01-01 00:00:00+00:00 NaN
2018-01-01 00:05:00+00:00 -0.003664
2018-01-01 00:10:00+00:00 0
2018-01-01 00:15:00+00:00 0
2018-01-01 00:20:00+00:00 0.000366
...
Then I reconstruct a price from ret_backtest
, which give me a simplified result of the backtest.
result = ret_backtest.add(1).cumprod().mul(100)
My question concerns the trading fees. Usually, these fees are calculated based on the volumes bought or sold. But how can I take into account these transaction costs from a list of returns? for example, by selecting the periods when signal have changed, and applying the fees on the performance of these periods?
t = signal.shift(1) != signal
trades_timestamp = (t.loc[t]).index