<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()
<Python, pandas> 縦にずらす。
縦にずらす。
In [22]: df = pd.DataFrame({'a':[1,2,3,4,5,6]}) In [23]: df Out[23]: a 0 1 1 2 2 3 3 4 4 5 5 6 In [24]: df.shift(-1) Out[24]: a 0 2.0 1 3.0 2 4.0 3 5.0 4 6.0 5 NaN In [25]: df.shift(1) Out[25]: a 0 NaN 1 1.0 2 2.0 3 3.0 4 4.0 5 5.0
ふーん。
横にもずらせる。
In [26]: df = pd.DataFrame([[1,2,3],[4,5,6]]) In [27]: df Out[27]: 0 1 2 0 1 2 3 1 4 5 6 In [28]: df.shift(-1,axis=1) Out[28]: 0 1 2 0 2.0 3.0 NaN 1 5.0 6.0 NaN In [29]: df.shift(-1,axis=0) Out[29]: 0 1 2 0 4.0 5.0 6.0 1 NaN NaN NaN
なるへそ。
シフトshift
のマニュアル。
<Python, numpy> 無限大
知ってましたか?
python
で無限大は、np.inf
か、float('inf')
で表現するらしいっす。
In [1]: float('inf') Out[1]: inf In [2]: float('inf') == 0 Out[2]: False In [3]: float('inf') < 1 Out[3]: False In [4]: float('inf') > 1 Out[4]: True In [5]: import numpy as np In [6]: np.inf Out[6]: inf In [7]: float('inf') == np.inf Out[7]: True In [8]: np.inf < 50000000 * 500000000 Out[8]: False In [9]: -np.inf Out[9]: -inf In [10]: -np.inf < 0 Out[10]: True In [11]: type(np.inf) Out[11]: float In [12]: np.isinf(np.inf) Out[12]: True In [13]: np.isinf(float('inf')) Out[13]: True
numpy.isinf
のマニュアル。
numpy.isinf — NumPy v1.12 Manual
追加。
In [14]: 0 / np.inf Out[14]: 0.0 In [15]: np.inf / np.inf Out[15]: nan In [16]: 1 / np.inf Out[16]: 0.0 In [17]: np.inf - np.inf Out[17]: nan In [18]: 1 * np.inf Out[18]: inf In [19]: 0 * np.inf Out[19]: nan In [20]: np.inf * np.inf Out[20]: inf
<Python, pandas> 日経平均を読み込む。
日経平均N225
を読み込む。
pandas-datareader
を使う。
pandas-datareader — pandas-datareader 0.1 documentation
まずはインストール。
% conda install pandas-datareader Fetching package metadata ......... Solving package specifications: .......... Package plan for installation in environment C:\Anaconda3: The following packages will be downloaded: package | build ---------------------------|----------------- requests-file-1.4.1 | py35_0 6 KB pandas-datareader-0.2.1 | py35_0 49 KB ------------------------------------------------------------ Total: 55 KB The following NEW packages will be INSTALLED: pandas-datareader: 0.2.1-py35_0 requests-file: 1.4.1-py35_0 Proceed ([y]/n)? y Fetching packages ... requests-file- 100% |###############################| Time: 0:00:00 829.29 kB/s pandas-datarea 100% |###############################| Time: 0:00:00 1.85 MB/s Extracting packages ... [ COMPLETE ]|##################################################| 100% Linking packages ... [ COMPLETE ]|##################################################| 100%
で、使う。
In [1]: import pandas_datareader.data as web In [2]: import datetime In [3]: s = datetime.datetime(2010,1,1) In [4]: e = datetime.datetime(2013,1,1) In [5]: web.DataReader('F', 'yahoo', s, e) Out[5]: Open High Low Close Volume Adj Close Date 2010-01-04 10.17 10.28 10.05 10.28 60855800 8.554412 2010-01-05 10.45 11.24 10.40 10.96 215620200 9.120268 2010-01-06 11.21 11.46 11.13 11.37 200070600 9.461446 2010-01-07 11.46 11.69 11.32 11.66 130201700 9.702767 2010-01-08 11.67 11.74 11.46 11.69 130463000 9.727731 2010-01-11 11.90 12.14 11.78 12.11 170626200 10.077230 2010-01-12 11.98 12.03 11.72 11.87 162995900 9.877516 2010-01-13 11.91 11.93 11.47 11.68 154527100 9.719410 2010-01-14 11.65 11.86 11.51 11.76 116531200 9.785981 2010-01-15 11.74 11.76 11.55 11.60 96149800 9.652838 2010-01-19 11.51 11.83 11.46 11.75 65934000 9.777659 2010-01-20 11.68 11.69 11.50 11.51 71649500 9.577946 2010-01-21 11.53 11.62 11.01 11.18 121451400 9.303339 2010-01-22 11.01 11.12 10.41 10.52 161530100 8.754126 2010-01-25 10.73 11.10 10.61 11.03 121621500 9.178517 2010-01-26 11.17 11.46 11.07 11.19 108250500 9.311660 2010-01-27 11.57 11.62 11.22 11.55 105091600 9.611231 2010-01-28 11.90 11.95 11.27 11.41 203320000 9.494731 2010-01-29 11.60 11.61 10.70 10.84 159741200 9.020411 2010-02-01 11.14 11.18 10.93 11.12 82748200 9.253410 2010-02-02 11.26 11.52 11.19 11.39 119714900 9.478089 2010-02-03 11.49 11.66 11.42 11.64 90125500 9.686124 2010-02-04 11.49 11.53 11.00 11.06 129792200 9.203482 2010-02-05 10.97 11.11 10.49 10.91 181535200 9.078661 2010-02-08 11.09 11.32 10.88 10.97 92031400 9.128589 2010-02-09 11.18 11.22 11.02 11.15 83207100 9.278374 2010-02-10 11.12 11.14 10.90 10.94 73395600 9.103625 2010-02-11 11.00 11.19 10.88 11.18 65116200 9.303339 2010-02-12 10.92 11.18 10.85 11.12 69465400 9.253410 2010-02-16 11.21 11.38 11.11 11.32 62537500 9.419838 ... ... ... ... ... ... ... 2012-11-16 10.58 10.64 10.38 10.50 45346200 8.900635 2012-11-19 10.65 10.90 10.65 10.83 39359100 9.180370 2012-11-20 10.85 11.02 10.76 10.85 34739800 9.197323 2012-11-21 10.84 11.00 10.80 10.92 21181700 9.256661 2012-11-23 10.98 11.10 10.96 11.10 16032200 9.409243 2012-11-26 11.05 11.14 10.97 11.11 26831700 9.417720 2012-11-27 11.10 11.27 11.10 11.10 37610000 9.409243 2012-11-28 11.05 11.26 10.98 11.25 38496900 9.536395 2012-11-29 11.32 11.53 11.32 11.53 57289300 9.773745 2012-11-30 11.52 11.60 11.33 11.45 41329600 9.705931 2012-12-03 11.56 11.70 11.40 11.41 47746300 9.672024 2012-12-04 11.40 11.44 11.23 11.31 37760200 9.587256 2012-12-05 11.32 11.40 11.18 11.31 33152400 9.587256 2012-12-06 11.26 11.31 11.19 11.24 31065800 9.527918 2012-12-07 11.27 11.50 11.26 11.48 38404500 9.731361 2012-12-10 11.41 11.53 11.41 11.47 26025200 9.722885 2012-12-11 11.51 11.58 11.40 11.49 36326900 9.739838 2012-12-12 11.52 11.56 11.43 11.47 31099900 9.722885 2012-12-13 11.46 11.50 11.21 11.27 35443200 9.553349 2012-12-14 11.27 11.27 11.03 11.10 36933500 9.409243 2012-12-17 11.16 11.41 11.14 11.39 46983300 9.655070 2012-12-18 11.48 11.68 11.40 11.67 61810400 9.892420 2012-12-19 11.79 11.85 11.62 11.73 54884700 9.943281 2012-12-20 11.74 11.80 11.58 11.77 47750100 9.977189 2012-12-21 11.55 11.86 11.47 11.86 94489300 10.053479 2012-12-24 11.67 12.40 11.67 12.40 91734900 10.511226 2012-12-26 12.31 12.79 12.31 12.79 140331900 10.841821 2012-12-27 12.79 12.81 12.36 12.76 108315100 10.816391 2012-12-28 12.55 12.88 12.52 12.87 95668600 10.909636 2012-12-31 12.88 13.08 12.76 12.95 106908900 10.977450 [754 rows x 6 columns]
なるへそ。
で、日経平均。
In [9]: from pandas_datareader.data import get_data_yahoo In [11]: get_data_yahoo(symbols='^N225', start=s) Out[11]: Open High Low Close Volume \ Date 2010-01-04 10609.339844 10694.490234 10608.139648 10654.790039 104400 2010-01-05 10719.440430 10791.040039 10655.570312 10681.830078 166200 2010-01-06 10709.549805 10768.610352 10661.169922 10731.450195 181800 2010-01-07 10742.750000 10774.000000 10636.669922 10681.660156 182600 2010-01-08 10743.299805 10816.450195 10677.559570 10798.320312 211800 2010-01-12 10770.349609 10905.389648 10763.679688 10879.139648 192800 2010-01-13 10795.480469 10866.620117 10729.860352 10735.030273 250000 2010-01-14 10778.070312 10909.940430 10774.250000 10907.679688 267400 2010-01-15 10917.410156 10982.099609 10878.830078 10982.099609 253000 2010-01-18 10887.610352 10895.099609 10781.030273 10855.080078 186600 2010-01-19 10866.830078 10866.830078 10749.469727 10764.900391 174600 2010-01-20 10834.910156 10860.929688 10724.570312 10737.519531 146400 2010-01-21 10704.790039 10886.639648 10649.839844 10868.410156 176800 2010-01-22 10740.209961 10768.070312 10528.330078 10590.549805 187400 2010-01-25 10478.309570 10557.639648 10414.580078 10512.690430 139800 2010-01-26 10506.150391 10566.490234 10324.980469 10325.280273 175400 2010-01-27 10344.070312 10373.820312 10252.080078 10252.080078 139400 2010-01-28 10309.730469 10462.700195 10296.980469 10414.290039 168000 2010-01-29 10308.049805 10324.370117 10198.040039 10198.040039 156800 2010-02-01 10212.360352 10224.830078 10129.910156 10205.019531 162000 2010-02-02 10310.980469 10396.480469 10287.740234 10371.089844 142000 2010-02-03 10428.120117 10436.519531 10356.030273 10404.330078 154200 2010-02-04 10434.519531 10438.410156 10279.570312 10355.980469 164600 2010-02-05 10162.339844 10166.299805 10036.330078 10057.089844 172000 2010-02-08 10007.469727 10063.530273 9942.049805 9951.820312 137400 2010-02-09 9876.610352 9956.790039 9867.389648 9932.900391 135000 2010-02-10 10024.259766 10049.870117 9963.990234 9963.990234 127000 2010-02-12 10085.349609 10099.459961 10014.500000 10092.190430 136000 2010-02-15 10097.820312 10119.469727 10012.530273 10013.299805 97400 2010-02-16 10044.530273 10062.269531 10019.429688 10034.250000 87000 ... ... ... ... ... ... 2016-11-10 16562.859375 17393.820312 1560.660034 17344.419922 222500 2016-11-11 17526.609375 17621.730469 17333.490234 17374.789062 241800 2016-11-14 17467.490234 17697.330078 17455.779297 17672.619141 0 2016-11-15 17668.150391 17668.150391 17668.150391 17668.150391 0 2016-11-16 17832.509766 17886.439453 17807.470703 17862.210938 194300 2016-11-17 17766.609375 17884.060547 17764.080078 17862.630859 0 2016-11-18 18024.210938 18043.720703 17967.410156 17967.410156 161800 2016-11-21 18038.089844 18129.029297 18007.789062 18106.019531 0 2016-11-22 18091.050781 18175.630859 18050.550781 18162.939453 118200 2016-11-24 18329.779297 18382.720703 18310.310547 18333.410156 144900 2016-11-25 18387.589844 18482.939453 18288.500000 18381.220703 160000 2016-11-28 18302.580078 18374.929688 18222.820312 18356.890625 0 2016-11-29 18263.630859 18327.509766 18258.820312 18307.039062 121800 2016-11-30 18356.029297 18370.310547 18280.660156 18308.480469 0 2016-12-01 18535.240234 18746.279297 18469.269531 18513.119141 182400 2016-12-02 18435.550781 18469.679688 18315.380859 18426.080078 191300 2016-12-05 18349.919922 18365.740234 18227.390625 18274.990234 130500 2016-12-06 18457.199219 18484.800781 18318.990234 18360.539062 154000 2016-12-07 18434.539062 18502.470703 18410.880859 18496.689453 151700 2016-12-08 18674.189453 18765.470703 18614.009766 18765.470703 0 2016-12-09 18839.980469 19042.480469 18821.410156 18996.369141 212300 2016-12-12 19183.820312 19280.929688 19054.000000 19155.029297 0 2016-12-13 19120.759766 19253.509766 19060.720703 19250.519531 0 2016-12-14 19270.009766 19284.279297 19184.750000 19253.609375 0 2016-12-15 19327.699219 19436.900391 19192.599609 19273.789062 0 2016-12-16 19438.390625 19439.970703 19360.359375 19401.150391 148700 2016-12-19 19345.839844 19399.259766 19307.140625 19391.599609 106000 2016-12-20 19367.839844 19511.199219 19356.810547 19494.529297 119600 2016-12-21 19547.279297 19592.900391 19375.189453 19444.490234 0 2016-12-22 19396.849609 19427.669922 19327.509766 19427.669922 113500 Adj Close Date 2010-01-04 10654.790039 2010-01-05 10681.830078 2010-01-06 10731.450195 2010-01-07 10681.660156 2010-01-08 10798.320312 2010-01-12 10879.139648 2010-01-13 10735.030273 2010-01-14 10907.679688 2010-01-15 10982.099609 2010-01-18 10855.080078 2010-01-19 10764.900391 2010-01-20 10737.519531 2010-01-21 10868.410156 2010-01-22 10590.549805 2010-01-25 10512.690430 2010-01-26 10325.280273 2010-01-27 10252.080078 2010-01-28 10414.290039 2010-01-29 10198.040039 2010-02-01 10205.019531 2010-02-02 10371.089844 2010-02-03 10404.330078 2010-02-04 10355.980469 2010-02-05 10057.089844 2010-02-08 9951.820312 2010-02-09 9932.900391 2010-02-10 9963.990234 2010-02-12 10092.190430 2010-02-15 10013.299805 2010-02-16 10034.250000 ... ... 2016-11-10 17344.419922 2016-11-11 17374.789062 2016-11-14 17672.619141 2016-11-15 17668.150391 2016-11-16 17862.210938 2016-11-17 17862.630859 2016-11-18 17967.410156 2016-11-21 18106.019531 2016-11-22 18162.939453 2016-11-24 18333.410156 2016-11-25 18381.220703 2016-11-28 18356.890625 2016-11-29 18307.039062 2016-11-30 18308.480469 2016-12-01 18513.119141 2016-12-02 18426.080078 2016-12-05 18274.990234 2016-12-06 18360.539062 2016-12-07 18496.689453 2016-12-08 18765.470703 2016-12-09 18996.369141 2016-12-12 19155.029297 2016-12-13 19250.519531 2016-12-14 19253.609375 2016-12-15 19273.789062 2016-12-16 19401.150391 2016-12-19 19391.599609 2016-12-20 19494.529297 2016-12-21 19444.490234 2016-12-22 19427.669922 [1726 rows x 6 columns]
なるほど。
グラフにしてみる。
In [12]: df = get_data_yahoo(symbols='^N225', start=s) In [16]: df['Close'].plot() Out[16]: <matplotlib.axes._subplots.AxesSubplot at 0x270c6670358> In [17]: import matplotlib.pyplot as plt In [18]: plt.show()
いいねー。