Python Draw Candlestick Chart

Source: Internet
Author: User

#数据源: http://pan.baidu.com/s/1hs5Wn0w

#要求: Matlibplot Simple Application

#1. Chart the price trend of the day-line level from June 2017 to August based on the data.

#2. Overlay MA5, MA10, MA20 into the diagram

ImportPandas as PDImportNumPy as NPImportMatplotlib.pyplot as PltImportMatplotlib.finance as MPFImportdatetime fromMatplotlib.pylabImportdate2numplt.rcparams['Font.sans-serif'] = ['Simhei']plt.rcparams['Axes.unicode_minus'] =False#read data and remove excess dataData=pd.read_csv ('000001.sz.csv', encoding='GBK', index_col=0). Iloc[:-2,:4]#To adjust an index to a datetime formatdata.index=pd.to_datetime (Data.index)#aggregating minute data into daily datadata_open=data.loc[:,'Open Price (yuan)'].resample ('D'). First (). Dropna () Data_high=data.loc[:,'high Price (yuan)'].resample ('D'). Max (). Dropna () Data_low=data.loc[:,'lowest price (yuan)'].resample ('D'). Min (). Dropna () Data_close=data.loc[:,'Close Price (yuan)'].resample ('D'). Last (). Dropna ()#Merge Open, close, highest, lowest data, note data order, consistent with Candlestick_ochlNew_data=pd.concat ([Data_open,data_close,data_high,data_low],axis=1)#new_data=new_data.ix[' 2017-06 ': ' 2017-08 '];p rint (new_data)#to adjust a date index to a columnNew_data=New_data.reset_index ()#Convert a date to num formatlst=[] forIinchRange (len (new_data)): lst.append (int (date2num (new_data.iloc[i,0 ))) new_data['Date']=lstquotes=Np.array (new_data) Fig,ax=plt.subplots (figsize= (8,5)) Mpf.candlestick_ochl (Ax,quotes,width=1,colorup='g', colordown='R')#draw the figure of the moving averages 5th, 10th and 20th respectivelynew_data.index=new_data['Date']pd.rolling_mean (new_data['Close Price (yuan)'],5). Plot () Pd.rolling_mean (new_data['Close Price (yuan)'],10). Plot () Pd.rolling_mean (new_data['Close Price (yuan)'],20). Plot ()#set X axis to date, adjust X-axis date rangeax.xaxis_date () Ax.set_xlim (Datetime.datetime (2017,6,1), Datetime.datetime (2017,8,31) ) plt.show ()

Python Draw Candlestick Chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.