May I get some help in replicating the function, with input a pandas series (daily series of closing-prices), below? It computes daily streaks.
cp = pd.Series(index=range(1,8+1), name='Closing_Price',
data=[20.00, 20.50, 20.75, 19.75, 19.50, 19.35, 19.35, 19.40])
1 20.00
2 20.50
3 20.75
4 19.75
5 19.50
6 19.35
7 19.35
8 19.40
Interpretation is:
- the closing price on Day 2 is higher than on Day 1, so we have a one-day up streak
- On Day 3, the price closes higher again, so we have a two-day up streak, i.e. the Streak Duration value is 2.
- On Day 4, the closing price falls, giving us a one-day down streak. The Streak Duration value is negative (-1) because the price movement is down, not up.
- The downward trend continues on Days 5 and 6, which our Streak Duration reflects with values of -2 and -3.
- On Day 7 the closing price is unchanged, so the Streak Duration is set to 0 indicating neither an up close nor a down close.
- Finally, on Day 8 the closing price rises again, bringing the Streak Duration value back to 1.
I think I will need cumsum() and groupby. Something like
def fxn(series):
x = series.diff()
but I'm not sure how to proceed