问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

用机器学习识K线的图像 智能识别k线形态

创作时间:
作者:
@小白创作中心

用机器学习识K线的图像 智能识别k线形态

引用
51CTO
1.
https://blog.51cto.com/u_13066/11679181

在股票交易中,K线形态是技术分析的重要组成部分。其中,“圆顶”形态是一种重要的见顶反转信号。本文将介绍如何使用机器学习识别K线形态中的"圆顶"形态,并提供相应的策略代码。

解说

圆顶是上涨过程中或横盘整理时,一根大阳线或中阳线后,在阳线顶部收出若干走势呈向下圆弧的小阴线和小阳线,最后以向下跳空缺口确认成立的K线组合形态。

技术特征

  1. 在上涨趋势或横盘整理时出现。
  2. 先收出一根大阳线或中阳线,然后在其顶部由若干小阴线、小阳线形成一个圆弧顶。
  3. 最后以向下跳空缺口确认圆顶形态成立。

技术含义

圆顶是见顶反转信号,后市看跌,卖出。圆顶是和圆底相对的K线形态,见顶反转的信号强于圆底。圆顶正式了股价快速上涨、上涨减速、停止上涨、缓慢下跌、加速下跌的全过程,完美再现了道氏理论关于趋势逆转的定义,是比较强烈的见顶信号。圆顶形成的头部,往往是中期趋势的头部。交易者见到圆顶形态,至少要减掉大部分仓位。

K线形态策略代码

def excute_strategy(daily_file_path):
    '''
    名称:圆顶
    识别:一根大阳线或中阳线后,在阳线顶部收出若干走势呈向下圆弧形的小阳线或小阴线,最后以向下跳空缺口确认成立
    自定义:
    1. 是否是圆弧 =》代码不判断,先依靠肉眼
    2. 若干 =》 至少5根
    3. 阳线顶部 =》最低价在阳线实体顶部三分之一以上
    前置条件:计算时间区间 2021-01-01 到 2022-01-01
    :param daily_file_path: 股票日数据文件路径
    :return:
    '''
    import pandas as pd
    import os
    start_date_str = '2004-01-01'
    end_date_str = '2005-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['m_body_type'] = 0
    df.loc[(df['type']==1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'m_body_type'] = 2
    df['small_type'] = 0
    df.loc[df['body_length']/df['closePrice'].shift(1)<0.015,'small_type'] = 1
    # 向下跳空缺口
    df['breakaway_gap'] = 0
    df.loc[(df['type']==1) & (df['type'].shift(1)==1),'breakaway_gap'] = df['openPrice'].shift(1) - df['closePrice']
    df.loc[(df['type']==1) & (df['type'].shift(1)==-1),'breakaway_gap'] = df['closePrice'].shift(1) - df['closePrice']
    df.loc[(df['type']==-1) & (df['type'].shift(1)==1),'breakaway_gap'] = df['openPrice'].shift(1) - df['openPrice']
    df.loc[(df['type']==-1) & (df['type'].shift(1)==-1),'breakaway_gap'] = df['closePrice'].shift(1) - df['openPrice']
    df.reset_index(inplace=True)
    df['i_row'] = [i for i in range(0,len(df))]
    df_m_n = df.loc[df['m_body_type']==2].copy()
    df_m_p = df.loc[df['breakaway_gap']>0].copy()
    i_row_n = df_m_n['i_row'].values.tolist()
    i_row_p = df_m_p['i_row'].values.tolist()
    i_row_two = i_row_n + i_row_p
    i_row_two.sort()
    n_list = []
    p_list = []
    for i in range(0,len(i_row_two)-1):
        if i_row_two[i] in i_row_n and i_row_two[i+1] in i_row_p:
            n_list.append(i_row_two[i])
            p_list.append(i_row_two[i+1])
        pass
    df['signal'] = 0
    df['signal_name'] = ''
    for n,p in zip(n_list,p_list):
        if p-n < 5:
            continue
        enter_yeah = True
        for i in range(n+1,p):
            if df.iloc[i]['small_type']!=1:
                enter_yeah = False
                break
            pass
        if enter_yeah:
            final_yeah = False
            target_body_length = df.iloc[n]['body_length']
            for m_i in range(n+1,p):
                if (df.iloc[n]['closePrice'] - target_body_length*0.33) > df.iloc[m_i]['lowestPrice'] or (df.iloc[n]['highestPrice'] + target_body_length*0.2) < df.iloc[m_i]['highestPrice']:
                    final_yeah = True
                    break
            if final_yeah:
                continue
            df.loc[(df['i_row']>=n) & (df['i_row']<=p),'signal'] = 1
            df.loc[(df['i_row']>=n) & (df['i_row']<=p),'signal_name'] = str(p-n)
            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  

结果

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号