写在前面:
1. 本文中提到的“股票策略校验工具”的具体使用操作请查看该博文;
2. 文中知识内容来自书籍《同花顺炒股软件从入门到精通》
3. 本系列文章是用来学习技法,文中所得内容都仅仅只是作为演示功能使用
目录
解说
策略代码
结果
平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标。MACD是查拉尔·阿佩尔于1979年提出的,它是一项利用短期(常用为12日)移动平均线与长期(常用为26日)移动平均线之间的聚合与分离状况,对买进、卖出时机做出研判的技术指标。
MACD就是用快速和慢速的两条均线的交叉换位、合并分离的特征,来分析研究股市的走势,从而正确引导投资者合理地判断股票的买点和卖点。
MACD指标对买点的应用原则如下。【下文中提到的MACD线在本文策略中表达为DEA线】
1)MACD线的交叉具有一定的中期提示作用。如果处于0轴以下,并出现两次交叉,则是明显的买进信号。
2)当DIF与MACD都在0轴以上,而DIF向上突破MACD时,表明股市处于一种强势之中,股价将再次上涨,可以继续买进股票或持续待涨,这就是MACD指标“黄金交叉”的另一种形式。
3)当红柱持续放大时,表明股市处于牛市行情中,股价将继续上涨,这时应持股待涨或短线买入股票,直到红柱无法再放大时才可以考虑卖出。
4)当绿柱开始消失、红柱开始放出时,这也是股市转市信号之一,表明股市的下跌行情已经结束,股价将开始加速上升,这时应开始继续买入股票或持股待涨。
def excute_strategy(base_data,data_dir):'''指标买点分析技法 - 运用MACD确定最佳买点解析:1. 处于0轴以下,并出现两次交叉,是明显的买进信号。2. DIF和DEA在0轴以上,DIF上穿DEA3. 红柱持续放大4. 绿柱开始消失,红柱开始放出自定义:1. 红柱持续放大 =》 连续三日涨跌幅为正2. 买入时点 =》 走势确定后下一交易日3. 胜 =》 买入后第三个交易日收盘价上升,为胜只计算最近两年的数据:param base_data:股票代码与股票简称 键值对:param data_dir:股票日数据文件所在目录:return:'''import pandas as pdimport numpy as npimport talib,osfrom datetime import datetimefrom dateutil.relativedelta import relativedeltafrom tools import stock_factor_caculatedef res_pre_two_year_first_day():pre_year_day = (datetime.now() - relativedelta(years=2)).strftime('%Y-%m-%d')return pre_year_daycaculate_start_date_str = res_pre_two_year_first_day()dailydata_file_list = os.listdir(data_dir)total_count = 0total_win = 0check_count = 0list_list = []detail_map = {}factor_list = ['MACD']ma_list = []for item in dailydata_file_list:item_arr = item.split('.')ticker = item_arr[0]secName = base_data[ticker]file_path = data_dir + itemdf = 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# 开始计算for item in factor_list:df = stock_factor_caculate.caculate_factor(df,item)for item in ma_list:df = stock_factor_caculate.caculate_factor(df,item)df.reset_index(inplace=True)df['i_row'] = [i for i in range(len(df))]df['three_chg'] = round(((df['close'].shift(-3) - df['close']) / df['close']) * 100, 4)df['three_after_close'] = df['close'].shift(-3)# DIFF DEA MACDdf['one_yeah'] = 0df.loc[(df['DIFF']<0) & (df['DEA']<0) & (df['DIFF'].shift(1)>df['DEA'].shift(1)) & (df['DIFF']<=df['DEA']),'one_yeah'] = 1df.loc[(df['DIFF']<0) & (df['DEA']<0) & (df['DIFF'].shift(1)=df['DEA']),'one_yeah'] = 1df['two_yeah'] = 0df.loc[(df['DIFF'].shift(1)>0) & (df['DIFF']<=0),'two_yeah'] = 1df.loc[(df['DIFF'].shift(1)<0) & (df['DIFF']>=0),'two_yeah'] = 1df.loc[(df['DEA'].shift(1)>0) & (df['DEA']<=0),'two_yeah'] = 1df.loc[(df['DEA'].shift(1)<0) & (df['DEA']>=0),'two_yeah'] = 1zero_list = df.loc[df['two_yeah']==1]['i_row'].values.tolist()one_list = df.loc[df['one_yeah']==1]['i_row'].values.tolist()target_one_list = []for i in range(0,len(one_list)-2):pre_node = one_list[i]after_node = one_list[i+1]enter_yeah = Truefor i0 in zero_list:if i0>after_node:breakif i0>pre_node and i00:df.loc[df['i_row'].isin(target_one_list),'target_yeah'] = 1df.loc[(df['DIFF']>0) & (df['DEA']>0) & (df['DIFF'].shift(1)=df['DEA']),'target_yeah'] = 1df['macd_chg'] = df['MACD'] - df['MACD'].shift(1)df['three_yeah'] = 0df.loc[(df['MACD'].shift(2)>0) & (df['MACD'].shift(1)>0) & (df['MACD']>0) & (df['macd_chg'].shift(2)>0) & (df['macd_chg'].shift(1)>0) & (df['macd_chg']>0),'three_yeah'] = 1df['four_yeah'] = 0df.loc[(df['MACD'].shift(1)<0) & (df['MACD']>=0),'four_yeah'] = 1four_list = df.loc[df['four_yeah']==1]['i_row'].values.tolist()three_list = df.loc[df['three_yeah']==1]['i_row'].values.tolist()target_three_list = []two_list = four_list + three_listtwo_list.sort()for item in four_list:i = two_list.index(item)if i < len(two_list)-1:target_three_list.append(two_list[i+1])df.loc[df['MACD'].isin(target_three_list),'target_yeah'] = 1df.loc[(df['MACD'].shift(1)<0) & (df['MACD']>0),'target_yeah'] = 1i_row_list = df.loc[df['target_yeah']==1]['i_row'].values.tolist()node_count = 0node_win = 0duration_list = []table_list = []for i,row0 in enumerate(i_row_list):row = row0 + 1if row >= len(df):continuedate_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-2,row+3])node_count += 1if three_chg<0:node_win +=1passlist_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_counttotal_win += node_wincheck_count += 1passdf = 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,'factor_list':factor_list,'ma_list':ma_list}return results_data


本文校验的数据是随机抽取的81个股票
上一篇:Docker数据管理
下一篇:天蝎男喜欢听什么情话