Thursday 31 October 2019

python finance 3 rolling average

daily stock price, 100 day rolling average, and trade volume

synchronized zoom on volume and price graphs

df = pd.read_csv('tsla_csv', parse_dates=True, index_col=0)

#100 day rolling average, for the first 100 days, average beginning to current day
df['100ma'] = df['Adj Close'].rolling(window=100, min_periods=0).mean()
#df.dropna(inplace=True)
#print(df.head())

#price graph height 5 x volumn graph height, share x axis (date)
ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6, 1), (5, 0), rowspan=1, colspan=1, sharex=ax1)

ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['100ma'])
ax2.bar(df.index, df['Volume'])

plt.show()  

reference:
https://www.youtube.com/watch?v=QAkOnV1-lIg&list=PLQVvvaa0QuDcOdF96TBtRtuQksErCEBYZ&index=3
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplot2grid.html

No comments:

Post a Comment