Exemplo n.º 1
0
def c_ths_back_test(lockobj, back_test_date, xg_data_list):
    tdx = dataTdx.DataTdx()
    tdx.select_fast_addr()
    ths_list = []
    rdkline = redisRW.redisrw(redisRW.db_kline)
    for xg_data in xg_data_list:
        # 把K线合并到选股数据中
        code = xg_data['code']
        klines = rdkline.read_klines_from_before_date_count_dec(
            code, back_test_date, 1)
        if len(klines) == 0:
            continue
        xg_data['kline'] = klines[0]
        data_ticks = tdx.get_history_ticks(
            code, datetime.datetime.strptime(klines[0]['datetime'], '%Y%m%d'))
        ticks_len = len(data_ticks)
        if (data_ticks is None) or ticks_len == 0:
            return
        close_tick = data_ticks[ticks_len - 1]
        xg_data['amount_open'] = Decimal(str(close_tick['amount_open']))
        th = threading.Thread(target=th_back_test_run, args=(lockobj, xg_data))
        ths_list.append(th)
        #break
    for th in ths_list:
        th.start()
    print('任务报告', os.getpid(), len(ths_list), '个线程启动完毕。')
    for th in ths_list:
        th.join()
Exemplo n.º 2
0
def th_tdx_run(lockobj, ip, port, xg_data, sleep_time_connect):
    try:
        p = push.Push(lockobj, xg_data)
    except:
        print(traceback.print_exc())
        return
    tdx = dataTdx.DataTdx(ip=ip, port=port, push=p, sleep_time_connect=sleep_time_connect)
    tdx.loop_get_realtime_tick(xg_data['code'])
Exemplo n.º 3
0
def start_quotation_process(commobj,
                            back_test_date: datetime.datetime = None,
                            process_count=3):
    # 交易时间不允许回放行情
    if commobj.is_backtest and comm.is_trade_time():
        return
    rdxg = redisRW.redisrw(redisRW.db_xg)
    rdsell = redisRW.redisrw(redisRW.db_sell)
    rdsell.del_db()
    # 读取选股数据
    xg_data_list = rdxg.read_all_dec()
    if len(xg_data_list) == 0:
        print('没有选股数据')
        wait_trade_time_end()
        return
    # 清空数据
    redisRW.redisrw(redisRW.db_realtime_tick).del_db()
    rdmainquotation = redisRW.redisrw(redisRW.db_mainquotaion)
    rdmainquotation.del_db()
    # 数据分块
    bk = math.ceil(len(xg_data_list) / process_count)
    bk_data_list = []
    temp = []
    p_list = []
    for i in range(len(xg_data_list)):
        if i > 0 and i % bk == 0:
            bk_data_list.append(temp)
            temp = []
        temp.append(xg_data_list[i])
    if len(temp) > 0:
        bk_data_list.append(temp)
    if back_test_date is None:
        tdx = dataTdx.DataTdx()
        # 可用服务器
        addrs_list_info = tdx.get_sort_address_info()
        addr_ky_count = len(addrs_list_info)
        if addr_ky_count == 0:
            print('没有可用服务器。')
            wait_trade_time_end()
            return
        print('可用服务器', addr_ky_count, '最大延时',
              addrs_list_info[addr_ky_count - 1][3], 'ms')
        # 启动通达信实时行情进程
        for b in bk_data_list:
            p = Process(target=c_ths_tdx, args=(commobj, addrs_list_info, b))
            p_list.append(p)
            p.start()
    else:
        # 启动回测行情模拟进程
        for b in bk_data_list:
            p = Process(target=c_ths_back_test,
                        args=(commobj, back_test_date, b))
            p_list.append(p)
            p.start()
    print('行情服务已启动,选股数量', len(xg_data_list))
    for p in p_list:
        p.join()
    print('行情服务结束。')
