<pandas, Python, sentdex> Python Programming for Finance
やってみた。
In [1]: import datetime as dt In [2]: import matplotlib.pyplot as plt In [3]: from matplotlib import style In [4]: import pandas as pd In [5]: import pandas_datareader.data as web In [6]: style.use('ggplot') In [7]: s = dt.datetime(2000,1,1) In [8]: e = dt.datetime(2016,12,31) In [9]: df = web.DataReader('TSLA', 'yahoo', s, e) In [10]: df.head() Out[10]: Open High Low Close Volume Adj Close Date 2010-06-29 19.000000 25.00 17.540001 23.889999 18766300 23.889999 2010-06-30 25.790001 30.42 23.299999 23.830000 17187100 23.830000 2010-07-01 25.000000 25.92 20.270000 21.959999 8218800 21.959999 2010-07-02 23.000000 23.10 18.709999 19.200001 5139800 19.200001 2010-07-06 20.000000 20.00 15.830000 16.110001 6866900 16.110001
ふーん、なるほど。
続いてその3もやった。
In [11]: df['100ma'] = df['Adj Close'].rolling(window=100, min_periods=0).mean() In [12]: df.head() Out[12]: Open High Low Close Volume Adj Close \ Date 2010-06-29 19.000000 25.00 17.540001 23.889999 18766300 23.889999 2010-06-30 25.790001 30.42 23.299999 23.830000 17187100 23.830000 2010-07-01 25.000000 25.92 20.270000 21.959999 8218800 21.959999 2010-07-02 23.000000 23.10 18.709999 19.200001 5139800 19.200001 2010-07-06 20.000000 20.00 15.830000 16.110001 6866900 16.110001 100ma Date 2010-06-29 23.889999 2010-06-30 23.860000 2010-07-01 23.226666 2010-07-02 22.220000 2010-07-06 20.998000 In [13]: ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1) In [14]: ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1) In [15]: ax1.plot(df.index, df['Adj Close']) Out[15]: [<matplotlib.lines.Line2D at 0xb9c5c88>] In [16]: ax1.plot(df.index, df['100ma']) Out[16]: [<matplotlib.lines.Line2D at 0xb463f98>] In [17]: ax2.bar(df.index, df['Volume']) Out[17]: <Container object of 1640 artists> In [18]: plt.show()