コード例 #1
0
def login():
    # 登录行情服务器
    user = MyMdApi(instruments=inst,
                   broker_id=BROKER_ID,
                   investor_id=INVESTOR_ID,
                   password=PASSWORD)
    user.Create("data")
    user.RegisterFront(ADDRESS_MD)
    user.Init()

    print('行情服务器登录成功')

    bars = load_data_from_server(server_base='http://106.14.119.122',
                                 instruments_id=inst[0],
                                 granularity=granularity)

    while True:

        if Utils.exit_flag:
            msg = 'Thread CTPDataCollectEngine say bye-bye'
            print(msg)
            logger.info(msg=msg)

            return

        try:
            payload = q_depth_market_data.get(timeout=1)
            q_depth_market_data.task_done()

            instrument_id = payload.InstrumentID
            action_day = payload.ActionDay
            update_time = payload.UpdateTime.replace(':', '')
            last_price = payload.LastPrice
            volume = payload.Volume

            if volume == 0:
                continue

            tickToBar(payload, 13)

            if not q_bar.empty():
                bar = q_bar.get()
                bars.append(bar)
                high = get_k_line_column(data=bars, depth=20)
                low = get_k_line_column(data=bars, ohlc='low')

                ma_5 = ma(elements=high, step=5)
                ma_10 = ma(elements=high, step=10)
                print(high)
                print(ma_5)
                print(ma_10)
                cu = cross(ma_5, ma_10)
                print(cu)
                far = be_apart_from(cu)
                print(far)

        except queue.Empty as e:
            pass
コード例 #2
0
    def run_loop(self):
        while True:
            if Utils.exit_flag:
                msg = 'Thread vir_event_loop_poll_run say bye-bye'
                print msg
                logger.info(msg=msg)
                return

            self.run_once()
コード例 #3
0
ファイル: data_arrange_engine.py プロジェクト: pqctp/quotesys
    def launch(cls):
        workdays = TradingPeriod.get_workdays(begin='2016-12-31',
                                              end='2019-12-31')
        workdays_exchange_trading_period_by_ts = \
            TradingPeriod.get_workdays_exchange_trading_period(
                _workdays=workdays, exchange_trading_period=EXCHANGE_TRADING_PERIOD)

        while True:
            if Utils.exit_flag:
                msg = 'Thread DataArrangeEngine say bye-bye'
                print msg
                logger.info(msg=msg)

                return

            try:
                awp_tick = db.r.lpop(app.config['data_stream_queue'])

                if awp_tick is None:
                    time.sleep(1)
                    continue

                awp_tick = json.loads(awp_tick)

                # 过滤交易量为 0 的假数据
                if 'volume' in awp_tick and awp_tick['volume'] == 0:
                    continue

                contract_code = pattern.match(
                    awp_tick['instrument_id']).group()
                action_day = awp_tick['action_day']
                update_time = awp_tick['update_time']
                # 时间合法性校验
                if not trading_time_filter(
                        date_time=' '.join([action_day, update_time]),
                        contract_code=contract_code,
                        exchange_trading_period_by_ts=
                        workdays_exchange_trading_period_by_ts['-'.join([
                            action_day[:4], action_day[4:6], action_day[6:]
                        ])]):
                    continue

                cls.data_arrange(awp_tick=awp_tick)

            except AttributeError as e:
                logger.error(traceback.format_exc())
                time.sleep(1)

                if db.r is None:
                    db.init_conn_redis()

            except Exception as e:
                logger.error(traceback.format_exc())
                time.sleep(1)
コード例 #4
0
ファイル: event_processor.py プロジェクト: zhaozlgit/JimV-C
    def launch(cls):
        logger.info(msg='Thread EventProcessor is launched.')
        while True:
            if Utils.exit_flag:
                msg = 'Thread EventProcessor say bye-bye'
                print msg
                logger.info(msg=msg)

                return

            try:
                report = db.r.lpop(app.config['upstream_queue'])

                if report is None:
                    time.sleep(1)
                    continue

                cls.message = json.loads(report)

                if cls.message['kind'] == EmitKind.log.value:
                    cls.log_processor()

                elif cls.message['kind'] == EmitKind.guest_event.value:
                    cls.guest_event_processor()

                elif cls.message['kind'] == EmitKind.host_event.value:
                    cls.host_event_processor()

                elif cls.message['kind'] == EmitKind.response.value:
                    cls.response_processor()

                elif cls.message[
                        'kind'] == EmitKind.guest_collection_performance.value:
                    cls.guest_collection_performance_processor()

                elif cls.message[
                        'kind'] == EmitKind.host_collection_performance.value:
                    cls.host_collection_performance_processor()

                else:
                    pass

            except AttributeError as e:
                logger.error(traceback.format_exc())
                time.sleep(1)

                if db.r is None:
                    db.init_conn_redis()

            except Exception as e:
                logger.error(traceback.format_exc())
                time.sleep(1)
