I am trying to calculate a percent change between 2 numbers in one column when a signal from another column is triggered.
The trigger can be found with np.where()
but what I am having trouble with is the percent change. .pct_change
does not work because if you .pct_change(-5)
you get 16.03/20.35
and I want the number the opposite way 20.35/16.03
. See table below. I have tried returning the array from the index in the np.where
and adding it to an .iloc
from the 'Close' column but it says I can't use that array to get an .iloc
position. Can anyone help me solve this problem. Thank you.
IdxNum | Close | Signal (1s)
==============================
0 21.45 0
1 21.41 0
2 21.52 0
3 21.71 0
4 20.8 0
5 20.35 0
6 20.44 0
7 16.99 0
8 17.02 0
9 16.69 0
10 16.03 1<< 26.9% <<< 20.35/16.03-1 (df.Close[5]/df.Close[10]-1)
11 15.67 0
12 15.6 0