Exemple #1
0
def synthesis(input, dir=''):
    file_name = os.path.split(input)[1]
    if dir == '':
        file_path = 'data/dgn/dgn_%s' % (file_name, )
    else:
        path = 'data/dgn/%s' % dir
        u_mk_dir(path)
        file_path = '%s/dgn_%s' % (path, file_name)
    # no = 1
    # while True:
    #     file_path = 'dgn_%s_%s.txt' % (file_name, no,)
    #     if os.path.exists(file_path):
    #         no += 1
    #     else:
    #         break
    with open(file_path, 'w', encoding='utf-8') as wfile:
        codes = u_read_input(input)
        codes = Fundamental.name_to_codes(codes)
        for code in codes:
            wfile.writelines(code + '\n')
        for code in codes:
            fundamental = Fundamental.get_fundamental(code)
            picking_info = get_picking_info(code)
            tech_info = DataHandler.get_tech_index_info(code)
            synthesis_info = '%s\n%s\n%s' % (fundamental, tech_info,
                                             picking_info)
            wfile.write(synthesis_info)
            wfile.write("\n\n\n")
    pass
Exemple #2
0
def check_acti():
    table_name = 'allacti'
    last_wave = 28
    last_dif = 10
    overflow = 2.8
    concepts = u_read_input('data/hot_concepts')
    concepts_codes = Fundamental.get_codes_by_concept(concepts)

    conn = sqlite3.connect(PICKING_DB)
    cursor = conn.cursor()
    cursor.execute(
        "select distinct code from '%s' where last_wave < %s and activity < %s and waves > %s and avg_limit > %s and last_dif < %s"
        % (table_name, last_wave, ACTIVITY_THRESHOLD, WAVE_COUNT_THRESHOLD,
           LIMIT_COUNT_THRESHOLD, last_dif))

    codes = [row[0] for row in cursor if row[0] in concepts_codes]
    codes = Fundamental.remove_st(codes)
    if codes:
        results = {}
        df = get_realtime_quotes([key for key in codes])
        for index, row in df.iterrows():
            code = row['code']
            pre_close = float(row['pre_close'])
            price = float(row['price'])
            rcp = round((price / pre_close - 1) * 100, 2)
            if rcp > overflow:
                results[code] = price
        u_write_to_file(u_create_path_by_system('t_acti_check.txt'), results)
        return results
    return {}
Exemple #3
0
def all_acti():
    table_name = 'allacti'
    scope = WAVE_SCOPE_THRESHOLD
    indent = 7
    stat_activity('allacti', True, scope, u_month_befor(indent * 4),
                  u_month_befor(0))
    # (waves > 30 and activity < 32) or (waves > 20 and activity < 30)
    conn = sqlite3.connect(PICKING_DB)
    cursor = conn.cursor()
    cursor.execute(
        "select distinct code from '%s' where activity < %s and waves > %s and avg_limit > %s order by activity"
        % (table_name, ACTIVITY_THRESHOLD, WAVE_COUNT_THRESHOLD,
           LIMIT_COUNT_THRESHOLD))

    u_write_to_file(u_create_path_by_system('t_acti_all.txt'),
                    Fundamental.remove_st([row[0] for row in cursor]))
    cursor.execute(
        "select distinct code from '%s' where l_close_p > 1 and last_wave < 0 and activity < %s and waves > %s and avg_limit > %s and last_dif < 10 order by activity"
        % (table_name, ACTIVITY_THRESHOLD, WAVE_COUNT_THRESHOLD,
           LIMIT_COUNT_THRESHOLD))
    u_write_to_file(u_create_path_by_system('t_acti_today.txt'),
                    Fundamental.remove_st([row[0] for row in cursor]))

    u_write_to_file(u_create_path_by_system('t_acti_lht.txt'),
                    picking_get_lht())
Exemple #4
0
def daily_run():
    # DataHandler.run_daily_batch()
    # DataHandler.download_concept_detail()
    # DataHandler.cook_raw_db()
    # DataHandler.create_today_table()
    # DataHandler.download_index()
    # DataHandler.create_attention_ex()
    #
    # dh_daily_run()
    # picking_daily()
    # find_macd_target()
    Fundamental.daily_run()
    PositionMgr.daily_run()
    daily_synthesis()
    daily_stat_rtm()