コード例 #5
0
ファイル: main.py プロジェクト: zhaozlgit/JimV-C
def ws_engine_for_vnc():

    logger.info(msg='VNC ws engine is launched.')

    while True:
        payload = db.r.lpop(app.config['ipc_queue'])

        if payload is None:
            time.sleep(1)
            continue

        payload = json.loads(payload)

        c_pid = os.fork()
        if c_pid == 0:
            instantiation_ws_vnc(payload['listen_port'], payload['target_host'], payload['target_port'])

        # 因为 WebSocketProxy 使用了 daemon 参数,所以当执行到 ws.start_server() 时,会退出其所在的子进程,
        # 故而这里设置wait来处理结束的子进程的环境,避免出现僵尸进程。
        os.wait()
コード例 #6
0
def login():
    # 登录行情服务器
    user = MyMdApi(instruments=inst,
                   broker_id=BROKER_ID,
                   investor_id=INVESTOR_ID,
                   password=PASSWORD)
    user.Create("data")
    user.RegisterFront(ADDRESS_MD)
    user.Init()

    print u'行情服务器登录成功'

    while True:

        if Utils.exit_flag:
            msg = 'Thread CTPDataCollectEngine say bye-bye'
            print msg
            logger.info(msg=msg)

            return

        try:
            payload = q_depth_market_data.get(timeout=1)
            q_depth_market_data.task_done()

            awp_tick = {
                'granularities': granularities,
                'instrument_id': payload.InstrumentID,
                'last_price': payload.LastPrice,
                'action_day': payload.ActionDay,
                'update_time': payload.UpdateTime.replace(':', ''),
                'volume': payload.Volume
            }

            print awp_tick
            db.r.rpush(app.config['data_stream_queue'],
                       json.dumps(awp_tick, ensure_ascii=False))

        except Queue.Empty as e:
            pass
コード例 #7
0
def login():
    # 登录行情服务器
    user = MyMdApi(instruments=inst, broker_id=BROKER_ID, investor_id=INVESTOR_ID, password=PASSWORD)
    user.Create("data")
    user.RegisterFront(ADDR_MD)
    user.Init()

    print('行情服务器登录成功')

    while True:

        if Utils.exit_flag:
            msg = 'Thread CTPDataCollectEngine say bye-bye'
            print(msg)
            logger.info(msg=msg)

            return

        try:
            payload = q_depth_market_data.get(timeout=1)
            print(payload)
            q_depth_market_data.task_done()

            instrument_id = payload.InstrumentID
            action_day = payload.ActionDay
            update_time = payload.UpdateTime.replace(':', '')
            last_price = payload.LastPrice
            volume = payload.Volume

            if volume == 0:
                continue

            if update_time.find('.') != -1:
                dt = datetime.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S.%f")
                timestamp = time.mktime(dt.timetuple()) + (dt.microsecond / 1e6)

            else:
                timestamp = int(time.mktime(time.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S")))

            date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))

            ohlc_key = generate_ohlc_key(instrument_id=instrument_id, granularity=granularity, timestamp=timestamp)

            if ohlc_key not in nest:
                nest[ohlc_key] = {
                    'date_time': date_time,
                    'last_timestamp': timestamp,
                    'high': last_price,
                    'low': last_price,
                    'close': last_price,
                    'open': last_price
                }

            nest[ohlc_key]['last_timestamp'] = timestamp
            nest[ohlc_key]['date_time'] = date_time

            nest[ohlc_key]['close'] = last_price

            if last_price > decimal.Decimal(nest[ohlc_key]['high']):
                nest[ohlc_key]['high'] = last_price

            elif last_price < decimal.Decimal(nest[ohlc_key]['low']):
                nest[ohlc_key]['low'] = last_price

            if nest.__len__() > 1:
                for k, v in list(nest.items()):
                    if k == ohlc_key:
                        continue
                    data.append(nest[k]) #
                    del nest[k]

                high = get_k_line_column(data=data, depth=20)
                ma_5 = ma(elements=high, step=5)
                ma_10 = ma(elements=high, step=10)
                # print high
                # print ma_5
                # print ma_10
                cu = cross(ma_5, ma_10)
                print(cu)
                far = be_apart_from(cu)
                print(far)

            print(nest)

        except queue.Empty as e:
            pass
