Use Python to generate stock candlestick charts

introduction

        In stock analysis, it is often necessary to use K-line charts to analyze stock price fluctuations and trading volume. This article will introduce how to use Python and pyecharts library to generate stock candlestick charts and save them as HTML files.

prepare data

        First, we need to prepare the stock data. Here, we use  stock.csv sample data, which includes five columns of date, open price, close price, low price, and high price. If you don't have a corresponding CSV file, you can create a similar one yourself. The data format is as follows:

date,open,close,high,low
2022-01-03,40.0,41.0,41.5,39.5
2022-01-04,41.2,42.0,42.5,41.2
2022-01-05,42.5,42.6,42.9,42.0
2022-01-06,42.4,41.8,42.5,41.0
2022-01-07,42.0,42.9,43.0,41.8
2022-01-10,43.0,40.5,43.0,40.5
2022-01-11,40.5,39.5,41.0,39.5
2022-01-12,39.5,40.6,40.8,39.5
2022-01-13,40.6,41.2,41.5,40.0
2022-01-14,41.5,40.9,41.7,40.5

Draw candlestick chart

        Next, we use the pandas library to read  stock.csv the data and keep the required columns and convert them into lists. Then, we format the data into a data structure suitable for candlestick charts. Finally, we use the pyecharts library to create a candlestick chart object, set options such as x-axis and y-axis, add data and display the chart.

import pandas as pd
from pyecharts.charts 

Guess you like

Origin blog.csdn.net/weixin_43263566/article/details/131155330