“This is the 19th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
The sense of heaven and earth, and all things are born; The saint is touching, and the world is peaceful.
preface
The previous article has described how to draw graphs based on K-line data and create price support modeling. Today, combined with the powerful financial computing function of Talib class library, I will introduce common technical index calculation methods and finally use Matplotlib to draw.
talib api
Before drawing, it is necessary to calculate the data first. K-line data has been used to calculate the moving average before. In this paper, K-line data is used to calculate MACD, KDJ, RSI and BOLL data.
K line graph
Mplfinance is used to draw the K-chart of stock data:
macd
Macd is commonly used in technical analysis of stocks, which has reference significance when buying and selling stocks. The data passed in by MACD is the closing price data of the stock. The parameter data of the fast, medium and slow lines are 9, 12 and 26 days respectively.
# calculate macD data, # talib.MACD(close, fastPeriod =12, slowPeriod =26, signalPeriod =9) data[" MACD "], data["macd_signal"], data["macd_hist"] = talib.MACD(data['Close'], fastperiod=12, slowperiod=26,signalperiod=9)Copy the code
kdj
KDJ is also a common measure of technical analysis of stocks, but the REACTION of KDJ is more sensitive than macD. The data passed in by KDJ is also the closing price data, and its parameter data are 9, 3 and 3 respectively.
# KDJ = STOCH data['slowk'] data['slowd'] = talib.STOCH( data['High'].values,data['Low'].values,data['Close'].values, Slowk_period =9,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0) J = (3*K)-(2*D) data['slowj'] = list(map(lambda x, y: 3 * x - 2 * y, data['slowk'], data['slowd']))Copy the code
Rsi and among
The input parameters of rSI and BOLL BolLING line are the closing price. The calculation period of the data of RSI and BOLL is 14 days, and the input period of Bolling line is 5, 2 and 2.
Data [" rSI "] = talib. Rsi (data["Close"], timeperiod=14) lower = talib.BBANDS(data["Close"], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)Copy the code
Data mapping
In the part of data drawing, it is divided into candle chart, trading volume data and technical analysis chart for K line. The data need to be merged into a chart for display, so the combined display of images is used, and ADD_AXES is used for the combined display:The code for drawing indicators is shown in the figure below, which can be compared by referring to the figure of stock software. Its shape and data indicators have a good fit.
The final display is as follows:
conclusion
Combined with talib and Matplotlib, this paper realizes the data calculation and graph display of stock trend chart, and learns the use of plot function and graph layout. In the following article, I will introduce how to use MACD and KDJ to select the stock of gold and dead forks. The relevant graphic display code has been uploaded to Github, welcome your continuous attention.