Exemple #5
0
def check_attention():
    overflow = 2.8
    # concepts = u_read_input('data/hot_concepts')
    # concepts_codes = Fundamental.get_codes_by_concept(concepts)
    codes1 = u_read_input('data/attention.txt')
    codes2 = u_read_input('data/selection.txt')

    codes = Fundamental.name_to_codes(set(codes1 + codes2))
    codes = Fundamental.remove_st(codes)
    # codes = [e for e in codes if e in concepts_codes]
    if codes:
        results = {}
        df = get_realtime_quotes([key for key in codes])
        for index, row in df.iterrows():
            code = row['code']
            pre_close = float(row['pre_close'])
            price = float(row['price'])
            rcp = round((price / pre_close - 1) * 100, 2)
            if rcp > overflow:
                results[code] = price
        u_write_to_file(u_create_path_by_system('t_attention_check.txt'),
                        results)
        return results
    return {}
Exemple #6
0
def analyze_activity(table_name, activity, avg_limit, count_limit):
    print('analyze_activity...')
    conn = sqlite3.connect(PICKING_DB)
    cursor = conn.cursor()
    cursor.execute("select distinct code from '%s'" % table_name)
    codes = [row[0] for row in cursor]
    result = []
    for code in codes:
        cursor.execute(
            "select activity,avg_limit from '%s' where code = '%s'" %
            (table_name, code))
        select = True
        count = 0
        for row in cursor:
            count += 1
            if row[0] > activity or row[0] == 0 or row[1] < avg_limit:
                select = False
        if select and count > count_limit:
            result.append(code)
    result = Fundamental.remove_st(result)
    u_write_to_file(u_create_path_by_system('t_acti_jx.txt'), result)
Exemple #7
0
def analyze_backtest_by_code(table_name, begin_date, end_date, output):
    print('begin analyze by code')
    codesql = "select distinct code from '%s'" % table_name
    selsql = "select * from '%s'" % table_name
    conds = []

    conds.append("s_date>='%s'" % begin_date)

    conds.append("s_date<='%s'" % end_date)

    codesql += " where " + ' and '.join(conds)

    conn = sqlite3.connect(BT_RESULT_DB)
    cursor = conn.cursor()
    cursor.execute(codesql)
    codes = []
    for row in cursor:
        codes.append(row[0])

    # 回测分析表
    analyze_table = 'analyze_code_%s' % table_name
    insert_sql = "insert into '%s' values (?,?,?,?,?,?,?,?,?)" % analyze_table
    cursor.execute("drop table if exists '%s'" % analyze_table)
    cursor.execute(
        "create table if not exists '%s' (code TEXT,win_time INT,loss_time INT,win_rate REAL,rt_rate REAL,max_win_rt REAL ,max_loss_rt REAL,avg_win_rt REAL,avg_loss_rt REAL)" % analyze_table)

    for code in codes:
        print('analyze code:%s' % code)
        codeconds = conds + ["code='%s'" % code]
        cursor.execute(selsql + " where " + ' and '.join(codeconds))
        win_time = 0
        loss_time = 0
        win_rate = 0
        max_rt_rate = 0
        min_rt_rate = 0
        avg_win_rt_rate = 0
        avg_loss_rt_rate = 0
        rt_rate = 0

        for row in cursor:
            if row[5] > 0:
                win_time += 1
                avg_win_rt_rate += row[5]
                if row[5] > max_rt_rate:
                    max_rt_rate = row[5]
            else:
                loss_time += 1
                if row[5] < min_rt_rate:
                    min_rt_rate = row[5]
                avg_loss_rt_rate += row[5]
        if win_time or loss_time:
            rt_rate = round((avg_loss_rt_rate + avg_win_rt_rate) / (win_time + loss_time), 3)
        if win_time:
            avg_win_rt_rate /= win_time
        if loss_time:
            avg_loss_rt_rate /= loss_time

        if win_time > 0:
            win_rate = round(win_time * 100 / (win_time + loss_time), 3)

        cursor.execute(insert_sql, (
            code, win_time, loss_time, win_rate, rt_rate, max_rt_rate, min_rt_rate, round(avg_win_rt_rate, 3),
            round(avg_loss_rt_rate, 3)))
        conn.commit()

    cursor.execute(
        "select code from '%s' where win_time >= 8 and win_rate > 50 and rt_rate > 0 order by win_rate desc" % analyze_table)
    u_write_to_file(output, Fundamental.remove_st([row[0] for row in cursor]))
