TA-Lib模块中提供的价格转换函数有四个,主要用于计算开盘价、收盘价、最高价、最低价之间的均值,具体如下表所示。
AVGPRICE:average Price, 平均价格函数:ta.AVGPRICE(open,high,low,close)
MEDPRICE:Median Price, 中位数价格:ta.MEDPRICE(high,low)
TYPPRICE:Typical Price,代表性价格:ta.TYPPRICE(high,low,close)
WCLPRICE:Weighted Close Price, 加权收盘价:ta.WCLPRICE(high,low,close)
import pandas as pd import numpy as np import matplotlib.pyplot as plt import talib as ta import tushare as ts plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False def get_data(code, start='2015-01-01'): df = ts.get_k_data(code, start) df.index = pd.to_datetime(df.date) df = df.sort_index() return df df = get_data('sh')[['open','close','high','low']] # 开盘价,最高价,最低价,收盘价的均值 df['average'] = ta.AVGPRICE(df.open, df.high, df.low, df.close) # 最高价,最低价中位数 df['median'] = ta.MEDPRICE(df.high, df.low) # 最高价,最低价,收盘价均值 df['typical'] = ta.TYPPRICE(df.high, df.low, df.close) # 最高价,最低价,收盘价的加权 df['weight'] = ta.WCLPRICE(df.high, df.low, df.close) df.head() df.loc['2019-01-01':,['close','average','median','typical','weight'] ].plot(figsize=(12,6)) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.title('上证指数及其价格转换', fontsize=15) plt.xlabel('') plt.show()