I have a dataframe like this with two columns, date
and indicator
:
date indicator
2019-10-26 06:48:49 -1.073525
2019-10-27 06:19:31 -0.375276
2019-10-28 06:50:44 0.643764
2019-10-29 07:21:35 0.863731
2019-10-30 07:52:36 1.022312
2019-10-31 08:23:18 1.125842
2019-11-01 08:52:35 0.863731
2019-11-02 09:16:28 0.831097
2019-11-03 09:42:20 0.529638
2019-11-04 10:09:01 -0.735926
2019-11-05 10:34:39 -1.743626
2019-11-06 11:00:39 -0.872055
the idea would be to create a column signal
, without doing a loop, which works like this :
- if
indicator
< -1 then :- if
signal
was 0, it becomes 1 and keeps this value untilindicator
become positive - if
signal
was already 1 it doesn't change
- if
- if
indicator
> 1 then :- if
signal
was 0, it becomes -1 and keeps this value untilindicator
become negative - if
signal
was already -1 it doesn't change
- if
- if
indicator
changes sign :- if
signal
was -1 or 1 it becomes 0 - if
signal
was 0 it doesn't change
- if
so it would give something like :
date indicator signal
2019-10-26 06:48:49 -1.073525 1
2019-10-27 06:19:31 -0.375276 1
2019-10-28 06:50:44 0.643764 0
2019-10-29 07:21:35 0.863731 0
2019-10-30 07:52:36 1.022312 -1
2019-10-31 08:23:18 1.125842 -1
2019-11-01 08:52:35 0.863731 -1
2019-11-02 09:16:28 0.831097 -1
2019-11-03 09:42:20 0.529638 -1
2019-11-04 10:09:01 -0.735926 0
2019-11-05 10:34:39 -1.743626 1
2019-11-06 11:00:39 -0.872055 1
i tried to create some column with 1 and -1 depending on indicator value then do a diff and cumulative sum but didn't succeed to obtain this exact column.