xtquant.xtview

  1#coding:utf-8
  2
  3import os, sys
  4import datetime as dt
  5import traceback
  6import json
  7
  8from . import xtbson as bson
  9
 10from .IPythonApiClient import IPythonApiClient as RPCClient
 11
 12def try_except(func):
 13    def wrapper(*args, **kwargs):
 14        try:
 15            return func(*args, **kwargs)
 16        except Exception:
 17            exc_type, exc_instance, exc_traceback = sys.exc_info()
 18            formatted_traceback = ''.join(traceback.format_tb(exc_traceback))
 19            message = '\n{0} raise {1}:{2}'.format(
 20                formatted_traceback,
 21                exc_type.__name__,
 22                exc_instance
 23            )
 24            # raise exc_type(message)
 25            print(message)
 26            return None
 27
 28    return wrapper
 29
 30
 31CLIENT = None
 32
 33from os.path import abspath, dirname
 34__curdir = dirname(abspath(__file__))
 35
 36__rpc_config = __curdir + '/xtdata.ini'
 37__xtdata_config = __curdir + '/xtdata.ini'
 38
 39from .IPythonApiClient import rpc_init
 40__rpc_init_status = rpc_init(__rpc_config)
 41if __rpc_init_status < 0:
 42    print(f'rpc初始化失败,配置文件:{__rpc_config}')
 43
 44def get_client():
 45    global CLIENT
 46    if not CLIENT:
 47        CLIENT = RPCClient('client_xtdata', __xtdata_config)
 48
 49        try:
 50            CLIENT.load_config(__xtdata_config)
 51            config = json.load(open(os.path.join('..', 'config', 'xtquantconfig.json')))
 52            port = config['port']
 53            CLIENT.set_remote_addr('localhost', port)
 54            CLIENT.reset()
 55            CLIENT.connect_ex()
 56        except Exception as e:
 57            pass
 58
 59        if not CLIENT.is_connected():
 60            CLIENT.load_config(__xtdata_config)
 61            CLIENT.reset()
 62
 63    if not CLIENT.is_connected():
 64        succ, errmsg = CLIENT.connect_ex()
 65        if not succ:
 66            raise Exception("无法连接服务!")
 67    return CLIENT
 68
 69
 70def reconnect(ip = 'localhost', port = 58610):
 71    global CLIENT
 72    CLIENT = None
 73    if not CLIENT:
 74        CLIENT = RPCClient('client_xtdata', __xtdata_config)
 75
 76        CLIENT.load_config(__xtdata_config)
 77        CLIENT.set_remote_addr(ip, port)
 78        CLIENT.reset()
 79        CLIENT.connect()
 80
 81    if not CLIENT.is_connected():
 82        if not CLIENT.connect():
 83            raise Exception("无法连接服务!")
 84    return
 85
 86def create_view(viewID, view_type, title, group_id):
 87    client = get_client()
 88    return client.createView(viewID, view_type, title, group_id)
 89
 90#def reset_view(viewID):
 91#    return
 92
 93def close_view(viewID):
 94    client = get_client()
 95    return client.closeView(viewID)
 96
 97#def set_view_index(viewID, datas):
 98#    '''
 99#    设置模型指标属性
100#    index: { "output1": { "datatype": se::OutputDataType } }
101#    '''
102#    client = get_client()
103#    return client.setViewIndex(viewID, datas)
104
105def push_view_data(viewID, datas):
106    '''
107    推送模型结果数据
108    datas: { "timetags: [t1, t2, ...], "outputs": { "output1": [value1, value2, ...], ... }, "overwrite": "full/increase" }
109    '''
110    client = get_client()
111    bresult = client.pushViewData(viewID, 'index', bson.BSON.encode(datas))
112    return bson.BSON.decode(bresult)
def try_except(func):
13def try_except(func):
14    def wrapper(*args, **kwargs):
15        try:
16            return func(*args, **kwargs)
17        except Exception:
18            exc_type, exc_instance, exc_traceback = sys.exc_info()
19            formatted_traceback = ''.join(traceback.format_tb(exc_traceback))
20            message = '\n{0} raise {1}:{2}'.format(
21                formatted_traceback,
22                exc_type.__name__,
23                exc_instance
24            )
25            # raise exc_type(message)
26            print(message)
27            return None
28
29    return wrapper
CLIENT = None
def get_client():
45def get_client():
46    global CLIENT
47    if not CLIENT:
48        CLIENT = RPCClient('client_xtdata', __xtdata_config)
49
50        try:
51            CLIENT.load_config(__xtdata_config)
52            config = json.load(open(os.path.join('..', 'config', 'xtquantconfig.json')))
53            port = config['port']
54            CLIENT.set_remote_addr('localhost', port)
55            CLIENT.reset()
56            CLIENT.connect_ex()
57        except Exception as e:
58            pass
59
60        if not CLIENT.is_connected():
61            CLIENT.load_config(__xtdata_config)
62            CLIENT.reset()
63
64    if not CLIENT.is_connected():
65        succ, errmsg = CLIENT.connect_ex()
66        if not succ:
67            raise Exception("无法连接服务!")
68    return CLIENT
def reconnect(ip='localhost', port=58610):
71def reconnect(ip = 'localhost', port = 58610):
72    global CLIENT
73    CLIENT = None
74    if not CLIENT:
75        CLIENT = RPCClient('client_xtdata', __xtdata_config)
76
77        CLIENT.load_config(__xtdata_config)
78        CLIENT.set_remote_addr(ip, port)
79        CLIENT.reset()
80        CLIENT.connect()
81
82    if not CLIENT.is_connected():
83        if not CLIENT.connect():
84            raise Exception("无法连接服务!")
85    return
def create_view(viewID, view_type, title, group_id):
87def create_view(viewID, view_type, title, group_id):
88    client = get_client()
89    return client.createView(viewID, view_type, title, group_id)
def close_view(viewID):
94def close_view(viewID):
95    client = get_client()
96    return client.closeView(viewID)
def push_view_data(viewID, datas):
106def push_view_data(viewID, datas):
107    '''
108    推送模型结果数据
109    datas: { "timetags: [t1, t2, ...], "outputs": { "output1": [value1, value2, ...], ... }, "overwrite": "full/increase" }
110    '''
111    client = get_client()
112    bresult = client.pushViewData(viewID, 'index', bson.BSON.encode(datas))
113    return bson.BSON.decode(bresult)

推送模型结果数据 datas: { "timetags: [t1, t2, ...], "outputs": { "output1": [value1, value2, ...], ... }, "overwrite": "full/increase" }