Article Directory
- 1. From mpl-finance to mplfinance
- 2. Obtain online data and draw K-line diagram
- 3. Read local data and draw K-line diagram
- Fourth, set the addplot parameter of the plot() function
-
- 1. Read the data of IBM from October 1 to October 23, 2020
- 2. Use the make_addplot() function to define addplot parameter values
- 3. Draw a K-line graph and add the curve corresponding to the MidValue column
- 4. Draw a candlestick chart and add the curves corresponding to High, MidValue and Low
- 5. Add a mark on the result graph
- Five, pandas_datareader supplementary instructions
mplfinance is a visual analysis module dedicated to financial data and a utility module program based on matplotlib.
1. From mpl-finance to mplfinance
1. Install the mpl-finance module
pip install mpl-finance
2. A warning appears when importing the mpl_finance module
import mpl_finance as mpf
- A warning message pops up: it
mpl_finance
is abandoned, please usemplfinance
(no dash, no underscore).
3. Install the mplfinance module
pip install mplfinance
4. Install the pandas.datareader module
pip install pandas.datareader
2. Obtain online data and draw K-line diagram
1. Use datareader to read online stock market data
import pandas_datareader as pdr
dir(pdr)
结果:
['DataReader',
'Options',
'__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'__version__',
'_utils',
'_version',
'av',
'bankofcanada',
'base',
'compat',
'data',
'econdb',
'enigma',
'eurostat',
'exceptions',
'famafrench',
'fred',
'get_components_yahoo',
'get_dailysummary_iex',
'get_data_alphavantage',
'get_data_enigma',
'get_data_famafrench',
'get_data_fred',
'get_data_moex',
'get_data_quandl',
'get_data_stooq',
'get_data_tiingo',
'get_data_yahoo',
'get_data_yahoo_actions',
'get_iex_book',
'get_iex_data_tiingo',
'get_iex_symbols',
'get_last_iex',
'get_markets_iex',
'get_nasdaq_symbols',
'get_quote_yahoo',
'get_recent_iex',
'get_records_iex',
'get_summary_iex',
'get_tops_iex',
'iex',
'io',
'moex',
'nasdaq_trader',
'naver',
'oecd',
'quandl',
'stooq',
'tiingo',
'yahoo']
import pandas_datareader as pdr
data = pdr.get_data_yahoo('INTC', '2020/9/1', '2020/10/1')
- If you want to access IBM's stock data, change the first parameter to'IBM'
data = pdr.get_data_yahoo(‘IBM’, ‘2020/9/1’, ‘2020/10/1’)
2. Use mplfinace's plot() to draw a K-line graph
3. Modify the plot drawing type
- Modify the drawing type through the parameter type. The default is ohlc, which can be changed to type='candle' or type='line'
4. Add drawing moving average
- Keyword parameter mav=(2, 5, 10), multiple moving averages use tuples, only one moving average is drawn, mav=10
5. Draw volume
- Keyword parameter volume=True
6. Automatically exclude non-blank trading days
- The keyword parameter show_nontrading, the default is False, set to True, you can see the time period of suspension
- Since the data read above does not have any suspension time period, the drawing is no different.
7. Draw a K-line chart of IBM stock in September 2020
3. Read local data and draw K-line diagram
1. View local data
- Explain that the parameter data of the plot() function must be pandas.DataFrame data type, and there are also requirements for the included columns. It must contain the data of'Open','High','Low' and'Close' (note that the first letter must be capitalized) , And the row index must be pandas.DatetimeIndex, the name of the row index must be'Date', and there is a column of'Volume' which is optional.
2. Read local data
import pandas as pd
import mplfinance as mpf
data = pd.read_csv('d:/python_work/202010/test2020.csv', index_col='Date')
3. Change the index type to DatetimeIndex
data.index = pd.DatetimeIndex(data.index)
4. Draw a K-line chart, excluding non-trading hours
mpf.plot(data, type=‘candle’, mav=(2, 5, 10), volume=True)
5. Draw a candlestick chart to show non-trading hours
mpf.plot(data, type=‘candle’, mav=(2, 5, 10), volume=True, show_nontrading=True)
Fourth, set the addplot parameter of the plot() function
1. Read the data of IBM from October 1 to October 23, 2020
import mplfinance as mpf
import pandas_datareader as pdr
data = pdr.get_data_yahoo('IBM', '2020/10/1', '2020/10/23')
data.head(5)
- Export data to ibm_data.csv
- Open ibm_data.csv and add a column of MidValue
- Read the local data file-ibm_data.csv
data = pd.read_csv('d:/python_work/202010/ibm_data.csv', index_col='Date')
data.index = pd.DatetimeIndex(data.index)
- Now, we need to add the newly added column to the candlestick chart-MidValue
2. Use the make_addplot() function to define addplot parameter values
add_plot = mpf.make_addplot(data['MidValue'])
3. Draw a K-line graph and add the curve corresponding to the MidValue column
mpf.plot(data, type='candle', addplot=add_plot)
4. Draw a candlestick chart and add the curves corresponding to High, MidValue and Low
add_plot = mpf.make_addplot(data[['High', 'MidValue', 'Low']])
mpf.plot(data, type='candle', addplot=add_plot)
5. Add a mark on the result graph
a_list = data.High.tolist()
b_list = data.Low.tolist()
add_plot = [
mpf.make_addplot(a_list, scatter=True, markersize=100, marker='v', color='y'),
mpf.make_addplot(b_list, scatter=True, markersize=100, marker='^', color='r'),
mpf.make_addplot(data['MidValue'])]
mpf.plot(data, type='candle', addplot=add_plot)
Five, pandas_datareader supplementary instructions
1. Accessible companies and institutions
pandas_datareader is a Python tool for remotely obtaining financial data. It can be used to easily obtain the data of the following companies and institutions:
- Yahoo Finance: Yahoo! Finance
- Google Finance: Google Finance
- Enigma public data search provider: Enigma
- St. Louis Federal Reserve Bank: St.Louis FED (FRED)
- Kenneth French's data library : Kenneth French's data library
- World Bank: World Bank
- OECD: OECD
- Eurostat: Eurostat
- Retired organizations managed by the US federal government: Thrift Savings Plan
- Nasdaq Trader symbol definitions : Nasdaq Trader symbol definitions
2. Access Google Finance data
- Access to IBM stock data for September 2020
- Draw the K-line chart of IBM's stock in September 2020
- Access to IBM stock data from January 1 to October 22, 2020
- Draw IBM stock K-line chart from January 1 to October 22, 2020
3. Access to the definition of Nasdaq trading symbols
4. Access to World Bank NY.GDP.PCAP.KD data
- Obtain NY.GDP.PCAP.KD data of the United States, China, and Japan in the last two decades
from pandas_datareader import wb
data = wb.download(indicator='NY.GDP.PCAP.KD', country=['US', 'CN', 'JP'], start=2001, end=2020)
data
Out[3]:
NY.GDP.PCAP.KD
country year
China 2020 NaN
2019 8254.300930
2018 7806.953095
2017 7346.611355
2016 6907.962011
2015 6500.281937
2014 6103.590270
2013 5710.587873
2012 5325.160106
2011 4961.234689
2010 4550.453596
2009 4132.902312
2008 3796.633363
2007 3480.152725
2006 3062.534905
2005 2732.165880
2004 2467.132843
2003 2253.929689
2002 2061.162284
2001 1901.407630
Japan 2020 NaN
2019 49187.833090
2018 48766.133663
2017 48510.609409
2016 47403.046912
2015 47102.580878
2014 46484.155267
2013 46249.209589
2012 45276.874335
2011 44538.726191
2010 44507.676386
2009 42724.760370
2008 45165.787919
2007 45687.273815
2006 44995.494492
2005 44393.626384
2004 43671.679974
2003 42744.011285
2002 42190.804873
2001 42239.184926
United States 2020 NaN
2019 55670.235709
2018 54659.198268
2017 53382.764823
2016 52555.518032
2015 52116.738813
2014 51028.824895
2013 50171.237133
2012 49603.253474
2011 48866.053277
2010 48467.515777
2009 47648.813250
2008 49319.478865
2007 49856.281491
2006 49405.767296
2005 48499.812376
2004 47287.593772
2003 45980.514585
2002 45087.367279
2001 44728.597475
- Find the mean of the data of three countries
data['NY.GDP.PCAP.KD'].groupby(level=0).mean()
Out[4]:
country
China 4702.903026
Japan 45359.972092
United States 49701.871926
Name: NY.GDP.PCAP.KD, dtype: float64
- Comparison of GDP per capita in the United States, China, and Japan in the past two decades
from pandas_datareader import wb
dat = wb.download(indicator='NY.GDP.PCAP.KD', country=['US', 'CN', 'JP'], start=2001, end=2020)
dat
Out[3]:
NY.GDP.PCAP.KD
country year
China 2020 NaN
2019 8254.300930
2018 7806.953095
2017 7346.611355
2016 6907.962011
2015 6500.281937
2014 6103.590270
2013 5710.587873
2012 5325.160106
2011 4961.234689
2010 4550.453596
2009 4132.902312
2008 3796.633363
2007 3480.152725
2006 3062.534905
2005 2732.165880
2004 2467.132843
2003 2253.929689
2002 2061.162284
2001 1901.407630
Japan 2020 NaN
2019 49187.833090
2018 48766.133663
2017 48510.609409
2016 47403.046912
2015 47102.580878
2014 46484.155267
2013 46249.209589
2012 45276.874335
2011 44538.726191
2010 44507.676386
2009 42724.760370
2008 45165.787919
2007 45687.273815
2006 44995.494492
2005 44393.626384
2004 43671.679974
2003 42744.011285
2002 42190.804873
2001 42239.184926
United States 2020 NaN
2019 55670.235709
2018 54659.198268
2017 53382.764823
2016 52555.518032
2015 52116.738813
2014 51028.824895
2013 50171.237133
2012 49603.253474
2011 48866.053277
2010 48467.515777
2009 47648.813250
2008 49319.478865
2007 49856.281491
2006 49405.767296
2005 48499.812376
2004 47287.593772
2003 45980.514585
2002 45087.367279
2001 44728.597475
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 23 12:13:00 2020
@author: howard
美国、中国、日本近二十年人均GDP对比图
"""
from pandas_datareader import wb
import matplotlib.pyplot as plt
dat = wb.download(indicator='NY.GDP.PCAP.KD', country=['US', 'CN', 'JP'], start=2001, end=2020)
dat2draw = dat.unstack(level=0)
plt.figure(figsize=(10,4))
plt.plot(dat2draw.iloc[:,0], 'r-', label="China")
plt.plot(dat2draw.iloc[:,1], 'b-*', label="Japan")
plt.plot(dat2draw.iloc[:,2], 'g--', label="USA")
plt.title("PER CAPITA GDP ($)", fontsize=20)
plt.legend()
- Run the program and view the results