kaq-quant-common 0.2.9__py3-none-any.whl → 0.2.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,106 +1,106 @@
1
- import threading
2
- import traceback
3
- from abc import abstractmethod
4
-
5
- import dolphindb as ddb
6
- import numpy as np
7
- from kaq_quant_common.common.monitor_base import MonitorBase
8
- from kaq_quant_common.utils import logger_utils
9
-
10
- mutex = threading.Lock()
11
-
12
-
13
- # ddb表订阅监听器
14
- class DdbTableMonitor(MonitorBase):
15
-
16
- def __init__(self, table_name: str, action_name: str, batch_size=1000, filter=[]):
17
- # 表名
18
- self._table_name = table_name
19
- #
20
- self._action_name = action_name
21
- #
22
- self._batch_size = batch_size
23
- #
24
- self._filter = filter
25
-
26
- # logger
27
- self._logger = logger_utils.get_logger(self)
28
- #
29
- super().__init__()
30
-
31
- # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ abstract methods
32
- def _do_init(self):
33
- # 初始化ddb
34
- self._init_ddb()
35
-
36
- def _do_start(self):
37
- # 开启ddb订阅
38
- self._start_subscribe()
39
-
40
- def _do_stop(self):
41
- # 关闭订阅
42
- self._stop_subscribe()
43
-
44
- # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
45
- # 初始化ddb
46
- def _init_ddb(self):
47
- '''
48
- 创建ddb连接 && 添加ddb流数据表支持
49
- '''
50
- try:
51
- ddb_config = self._on_get_ddb_config()
52
- host, port, user, passwd = ddb_config['host'], ddb_config['port'], ddb_config['user'], ddb_config['passwd']
53
- mutex.acquire()
54
- self._session = ddb.session(enableASYNC=True)
55
- self._host = host
56
- self._port = port
57
- self._user = user
58
- self._passwd = passwd
59
- self._session.connect(host, port, user, passwd)
60
- self._session.enableStreaming()
61
- except Exception as e:
62
- self._logger.error(f'DdbTableMonitor._init_ddb error: {str(e)} - {str(traceback.format_exc())}')
63
- finally:
64
- mutex.release()
65
-
66
- # 开启订阅
67
- def _start_subscribe(self):
68
- '''
69
- 订阅ddb表
70
- '''
71
- self._session.subscribe(
72
- self._host,
73
- self._port,
74
- self._handle,
75
- tableName=self._table_name,
76
- actionName=self._action_name,
77
- filter=np.array(self._filter),
78
- offset=-1,
79
- batchSize=self._batch_size,
80
- throttle=5,
81
- msgAsTable=True,
82
- )
83
- self._logger.info(f'开始订阅 {self._host}:{self._port} {self._table_name} - {self._action_name}')
84
-
85
- def _stop_subscribe(self):
86
- # TODO
87
- # script = f"""
88
- # existsSubscriptionTopic(,`{self._table_name},`{self._action_name})
89
- # """
90
- # exitsTopic = self._session.run(script)
91
- exitsTopic = True
92
- if exitsTopic is True:
93
- self._session.unsubscribe(self._host, self._port, self._table_name, self._action_name)
94
- self._logger.info(f'取消订阅 {self._table_name} - {self._action_name}')
95
- if not self._session.isClosed():
96
- self._session.close()
97
-
98
- # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ abstract methods
99
- # 需要返回ddb配置,包含host, port, user, passwd 添加类型提示
100
- @abstractmethod
101
- def _on_get_ddb_config(self, data) -> dict:
102
- pass
103
-
104
- @abstractmethod
105
- def _handle(self, data):
106
- pass
1
+ import threading
2
+ import traceback
3
+ from abc import abstractmethod
4
+
5
+ import dolphindb as ddb
6
+ import numpy as np
7
+ from kaq_quant_common.common.monitor_base import MonitorBase
8
+ from kaq_quant_common.utils import logger_utils
9
+
10
+ mutex = threading.Lock()
11
+
12
+
13
+ # ddb表订阅监听器
14
+ class DdbTableMonitor(MonitorBase):
15
+
16
+ def __init__(self, table_name: str, action_name: str, batch_size=1000, filter=[]):
17
+ # 表名
18
+ self._table_name = table_name
19
+ #
20
+ self._action_name = action_name
21
+ #
22
+ self._batch_size = batch_size
23
+ #
24
+ self._filter = filter
25
+
26
+ # logger
27
+ self._logger = logger_utils.get_logger(self)
28
+ #
29
+ super().__init__()
30
+
31
+ # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ abstract methods
32
+ def _do_init(self):
33
+ # 初始化ddb
34
+ self._init_ddb()
35
+
36
+ def _do_start(self):
37
+ # 开启ddb订阅
38
+ self._start_subscribe()
39
+
40
+ def _do_stop(self):
41
+ # 关闭订阅
42
+ self._stop_subscribe()
43
+
44
+ # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
45
+ # 初始化ddb
46
+ def _init_ddb(self):
47
+ '''
48
+ 创建ddb连接 && 添加ddb流数据表支持
49
+ '''
50
+ try:
51
+ ddb_config = self._on_get_ddb_config()
52
+ host, port, user, passwd = ddb_config['host'], ddb_config['port'], ddb_config['user'], ddb_config['passwd']
53
+ mutex.acquire()
54
+ self._session = ddb.session(enableASYNC=True)
55
+ self._host = host
56
+ self._port = port
57
+ self._user = user
58
+ self._passwd = passwd
59
+ self._session.connect(host, port, user, passwd)
60
+ self._session.enableStreaming()
61
+ except Exception as e:
62
+ self._logger.error(f'DdbTableMonitor._init_ddb error: {str(e)} - {str(traceback.format_exc())}')
63
+ finally:
64
+ mutex.release()
65
+
66
+ # 开启订阅
67
+ def _start_subscribe(self):
68
+ '''
69
+ 订阅ddb表
70
+ '''
71
+ self._session.subscribe(
72
+ self._host,
73
+ self._port,
74
+ self._handle,
75
+ tableName=self._table_name,
76
+ actionName=self._action_name,
77
+ filter=np.array(self._filter),
78
+ offset=-1,
79
+ batchSize=self._batch_size,
80
+ throttle=5,
81
+ msgAsTable=True,
82
+ )
83
+ self._logger.info(f'开始订阅 {self._host}:{self._port} {self._table_name} - {self._action_name}')
84
+
85
+ def _stop_subscribe(self):
86
+ # TODO
87
+ # script = f"""
88
+ # existsSubscriptionTopic(,`{self._table_name},`{self._action_name})
89
+ # """
90
+ # exitsTopic = self._session.run(script)
91
+ exitsTopic = True
92
+ if exitsTopic is True:
93
+ self._session.unsubscribe(self._host, self._port, self._table_name, self._action_name)
94
+ self._logger.info(f'取消订阅 {self._table_name} - {self._action_name}')
95
+ if not self._session.isClosed():
96
+ self._session.close()
97
+
98
+ # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ abstract methods
99
+ # 需要返回ddb配置,包含host, port, user, passwd 添加类型提示
100
+ @abstractmethod
101
+ def _on_get_ddb_config(self, data) -> dict:
102
+ pass
103
+
104
+ @abstractmethod
105
+ def _handle(self, data):
106
+ pass
@@ -1,69 +1,69 @@
1
- import threading
2
- import time
3
- from abc import abstractmethod
4
-
5
- from kaq_quant_common.common.monitor_base import MonitorBase
6
- from kaq_quant_common.common.ws_wrapper import WsWrapper
7
- from kaq_quant_common.utils import logger_utils
8
-
9
-
10
- # 封装http定时请求
11
- class HttpMonitor(MonitorBase):
12
- def __init__(self, interval=5):
13
- super().__init__()
14
- # 执行间隔
15
- self._interval = interval
16
- self._logger = logger_utils.get_logger()
17
-
18
- def _do_start(self):
19
- # 开启一条线程,定时执行http请求
20
- self._ticker_thread_event = threading.Event()
21
-
22
- def http_request():
23
- # 上次请求时间
24
- last_request_time = 0
25
- while True:
26
- # 检查是否需要退出
27
- if self._ticker_thread_event.is_set():
28
- self._logger.info("ticker thread exit")
29
- break
30
-
31
- # 当前时间
32
- current_time = time.time()
33
- # 如果上次请求时间距离当前时间不足,等待
34
- if current_time - last_request_time < self._interval:
35
- time.sleep(0.1)
36
- continue
37
-
38
- #
39
- last_request_time = time.time()
40
-
41
- # self._logger.debug('tick start')
42
- try:
43
- self._do_request()
44
- except Exception as e:
45
- self._logger.error(f"http request error: {e}")
46
- # self._logger.debug('tick finish')
47
- #
48
- time.sleep(0.1)
49
-
50
- # 开启线程
51
- self._ticker_thread = threading.Thread(target=http_request)
52
- # 设置为守护线程
53
- self._ticker_thread.daemon = True
54
- self._ticker_thread.start()
55
-
56
- def _do_stop(self):
57
- if self._ticker_thread_event is not None:
58
- self._ticker_thread_event.set()
59
-
60
- # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
61
-
62
- @abstractmethod
63
- def _do_request(self):
64
- """
65
- 子类实现
66
- """
67
- pass
68
-
69
- # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
1
+ import threading
2
+ import time
3
+ from abc import abstractmethod
4
+
5
+ from kaq_quant_common.common.monitor_base import MonitorBase
6
+ from kaq_quant_common.common.ws_wrapper import WsWrapper
7
+ from kaq_quant_common.utils import logger_utils
8
+
9
+
10
+ # 封装http定时请求
11
+ class HttpMonitor(MonitorBase):
12
+ def __init__(self, interval=5):
13
+ super().__init__()
14
+ # 执行间隔
15
+ self._interval = interval
16
+ self._logger = logger_utils.get_logger()
17
+
18
+ def _do_start(self):
19
+ # 开启一条线程,定时执行http请求
20
+ self._ticker_thread_event = threading.Event()
21
+
22
+ def http_request():
23
+ # 上次请求时间
24
+ last_request_time = 0
25
+ while True:
26
+ # 检查是否需要退出
27
+ if self._ticker_thread_event.is_set():
28
+ self._logger.info("ticker thread exit")
29
+ break
30
+
31
+ # 当前时间
32
+ current_time = time.time()
33
+ # 如果上次请求时间距离当前时间不足,等待
34
+ if current_time - last_request_time < self._interval:
35
+ time.sleep(0.1)
36
+ continue
37
+
38
+ #
39
+ last_request_time = time.time()
40
+
41
+ # self._logger.debug('tick start')
42
+ try:
43
+ self._do_request()
44
+ except Exception as e:
45
+ self._logger.error(f"http request error: {e}")
46
+ # self._logger.debug('tick finish')
47
+ #
48
+ time.sleep(0.1)
49
+
50
+ # 开启线程
51
+ self._ticker_thread = threading.Thread(target=http_request)
52
+ # 设置为守护线程
53
+ self._ticker_thread.daemon = True
54
+ self._ticker_thread.start()
55
+
56
+ def _do_stop(self):
57
+ if self._ticker_thread_event is not None:
58
+ self._ticker_thread_event.set()
59
+
60
+ # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
61
+
62
+ @abstractmethod
63
+ def _do_request(self):
64
+ """
65
+ 子类实现
66
+ """
67
+ pass
68
+
69
+ # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
@@ -68,10 +68,16 @@ class FundingRateHelper:
68
68
  for symbol, (data, arg) in to_process:
