Example #1
0
def get_pnf(hl, ll, cl, boxtype="m"):
    
    lenc = len(cl)
    if lenc < 5:
        return [], [], []
    
    cl = np.array(cl)
    hl = np.array(hl)
    ll = np.array(ll)
    tl = (cl+hl+ll)/3
            
    joints = [cl[0]]
    indexes = [0]
    pmmask = [0]
    df = tl[1:]-tl[:-1]
        
    last_peak = cl[0]
    peak_idx = 0
    jidx = 1
    boxnum = 3
    boxsizes = [0]*boxnum
    uptrend = (df[0]>=0)
    for day in range(1,lenc):
        filter = 0
        cnt = 0
        for i in range(boxnum-1):
            if boxsizes[i] > 0:
                filter += boxsizes[i]
                cnt += 1
        if boxtype == "l":
            boxsizes[-1] = kf.get_lboxsize(tl[day])
        if boxtype == "m":
            boxsizes[-1] = kf.get_boxsize(tl[day])
        if boxtype == "b":
            boxsizes[-1] = kf.get_bollinger_boxsize(tl[day])
        if cnt < boxnum-1:
            filter = boxsizes[-1]*boxnum
        else:
            filter += boxsizes[-1]
        for i in range(boxnum-1):
            boxsizes[i] = boxsizes[i+1]
        
        if day < lenc-1:
            dfp = df[day-1]
            
        curp = tl[day]
            
        if uptrend and dfp>=0:
            if last_peak < curp:
                last_peak = curp
                peak_idx = day
        elif uptrend==False and dfp<0:
            if last_peak > curp:
                last_peak = curp
                peak_idx = day
        elif (uptrend and dfp<0) or (uptrend==False and dfp>=0):
            if abs(last_peak - curp) > filter:
                if uptrend:
                    joints.append(hl[peak_idx])
                    pmmask.append(1)
                else:
                    joints.append(ll[peak_idx])
                    pmmask.append(-1)
                indexes.append(peak_idx)
                uptrend = not uptrend
                jidx += 1
                last_peak = curp
                peak_idx = day
    
    return joints, indexes, pmmask
Example #2
0
def old2_get_pnf(hl, ll, cl):
    
    lenc = len(cl)
    joints = np.ones(lenc)*(-1)
    #joints2 = np.ones(lenc)*(-1)
    indexes = np.ones(lenc)*(-1)
    #indexes2 = np.ones(lenc)*(-1)
    
    cl = np.array(cl)
    hl = np.array(hl)
    ll = np.array(ll)
    tl = (cl+hl+ll)/3
            
    joints[0] = cl[0]
    indexes[0] = 0
    df = tl[1:]-tl[:-1]
        
    last_peak = cl[0]
    peak_idx = 0
    jidx = 1
    boxnum = 3
    boxsizes = [0]*boxnum
    uptrend = (df[0]>=0)
    for day in range(1,lenc):
        filter = 0
        cnt = 0
        for i in range(boxnum-1):
            if boxsizes[i] > 0:
                filter += boxsizes[i]
                cnt += 1
        boxsizes[-1] = kf.get_bollinger_boxsize(cl[day])
        if cnt < boxnum-1:
            filter = boxsizes[-1]*boxnum
        else:
            filter += boxsizes[-1]
        for i in range(boxnum-1):
            boxsizes[i] = boxsizes[i+1]
        
        if day < lenc-1:
            dfp = df[day-1]
        
    
        curp = tl[day]
        #if day == lenc-1:
        #    joints2[-1] = curp
        #    indexes2[-1] = day
        #    for j in range(1, jidx+1):
        #        joints2[(-1)*j-1] = joints[jidx-j]
        #        indexes2[(-1)*j-1] = indexes[jidx-j]                    
            
        if uptrend and dfp>=0:
            if last_peak < curp:
                last_peak = curp
                peak_idx = day
        elif uptrend==False and dfp<0:
            if last_peak > curp:
                last_peak = curp
                peak_idx = day
        elif (uptrend and dfp<0) or (uptrend==False and dfp>=0):
            if abs(last_peak - curp) > filter:
                if uptrend:
                    joints[jidx] = hl[peak_idx]
                else:
                    joints[jidx] = ll[peak_idx]
                indexes[jidx] = peak_idx
                uptrend = not uptrend
                jidx += 1
                last_peak = curp
                peak_idx = day
    
    joints = (np.intc(joints)).tolist()
    indexes = (np.intc(indexes)).tolist()
    return joints, indexes