Exemple #8
0
def rtm(debug=False):
    already_notify = rtm_get_already_notified()

    # import itchat
    # itchat.auto_login(hotReload=True)
    # groups = itchat.get_chatrooms(update=True)
    # target_group = ''
    # nickname = '三语股票测试'
    # # nickname = '三语股票测试' if debug else '三语股票'
    # for group in groups:
    #     if group['NickName'] == nickname:
    #         target_group = group['UserName']
    #         break

    def notify_fu(type, result, already_notify):
        need_notify = set(result) - already_notify
        already_notify |= set(result)
        if need_notify:
            rtm_save_item({e: result[e] for e in need_notify}, type)
            msg = '%s tagrgets:\n%s' % (type, ' '.join(need_notify))

            tempname = '%s_%s_%s.txt' % (u_time_now_filename(),
                                         random.randint(1, 5), type)
            temppath = 'data/%s' % u_day_befor(0)
            u_mk_dir(temppath)
            u_write_to_file('%s/%s' % (temppath, tempname), need_notify)
            all_notify = 'data/%s/all_notify.txt' % u_day_befor(0)
            u_write_to_file(all_notify, already_notify)
            synthesis(all_notify, u_day_befor(0))
            synthesis('%s/%s' % (temppath, tempname), u_day_befor(0))
            # itchat.send(msg, toUserName=target_group)
            # u_itchat_send_file('data/dgn/%s/dgn_%s' % (u_day_befor(0),tempname), toUserName=target_group)

    print('enter while')
    while True:

        result = check_attention()
        if result:
            synthesis(u_create_path_by_system('t_attention_check.txt'))
        notify_fu('attention', result, already_notify)

        result = dh_check_macd()
        if result:
            synthesis(u_create_path_by_system('t_macd_check.txt'))
        notify_fu('macd', result, already_notify)

        result = check_acti()
        if result:
            synthesis(u_create_path_by_system('t_acti_check.txt'))
        notify_fu('acti', result, already_notify)

        result = Fundamental.check_doctor()
        if result:
            synthesis(u_create_path_by_system('t_doctor_check.txt'))
        notify_fu('doctor', result, already_notify)

        result = Fundamental.check_yuzen()
        if result:
            synthesis(u_create_path_by_system('t_yuzen_check.txt'))
        notify_fu('yuzen', result, already_notify)

        now = datetime.datetime.now()
        if now.hour >= 15:
            break
        if (now.hour == 11 and now.minute >= 30) or (
                now.hour >= 12 and now.hour < 13) or (now.hour == 9
                                                      and now.minute < 26):
            time.sleep(5)
        continue

    u_write_to_file(u_create_path_by_system('t_notified.txt'), already_notify)
    synthesis(u_create_path_by_system('t_notified.txt'))
    # u_itchat_send_file('data/dgn/dgn_t_notified.txt', toUserName=target_group)
    daily_stat_rtm()

    print('safe quit')
Exemple #9
0
def stat_rtm(days):
    conn = sqlite3.connect(RTM_DB)
    cursor = conn.cursor()
    table_name = RTM_TB
    cursor.execute(
        "select distinct date_ from '%s' order by date_ desc limit %s" %
        (table_name, days))
    dates = [row[0] for row in cursor]
    cursor.execute("select * from '%s' where date_ in (%s)" %
                   (table_name, ','.join(['%s' % e for e in dates])))
    records = [row for row in cursor]
    code_prices = u_get_realtime_price({e[0] for e in records})
    code_names = Fundamental.get_codes()
    code_details = []

    types = set()
    result = []
    codes = []

    for rc in records:
        code = rc[0]
        codes.append(code)
        ntfy_price = rc[3]
        now_price = code_prices[rc[0]]
        range = round((now_price / ntfy_price - 1) * 100, 2)
        type = rc[4]
        code_details.append(
            '%-6s  %s %s   %-10s  当时价格:%6s元     现价:%6s元   价格变化幅度:%6s%%' %
            (code_names[code], rc[1], rc[2], type, ntfy_price, now_price,
             range))
        types.add(type)
        result.append({'type': type, 'range': range})

    types.add('')
    stat_detail = []
    for type in types:
        win = len([
            e for e in result
            if (type == '' or e['type'] == type) and e['range'] > 0
        ])
        los = len([
            e for e in result
            if (type == '' or e['type'] == type) and e['range'] <= 0
        ])
        avg_rr = round(
            sum([
                e['range'] for e in result if (type == '' or e['type'] == type)
            ]) / (win + los), 2)
        win_r = round(win * 100 / (win + los), 2)

        stat_detail.append(
            '%-10s  成功:%2s  次   失败:%2s  次   平均回报:%6s%%    胜率:%6s%%' %
            (type if type != '' else 'total', win, los, avg_rr, win_r))
    info = '''%s
    
    
%s
''' % ('\n'.join(stat_detail), '\n'.join(code_details))
    with open('data/reports/rtm_%s.txt' % (days), 'w',
              encoding='utf-8') as wfile:
        wfile.write(info)

    u_write_to_file(u_create_path_by_system('t_notified_%s.txt') % days, codes)
    # synthesis('/Users/hero101/Documents/t_notified_%s.txt' % days)
    pass