69
69
  sub_data = self._build_data(symbol, data, arg)
70
70
 
71
+ if sub_data is None:
72
+ continue
73
+
74
+ if len(sub_data) == 0:
75
+ continue
76
+
71
77
  if not self._isMtwDdb:
72
78
  # 可以是数组,可以是dataFrame
73
79
  is_df = type(sub_data) is pd.DataFrame
74
-
80
+
75
81
  if is_df:
76
82
  # df就用df的方式写入
77
83
  # data_first_now = int(sub_data["create_time"].iloc[0])
@@ -85,21 +91,30 @@ class FundingRateHelper:
85
91
  df = pd.concat([df, sub_data], ignore_index=True)
86
92
  else:
87
93
  # 数组就用数组的方式写入
88
- # data_first_now = int(sub_data[0])
89
- # if now - data_first_now > 2000:
90
- # self._logger.warning(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
91
- # pass
92
-
93
- list_data.append(sub_data)
94
+ # 子元素是否数组
95
+ is_sub_list = type(sub_data) is list
96
+ if is_sub_list:
97
+ # 多条数据
98
+ # data_first_now = int(sub_data[0][0])
99
+ # if now - data_first_now > 2000:
100
+ # self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
101
+ # pass
102
+ list_data.extend(sub_data)
103
+ else:
104
+ # 单条数据
105
+ # data_first_now = int(sub_data[0])
106
+ # if now - data_first_now > 2000:
107
+ # self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
108
+ # pass
109
+ list_data.append(sub_data)
94
110
  else:
