Get financial data with python (then a little tinkering)

Check version

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

Acquisition of financial data

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. ..

Play with financial data

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

Get financial data with python (then a little tinkering)
Get Youtube data with python
[Python] Get economic data with DataReader
[Note] Get data from PostgreSQL with Python
Add a Python data source with Redash
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
A story stuck with handling Python binary data
Folium: Visualize data on a map with Python
A layman wants to get started with Python
Extract data from a web page with Python
Get a quick Python development environment with Poetry
Data analysis with python 2
Get date with python
Csv tinkering with python
Data analysis with Python
[Introduction to Python] How to get the index of data with a for statement
Python Ver. To introduce WebPay with a little code.
A server that echoes data POSTed with flask / python
Get data from database via ODBC with Python (Access)
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
Get country code with python
Sample data created with python
Make a fortune with Python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Create a directory with python
A little stuck with chainer
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
Read json data with python
Get a list of purchased DMM eBooks with Python + Selenium
Get rid of dirty data with Python and regular expressions
Get a clean Python development environment with pyenv + pipx + Poetry
Get data from analytics API with Google API Client for python
Get additional data to LDAP with python (Writer and Reader)
[Introduction to Python] How to get data with the listdir function
Get Leap Motion data in Python.
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
A python graphing manual with Matplotlib.
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get data from Quandl in Python
Get reviews with python googlemap api
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
[Python] A memo that I tried to get started with asyncio
Get the weather with Python requests
Create a virtual environment with Python!
Get web screen capture with python
Get the weather with Python requests 2
I made a fortune with Python.
Building a virtual environment with Python 3
How to get started with Python
Solve ABC168 A ~ C with Python