The version of python used this time is as follows.
python --version
Python 3.5.1 :: Anaconda 4.0.0 (x86_64)
Install the one you use this time.
pip install quandl
pip install pandas-datareader
pip install jsm
pip install stockstats
First of all, in conclusion, there are around Quandl, pandas_datareader, jsm, and it seems good to use Quandl, pandas_datareader. (Please tell me if there is anything else. Likely.)
Quandl
The official documentation is here. I will try to get the stock price of Apple for the time being.
import quandl
d = quandl.get("WIKI/AAPL")
d.columns
# Index(['Open', 'High', 'Low', 'Close', 'Volume', 'Ex-Dividend', 'Split Ratio',
# 'Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume'],
# dtype='object')
d.shape
# (9162, 12)
There is a mysterious word WIKI here, which is the source of the data and is written in Konohen.
The data looks like the date and time.
Also, in some past articles, it was ʻimport Quandl`, but it seems that it became lowercase in the middle.
pandas_datareader The official document.
import pandas_datareader as pdr
d = web.DataReader("AAPL", 'google')
# Parameters
# ----------
# name : str or list of strs
# the name of the dataset. Some data sources (yahoo, google, fred) will
# accept a list of names.
# data_source: {str, None}
# the data source ("yahoo", "yahoo-actions", "yahoo-dividends",
# "google", "fred", "ff", or "edgar-index")
#The following is omitted
d.shape
(1831, 5)
d.columns
# Index(['Open', 'High', 'Low', 'Close', 'Volume'], dtype='object')
Apple is specified as the first argument, and google is specified as the data source.
The data is also a daily pattern.
The acquisition period can be specified as follows.
import pandas_datareader as pdr
import datetime
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2017, 4, 12)
d = pdr.DataReader("AAPL", 'google', start, end)
d.shape
# (574, 5)
jsm It says that jsm has stopped development in 2015 and is scraping data from yahoo. yahoo is [here](https://www.yahoo-help.jp/app/answers/detail/p/546/a_id/93575/~/yahoo%21%E3%83%95%E3%82%A1% E3% 82% A4% E3% 83% 8A% E3% 83% B3% E3% 82% B9% E6% 8E% B2% E8% BC% 89% E6% 83% 85% E5% A0% B1% E3% 81% AE% E8% 87% AA% E5% 8B% 95% E5% 8F% 96% E5% BE% 97% EF% BC% 88% E3% 82% B9% E3% 82% AF% E3% 83% AC% E3% 82% A4% E3% 83% 94% E3% 83% B3% E3% 82% B0% EF% BC% 89% E3% 81% AF% E7% A6% 81% E6% AD% A2% E3% 81% 97% E3% 81% A6% E3% 81% 84% E3% 81% BE% E3% 81% 99) says scraping is prohibited, and I think it's better not to use it. ..
stockstats There is stockstats as a package to tamper with the acquired financial data.
import stockstats
import quandl
d = quandl.get("WIKI/AAPL")
d.shape
# (9162, 12)
type(d)
# pandas.core.frame.DataFrame
stock = stockstats.StockDataFrame.retype(d)
type(stock)
# stockstats.StockDataFrame
stockstats replaces the type stockstats.StockDataFrame, which is an extension of the pandas dataframe with the retype method. By doing this, for example, MACD calculation, etc.
stock['macd']
# Date
# 1980-12-12 0.000000
# 1980-12-15 -0.033654
# 1980-12-16 -0.104902
# 1980-12-17 -0.112351
It's easy like this.
If you look at the code inside, the key of the data frame passed to retype is made lowercase by the lower function, so it seems that it will absorb the difference between uppercase and lowercase letters, but if there is no data you want to calculate, it will be a key error. .. Therefore, when using variables that are not normally used, or when there are few variables, it may be better to write as pandas.
Recommended Posts