コード例 #8
0
ファイル: main.py プロジェクト: 1032231418/JimV-N
def main():
    pidfile = PidFile(file_name=config['pidfile'])
    pidfile.create(pid=os.getpid())
    atexit.register(pidfile.unlink)

    threads = []

    signal.signal(signal.SIGTERM, Utils.signal_handle)
    signal.signal(signal.SIGINT, Utils.signal_handle)

    guest_creating_progress_report_engine_engine = Host()
    t_ = threading.Thread(target=guest_creating_progress_report_engine_engine.
                          guest_creating_progress_report_engine,
                          args=())
    threads.append(t_)

    host_use_for_instruction_process_engine = Host()
    t_ = threading.Thread(target=host_use_for_instruction_process_engine.
                          instruction_process_engine,
                          args=())
    threads.append(t_)

    host_use_for_host_state_report_engine = Host()
    t_ = threading.Thread(
        target=host_use_for_host_state_report_engine.host_state_report_engine,
        args=())
    threads.append(t_)

    host_use_for_guest_performance_collection_engine = Host()
    t_ = threading.Thread(
        target=host_use_for_guest_performance_collection_engine.
        guest_performance_collection_engine,
        args=())
    threads.append(t_)

    host_use_for_host_performance_collection_engine = Host()
    t_ = threading.Thread(
        target=host_use_for_host_performance_collection_engine.
        host_performance_collection_engine,
        args=())
    threads.append(t_)

    vir_event_loop_poll_register()
    t_ = threading.Thread(target=vir_event_loop_poll_run,
                          name="libvirtEventLoop")
    threads.append(t_)

    host_use_for_refresh_guest_state = Host()
    host_use_for_refresh_guest_state.refresh_guest_state()

    for t in threads:
        t.setDaemon(True)
        t.start()

    EventProcess.guest_event_register()

    while True:
        if Utils.exit_flag:
            # 主线程即将结束
            EventProcess.guest_event_deregister()
            break

        if config['DEBUG']:
            print threads_status

        time.sleep(1)

    # 等待子线程结束
    for t in threads:
        t.join()

    msg = 'Main say bye-bye!'
    print msg
    logger.info(msg=msg)
コード例 #9
0
def main():
    pidfile = PidFile(file_name=config['pidfile'])
    pidfile.create(pid=os.getpid())
    atexit.register(pidfile.unlink)

    threads = []

    signal.signal(signal.SIGTERM, Utils.signal_handle)
    signal.signal(signal.SIGINT, Utils.signal_handle)

    t_ = threading.Thread(target=Host().guest_creating_progress_report_engine,
                          args=())
    threads.append(t_)

    t_ = threading.Thread(target=Host().guest_state_report_engine, args=())
    threads.append(t_)

    t_ = threading.Thread(target=Host().instruction_process_engine, args=())
    threads.append(t_)

    t_ = threading.Thread(target=Host().host_state_report_engine, args=())
    threads.append(t_)

    t_ = threading.Thread(target=Host().guest_performance_collection_engine,
                          args=())
    threads.append(t_)

    t_ = threading.Thread(target=Host().host_performance_collection_engine,
                          args=())
    threads.append(t_)

    vir_event_loop_poll_register()
    t_ = threading.Thread(target=vir_event_loop_poll_run,
                          name="libvirtEventLoop")
    threads.append(t_)

    for t in threads:
        t.setDaemon(True)
        t.start()

    i = 0
    while not eventLoop.runningPoll and i <= 10:
        """
        避免在 timer 还没启动的时候,就去注册事件。那样会抛出如下异常:
        libvirtError: internal error: could not initialize domain event timer
        """
        i += 1
        time.sleep(1)

    EventProcess.guest_event_register()

    while True:
        if Utils.exit_flag:
            # 主线程即将结束
            EventProcess.guest_event_deregister()
            break

        if config['DEBUG']:
            print threads_status

        for k, v in threads_status.items():
            # 如果某个引擎脱线 120 秒,则自动重启 JimV-N。
            if (ji.Common.ts() - v['timestamp']) > 120:
                Host.restart()

        time.sleep(1)

    # 等待子线程结束
    for t in threads:
        t.join()

    msg = 'Main say bye-bye!'
    print msg
    logger.info(msg=msg)