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

The method of drawing moving average by Python and how to write the source code

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about the Python method of drawing mobile moving averages and how to write the source code. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Obtain data

We obtain historical data and sentiment data of the stock market from the permanent financial data community. We obtain the daily market data of Hang Seng Electronics (600570.SH) from March 1, 2021 to June 1, 2021, and do simple processing, the code and implementation results are as follows.

Load the function package required for fetching and drawing

Import pandas as pdimport datetimefrom hs_udata import set_token,stock_quote_dailyfrom mpl_finance import candlestick_ohlcimport matplotlib as mplimport matplotlib.pyplot as pltimport matplotlib.dates as mdatesmpl.rcParams ['font.sans-serif'] = [' SimHei'] # specify the default font mpl.rcParams ['axes.unicode_minus'] = False # solve the problem that the saved image is a negative sign'-'displayed as a square def GetData (stock_code,start) End): # stock_code: stock symbol # start: start date # end: end date date_start=datetime.datetime.strptime (start,'%Y-%m-%d') date_end = datetime.datetime.strptime (end,'%Y-%m-%d') data = pd.DataFrame ([]) while date_start= row ['open_price'] else 0 Axis=1) # calculate the color corresponding to the volume bar chart Make it consistent with the color of the K line data_volume.Date = ohlc.Date # draw volume ax2 = fig.add_subplot (grid [8ax2 10j0j12]) # set volume graphic size ax2.bar (data_volume.query ('color==1') [' Date'], data_volume.query ('color==1') [' business_amount'] Color='r') # draw red bar chart ax2.bar (data_volume.query ('color==0') [' Date'], data_volume.query ('color==0') [' business_amount'], color='g') # draw green bar chart plt.xticks (rotation=30) plt.xlabel ('date') Fontsize = 14) # set horizontal axis title # modify horizontal axis date tag date_list = ohlc.index.tolist () # get date list xticks_len = round (len (date_list) / (len (ax2.get_xticks ()-1)) # get the interval of default horizontal axis label xticks_num = range (0Len (date_list)) Xticks_len) # generate a list of horizontal axis callout positions xticks_str = list (map (lambda XRV date _ list [int (x)]) Xticks_num)) # generate a list of dates being marked ax2.set_xticks (xticks_num) # set horizontal axis location ax2.set_xticklabels (xticks_str) # set horizontal axis marked date plt.show () finish reading the above Do you have any further understanding of Python's method of drawing moving averages and how to write the source code? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report