Exemplo n.º 4
0
 def stock_select(self, rdchicang, date: datetime = None):
     rdxg = redisRW.redisrw(redisRW.db_xg)
     rdxg.del_db()
     # 实例化通达信对象
     tdx = dataTdx.DataTdx()
     tdx.select_fast_addr()
     # 读取市场股票
     codes_info = tdx.get_szsh_a_codes()
     # 数据分块
     process_count = 4
     bk = math.ceil(len(codes_info) / process_count)
     bk_data_list = []
     temp = []
     p_list = []
     for i in range(len(codes_info)):
         if i > 0 and i % bk == 0:
             bk_data_list.append(temp)
             temp = []
         temp.append(codes_info[i])
     if len(temp) > 0:
         bk_data_list.append(temp)
     for b in bk_data_list:
         p = Process(target=self._xg_hot_p, args=(rdchicang, b, date))
         p_list.append(p)
         p.start()
     tm = datetime.datetime.now()
     print('正在选股...')
     for p in p_list:
         p.join()
     # 只选择3只
     """top_xg = []
     for d in rdxg.read_all_dec():
         top_xg.append(d)
         rdxg.delete(d['code'])
     top_xg = sorted(top_xg, key=lambda x: x['avg_line_zdf'], reverse=False)
     cc_len = len(rdchicang.read_codes())
     i = 0
     for d in top_xg:
         if not rdxg.write_json(d['code'], d):
             print(d['code'], '选股数据写入错误。')
         i += 1
         # 只选择3只
         if i == 3 - cc_len:
             break"""
     # 持仓
     for d in rdchicang.read_all_dec():
         rdxg.write_json(d['code'], d)
     print(datetime.datetime.now() - tm)
     print('被选股票', len(rdxg.read_codes()), '其中持仓',
           len(rdchicang.read_codes()))
Exemplo n.º 5
0
def c_ths_tdx(lockobj, addrs_list_info, xg_data_list):
    tdx = dataTdx.DataTdx()
    tdx.select_fast_addr()
    ths_list = []
    addr_ky_count = len(addrs_list_info)
    addrs_sleep_time = {}
    for ad in addrs_list_info:
        addrs_sleep_time[ad[1]] = 0.0
    rdkline = redisRW.redisrw(redisRW.db_kline)
    addr_index = 0
    for xg_data in xg_data_list:
        # 把K线合并到选股数据中
        code = xg_data['code']
        klines = rdkline.read_l_dec(code, 0, 0)
        if len(klines) == 0:
            continue
        xg_data['kline'] = klines[0]
        # 前一天ticks
        data_ticks = tdx.get_history_ticks(code, datetime.datetime.strptime(klines[0]['datetime'], '%Y%m%d'))
        ticks_len = len(data_ticks)
        if (data_ticks is None) or ticks_len == 0:
            print('前一天tick获取错误。', code)
            continue
        close_tick = data_ticks[ticks_len - 1]
        xg_data['amount_open'] = Decimal(str(close_tick['amount_open']))
        ip = addrs_list_info[addr_index][1]
        port = addrs_list_info[addr_index][2]
        th = threading.Thread(target=th_tdx_run, args=(lockobj,
                                                       ip,
                                                       port,
                                                       xg_data,
                                                       addrs_sleep_time[ip]))
        ths_list.append(th)
        addr_index += 1
        if addr_index >= addr_ky_count:
            addr_index = 0
        addrs_sleep_time[ip] += 0.5
    for th in ths_list:
        th.start()
    m = sorted(addrs_sleep_time.items(), key=lambda x: x[1], reverse=True)
    print('任务报告', os.getpid(), len(ths_list), '个线程启动完毕。最后开始时间', m[0][1], '秒。')
    for th in ths_list:
        th.join()
Exemplo n.º 6
0
def get_codes_bk():
    # 实例化通达信对象
    tdx = dataTdx.DataTdx()
    tdx.select_fast_addr()
    # 读取市场股票
    codes_info = tdx.get_szsh_a_codes()
    # 数据分块
    process_count = 4
    bk = math.ceil(len(codes_info) / process_count)
    bk_data_list = []
    temp = []
    for i in range(len(codes_info)):
        if i > 0 and i % bk == 0:
            bk_data_list.append(temp)
            temp = []
        temp.append(codes_info[i])
    if len(temp) > 0:
        bk_data_list.append(temp)
    return bk_data_list
