I have the following DataFrame:
macd_hist
Out[10]:
ADANIPORTS.NS ASIANPAINT.NS ... WIPRO.NS ZEEL.NS
Date ...
2015-06-22 NaN NaN ... NaN NaN
2015-06-23 NaN NaN ... NaN NaN
2015-06-24 NaN NaN ... NaN NaN
2015-06-25 NaN NaN ... NaN NaN
2015-06-26 NaN NaN ... NaN NaN
... ... ... ... ...
2020-06-12 -0.064481 1.635353 ... 0.213215 -1.800832
2020-06-15 -0.702969 0.135702 ... -0.096160 -3.020285
2020-06-16 -1.125824 -0.567845 ... -0.438076 -3.804984
2020-06-17 -1.423891 -2.635996 ... -0.347506 -4.095071
2020-06-18 -1.497237 -3.613468 ... -0.312098 -3.520918
[1227 rows x 50 columns]
How can i count the number of days where the number had turned from positive to negative for each ticker column. So if the number was positive yesterday and it turns negative today, that would be 1 but it should not count until it goes negative again and turns positive and then turns negative again, that would be another count.
What i am trying to count is this:
If I get it right, you can try the following:
((macd_hist > 0).astype(int).diff() > 0).sum()
Let's break it down. It will do the following :
(macd_hist >= 0)
: to check if your number is positive or not.astype(int)
: to convert into integers.diff()
: to detect changes (-1 for pos to neg, else 1)< 0
: to only keep the changes from pos to neg.sum()
: to count the number of such changes