Ejemplo n.º 1
0
def gen_pf_bollinger(dl, ol, hl, ll, cl, vl, need_y=True):
    span = 25
    # vspan = 5
    period = 52
    futspan = 20
    pointsnum = 7
    # div_num = 9

    calc_past = max(span, period)
    invalid_past = max(span * 2, span + period)
    invalid_fut = futspan

    if len(dl) < invalid_past + invalid_fut:
        return []

    ###########################
    # Get history             #
    ###########################
    # om = vct.add_history(ol, calc_past)
    hm = vct.add_history(hl, calc_past)
    lm = vct.add_history(ll, calc_past)
    cm = vct.add_history(cl, calc_past)
    vm = vct.add_history(vl, calc_past)

    ###########################
    # Modify data             #
    ###########################
    pmfi = idc.mfi(hm, lm, cm, vl, span)
    me = idc.moving_average(cm, span)
    # vme = idc.moving_average(vm, vspan)
    std = idc.moving_std(cm, span)
    sz = idc.get_size(std, me)
    uboll = me + std * 2.0
    dboll = me - std * 2.0

    # ds = idc.moving_average(((hm-lm)/lm)[:,-span:], span)
    (joints, subjoints, indexes) = vct.get_pnf_plus(cm, hm, lm, pointsnum, [uboll, dboll, pmfi])
    # (joints, subjoints, indexes) = vct.get_pnf(cm, hm, lm, pointsnum, [uboll, dboll, pmfi, vme])
    # (vjoints, vindexes) = vct.get_vpnf(vm, pointsnum)
    uboll = idc.add_history(uboll, calc_past)
    dboll = idc.add_history(dboll, calc_past)
    ma = np.max(np.c_[uboll, hm], axis=1)
    mi = np.min(np.c_[dboll, lm], axis=1)
    # vma = np.max(vm, axis=1)
    # vmi = np.min(vm, axis=1)

    doplot = False
    if doplot:
        tmp = pmfi[invalid_past:]
        pl.plot_kabuka(range(0, len(tmp)), [tmp], ["mfi"])

    ###########################
    # Cut invalid past        #
    ###########################
    ma = ma[invalid_past:]
    mi = mi[invalid_past:]
    # vma = vma[invalid_past:]
    # vmi = vmi[invalid_past:]
    joints = joints[invalid_past:, :]
    dl = dl[invalid_past:]
    indexes = indexes[invalid_past:]
    for i in range(0, len(subjoints)):
        subjoints[i] = subjoints[i][invalid_past:, :]

    sz = sz[invalid_past:]

    # vjoints = vjoints[invalid_past:,:]
    # vindexes = vindexes[invalid_past:]

    ###########################
    # Normalization           #
    ###########################
    # subjoints = [uboll, dboll, pmfi, vme]
    joints = vct.rate_norm(joints, ma, mi)
    subjoints[0] = vct.rate_norm(subjoints[0], ma, mi)
    subjoints[1] = vct.rate_norm(subjoints[1], ma, mi)
    # subjoints[3] = vct.rate_norm(subjoints[3], vma, vmi)
    indexes = indexes / period

    # vjoints = vct.rate_norm(vjoints, vma, vmi)

    ###########################
    # Plot                    #
    ###########################
    doplot = False
    if doplot:
        y1 = []
        x1 = indexes[-1]
        y1.append(joints[-1, :])
        for i in range(0, len(subjoints)):
            tmp = subjoints[i][-1, :]
            y1.append(subjoints[i][-1, :])
        labels1 = ["p&f", "plus theta", "minus theta", "mfi", "volume"]

        pl.plot_kabuka([x1], [y1], [labels1])

    # X = np.c_[indexes, joints]
    # sz = sz
    X = np.array(joints)
    for s in subjoints:
        X = np.c_[X, s]

    ###########################
    # Emphasize main points   #
    ###########################
    joints[:, -1] = 4 * joints[:, -1]
    joints[:, -2] = 3 * joints[:, -2]
    for i in range(0, len(subjoints)):
        subjoints[i][:, -1] = 2 * subjoints[i][:, -1]

    # i = 0
    # for i in range(len(X)):
    #    X[i] = X[i].tolist()
    if need_y == False:
        return (dl, sz, X)

    ###########################
    # Get max/min prices      #
    ###########################
    fut = add_history(cl, futspan, False)
    # (m, n) = fut.shape
    max_prices = (np.max(fut, axis=1) - fut[:, 0]) * 1.0 / fut[:, 0]
    min_prices = (fut[:, 0] - np.min(fut, axis=1)) * 1.0 / fut[:, 0]

    max_prices = _cut_invalid(max_prices, invalid_past, invalid_fut)
    min_prices = _cut_invalid(min_prices, invalid_past, invalid_fut)

    # for i in range(len(Y)):
    #    Y[i] = Y[i].tolist()

    return (dl, sz, X, max_prices * 100, min_prices * 100)
