0

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

smci
  • 32,567
  • 20
  • 113
  • 146
  • 2
    Welcome to Stack Overflow. Please read how to ask good [questions](https://stackoverflow.com/help/how-to-ask). Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) 3. Error Message (preferably full Traceback to help others review and provide feedback). – Joe Ferndz Nov 21 '20 at 04:11
  • 2
    ,Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). ,, [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Nov 21 '20 at 04:14
  • 1
    `but I'm not sure how to proceed` - SO isn't a discussion forum or tutorial. – wwii Nov 21 '20 at 04:15
  • 1
    I already solved this, but **SO requires that you try to attempt it and post your code and where you got stuck**. Break this into multiple steps: a) get the series of differences in Closing_Price. Call that `CPDiff` b) convert `CPDiff` to a +1/0/-1 value (signum function), call that `CPDiffS` c) only then consider whether `CPDiffS` changes or stays the same, before d) finally you can count the streaks – smci Nov 21 '20 at 04:24

1 Answers1

1

Might this be the answer? Shout out to @smci for providing the much-needed and much-appreciated hints!!

def streak(x):
  c = x.copy()
  c['diff'] = c.diff()
  c['sign'] = c['diff'].apply(func = lambda y: y if not y else y // abs(y))
  grouper = (c['sign'] != c['sign'].shift()).cumsum()
  c['streak'] = c['sign'].groupby(grouper).cumsum()
  return c['streak']
  • Good job. [pandas passes dataframes by-reference](https://stackoverflow.com/questions/38895768/python-pandas-dataframe-is-it-pass-by-value-or-pass-by-reference), so you don't need the `.copy()`. Depends what you're assigning the dataframe result to? – smci Nov 21 '20 at 06:11
  • @smci thanks for pointing it out. I have another problem, can I ask you for some hints? I would like to create a function that looks at the size of the current day’s price change relative to previous price changes. This calculation is referred to as “Percent Rank”, or percentile. This tells us the percentage of values in the look-back period that are less than the current value. So the Percent Rank is the number of values in the look back period that are less than the current value, divided by the total number of values. – finmathstudent Nov 21 '20 at 06:22
  • For example, if the look-back period is 20 days, then we would compare today’s 2.0% return to the one-day returns from each of the previous 20 days. Let’s assume that three of those values are less than 2.0%. We would calculate Percent Rank as: Percent Rank = 3 / 20 = 0.15 = 15% – finmathstudent Nov 21 '20 at 06:22