forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
technical_indicators.py
71 lines (51 loc) · 1.52 KB
/
technical_indicators.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
import yfinance as yf
import pandas_datareader as pdr
from mpl_finance import candlestick_ohlc
import matplotlib.pyplot as plt
# %%
# import SPY stock price
df_spy = pdr.get_data_yahoo("SPY", start="2019-01-01", end="2019-09-30")
# import AAPL stock price
df_aapl = pdr.get_data_yahoo("AAPL", start="2019-01-01", end="2019-09-30")
# %%
df_spy.head()
# %%
df_aapl[["Open", "High", "Low", "Close"]].plot()
plt.show()
# %%
fig = plt.figure(figsize=(10, 10))
ax = plt.subplot()
plot_data = []
for i in range(150, len(df_aapl)):
row = [
i,
df_aapl.Open.iloc[i],
df_aapl.High.iloc[i],
df_aapl.Low.iloc[i],
df_aapl.Close.iloc[i],
]
plot_data.append(row)
candlestick_ohlc(ax, plot_data)
plt.show()
# %%
from stockstats import StockDataFrame
stocks = StockDataFrame.retype(df_aapl[["Open", "Close", "High", "Low", "Volume"]])
# %%
plt.plot(stocks["close_10_sma"], color="b", label="SMA")
plt.plot(df_aapl.Close, color="g", label="Close prices")
plt.legend(loc="lower right")
plt.show()
# %%
plt.plot(stocks["close_10_sma"], color="b", label="SMA") # plotting SMA
plt.plot(stocks["close_10_ema"], color="k", label="EMA")
plt.plot(df_aapl.Close, color="g", label="Close prices") # plotting close prices
plt.legend(loc="lower right")
plt.show()
# %%
plt.plot(stocks["macd"], color="b", label="MACD")
plt.plot(stocks["macds"], color="g", label="Signal Line")
plt.legend(loc="lower right")
plt.show()