Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 07:22:53 10月04日 Update

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.

Tags: Data date volume horizontal axis moving average movement code quotation line graph stock content picture title column method source code number price location size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft MySQL MariaDB Apple