95
- # 只能是数组
96
- if len(sub_data) > 0:
97
- # 直接调用 save2stream_list 写入
98
- try:
99
- self._ddb.save2stream_list(sub_data)
100
- except Exception as e:
101
- # 避免刷库异常导致线程退出
102
- self._logger.error(f"批量写入数组失败: {e}")
111
+ # 直接调用 save2stream_list 写入
112
+ try:
113
+ self._ddb.save2stream_list(sub_data)
114
+ except Exception as e:
115
+ # 避免刷库异常导致线程退出
116
+ self._logger.error(f"批量写入数组失败: {e}")
117
+
103
118
 
104
119
  # 入库
105
120
  if not self._isMtwDdb:
@@ -70,6 +70,12 @@ class LimitOrderHelper:
70
70
  for symbol, (data, arg) in to_process:
71
71
  sub_data = self._build_data(symbol, data, arg)
72
72
 
73
+ if sub_data is None:
74
+ continue
75
+
76
+ if len(sub_data) == 0:
77
+ continue
78
+
73
79
  if not self._isMtwDdb:
74
80
  # 可以是数组,可以是dataFrame
75
81
  is_df = type(sub_data) is pd.DataFrame
@@ -77,9 +83,9 @@ class LimitOrderHelper:
77
83
  if is_df:
