策略验证_买入口诀_五阳上阵股价弹升

写在前面:
1. 本文中提到的“股票策略校验工具”的具体使用操作请查看该博文
2. 文中知识内容来自书籍《同花顺炒股软件从入门到精通》
3. 本系列文章是用来学习技法,文中所得内容都仅仅只是作为演示功能使用

目录

解说

策略代码

结果 


解说

        所谓“五阳上阵”指的是底部连续出现的5条小阳线,5条小阳线在低位出现时,表明底部做多的力量较强,连续5天都是多方获胜,空头已无立身之地,后市股价就会趁机上升。

         出现“五阳上阵”的形态后,股票投资者需遵循以下操作原则。

        1)5条阳线的买入信号是比较可靠的,但必须是股价经过一段深跌后,在底部的低点出现时才起作用。必须注意的是如果5条阳线在下降行情的反弹走势中出现,则反向操作,迅速卖出股票。

        2)5条阳线的排列一般为横向形态,第一条阳线的开盘价与第五条阳线的收盘价差额不应很大,总升幅最好保持在3%以下,如果总升幅达到5%以上时,应慎重操作。

        3)如果5条阳线中间出现了一两条小阴线,其后的升幅一般不受影响,投资者可放心地买入。

策略代码

def excute_strategy(base_data,data_dir):
    '''
    买入口诀 - 五阳上阵,股价弹升
    解析:
    1. 连续出现5条小阳线
    2. 第一条阳线的开盘价与第五条阳线的开盘价差额不应很大,总升幅最好保持在3%以下
    自定义:
    1. 小阳线 =》实体长度是昨日收盘价的0.5%到1.5%之间
    2. 总升幅 =》5根阳线中升幅最大的值
    3. 买入点 =》五阳后一个交易日收盘价
    4. 胜 =》 买入后第三日收盘价涨跌幅,正为胜
    只计算最近两年的数据
    :param base_data:股票代码与股票简称 键值对
    :param data_dir:股票日数据文件所在目录
    :return:
    '''
    import pandas as pd
    import numpy as np
    import talib,os
    from datetime import datetime
    from dateutil.relativedelta import relativedelta

    def res_pre_two_year_first_day():
        pre_year_day = (datetime.now() - relativedelta(years=2)).strftime('%Y-%m-%d')
        return pre_year_day
    caculate_start_date_str = res_pre_two_year_first_day()

    def pd_child_method(x):
        max_one = x['ext_1']
        temp_list = [x['ext_2'],x['ext_3'],x['ext_4']]
        for item in temp_list:
            if max_one < item:
                max_one = item
        return max_one

    dailydata_file_list = os.listdir(data_dir)

    total_count = 0
    total_win = 0
    check_count = 0
    list_list = []
    detail_map = {}
    for item in dailydata_file_list:
        item_arr = item.split('.')
        ticker = item_arr[0]
        secName = base_data[ticker]
        file_path = data_dir + item
        df = pd.read_csv(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'] >= caculate_start_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']

        if len(df)<=0:
            continue

        # 开始计算
        df.reset_index(inplace=True)
        df['i_row'] = [i for i in range(len(df))]
        df['body_length'] = abs(df['closePrice']-df['openPrice'])
        df['small_body'] = 0
        df.loc[(df['closePrice']>df['openPrice']) & (df['body_length']/df['closePrice'].shift(1)>0.005) & (df['body_length']/df['closePrice'].shift(1)<0.015),'small_body'] = 1
        df['five_yeah'] = 0
        df.loc[(df['small_body']==1) & (df['small_body'].shift(1)==1) & (df['small_body'].shift(2)==1) & (df['small_body'].shift(3)==1) & (df['small_body'].shift(4)==1),'five_yeah'] = 1
        df['ext_1'] = df['closePrice'] - df['closePrice'].shift(4)
        df['ext_2'] = df['closePrice'].shift(1) - df['closePrice'].shift(4)
        df['ext_3'] = df['closePrice'].shift(2) - df['closePrice'].shift(4)
        df['ext_4'] = df['closePrice'].shift(3) - df['closePrice'].shift(4)
        df['highest_chg'] = df.apply(pd_child_method,axis=1)
        df['target_yeah'] = 0
        df.loc[(df['five_yeah']==1) & (df['highest_chg']/df['closePrice']<0.03),'target_yeah'] = 1

        df['three_chg'] = round(((df['close'].shift(-3) - df['close']) / df['close']) * 100, 4)
        df['three_after_close'] = df['close'].shift(-3)

        df_target = df.loc[df['target_yeah']==1].copy()

        node_count = 0
        node_win = 0
        duration_list = []
        table_list = []
        i_row_list = df_target['i_row'].values.tolist()
        for i,row0 in enumerate(i_row_list):
            row = row0 + 1
            if row >= len(df):
                continue
            date_str = df.iloc[row]['tradeDate']
            cur_close = df.iloc[row]['close']
            three_after_close = df.iloc[row]['three_after_close']
            three_chg = df.iloc[row]['three_chg']

            table_list.append([
                i,date_str,cur_close,three_after_close,three_chg
            ])
            duration_list.append([row-5,row+3])
            node_count += 1
            if three_chg>0:
                node_win +=1
            pass

        list_list.append({
            'ticker':ticker,
            'secName':secName,
            'count':node_count,
            'win':0 if node_count<=0 else round((node_win/node_count)*100,2)
        })
        detail_map[ticker] = {
            'table_list': table_list,
            'duration_list': duration_list
        }

        total_count += node_count
        total_win += node_win
        check_count += 1
        pass
    df = pd.DataFrame(list_list)

    results_data = {
        'check_count':check_count,
        'total_count':total_count,
        'total_win':0 if total_count<=0 else round((total_win/total_count)*100,2),
        'start_date_str':caculate_start_date_str,
        'df':df,
        'detail_map':detail_map
    }
    return results_data

结果 

 本文校验的数据是随机抽取的81个股票

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值