米筐和聚宽没有直接可以获取月k线,周k线的函数,为此写了一个极简函数可以获取日k线,周k线,月k线,乃至分钟k线。如下:
聚宽:
def get_period_price_wjlec(stock, rule='W', start_date=None, end_date=None, frequency='daily', fields=None, \
skip_paused=True, fq='pre', count=None):
'''
极简方法获取真实的日线、周线、月线或其他周期数据,本函数考虑了假期情况,不会列出不交易的日期。
参数:
stock : 证券代码
rule : K线类别,默认是周线'W',月线是'M',季度线是'Q',年线是'A'。期间代码也可以使用周K('W-FRI')、月K('BM')、
年K('A-DEC')
end_date : 默认None, 对应使用attribute_history; 否则使用get_price
其他参数:同get_price
返回:
dataframe: index:时间序列
范例:
df=get_period_price_wjlec('601668.XSHG', start_date='2017-1-1', end_date='2017-5-31')#获取周线
df=get_period_price_wjlec('601668.XSHG', rule='W', count=120, end_date='2017-6-1')#获取周线
df=get_period_price_wjlec('601668.XSHG', rule='M', start_date='2017-1-1', end_date='2017-5-31')#获取月线
df=get_period_price_wjlec('601668.XSHG', rule='M', count=1000, end_date='2017-5-31')#获取月线
'''
#设定转换周期period_type 转换为周是'W',月'M',季度线'Q',五分钟'5min',12天'12D'
period_type = rule
if "attribute_history" not in dir() or end_date:
stock_data = get_price(stock, start_date=start_date, end_date=end_date, frequency=frequency, fields=\
['open','high','low'