写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
冉冉上升形式指在上涨初期或盘整后期,股价或指数收出若干夹着一些小阴线、十字线的小阳线(一般不少于8根),整体走势略向上倾斜的K线组合形态。
技术特征
1)出现在上涨初期或盘整后期。
2)由若干小K线组成(一般不少于8根)。
3)一般以小阳线居多,中间也可以夹着一些小阴线、十字线。
4)K线组合形态排列略向上倾斜。
技术含义
冉冉上升是买进信号,后市看涨。
冉冉上升的意思是股价就像东方升起的朝阳,涨速虽然很慢,毫不起眼,但却往往是后市股价大涨的预兆。
冉冉上升的走势说明多方的推升正在有条不紊地进行,很多情况下是有主力介入的表现。因为主力买入必然导致买盘增加,股价上升。如果股价上升过快,或者某一日涨幅过大,都会引起其他交易者的关注和跟风。这样既增大了主力吃货的成本,也不利于主力日后拉升和派发,因此很多主力都会刻意隐藏吸货意图。但股价走势往往在不知不觉中暴露其行踪。至于公众交易者是否能够发现,那就要看谁能够用心聆听K线的细语。
k线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:冉冉上升
识别:
1. 股价或指数收出若干(至少8根)夹着一些小阴线、十字线、小阳线,小阳线居多
2. K线组合形态排列略向上倾斜
自定义:
1. 略向上倾斜 =》
1)第一根与后面的K线斜率为正的占比要大于三分之二
2)第一根与最后一根斜率要大于前面所有的斜率
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2021-01-01'
end_date_str = '2022-01-01'
df = pd.read_csv(daily_file_path,encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice'] > 0].copy()
df['o_date'] = df['tradeDate']
df['o_date'] = pd.to_datetime(df['o_date'])
df = df.loc[(df['o_date'] >= start_date_str) & (df['o_date']<=end_date_str)].copy()
# 保存未复权收盘价数据
df['close'] = df['closePrice']
# 计算前复权数据
df['openPrice'] = df['openPrice'] * df['accumAdjFactor']
df['closePrice'] = df['closePrice'] * df['accumAdjFactor']
df['highestPrice'] = df['highestPrice'] * df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice'] * df['accumAdjFactor']
# 开始计算
df['type'] = 0
df.loc[df['closePrice'] >= df['openPrice'], 'type'] = 1
df.loc[df['closePrice'] < df['openPrice'], 'type'] = -1
df['body_length'] = abs(df['closePrice']-df['openPrice'])
df['small_type'] = 0
df.loc[df['body_length']/df['closePrice'].shift(1)<0.015,'small_type'] = 1
df['ext_0'] = df['small_type'] - df['small_type'].shift(1)
df['ext_1'] = df['small_type'] - df['small_type'].shift(-1)
df.reset_index(inplace=True)
df['i_row'] = [i for i in range(0, len(df))]
df_m_s = df.loc[df['ext_0'] == 1].copy()
df_m_e = df.loc[df['ext_1'] == 1].copy()
i_row_s = df_m_s['i_row'].values.tolist()
i_row_e = df_m_e['i_row'].values.tolist()
i_row_two = i_row_s + i_row_e
i_row_two.sort()
df['signal'] = 0
df['signal_name'] = ''
for s, e in zip(i_row_s, i_row_e):
if e - s < 8:
continue
enter_yeah = True
last_smaller = False
rate_p_num = 0
last_chg = df.iloc[e]['closePrice'] - df.iloc[s]['closePrice']
for i in range(e,s,-1):
cur_chg = df.iloc[i]['closePrice'] - df.iloc[s]['closePrice']
if cur_chg > last_chg:
last_smaller = True
break
if cur_chg>0:
rate_p_num += 1
pass
if last_smaller:
continue
if float(rate_p_num)/(e-s) < 0.66:
enter_yeah = False
if enter_yeah:
df.loc[(df['i_row'] >= s) & (df['i_row'] <= e), 'signal'] = 1
df.loc[(df['i_row'] >= s) & (df['i_row'] <= e), 'signal_name'] = str(e - s)
pass
file_name = os.path.basename(daily_file_path)
title_str = file_name.split('.')[0]
line_data = {
'title_str':title_str,
'whole_header':['日期','收','开','高','低'],
'whole_df':df,
'whole_pd_header':['tradeDate','closePrice','openPrice','highestPrice','lowestPrice'],
'start_date_str':start_date_str,
'end_date_str':end_date_str,
'signal_type':'duration_detail',
'duration_len':[],
'temp':len(df.loc[df['signal']==1])
}
return line_data