-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechnical_indicator.py
More file actions
91 lines (48 loc) · 1.98 KB
/
technical_indicator.py
File metadata and controls
91 lines (48 loc) · 1.98 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import ie
import matplotlib.dates as mdates
ticker = 'NFLX'
# Already imported the NFLX close prices into the nflx dataframe table. Got the prices from IEX Finance
# Timeframe=1 year
# nflx
# Rolling means
rolling_mean_10 = nflx.close.rolling(10).mean()
rolling_mean_50 = nflx.close.rolling(50).mean()
nflx.plot(figsize=(10, 5), title='Simple Moving Average')
plt.plot(rolling_mean_10, label='10 Day SMA', color='red')
plt.plot(rolling_mean_50, label='50 Day SMA', color='orange')
plt.legend()
# EMA
EMA_10 = nflx.close.ewm(span=10, adjust=False).mean()
EMA_50 = nflx.close.ewm(span=50, adjust=False).mean()
nflx.plot(figsize=(10, 5), title='Exponential Moving Average')
plt.plot(EMA_10, label='10 Day EMA', color='red')
plt.plot(EMA_50, label='50 Day EMA', color='orange')
plt.legend()
# Bollinger Bands
rolling_mean_20 = nflx.close.rolling(20).mean()
upper_band = rolling_mean_20 + 2 * nflx.close.rolling(20).std()
lower_band = rolling_mean_20 - 2 * nflx.close.rolling(20).std()
nflx.plot(figsize=(10, 5), title='20 Day Rolling Bollinger Bands').fill_between(nflx.index, lower_band, upper_band,
alpha=0.1)
plt.plot(upper_band, label='Upper Band', color='red')
plt.plot(lower_band, label='Lower Band', color='orange')
plt.legend()
plt.show()
# MACD
EMA_12 = nflx.close.ewm(span=12, adjust=False).mean()
EMA_26 = nflx.close.ewm(span=26, adjust=False).mean()
MACD = EMA_12 - EMA_26
SignalLine = MACD.ewm(span=9, adjust=False).mean()
nflx.plot(subplots=True, figsize=(10, 5), title='Moving Average Convergence Divergence')
plt.plot(EMA_12, label='12 Day EMA', color='red')
plt.plot(EMA_26, label='26 Day EMA', color='orange')
plt.legend()
plt.show()
MACD.plot(figsize=(10, 5), label='MACD', color='green', title='MACD vs Signal')
plt.plot(SignalLine, label='Signal Line', color='Purple')
plt.legend()
plt.show()