78
84
  # df就用df的方式写入
79
85
  data_first_now = int(sub_data["create_time"].iloc[0])
80
- if now - data_first_now > 2000:
81
- self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
82
- pass
86
+ # if now - data_first_now > 2000:
87
+ # self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
88
+ # pass
83
89
 
84
90
  if df is None:
85
91
  df = sub_data
@@ -87,21 +93,30 @@ class LimitOrderHelper:
87
93
  df = pd.concat([df, sub_data], ignore_index=True)
88
94
  else:
89
95
  # 数组就用数组的方式写入
90
- data_first_now = int(sub_data[0])
91
- if now - data_first_now > 2000:
92
- self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
93
- pass
96
+ # 子元素是否数组
97
+ is_sub_list = type(sub_data[0]) is list
98
+ if is_sub_list:
99
+ # 多条数据
100
+ data_first_now = int(sub_data[0][0])
101
+ # if now - data_first_now > 2000:
102
+ # self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
103
+ # pass
104
+ list_data.extend(sub_data)
105
+ else:
106
+ # 单条数据
107
+ data_first_now = int(sub_data[0])
108
+ # if now - data_first_now > 2000:
109
+ # self._logger.debug(f"数据时间{data_first_now} 与当前时间{now} 差值{now - data_first_now} 超过2000ms")
110
+ # pass
111
+ list_data.append(sub_data)
94
112
 
95
- list_data.append(sub_data)
96
113
  else:
97
- # 只能是数组
98
- if len(sub_data) > 0:
99
- # 直接调用 save2stream_list 写入
100
- try:
101
- self._ddb.save2stream_list(sub_data)
102
- except Exception as e:
103
- # 避免刷库异常导致线程退出
104
- self._logger.error(f"批量写入数组失败: {e}")
114
+ # 直接调用 save2stream_list 写入
115
+ try:
116
+ self._ddb.save2stream_list(sub_data)
117
+ except Exception as e:
118
+ # 避免刷库异常导致线程退出
119
+ self._logger.error(f"批量写入数组失败: {e}")
105
120
 
106
121
  convert_time = int(datetime.datetime.now().timestamp() * 1000)
107
122
 
@@ -133,10 +148,10 @@ class LimitOrderHelper:
133
148
  cum_write_ddb_time += write_ddb_use
134
149
  cum_total_use_time += total_use_time
135
150
 
136
- if total_use_time > 500 and cum_count > 0:
137
- self._logger.debug(
138
- f"批量写入{len(to_process)}条数据耗时{total_use_time}ms(avg {cum_total_use_time / cum_count:.2f}ms) 转换耗时{convert_use}ms(avg {cum_convert_time / cum_count:.2f}ms) 写入ddb耗时{write_ddb_use}ms(avg {cum_write_ddb_time / cum_count:.2f}ms)"
139
- )
151
+ # if total_use_time > 500 and cum_count > 0:
152
+ # self._logger.debug(
153
+ # f"批量写入{len(to_process)}条数据耗时{total_use_time}ms(avg {cum_total_use_time / cum_count:.2f}ms) 转换耗时{convert_use}ms(avg {cum_convert_time / cum_count:.2f}ms) 写入ddb耗时{write_ddb_use}ms(avg {cum_write_ddb_time / cum_count:.2f}ms)"
154
+ # )
140
155
 
141
156
  # mtw交由ddb自己控制节奏
142
157
  if self._isMtwDdb: