I'm looking to do vectorized backtesting using a Pandas Dataframe and I'm having trouble setting a fixed stop loss and take profit.
For example (see image): On 2021-11-26 at 17:44 there is a buy signal (setup == 1) . The purchase would be made if the price breaks the price_entry(5588.5), with the stop loss at 5585.0 and the take profit at 5595.5
I currently do this backtesting using loop, but I need it to be vectored for better performance.
EDIT: I hope to get that result. That is, when finding the exit (stop loss or take profit) of the trading, record the difference (entry price - exit price) in a column.
datetime |high |low |open |close |setup |price_entry |stop loss |take_profit|is_bought |price_exit |diff
-----------------------------------------------------------------------------------------------------------------------------------------------
0 2021-11-26T17:38:00+00:00 |5586 |5583,5 |5586 |5584,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
1 2021-11-26T17:40:00+00:00 |5585,5 |5583 |5584,5 |5583,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
2 2021-11-26T17:42:00+00:00 |5588,5 |5583,5 |5583,5 |5587 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
3 2021-11-26T17:44:00+00:00 |5588 |5585,5 |5586,5 |5585,5 |1 |5588,50 |5585 |5595,5 | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
4 2021-11-26T17:46:00+00:00 |5589 |5585 |5586 |5587,5 |NaT | NaT |NaN |NaN |True | |
-----------------------------------------------------------------------------------------------------------------------------------------------
5 2021-11-26T17:48:00+00:00 |5587,5 |5583 |5587,5 |5584,5 |NaT | NaT |NaN |NaN |True |5585,00 |-3,50
-----------------------------------------------------------------------------------------------------------------------------------------------
6 2021-11-26T17:50:00+00:00 |5587 |5583,5 |5584,5 |5586 |1 |5587,5 |5583 |5596,5 | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
7 2021-11-26T17:52:00+00:00 |5586 |5583 |5586 |5584,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
8 2021-11-26T17:54:00+00:00 |5585 |5581 |5584,5 |5583,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
9 2021-11-26T17:56:00+00:00 |5585 |5579 |5584 |5579,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
10 2021-11-26T17:58:00+00:00 |5584 |5577,5 |5579,5 |5581,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
11 2021-11-26T18:00:00+00:00 |5588 |5579,5 |5581,5 |5588 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
12 2021-11-26T18:02:00+00:00 |5589 |5585,5 |5587,5 |5585,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
13 2021-11-26T18:04:00+00:00 |5588 |5584,5 |5585,5 |5586,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
14 2021-11-26T18:06:00+00:00 |5586,5 |5583 |5586 |5583,5 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------
15 2021-11-26T18:08:00+00:00 |5585,5 |5582 |5584 |5582 |NaT | NaT |NaN |NaN | | |
-----------------------------------------------------------------------------------------------------------------------------------------------