Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to display Stock data with Line Chart by Python

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Python how to achieve line chart to display stock data". In daily operation, I believe that many people have doubts about how to achieve line chart display of stock data in Python. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve line chart display of stock data in Python". Next, please follow the editor to study!

Steps:

Prepare data

Visualization data, review data

Processing data

Determine the order according to ACF and PACF

Fitting ARIMA model

Forecast

#-*-coding: utf-8-*-"" Spyder EditorThis is a temporary script file. "" import pandas as pdimport pandas_datareaderimport datetimeimport matplotlib.pylab as pltfrom matplotlib.pylab import stylefrom statsmodels.tsa.arima_model import ARIMAfrom statsmodels.graphics.tsaplots import plot_acf Plot_pacfstyle.use ('ggplot') # set the theme style of picture display # solve the problem of matplotlib displaying Chinese plt.rcParams [' axes.unicode_minus'] = ['SimHei'] # specify the default font plt.rcParams [' axes.unicode_minus'] = False # solve the problem that the saved image is a minus sign-'display as a square def run_main (): "main function"# 1. Prepare data # specify stock analysis start date start_date = datetime.datetime (2009, 1) # specify stock analysis closing date end_date = datetime.datetime (2019, 4, 1) # stock symbol stock_code = '600519.SS' # Shanghai Kweichow Moutai stock_df = pandas_datareader.data.DataReader (stock_code,' yahoo', start_date End_date) # Preview data print (stock_df.head ()) # 2. Visual data plt.plot (stock_df ['Close']) plt.title (' daily closing price') plt.show () # resample on a weekly basis stock_s = stock_df ['Close'] .resample (' Wmermon'). Mean () stock_train = stock_s ['2014 stock stock_train 2018'] plt.plot (stock_train) plt.title ('stock weekly closing price Mean') plt.show () # Analysis ACF acf = plot_acf (stock_train Lags=20) plt.title ("ACF of stock index") acf.show () # Analysis PACF pacf = plot_pacf (stock_train, lags=20) plt.title ("PACF of stock index") pacf.show () # 3. Processing data, smoothing data # here is just a simple difference There are other methods to stabilize time series stock_diff = stock_train.diff () diff = stock_diff.dropna () print (diff.head ()) print (diff.dtypes) plt.figure () plt.plot (diff) plt.title ('first order difference') plt.show () acf_diff = plot_acf (diff) Lags=20) plt.title ("ACF of first order difference") acf_diff.show () pacf_diff = plot_pacf (diff, lags=20) plt.title ("PACF of first order difference") pacf_diff.show () # 4. Determine the order according to ACF and PACF and establish the model model = ARIMA (stock_train, order= (1,1,1), freq='W-MON') # fitting model arima_result = model.fit () print (arima_result.summary ()) # 5. Forecast pred_vals = arima_result.predict (start=str ('2019-01'), end=str ('2019-03'), dynamic=False, typ='levels') print (pred_vals) # 6. Visual prediction result stock_forcast = pd.concat ([stock_s, pred_vals], axis=1, keys= ['original',' predicted']) plt.figure () plt.plot (stock_forcast) plt.title ('true vs predicted value') plt.savefig ('. / stock_pred.png', format='png') plt.show () if _ name__ = ='_ main__': run_main () so far On the "Python how to achieve the line chart to display stock data" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

  • What if the log4j configuration file is overwritten by the configuration file in jar?

    You can specify the configuration file to load the log4j in web.xml. The configuration is as follows:

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report