admin 發表於 2023-4-6 00:48:23

图解Pandas(mplfinance 进行展开)

使用 mplfinance 有固定的格式! 数据必须是 Pandas DataFrame 格式,必须包含开盘价、最高价、最低价和收盘价,需要使用 DatetimeIndex 作为索引!
<p style="box-sizing: border-box; -webkit-font-smoothing: antialiased; text-align: justify; line-height: 1.8rem; padding-right: 1.4rem; padding-left: 1.4rem;"><font face="-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica Neue, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-size: 12.88px;">df_new = df1[['日期','开盘','最高','最低','收盘','成交量']]</span></font></p><p style="box-sizing: border-box; -webkit-font-smoothing: antialiased; text-align: justify; line-height: 1.8rem; padding-right: 1.4rem; padding-left: 1.4rem;"><font face="-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica Neue, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-size: 12.88px;">df_new.head()</span></font></p>
数据处理¶注意使用 mplfinance 有固定的格式!数据必须是 Pandas DataFrame 格式,必须包含开盘价、最高价、最低价和收盘价,需要使用 DatetimeIndex 作为索引!
选择列¶筛选 df1 中'日期','开盘','最高','最低','收盘','成交量'几列,并命名为新数据框 df_new

https://pandas.liuzaoqi.com/_static/copy-button.svg



df_new = df1[['日期','开盘','最高','最低','收盘','成交量']]df_new.head()https://pandas.liuzaoqi.com/_static/copy-button.svg




日期开盘最高最低收盘成交量
02017/1/38.278.348.258.32459840
12017/1/48.318.348.308.32449330
22017/1/58.338.348.318.33344373
32017/1/68.338.338.278.29358154
42017/1/98.298.338.278.31361082





修改类型¶将 df_new 的 日期 修改为时间类型df_new['日期'] = pd.to_datetime(df_new['日期'])https://pandas.liuzaoqi.com/_static/copy-button.svg




修改列名¶将 df_new 的列名修改为 'Date','Open','High','Low','Close','Volume'df_new.columns = ['Date','Open','High','Low','Close','Volume']df_new.head()https://pandas.liuzaoqi.com/_static/copy-button.svg




DateOpenHighLowCloseVolume
02017-01-038.278.348.258.32459840
12017-01-048.318.348.308.32449330
22017-01-058.338.348.318.33344373
32017-01-068.338.338.278.29358154
42017-01-098.298.338.278.31361082





修改索引¶将 df_new 的索引列修改为 Date 列df_new.set_index(["Date"], inplace=True)df_new.head()https://pandas.liuzaoqi.com/_static/copy-button.svg




OpenHighLowCloseVolume
Date



2017-01-038.278.348.258.32459840
2017-01-048.318.348.308.32449330
2017-01-058.338.348.318.33344373
2017-01-068.338.338.278.29358154
2017-01-098.298.338.278.31361082






制作K线图¶日线¶根据 df_new 绘制日线级K线mpf.plot(df_new, type='line')https://pandas.liuzaoqi.com/_static/copy-button.svg


https://pandas.liuzaoqi.com/_images/%E5%88%B6%E4%BD%9CK%E7%BA%BF%E5%9B%BE_11_0.png


添加移动均线¶在上一题的基础上,添加5日、10日、15日移动均线mpf.plot(df_new, type='line',mav=(5,10,15))https://pandas.liuzaoqi.com/_static/copy-button.svg


https://pandas.liuzaoqi.com/_images/%E5%88%B6%E4%BD%9CK%E7%BA%BF%E5%9B%BE_14_0.png


添加成交量¶在上一题的基础上,添加成交量mpf.plot(df_new, type='line',mav=(5,10,15), volume=True)https://pandas.liuzaoqi.com/_static/copy-button.svg


https://pandas.liuzaoqi.com/_images/%E5%88%B6%E4%BD%9CK%E7%BA%BF%E5%9B%BE_17_0.png


蜡烛图¶使用 df2 数据,筛选出 8月3日 的数据,制作蜡烛图df_new = df2[(df2['时间'] > '2021-08-03 09:35:00') & (df2['时间'] < '2021-08-03 15:00:00')]df_new = df_new[['时间','开盘','最高','最低','收盘','成交量']]df_new.columns = ['Date','Open','High','Low','Close','Volume']df_new.set_index(["Date"], inplace=True)mpf.plot(df_new, type='candle',mav=(3,6,9), volume=True)https://pandas.liuzaoqi.com/_static/copy-button.svg


https://pandas.liuzaoqi.com/_images/%E5%88%B6%E4%BD%9CK%E7%BA%BF%E5%9B%BE_20_0.png


展示非交易时间¶使用 df2 数据,筛选 8月3日-8月4日的数据,制作蜡烛图并展示非交易时间区间df_new = df2[(df2['时间'] > '2021-08-03 9:00:00') & (df2['时间'] < '2021-08-04 15:00:00')]df_new = df_new[['时间','开盘','最高','最低','收盘','成交量']]df_new.columns = ['Date','Open','High','Low','Close','Volume']df_new.set_index(["Date"], inplace=True)mpf.plot(df_new,type='candle',show_nontrading=True, volume=True)https://pandas.liuzaoqi.com/_static/copy-button.svg


https://pandas.liuzaoqi.com/_images/%E5%88%B6%E4%BD%9CK%E7%BA%BF%E5%9B%BE_22_0.png







頁: [1]
查看完整版本: 图解Pandas(mplfinance 进行展开)