Jonathan Bechtel
Jonathan Bechtel

Reputation: 3607

Calculating Bull/Bear Markets in Pandas

I was recently given a challenge of calculating the presence of Bull/Bear markets using the values of -1, 1 to denote which one is which.

It is straight forward enough to do this with a for-loop but I know this is the worst way to do these things and it's better to use numpy/pandas methods if possible. However, I'm not seeing an easy way to do it.

Any ways of how to do this, maybe using changes of +/- 20% from current place to determine which regime you're in.

Here's a sample dataframe:

dates = pd.date_range(start='1950-01-01', periods=25000)
rand  = np.random.RandomState(42)

vals    = np.zeros(25000)
vals[0] = 15

for i in range(1, 25000):
    vals[i] = vals[i-1] + rand.normal(0, 1)

df = pd.DataFrame(vals, columns=['Price'], index=dates)

The plot of these prices looks like this:

enter image description here

Anyone have any recommendations to calculate what regime the current point is in?

If you have to use a for loop then that's fine.

Upvotes: 2

Views: 1504

Answers (1)

teepee
teepee

Reputation: 2714

I think this might work:

import numpy as np
vals = np.random.normal(0, 1, 25000)
vals[0] = 15
vals = np.cumsum(vals)

Upvotes: 1

Related Questions