-
Notifications
You must be signed in to change notification settings - Fork 8
/
MonthlyBullBearMarkets.py
47 lines (29 loc) · 1.04 KB
/
MonthlyBullBearMarkets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# http://github.com/timestocome
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
# find best and worst weeks for Nasdaq
data = pd.read_csv('nasdaq.csv')
# scale data
data = np.log(data[['Open']])
# level data
ix = np.arange(0, len(data))
dx = (data['Open'].iloc[len(data)-1] - data['Open'].iloc[0]) / len(data)
data['dx'] = dx * ix
data['Leveled'] = data['Open'] - data['dx']
data = data[['Open', 'Leveled']]
data['dx'] = data['Leveled'] - data['Leveled'].shift(1)
# smooth daily changes
data['smoothed_dx'] = data['dx'].rolling(window=21).mean()
data = data.dropna()
data['Bear'] = data['smoothed_dx'].apply(lambda z: 0 if z > 0 else 1)
data['Bull'] = data['smoothed_dx'].apply(lambda z: 1 if z > 0 else 0)
ix = np.arange(0, len(data))
plt.figure(figsize=(30,16))
plt.title('Monthly Nasdaq Bull/Bear Market')
plt.plot(data['Open'])
plt.scatter(ix, data['Bull'] * data['Leveled'] + 1, c='green')
plt.scatter(ix, data['Bear'] * data['Leveled'], c='red')
plt.savefig('MontlyBullBear.png')
plt.show()