Exemplo n.º 7
0
def get_all_clear_finance_qfq_kline_from_start_date(start_date: datetime.date):
    rdkline = redisRW.redisrw(redisRW.db_kline)
    rdkline_week = redisRW.redisrw(redisRW.db_kline_week)
    rdbaseinfo = redisRW.redisrw(redisRW.db_baseinfo)
    rdhangye = redisRW.redisrw(redisRW.db_hangye)
    rdfinance = redisRW.redisrw(redisRW.db_finance)
    # 清空数据
    rdkline.del_db()
    rdkline_week.del_db()
    # 3年的财务信息
    cw_start_year = datetime.datetime.now().year - 3
    # 获取指数
    _get_sz_index_kline(start_date)
    # 实例化通达信对象
    tdx = dataTdx.DataTdx()
    tdx.select_fast_addr()
    # 没有行业信息则更新
    if len(rdhangye.read_codes()) == 0:
        hy = dataBaostock.get_hangye()
        for h in hy:
            rdhangye.write_json(h['code'], h)
    # 读取市场股票
    codes_info = tdx.get_szsh_a_codes()
    bar = progressBar.progressbar()
    bar.start('存储%s至今历史数据' % start_date.strftime('%Y%m%d'), len(codes_info))
    for d in codes_info:
        bar.progress()
        code = d['code']
        if comm.is_kzz(code):
            klines = tdx.get_kline_from_count(code, 800)
            none_klines = tdx.get_kline_from_count(code, 800)
            k_len = len(klines)
            if k_len == 0:
                bar.out_text(code, '没有K线数据。')
                continue
            # 复权因子
            dataBaostock.qfq_adjust_factor(none_klines, klines)
            zbKline.klines_calculate(code, klines)
            if k_len != rdkline.wirte_l_datas(code, klines):
                bar.out_text(code, '历史K线数据存储错误。')
                continue
            continue
        # 前复权k线
        klines = dataBaostock.get_kline_from_start_date(code, start_date)
        # 不复权k线
        none_klines = dataBaostock.get_kline_from_start_date(code,
                                                             start_date,
                                                             adjustflag='3')
        #klines = akshareData.get_kline(code, start_date)
        k_len = len(klines)
        if k_len == 0:
            bar.out_text(code, '没有K线数据。')
            continue
        # 复权因子
        dataBaostock.qfq_adjust_factor(none_klines, klines)
        # 前复权周k线
        klines_week = dataBaostock.get_kline_from_start_date(code,
                                                             start_date,
                                                             frequency='w')
        # 计算
        zbKline.klines_calculate(code, klines_week)
        zbKline.klines_calculate(code, klines)
        if k_len != rdkline.wirte_l_datas(code, klines) or \
            len(klines_week) != rdkline_week.wirte_l_datas(code, klines_week):
            bar.out_text(code, '历史K线数据存储错误。')
            continue
        # 没有财务数据则更新
        if rdfinance.read_str(code) is None:
            # 财务信息
            finance_data = {
                'code':
                code,
                'profit':
                dataBaostock.get_profit_from_start_year(code, cw_start_year),
                'balance':
                dataBaostock.get_balance_from_start_year(code, cw_start_year),
                'cash_flow':
                dataBaostock.get_cash_flow_from_start_year(
                    code, cw_start_year),
                'growth':
                dataBaostock.get_growth_from_start_year(code, cw_start_year),
                'operation':
                dataBaostock.get_operation_from_start_year(
                    code, cw_start_year)
            }
            if not rdfinance.write_json(code, finance_data):
                bar.out_text(code, '财务信息存储错误。')
        # 没有基本信息则更新
        '''if len(rdbaseinfo.read_l_str(code, 0, 0)) == 0: