Python study notes: use mplfinance's plot to draw K-line graphs

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

Insert picture description here

2. A warning appears when importing the mpl_finance module

import mpl_finance as mpf

Insert picture description here

  • A warning message pops up: it mpl_financeis abandoned, please use mplfinance(no dash, no underscore).

3. Install the mplfinance module

pip install mplfinance

Insert picture description here

4. Install the pandas.datareader module

pip install pandas.datareader

Insert picture description here

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')

Insert picture description here

  • 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

Insert picture description here

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'
    Insert picture description here
    Insert picture description here

4. Add drawing moving average

  • Keyword parameter mav=(2, 5, 10), multiple moving averages use tuples, only one moving average is drawn, mav=10
    Insert picture description here
    Insert picture description here

5. Draw volume

  • Keyword parameter volume=True
    Insert picture description here

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
    Insert picture description here
  • 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

Insert picture description here

3. Read local data and draw K-line diagram

1. View local data

Insert picture description here

  • 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')

Insert picture description here

3. Change the index type to DatetimeIndex

data.index = pd.DatetimeIndex(data.index)

Insert picture description here

4. Draw a K-line chart, excluding non-trading hours

mpf.plot(data, type=‘candle’, mav=(2, 5, 10), volume=True)

Insert picture description here

5. Draw a candlestick chart to show non-trading hours

mpf.plot(data, type=‘candle’, mav=(2, 5, 10), volume=True, show_nontrading=True)

Insert picture description here

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)

Insert picture description here

  • Export data to ibm_data.csv
    Insert picture description here
  • Open ibm_data.csv and add a column of MidValue
    Insert picture description here
  • 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)

Insert picture description here

  • 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'])

Insert picture description here

3. Draw a K-line graph and add the curve corresponding to the MidValue column

mpf.plot(data, type='candle', addplot=add_plot)

Insert picture description here

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)

Insert picture description here

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)

Insert picture description here

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:

2. Access Google Finance data

  • Access to IBM stock data for September 2020
    Insert picture description here
  • Draw the K-line chart of IBM's stock in September 2020
    Insert picture description here
  • Access to IBM stock data from January 1 to October 22, 2020
    Insert picture description here
  • Draw IBM stock K-line chart from January 1 to October 22, 2020
    Insert picture description here

3. Access to the definition of Nasdaq trading symbols

Insert picture description here

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

Insert picture description here

# -*- 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
    Insert picture description here

Guess you like

Origin blog.csdn.net/howard2005/article/details/109227653