예제 #1
0
def identify_data_hist_price_wave(periods=10, start='2015-05-01'):
    ps = Postgresql()
    cs = getinfo.get_codes()
    i, s = 0, len(cs)
    for c in cs:
        starttime = datetime.datetime.now()
        i += 1
        sql = "SELECT date, close, qfq, high, low FROM data_hist WHERE code='%s' AND date>='%s' ORDER BY date DESC" % (
            c, start)
        rs = ps.fetchall(sql)
        rows = len(rs)
        if rows <= periods:
            continue
        sql = "UPDATE data_hist SET ispeak=null, isbott=null WHERE (ispeak OR isbott) AND code='%s';" % c
        for j in range(rows - periods):
            ispeak = isbott = True
            for k in range(1, periods + 1):
                j_k = max([j - k, 0])
                hn = rs[j][3] * rs[j][2]
                hb = rs[j + k][3] * rs[j + k][2]
                ha = rs[j_k][3] * rs[j_k][2]
                ln = rs[j][4] * rs[j][2]
                lb = rs[j + k][4] * rs[j + k][2]
                la = rs[j_k][4] * rs[j_k][2]
                if hn <= hb or hn < ha: ispeak = False
                if ln >= lb or ln > la: isbott = False
            if ispeak:
                sql += "UPDATE data_hist SET ispeak=true WHERE code='%s' AND date='%s';" % (
                    c, rs[j][0])
            if isbott:
                sql += "UPDATE data_hist SET isbott=true WHERE code='%s' AND date='%s';" % (
                    c, rs[j][0])
        ps.execute(sql)
        endtime = datetime.datetime.now()
        print(
            "[%d/%d=%.1f%%] [%s] ispeak and isbott are identified! time=[%s]" %
            (i, s, float(i) / float(s) * 100, c, endtime - starttime))
    ps.close()
    print('is ok')
예제 #2
0
def reflesh_profit_stock_info():
    fb.write('start refleshing stock info on table [profit] .')
    ps = Postgresql()
    sql = "SELECT code, name, industry, area, bvps, esp, outstanding, totals, \"timeToMarket\" FROM stock_basics ORDER BY code"
    rs = ps.fetchall(sql)
    i = 0
    for r in rs:
        ps.update_insert(table='profit',
                         where="code='%s'" % r[0],
                         code=r[0],
                         name=r[1],
                         industry=r[2],
                         area=r[3],
                         bvps=r[4],
                         esp=r[5],
                         outs=r[6],
                         tots=r[7],
                         market=str(r[8]))
        i += 1
        if i % 100 == 0:
            fb.write('.')
    ps.close()
    fb.write(' is done!\n')
예제 #3
0
def reflesh_data_hist_fq():
    print('start refleshing hfq & qfq values on table [data_hist] ...')
    ps = Postgresql()
    rs = ps.fetchall("SELECT DISTINCT code FROM data_hist ORDER BY code;")
    j, jj, s = 0, 0, len(rs)
    for r in rs:
        starttime = datetime.datetime.now()
        c = r[0]
        j += 1
        date = '1975-09-02'
        #r = ps.fetchone("SELECT date FROM data_hist WHERE code='%s' AND hfq>0 ORDER BY date DESC LIMIT 1;" % c)
        r = ps.fetchone(
            "SELECT max(date) FROM data_hist WHERE code='%s' AND hfq>0;" % c)
        if r[0] is not None:  # if r: = if r is not None:
            date = r[0]
        else:
            tmp = ps.fetchone(
                "SELECT min(date) FROM data_hist WHERE code='%s';" % c)
            if tmp[0] is None: continue
            date = tmp[0]
            ps.execute(
                "UPDATE data_hist SET hfq=1 WHERE code='%s' AND date='%s';" %
                (c, date))
        # start set hfq value
        b_cls, b_hfq, hfq, i, sql = 0, 0, 0, 0, ''
        rs = ps.fetchall(
            "SELECT date, close, price_change, hfq FROM data_hist WHERE code='%s' AND date>='%s' ORDER BY date;"
            % (c, date))
        if rs is not None:
            for R in rs:
                i += 1
                cls, pcg = float(R[1]), float(R[2])
                if i == 1:
                    hfq = R[3]
                else:
                    if cls - pcg - b_cls < -0.02:
                        hfq = b_cls / (cls - pcg) * b_hfq
                    else:
                        hfq = b_hfq
                    sql += "UPDATE data_hist SET hfq=%f WHERE code='%s' AND date='%s';" % (
                        hfq, c, R[0])
                b_cls = cls
                b_hfq = hfq

        # start set qfq value
        mr = ps.fetchone(
            "SELECT hfq FROM data_hist WHERE code='%s' AND qfq is null ORDER BY date DESC"
            % c)  #second newly hfq
        if mr is not None:
            if hfq == mr[0]:
                sql += "UPDATE data_hist SET qfq=hfq/%f WHERE code='%s' AND qfq is null;" % (
                    hfq, c)
            else:
                sql += "UPDATE data_hist SET qfq=hfq/%f WHERE code='%s';" % (
                    hfq, c)
            if sql != '': ps.execute(sql)
        endtime = datetime.datetime.now()
        print(
            '[%d/%d=%.1f%%] [%s] hfq & qfq values is computed and saved! time=[%s]'
            % (j, s, float(j) / float(s) * 100, c, endtime - starttime))
    print('all hfq & qfq data on table [data_hist] computed and saved!')