def build(df_dr): #一、先合并 group_no_last = 0 #最后的组号 ingroup_n_last = 0 #最后一个在组内的n group_first_n = 0 #最后一个组的第一个n chan_low_pre = None chan_high_pre = None for n, k in df_dr.iterrows(): open = k.open close = k.close low = k.low high = k.high #每条的变量 updown='flat' #升降 is_group=False #是否在组里 is_group_m=False #是否组长,组长是最高的 group_no=0 group_high=0 group_low=0 if(chan_low_pre==None or chan_high_pre==None): chan_low_pre = low chan_high_pre = high else: #上一个原始K k_pre = df_dr.iloc[n-1-1] low_pre = k_pre.low high_pre = k_pre.high #上升下降 #上升(跟原始K比较平均高度) if high_pre+low_pre < high+low: updown='up' #下降(跟原始K比较平均高度) elif high_pre+low_pre > high+low: updown='down' elif open=low) or (chan_high_pre>=high and chan_low_pre<=low)) and n=low: is_group_m = True elif chan_high_pre>=high and chan_low_pre<=low and ingroup_n_last!=n-1: #新组并且前面的高,需要设置前面的是组长 df_dr.at[n-1,'group_master']= True group_high=max(high, chan_high_pre) group_low=min(low, chan_low_pre) chan_high_pre = group_high chan_low_pre = group_low #上一个本来在组内,这个合并到原来的组 if ingroup_n_last==n-1: group_no=group_no_last #新组 else: group_no=group_no_last+1 group_first_n=n-1 group_no_last=group_no is_group=True ingroup_n_last=n else: chan_low_pre = low chan_high_pre = high #写标识值到表中 df_dr.at[n,'updown']= updown df_dr.at[n,'group']= is_group df_dr.at[n,'group_no']= group_no df_dr.at[n,'group_high']= group_high df_dr.at[n,'group_low']= group_low df_dr.at[n,'group_master']= is_group_m if is_group: df_dr.at[n-1,'group']= is_group df_dr.at[n-1,'group_no']= group_no #刷新同组内的group_high and group_low for j in range(group_first_n, n): df_dr.at[j,'group_high']= group_high df_dr.at[j,'group_low']= group_low if is_group_m: df_dr.at[j,'group_master']= False #踢掉原来的组长 #二、找分型 fx_n_maybe_pre = 0 #前面可能是分型的n,如果有方向则临时记录 direction_pre = "flat" chan_low_pre = None chan_high_pre = None df_dr['fx']='' for n, k in df_dr.iterrows(): is_group = k.group is_group_master = k.group_master group_high = k.group_high group_low = k.group_low low = k.low high = k.high is_confirm_fx = False #确定分型,前一个组或者个体是分型,为顶分型 is_confirm_fx_n = 0 #已确定分型的分型顶点 fx_type = '' if(chan_low_pre==None or chan_high_pre==None): chan_low_pre=low chan_high_pre=high elif (is_group and is_group_master) or not is_group: #只管组长和非组成员 cal_high = high cal_low = low if (is_group and is_group_master): cal_high = group_high cal_low = group_low #与前面的比较 if chan_high_pre+chan_low_pre>cal_high+cal_low: #下降 if direction_pre=="up": is_confirm_fx=True is_confirm_fx_n=fx_n_maybe_pre fx_type='top' direction_pre="down" fx_n_maybe_pre=n elif chan_high_pre+chan_low_prenode_high_pre)): df_dr.at[node_n_pre,'bi']= '' is_node=True #与前节点类型不同,且超过3个差,是新笔 elif node_type_pre!=fx_type and n-node_n_pre>3: is_node=True fx_n_pre=n if is_node: node_n_pre=n node_type_pre=fx_type node_high_pre=high node_low_pre=low if is_node: df_dr.at[n,'bi']= fx_type #四、找线段 (n是索引从0开始, i是第几个) dot_i_pre = None #dot是前面确认的线段的节点 dot_type_pre ='' dot_high_pre = None dot_low_pre = None #只保留笔的K df_bi = df_dr.loc[(df_dr["bi"]=='top') | (df_dr["bi"]=='bottom')] df_dr['line']='' #找第一个线段,第一个形成走势的三笔(如果上升,第三笔的结束点高于第一笔的结束点) for i in range(len(df_bi)): #i是笔表的索引 n是原表的index字段即第几个 k=df_bi.iloc[i] n=df_bi.index[i] bi_type=k.fx high=k.high low=k.low if i df_bi.iloc[i+3].low: df_dr.at[n,'line']='top' df_dr.at[next3n,'line']='bottom' dot_i_pre=i+3 dot_type_pre='bottom' break if bi_type=='bottom' and df_bi.iloc[i+1].high < df_bi.iloc[i+3].high: df_dr.at[n,'line']='bottom' df_dr.at[next3n,'line']='top' dot_i_pre=i+3 dot_type_pre='top' break #继续找线段 i=dot_i_pre while i!=None and ihigh: #看是否突破延申 j_n=df_bi.index[j] df_dr.at[n,'line']='' df_dr.at[j_n,'line']='top' dot_i_pre=j_n dot_type_pre='top' i=j is_found=True break elif (j-i)%2==1 and jdf_bi.iloc[i+1].high: #没有延申,反弹形成新的线段 j_n=df_bi.index[j] j_n=df_bi.index[j] df_dr.at[j_n,'line']='top' dot_i_pre=j_n dot_type_pre='top' i=j is_found=True break j=j+1 if not is_found: break #没找到下一个线段点,退出 else: continue #修正线段中间的凹凸漏点,例如上升的线段中有笔的低点低于线段的开始点,需要将这个线段的开始点移到这个更低的点,反之亦然 #有时候突然下来一笔,但是不成线段就会遗漏,此处待验证合理性 dot_type_pre ='' dot_n_pre = None dot_high_pre = None dot_low_pre = None i=0 while idot_high_pre: df_dr.at[dot_n_pre,'line']='' df_dr.at[n,'line']='top' dot_n_pre=n dot_high_pre=high dot_low_pre=low elif dot_type_pre=='bottom' and low5) or (j_bi_type=='bottom' and j_low>zg and j-i>5): #找到中枢(这时肯定已经两个点脱离,没有走回去),保存,退出,往后面继续找(考虑到两个中枢被一条线连接的情况,需要从j-2开始找) # /\ ------------- # / or ------------- # ------------- \ # ------------- \/ #print(i,j,'找到中枢') zs_no=zs_no+1 df_dr.b_zs_no[(df_dr.index>n) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexhigh): #前三段zg zd超出起始线段的高、低点,中枢破坏 #print(i,j,'zg zd超出起始线段的高、低点,中枢破坏') break elif j-i<=5 and j-i>2 and zgzg) or (j-i==5 and j_bi_type=='top' and j_high5) or (j_line_type=='bottom' and j_low>zg and j-i>5): #找到中枢(这时肯定已经两个点脱离,没有走回去),保存,退出,往后面继续找(考虑到两个中枢被一条线连接的情况,需要从j-2开始找) # /\ ------------- # / or ------------- # ------------- \ # ------------- \/ #print(i,j,'找到中枢') zs_no=zs_no+1 end_direction='down' if j_k.line=='top' else 'up' #小尾巴是top则是下降方向 df_dr.zs_no[(df_dr.index>n) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexn) & (df_dr.indexhigh): #前三段zg zd超出起始线段的高、低点,中枢破坏 #print(i,j,'zg zd超出起始线段的高、低点,中枢破坏') break elif j-i<=5 and j-i>2 and zgzg) or (j-i==5 and j_line_type=='top' and j_high