Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

At the same time, I participated in the “Digitalstar Project” to win the creative gift package and challenge the creative incentive money

I am my cousin, a lifelong Python learner, data analysis enthusiast, and cat lover

This article will share how to use Python to capture and visualize stock and fund data.

First, basic preparation

  • Environment: Python 3.7+
  • Third party modules need to be installed: mplFinance, akshare
  • Run the editor: Jupyter Notebook

Second, use your head

2.1 Environment Preparation

First, you need to have the Python environment installed and Python development tools installed on your computer.

If you haven’t installed it yet, check out the following article:

If you only use Python to process data, crawlers, data analysis, automatic scripts, and machine learning, you are advised to use the Python basic environment and Jupyter. For details, see Windows/Mac Installation and Using The Python Environment and Jupyter Notebook

2.2 Module Installation

This experiment is conducted on Jupyter Notebook. By default, you have installed the Basic Python environment and Jupyter. If not, please refer to the environment installation section above.

First open the terminal and type:

jupyter notebook
Copy the code

The jupyter server is automatically connected to the jupyter server and a new Python file is created on the web page.Rename files to make them easily distinguishable:

To install mPLFinance (financial data visualization based on Matplotlibe and PANDAS) and Akshare (financial data capture module), enter the following commands in the code block and run:

! pip3 install mplfinance --user ! pip3 install akshare --userCopy the code

2.3 Basic introduction and use of the module

Mplfinance is a matplotlib utility for financial data visualization and analysis, developed based on Matplotlibe and well integrated with pandas’ DataFrame data.

Project address: github.com/matplotlib/…

Basic usage:

import mplfinance as mpf

mpf.plot(data)
Copy the code

Where data is a DataFrame data type, it must contain four columns: Open, High, Low and Close, and has a date and time index (type: DatetimeIndex).

Next, let’s create a dummy data to see the effect:

import mplfinance as mpf
import pandas as pd

Create date index
index_date = pd.DatetimeIndex(['20210906'.'20210907'.'20210908'.'20210909'])
Create whatever data you want
data = pd.DataFrame({
    'Open': [1.2.3.4].'High': [2.3.4.5].'Low' : [0.5.1.2.2].'Close': [2.3.4.3]
}, index=index_date)
Call the plot function to plot
mpf.plot(data)
Copy the code

We see that we have now simply drawn a simple OHLC line based on the simulation data.

Type parameter is used to make the drawing more exciting:

mpf.plot(data,type='candle')
Copy the code

mpf.plot(data,type='line')
Copy the code

The value of type can also be renko or PNF.

Mav parameters can be added to the moving average to better see the trend.

mpf.plot(data, type='candle', mav=2)
Copy the code

You can also add multiple moving averages:

mpf.plot(data, type='candle', mav=(2.3))
Copy the code

You are also welcome to go to Github to learn by yourself and communicate with me and other members of the Simple Python learning group.

Akshare is an elegant and simple Python financial data interface library that allows you to easily access financial data.

Project address: github.com/jindaxiang/… Basic usage:

  • Get index data
import akshare as ak
Obtain daily change data of Sse Index OHLC
sz_index = ak.stock_zh_index_daily(symbol="sh000001")
Copy the code

In this way, you can get all the historical data of the Shanghai Composite Index

  • Obtain A share data
# To obtain the daily change data of Moutai stock, the method of pre-weighting is adopted. (For more knowledge on the calculation of post-weighting, you can find relevant information online.)
stock_zh_mt_hist_df = ak.stock_zh_a_hist(symbol="600519", period="daily", start_date="20170301", end_date='20210913', adjust="qfq")
Copy the code

  • Obtain Hong Kong stock data
# Obtain the historical data of Tencent stock in Hong Kong
stock_hk_tx_hist_df = ak.stock_hk_hist(symbol="00700", start_date="20170301", end_date="20210913", adjust="qfq")
Copy the code

  • Get us stock data
# Get the historical stock data of Apple inc
stock_us_apple_hist_df = ak.stock_us_hist(symbol='105.AAPL', start_date="20100101", end_date="20210913", adjust="qfq")
Copy the code

  • Access to Fund data
Obtain the net value data of Efonda Blue Chip from its issuance to the present
yfd_fund = ak.fund_em_open_fund_info(fund="005827", indicator="Net Unit Value Trend")
Copy the code

You are also welcome to go to Github to learn by yourself and communicate with me and other members of the Simple Python learning group.

To,

According to the above method, we obtained the data of Shanghai Composite Index, selected the data from 2020-01-01 to the present for visualization, and then carried out subdivision visualization, and selected different moving averages.

  • 2020-01-01'the 2021-09-13.mav=(200, 300, 350)
import akshare as ak
import mplfinance as mpf
import pandas as pd

Obtain daily change data of Sse Index OHLC
sz_index = ak.stock_zh_index_daily(symbol="sh000001")
data1 = sz_index.loc['2020-01-01 00:00:00 + 00:00':'2021-09-13 00:00:00 + 00:00']
mpf.plot(data1, type='candle', mav=(200.300.350), volume=True)
Copy the code

  • 2021-01-01'the 2021-09-13.mav=(30, 60, 120)
data2 = sz_index.loc['2021-01-01 00:00:00 + 00:00':'2021-09-13 00:00:00 + 00:00']
mpf.plot(data2, type='candle', mav=(30.60.120), volume=True)
Copy the code

  • 2021-04-01'the 2021-09-13.mav=(5, 10, 20)
data3 = sz_index.loc['2021-04-01 00:00:00 + 00:00':'2021-09-13 00:00:00 + 00:00']
mpf.plot(data3, type='candle', mav=(5.10.20), volume=True)
Copy the code

Today is here, if you think the article is good, remember to point a praise ha ~ financial analysis & visualization series will continue, in this paper, all the source code is still in finishing, you can add first class WeChat, note: financial analysis, to join financial analysis & visualization communication group, tidy up the code behind will be announced within the first group of access methods.

Like + message + forward, is the biggest support for me ~