Ejemplo n.º 2
0
def gen_pf_pnf(dl, ol, hl, ll, cl, vl, need_y=True):
    span = 25
    #vspan = 5
    period = 52
    futspan = 20
    pointsnum = 7
    #div_num = 9
    

    calc_past = max(span, period)
    invalid_past = max(span*2, span+period)
    invalid_fut = futspan
    
    if len(dl) < invalid_past + invalid_fut:
        return []
    
    ###########################
    # Get history             #
    ###########################
    #om = vct.add_history(ol, calc_past)
    hm = vct.add_history(hl, calc_past)
    lm = vct.add_history(ll, calc_past)
    cm = vct.add_history(cl, calc_past)
    vm = vct.add_history(vl, calc_past)
        
    
    ###########################
    # Modify data             #
    ###########################    
    me = idc.moving_average(cm, span)
    std = idc.moving_std(cm, span)
    sz = idc.get_size(std, me)
    uboll = me + std*2.0
    dboll = me - std*2.0
    
    (joints, subjoints, indexes) = vct.get_pnf(cm, hm, lm, pointsnum, [uboll, dboll])
    uboll = idc.add_history(uboll, calc_past)
    dboll = idc.add_history(dboll, calc_past)
    ma = np.max(np.c_[uboll,hm], axis=1)
    mi = np.min(np.c_[dboll,lm], axis=1)

        
    
    ###########################
    # Cut invalid past        #
    ###########################
    ma = ma[invalid_past:]
    mi = mi[invalid_past:]
    joints = joints[invalid_past:,:]
    dl = dl[invalid_past:]
    indexes = indexes[invalid_past:]
    for i in range(0, len(subjoints)):
        subjoints[i] = subjoints[i][invalid_past:,:]
    
    sz = sz[invalid_past:]
    
        
    ###########################
    # Normalization           #
    ###########################
    joints = vct.rate_norm(joints, ma, mi)
    subjoints[0] = vct.rate_norm(subjoints[0], ma, mi)
    subjoints[1] = vct.rate_norm(subjoints[1], ma, mi)
    indexes = indexes/period
    

    ###########################
    # Plot                    #
    ###########################
    doplot = False
    if doplot:
        y1 = []
        x1 = indexes[-1]
        y1.append(joints[-1,:])
        for i in range(0, len(subjoints)):
            tmp = subjoints[i][-1,:]
            y1.append(subjoints[i][-1,:])
        labels1 = ["p&f", "plus theta", "minus theta", "mfi", "volume"]

        pl.plot_kabuka([x1], [y1], [labels1])

    X = np.array(joints)*2
    X = np.c_[X, indexes]

    if need_y == False:
        return (dl, sz, X)
    
    ###########################
    # Get max/min prices      #
    ###########################     
    fut = add_history(cl, futspan, False)
    #(m, n) = fut.shape
    max_prices = (np.max(fut, axis=1)-fut[:,0])*1.0/fut[:,0]
    min_prices = (fut[:,0] - np.min(fut, axis=1))*1.0/fut[:,0]
    
    max_prices = _cut_invalid(max_prices, invalid_past, invalid_fut)
    min_prices = _cut_invalid(min_prices, invalid_past, invalid_fut)
    
    
    return (dl, sz, X, max_prices*100, min_prices*100)