├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── data_api.py ├── jrpc_py.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DataApi 2 | 3 | 标准数据API定义。 4 | 5 | # 安装步骤 6 | 7 | ## 1、安装Python环境 8 | 9 | 如果本地还没有安装Python环境,强烈建议安装Anaconda(Python的集成开发环境,包含众多常用包,且易于安装,避免不必要的麻烦)。打开[Anaconda官网](http://www.continuum.io/downloads),选择相应的操作系统,确定要安装的Python版本,进行下载。 10 | 11 | 下载完成以后,按照图形界面步骤完成安装。在默认情况下,Anaconda会自动设置PATH环境。 12 | 13 | ***注***:如果安装过程遇到问题,或需要更详细的步骤,请参见[安装Anaconda Python环境教程](https://github.com/quantOS-org/JAQS/blob/master/doc/install.md#1安装python环境) 14 | 15 | ## 2、安装依赖包 16 | 17 | 如果Python环境不是类似Anaconda的集成开发环境,我们需要单独安装依赖包,在已经有pandas/numpy包前提下,还需要有以下几个包: 18 | - `pyzmq` 19 | - `msgpack_python` 20 | - `python-snappy` 21 | 22 | 可以通过单个安装完成,例如: `pip install pyzmq` 23 | 24 | 需要注意的是,`python-snappy`的安装需要比较多的编译依赖,请按照[如何安装python-snappy包](https://github.com/quantOS-org/JAQS/blob/master/doc/install.md#如何安装python-snappy包)所述安装。 25 | 26 | 27 | ## 3、使用DataApi 28 | 29 | DataApi的使用文档参照[该页面](https://github.com/quantOS-org/DataCore/blob/master/doc/api_ref.md) 30 | 31 | ```python 32 | from DataApi import DataApi # 这里假设项目目录名为DataApi, 且存放在工作目录下 33 | 34 | api = DataApi(addr="tcp://data.tushare.org:8910") 35 | result, msg = api.login("phone", "token") # 示例账户,用户需要改为自己在www.quantos.org上注册的账户 36 | print(result) 37 | print(msg) 38 | ``` 39 | 40 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | """ 3 | dataapi defines standard APIs for communicating with data service. 4 | 5 | """ 6 | from __future__ import absolute_import 7 | from __future__ import division 8 | from __future__ import print_function 9 | from __future__ import unicode_literals 10 | 11 | from .data_api import DataApi 12 | 13 | __all__ = ['DataApi'] 14 | -------------------------------------------------------------------------------- /data_api.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | from __future__ import unicode_literals 5 | 6 | import time 7 | 8 | import numpy as np 9 | 10 | from . import jrpc_py 11 | # import jrpc 12 | from . import utils 13 | 14 | 15 | # def set_log_dir(log_dir): 16 | # if log_dir: 17 | # jrpc.set_log_dir(log_dir) 18 | 19 | 20 | def _str2bytes(s): 21 | if hasattr(s, 'encode'): 22 | return s.encode('utf-8') 23 | else: 24 | return s 25 | 26 | 27 | def _to_int(x): 28 | return int(x) 29 | 30 | 31 | class DataApiCallback(object): 32 | """DataApi Callback 33 | 34 | def on_bar(quote): 35 | pass 36 | 37 | def on_connection() 38 | """ 39 | 40 | def __init__(self): 41 | self.on_bar = None 42 | 43 | 44 | class DataApi(object): 45 | """ 46 | Abstract base class providing both historic and live data 47 | from various data sources. 48 | Current API version: 1.0 49 | 50 | Attributes 51 | ---------- 52 | 53 | 54 | Methods 55 | ------- 56 | subscribe 57 | quote 58 | daily 59 | bar 60 | bar_quote 61 | 62 | """ 63 | 64 | def __init__(self, addr="tcp://data.tushare.org:8910", use_jrpc=False): 65 | """Create DataApi client. 66 | 67 | If use_jrpc, try to load the C version of JsonRpc. If failed, use pure 68 | Python version of JsonRpc. 69 | """ 70 | self._remote = None 71 | # if use_jrpc: 72 | # try: 73 | # import jrpc 74 | # self._remote = jrpc.JRpcClient() 75 | # except Exception as e: 76 | # print "Can't load jrpc", e.message 77 | 78 | if not self._remote: 79 | self._remote = jrpc_py.JRpcClient() 80 | 81 | self._remote.on_rpc_callback = self._on_rpc_callback 82 | self._remote.on_disconnected = self._on_disconnected 83 | self._remote.on_connected = self._on_connected 84 | self._remote.connect(addr) 85 | 86 | self._on_jsq_callback = None 87 | 88 | self._connected = False 89 | self._loggined = False 90 | self._username = "" 91 | self._password = "" 92 | self._data_format = "default" 93 | self._callback = None 94 | self._schema = [] 95 | self._schema_id = 0 96 | self._schema_map = {} 97 | self._sub_hash = "" 98 | self._subscribed_set = set() 99 | self._timeout = 20 100 | 101 | def login(self, username, password): 102 | 103 | """ 104 | Login before using data api. 105 | 106 | Parameters 107 | ---------- 108 | username : str 109 | username 110 | password : str 111 | password 112 | """ 113 | for i in range(3): 114 | if self._connected: 115 | break 116 | time.sleep(1) 117 | 118 | if not self._connected: 119 | return (None, "-1,no connection") 120 | 121 | self._username = username 122 | self._password = password 123 | return self._do_login() 124 | 125 | def logout(self): 126 | """ 127 | Logout to stop using the data api or switch users. 128 | 129 | """ 130 | self._loggined = None 131 | 132 | rpc_params = {} 133 | 134 | cr = self._remote.call("auth.logout", rpc_params) 135 | return utils.extract_result(cr) 136 | 137 | def close(self): 138 | """ 139 | Close the data api. 140 | 141 | """ 142 | 143 | self._remote.close() 144 | 145 | # def set_callback(self, callback): 146 | 147 | # self._callback = callback 148 | 149 | def set_timeout(self, timeout): 150 | """ 151 | Set timeout for data api. 152 | Default timeout is 20s. 153 | 154 | Parameters 155 | ---------- 156 | timeout : int 157 | the max waiting time for the api return 158 | 159 | """ 160 | self._timeout = timeout 161 | 162 | def set_data_format(self, format): 163 | """Set queried data format. 164 | 165 | Available formats are: 166 | "" -- Don't convert data, usually the type is map 167 | "pandas" -- Convert table likely data to DataFrame 168 | """ 169 | self._data_format = format 170 | 171 | def set_heartbeat(self, interval, timeout): 172 | self._remote.set_hearbeat_options(interval, timeout) 173 | 174 | def quote(self, symbol, fields="", data_format="", **kwargs): 175 | 176 | r, msg = self._call_rpc("jsq.query", 177 | self._get_format(data_format, "pandas"), 178 | "Quote", 179 | _index_column="symbol", 180 | symbol=_str2bytes(symbol), 181 | fields=fields, 182 | **kwargs) 183 | return (r, msg) 184 | 185 | def bar(self, symbol, start_time=200000, end_time=160000, 186 | trade_date=0, freq="1M", fields="", data_format="", **kwargs): 187 | 188 | """ 189 | Query minute bars of various type, return DataFrame. 190 | 191 | Parameters 192 | ---------- 193 | symbol : str 194 | support multiple securities, separated by comma. 195 | start_time : int (HHMMSS) or str ('HH:MM:SS') 196 | Default is market open time. 197 | end_time : int (HHMMSS) or str ('HH:MM:SS') 198 | Default is market close time. 199 | trade_date : int (YYYMMDD) or str ('YYYY-MM-DD') 200 | Default is current trade_date. 201 | fields : str, optional 202 | separated by comma ',', default "" (all fields included). 203 | freq : trade.common.MINBAR_TYPE, optional 204 | {'1m', '5m', '15m'}, Minute bar type, default is '1m' 205 | 206 | Returns 207 | ------- 208 | df : pd.DataFrame 209 | columns: 210 | symbol, code, date, time, trade_date, freq, open, high, low, close, volume, turnover, vwap, oi 211 | msg : str 212 | error code and error message joined by comma 213 | 214 | Examples 215 | -------- 216 | df, msg = api.bar("000001.SH,cu1709.SHF", start_time="09:56:00", end_time="13:56:00", 217 | trade_date="20170823", fields="open,high,low,last,volume", freq="5m") 218 | 219 | """ 220 | 221 | begin_time = utils.to_time_int(start_time) 222 | if (begin_time == -1): 223 | return (-1, "Begin time format error") 224 | end_time = utils.to_time_int(end_time) 225 | if (end_time == -1): 226 | return (-1, "End time format error") 227 | trade_date = utils.to_date_int(trade_date) 228 | if (trade_date == -1): 229 | return (-1, "Trade date format error") 230 | 231 | return self._call_rpc("jsi.query", 232 | self._get_format(data_format, "pandas"), 233 | "Bar", 234 | symbol=_str2bytes(symbol), 235 | fields=fields, 236 | freq=freq, 237 | trade_date=_to_int(trade_date), 238 | begin_time=_to_int(begin_time), 239 | end_time=_to_int(end_time), 240 | **kwargs) 241 | 242 | 243 | def tick(self, symbol, start_time=200000, end_time=160000, 244 | trade_date=0, fields="", data_format="", **kwargs): 245 | 246 | """ 247 | Query ticks, return DataFrame. 248 | 249 | Parameters 250 | ---------- 251 | symbol : str 252 | support multiple securities, separated by comma. 253 | start_time : int (HHMMSS) or str ('HH:MM:SS') 254 | Default is market open time. 255 | end_time : int (HHMMSS) or str ('HH:MM:SS') 256 | Default is market close time. 257 | trade_date : int (YYYMMDD) or str ('YYYY-MM-DD') 258 | Default is 0. 259 | fields : str, optional 260 | separated by comma ',', default "" (all fields included). 261 | 262 | Returns 263 | ------- 264 | df : pd.DataFrame 265 | columns: 266 | symbol, code, date, time, trade_date, last, open, high, low, close, volume, turnover, 267 | vwap, oi, settle, iopv, limit_up, limit_low, preclose, presettle, preoi, 268 | askprice1, askprice2, askprice3, askprice4, askprice5, 269 | bidprice1, bidprice2, bidprice3, bidprice4, bidprice5, 270 | askvolume1, askvolume2, askvolume3, askvolume4, askvolume5, 271 | bidvolume1, bidvolume2, bidvolume3, bidvolume4, bidvolume5 272 | msg : str 273 | error code and error message joined by comma 274 | 275 | Examples 276 | -------- 277 | df, msg = api.bar("000001.SH,cu1709.SHF", start_time="09:56:00", end_time="13:56:00", 278 | trade_date="20170823", fields="open,high,low,last,volume", freq="5m") 279 | 280 | """ 281 | 282 | begin_time = utils.to_time_int(start_time) 283 | if (begin_time == -1): 284 | return (-1, "Begin time format error") 285 | end_time = utils.to_time_int(end_time) 286 | if (end_time == -1): 287 | return (-1, "End time format error") 288 | trade_date = utils.to_date_int(trade_date) 289 | if (trade_date == -1): 290 | return (-1, "Trade date format error") 291 | 292 | return self._call_rpc("jst.query", 293 | self._get_format(data_format, "pandas"), 294 | "Tick", 295 | symbol=_str2bytes(symbol), 296 | fields=fields, 297 | trade_date=_to_int(trade_date), 298 | begin_time=_to_int(begin_time), 299 | end_time=_to_int(end_time), 300 | **kwargs) 301 | 302 | 303 | 304 | def bar_quote(self, symbol, start_time=200000, end_time=160000, 305 | trade_date=0, freq="1M", fields="", data_format="", **kwargs): 306 | """ 307 | Query minute bars of various type, return DataFrame. 308 | It will also return ask/bid informations of the last quote in this bar 309 | 310 | 311 | Parameters 312 | ---------- 313 | symbol : str 314 | support multiple securities, separated by comma. 315 | start_time : int (HHMMSS) or str ('HH:MM:SS') 316 | Default is market open time. 317 | end_time : int (HHMMSS) or str ('HH:MM:SS') 318 | Default is market close time. 319 | trade_date : int (YYYMMDD) or str ('YYYY-MM-DD') 320 | Default is current trade_date. 321 | fields : str, optional 322 | separated by comma ',', default "" (all fields included). 323 | freq : trade.common.MINBAR_TYPE, optional 324 | {'1m', '5m', '15m'}, Minute bar type, default is '1m' 325 | 326 | Returns 327 | ------- 328 | df : pd.DataFrame 329 | columns: 330 | symbol, code, date, time, trade_date, freq, open, high, low, close, volume, turnover, vwap, oi, 331 | askprice1, askprice2, askprice3, askprice4, askprice5, 332 | bidprice1, bidprice2, bidprice3, bidprice4, bidprice5, 333 | askvolume1, askvolume2, askvolume3, askvolume4, askvolume5, 334 | bidvolume1, bidvolume2, bidvolume3, bidvolume4, bidvolume5 335 | 336 | msg : str 337 | error code and error message joined by comma 338 | 339 | Examples 340 | -------- 341 | df, msg = api.bar_quote("000001.SH,cu1709.SHF", start_time="09:56:00", end_time="13:56:00", 342 | trade_date="20170823", fields="open,high,low,last,volume", freq="5m") 343 | 344 | """ 345 | begin_time = utils.to_time_int(start_time) 346 | if (begin_time == -1): 347 | return (-1, "Begin time format error") 348 | end_time = utils.to_time_int(end_time) 349 | if (end_time == -1): 350 | return (-1, "End time format error") 351 | trade_date = utils.to_date_int(trade_date) 352 | if (trade_date == -1): 353 | return (-1, "Trade date format error") 354 | 355 | return self._call_rpc("jsi.bar_view", 356 | self._get_format(data_format, "pandas"), 357 | "BarQuote", 358 | symbol=_str2bytes(symbol), 359 | fields=fields, 360 | freq=freq, 361 | trade_date=_to_int(trade_date), 362 | begin_time=_to_int(begin_time), 363 | end_time=_to_int(end_time), 364 | **kwargs) 365 | 366 | def daily(self, symbol, start_date, end_date, 367 | adjust_mode=None, freq="1d", fields="", 368 | data_format="", **kwargs): 369 | 370 | """ 371 | Query dar bar, 372 | support auto-fill suspended securities data, 373 | support auto-adjust for splits, dividends and distributions. 374 | 375 | Parameters 376 | ---------- 377 | symbol : str 378 | support multiple securities, separated by comma. 379 | start_date : int or str 380 | YYYMMDD or 'YYYY-MM-DD' 381 | end_date : int or str 382 | YYYMMDD or 'YYYY-MM-DD' 383 | fields : str, optional 384 | separated by comma ',', default "" (all fields included). 385 | adjust_mode : str or None, optional 386 | None for no adjust; 387 | 'pre' for forward adjust; 388 | 'post' for backward adjust. 389 | 390 | Returns 391 | ------- 392 | df : pd.DataFrame 393 | columns: 394 | symbol, code, trade_date, open, high, low, close, volume, turnover, vwap, oi, suspended 395 | msg : str 396 | error code and error message joined by comma 397 | 398 | Examples 399 | -------- 400 | df, msg = api.daily("000001.SH,cu1709.SHF",start_date=20170503, end_date=20170708, 401 | fields="open,high,low,last,volume", adjust_mode = "post") 402 | 403 | """ 404 | 405 | if adjust_mode == None: 406 | adjust_mode = "none" 407 | 408 | begin_date = utils.to_date_int(start_date) 409 | if (begin_date == -1): 410 | return (-1, "Begin date format error") 411 | end_date = utils.to_date_int(end_date) 412 | if (end_date == -1): 413 | return (-1, "End date format error") 414 | 415 | return self._call_rpc("jsd.query", 416 | self._get_format(data_format, "pandas"), 417 | "Daily", 418 | symbol=_str2bytes(symbol), 419 | fields=fields, 420 | begin_date=_to_int(begin_date), 421 | end_date=_to_int(end_date), 422 | adjust_mode=adjust_mode, 423 | freq=freq, 424 | **kwargs) 425 | 426 | def query(self, view, filter="", fields="", data_format="", **kwargs): 427 | """ 428 | Get various reference data. 429 | 430 | Parameters 431 | ---------- 432 | view : str 433 | data source. 434 | fields : str 435 | Separated by ',' 436 | filter : str 437 | filter expressions. 438 | kwargs 439 | 440 | Returns 441 | ------- 442 | df : pd.DataFrame 443 | msg : str 444 | error code and error message, joined by ',' 445 | 446 | Examples 447 | -------- 448 | res3, msg3 = ds.query("lb.secDailyIndicator", fields="price_level,high_52w_adj,low_52w_adj",\ 449 | filter="start_date=20170907&end_date=20170907",\ 450 | data_format='pandas') 451 | view does not change. fileds can be any field predefined in reference data api. 452 | 453 | """ 454 | return self._call_rpc("jset.query", 455 | self._get_format(data_format, "pandas"), 456 | "JSetData", 457 | view=view, 458 | fields=fields, 459 | filter=filter, 460 | **kwargs) 461 | 462 | def subscribe(self, symbol, func=None, fields=""): 463 | """Subscribe securites 464 | 465 | This function adds new securities to subscribed list on the server. If 466 | success, return subscribed codes. 467 | 468 | If securities is empty, return current subscribed codes. 469 | """ 470 | r, msg = self._check_session() 471 | if not r: 472 | return (r, msg) 473 | 474 | if func: 475 | self._on_jsq_callback = func 476 | 477 | rpc_params = {"symbol": symbol, 478 | "fields": fields} 479 | 480 | cr = self._remote.call("jsq.subscribe", rpc_params) 481 | 482 | rsp, msg = utils.extract_result(cr, data_format="", class_name="SubRsp") 483 | if not rsp: 484 | return (rsp, msg) 485 | 486 | new_codes = [x.strip() for x in symbol.split(',') if x] 487 | 488 | self._subscribed_set = self._subscribed_set.union(set(new_codes)) 489 | self._schema_id = rsp['schema_id'] 490 | self._schema = rsp['schema'] 491 | self._sub_hash = rsp['sub_hash'] 492 | self._make_schema_map() 493 | return (rsp['symbols'], msg) 494 | 495 | def unsubscribe(self, symbol): 496 | """Unsubscribe securities. 497 | 498 | Unscribe codes and return list of subscribed code. 499 | """ 500 | assert False, "NOT IMPLEMENTED" 501 | 502 | def __del__(self): 503 | self._remote.close() 504 | 505 | def _on_disconnected(self): 506 | """JsonRpc callback""" 507 | # print "DataApi: _on_disconnected" 508 | self._connected = False 509 | 510 | if self._callback: 511 | self._callback("connection", False) 512 | 513 | def _on_connected(self): 514 | """JsonRpc callback""" 515 | self._connected = True 516 | 517 | self._do_login() 518 | self._do_subscribe() 519 | 520 | if self._callback: 521 | self._callback("connection", True) 522 | 523 | def _check_session(self): 524 | if not self._connected: 525 | return (False, "no connection") 526 | elif self._loggined: 527 | return (True, "") 528 | elif self._username and self._password: 529 | return self._do_login() 530 | else: 531 | return (False, "no login session") 532 | 533 | def _get_format(self, format, default_format): 534 | if format: 535 | return format 536 | elif self._data_format != "default": 537 | return self._data_format 538 | else: 539 | return default_format 540 | 541 | def set_callback(self, callback): 542 | self._callback = callback 543 | 544 | def _convert_quote_ind(self, quote_ind): 545 | """Convert original quote_ind to a map. 546 | 547 | The original quote_ind contains field index instead of field name! 548 | """ 549 | 550 | if quote_ind['schema_id'] != self._schema_id: 551 | return None 552 | 553 | indicators = quote_ind['indicators'] 554 | values = quote_ind['values'] 555 | 556 | max_index = len(self._schema) 557 | 558 | quote = {} 559 | for i in range(len(indicators)): 560 | if indicators[i] < max_index: 561 | quote[self._schema_map[indicators[i]]['name']] = values[i] 562 | else: 563 | quote[str(indicators[i])] = values[i] 564 | 565 | return quote 566 | 567 | def _on_rpc_callback(self, method, data): 568 | # print "_on_rpc_callback:", method, data 569 | 570 | try: 571 | if method == "jsq.quote_ind": 572 | if self._on_jsq_callback: 573 | q = self._convert_quote_ind(data) 574 | if q: 575 | self._on_jsq_callback("quote", q) 576 | 577 | elif method == ".sys.heartbeat": 578 | if 'sub_hash' in data: 579 | if self._sub_hash and self._sub_hash != data['sub_hash']: 580 | print("sub_hash is not same", self._sub_hash, data['sub_hash']) 581 | self._do_subscribe() 582 | 583 | except Exception as e: 584 | print("Can't load jrpc", e.message) 585 | 586 | def _call_rpc(self, method, data_format, data_class, **kwargs): 587 | 588 | r, msg = self._check_session() 589 | if not r: 590 | return (r, msg) 591 | 592 | index_column = None 593 | rpc_params = {} 594 | for key, value in kwargs.items(): 595 | if key == '_index_column': 596 | index_column = value 597 | else: 598 | if isinstance(value, (int, np.integer)): 599 | value = int(value) 600 | rpc_params[key] = value 601 | 602 | cr = self._remote.call(method, rpc_params, timeout=self._timeout) 603 | 604 | return utils.extract_result(cr, data_format=data_format, index_column=index_column, class_name=data_class) 605 | 606 | def _make_schema_map(self): 607 | self._schema_map = {} 608 | for schema in self._schema: 609 | self._schema_map[schema['id']] = schema 610 | 611 | def _do_login(self): 612 | # Shouldn't check connected flag here. ZMQ is a mesageq queue! 613 | # if !self._connected : 614 | # return (False, "-1,no connection") 615 | 616 | if self._username and self._password: 617 | rpc_params = {"username": self._username, 618 | "password": self._password} 619 | 620 | cr = self._remote.call("auth.login", rpc_params) 621 | r, msg = utils.extract_result(cr, data_format="", class_name="UserInfo") 622 | self._loggined = r 623 | return (r, msg) 624 | else: 625 | self._loggined = None 626 | return (False, "-1,empty username or password") 627 | 628 | def _do_subscribe(self): 629 | """Subscribe again when reconnected or hash_code is not same""" 630 | if not self._subscribed_set: return 631 | 632 | codes = list(self._subscribed_set) 633 | codes.sort() 634 | 635 | # XXX subscribe with default fields! 636 | rpc_params = {"symbol": ",".join(codes), 637 | "fields": ""} 638 | 639 | cr = self._remote.call("jsq.subscribe", rpc_params) 640 | 641 | rsp, msg = utils.extract_result(cr, data_format="", class_name="SubRsp") 642 | if not rsp: 643 | # return (rsp, msg) 644 | return 645 | 646 | self._schema_id = rsp['schema_id'] 647 | self._schema = rsp['schema'] 648 | self._sub_hash = rsp['sub_hash'] 649 | # return (rsp.securities, msg) 650 | 651 | self._make_schema_map() 652 | -------------------------------------------------------------------------------- /jrpc_py.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | from __future__ import unicode_literals 5 | 6 | import json 7 | import random 8 | import time 9 | 10 | import zmq 11 | 12 | try: 13 | import queue 14 | except ImportError: 15 | import Queue as queue 16 | import threading 17 | import msgpack 18 | import snappy 19 | import copy 20 | 21 | qEmpty = copy.copy(queue.Empty) 22 | 23 | 24 | def _unpack_msgpack_snappy(str): 25 | if str.startswith(b'S'): 26 | tmp = snappy.uncompress(str[1:]) 27 | # print "SNAPPY: ", len(str), len(tmp) 28 | obj = msgpack.loads(tmp, encoding='utf-8') 29 | elif str.startswith(b'\0'): 30 | obj = msgpack.loads(str[1:], encoding='utf-8') 31 | else: 32 | return None 33 | 34 | return obj 35 | 36 | 37 | def _pack_msgpack_snappy(obj): 38 | # print "pack", obj 39 | tmp = msgpack.dumps(obj, encoding='utf-8') 40 | if len(tmp) > 1000: 41 | return b'S' + snappy.compress(tmp) 42 | else: 43 | return b'\0' + tmp 44 | 45 | 46 | def _unpack_msgpack(str): 47 | return msgpack.loads(str, encoding='utf-8') 48 | 49 | 50 | def _pack_msgpack(obj): 51 | return msgpack.dumps(obj, encoding='utf-8') 52 | 53 | 54 | def _unpack_json(str): 55 | return json.loads(str, encoding='utf-8') 56 | 57 | 58 | def _pack_json(obj): 59 | return json.dumps(obj, encoding='utf-8') 60 | 61 | 62 | class JRpcClient(object): 63 | def __init__(self, data_format="msgpack_snappy"): 64 | self._waiter_lock = threading.Lock() 65 | self._waiter_map = {} 66 | 67 | self._should_close = False 68 | self._next_callid = 0 69 | self._send_lock = threading.Lock() 70 | self._callid_lock = threading.Lock() 71 | 72 | self._last_heartbeat_rsp_time = 0 73 | self._connected = False 74 | 75 | self.on_disconnected = None 76 | self.on_rpc_callback = None 77 | self._callback_queue = queue.Queue() 78 | self._call_wait_queue = queue.Queue() 79 | 80 | self._ctx = zmq.Context() 81 | self._pull_sock = self._ctx.socket(zmq.PULL) 82 | self._pull_sock.bind("inproc://pull_sock") 83 | self._push_sock = self._ctx.socket(zmq.PUSH) 84 | self._push_sock.connect("inproc://pull_sock") 85 | 86 | self._heartbeat_interval = 1 87 | self._heartbeat_timeout = 3 88 | 89 | self._addr = None 90 | 91 | if data_format == "msgpack_snappy": 92 | self._pack = _pack_msgpack_snappy 93 | self._unpack = _unpack_msgpack_snappy 94 | 95 | elif data_format == "msgpack": 96 | self._pack = _pack_msgpack 97 | self._unpack = _unpack_msgpack 98 | 99 | elif data_format == "json": 100 | self._pack = _pack_json 101 | self._unpack = _unpack_json 102 | 103 | else: 104 | assert False, "unknown data_format " + data_format 105 | 106 | t = threading.Thread(target=self._recv_run) 107 | t.setDaemon(True) 108 | t.start() 109 | self._recv_thread = t 110 | 111 | t = threading.Thread(target=self._callback_run) 112 | t.setDaemon(True) 113 | t.start() 114 | self._callback_thread = t 115 | 116 | def __del__(self): 117 | self.close() 118 | 119 | def next_callid(self): 120 | self._callid_lock.acquire() 121 | self._next_callid += 1 122 | callid = self._next_callid 123 | self._callid_lock.release() 124 | return callid 125 | 126 | def set_heartbeat_options(self, interval, timeout): 127 | self._heartbeat_interval = interval 128 | self._heartbeat_timeout = timeout 129 | 130 | def _recv_run(self): 131 | 132 | heartbeat_time = 0 133 | 134 | poller = zmq.Poller() 135 | poller.register(self._pull_sock, zmq.POLLIN) 136 | 137 | remote_sock = None 138 | 139 | while not self._should_close: 140 | 141 | try: 142 | if self._connected and time.time() - self._last_heartbeat_rsp_time > self._heartbeat_timeout: 143 | self._connected = False 144 | if self.on_disconnected: self._async_call(self.on_disconnected) 145 | 146 | if remote_sock and time.time() - heartbeat_time > self._heartbeat_interval: 147 | self._send_hearbeat() 148 | heartbeat_time = time.time() 149 | 150 | socks = dict(poller.poll(500)) 151 | if self._pull_sock in socks and socks[self._pull_sock] == zmq.POLLIN: 152 | cmd = self._pull_sock.recv() 153 | if cmd == b"CONNECT": 154 | # print time.ctime(), "CONNECT " + self._addr 155 | if remote_sock: 156 | poller.unregister(remote_sock) 157 | remote_sock.close() 158 | remote_sock = None 159 | 160 | remote_sock = self._do_connect() 161 | 162 | if remote_sock: 163 | poller.register(remote_sock, zmq.POLLIN) 164 | 165 | elif cmd.startswith(b"SEND:") and remote_sock: 166 | # print time.ctime(), "SEND " + cmd[5:] 167 | remote_sock.send(cmd[5:]) 168 | 169 | if remote_sock and remote_sock in socks and socks[remote_sock] == zmq.POLLIN: 170 | data = remote_sock.recv() 171 | if data: 172 | # if not data.find("heartbeat"): 173 | # print time.ctime(), "RECV", data 174 | self._on_data_arrived(data) 175 | 176 | except zmq.error.Again as e: 177 | # print "RECV timeout: ", e 178 | pass 179 | except Exception as e: 180 | print("_recv_run:", e) 181 | 182 | def _callback_run(self): 183 | while not self._should_close: 184 | try: 185 | r = self._callback_queue.get(timeout=1) 186 | if r: 187 | r() 188 | except qEmpty as e: 189 | pass 190 | except TypeError as e: 191 | if str(e) == "'NoneType' object is not callable": 192 | pass 193 | else: 194 | print("_callback_run {}".format(r), type(e), e) 195 | except Exception as e: 196 | print("_callback_run {}".format(r), type(e), e) 197 | 198 | def _async_call(self, func): 199 | self._callback_queue.put(func) 200 | 201 | def _send_request(self, json): 202 | 203 | try: 204 | self._send_lock.acquire() 205 | self._push_sock.send(b"SEND:" + json) 206 | 207 | finally: 208 | self._send_lock.release() 209 | 210 | def connect(self, addr): 211 | self._addr = addr 212 | self._push_sock.send_string('CONNECT', encoding='utf-8') 213 | 214 | def _do_connect(self): 215 | 216 | client_id = str(random.randint(1000000, 100000000)) 217 | 218 | socket = self._ctx.socket(zmq.DEALER) 219 | identity = (client_id) + '$' + str(random.randint(1000000, 1000000000)) 220 | identity = identity.encode('utf-8') 221 | socket.setsockopt(zmq.IDENTITY, identity) 222 | socket.setsockopt(zmq.RCVTIMEO, 500) 223 | socket.setsockopt(zmq.SNDTIMEO, 500) 224 | socket.setsockopt(zmq.LINGER, 0) 225 | socket.connect(self._addr) 226 | 227 | return socket 228 | 229 | def close(self): 230 | self._should_close = True 231 | self._callback_thread.join() 232 | self._recv_thread.join() 233 | 234 | def _on_data_arrived(self, str): 235 | try: 236 | msg = self._unpack(str) 237 | # print "RECV", msg 238 | 239 | if not msg: 240 | print("wrong message format") 241 | return 242 | 243 | if 'method' in msg and msg['method'] == '.sys.heartbeat': 244 | self._last_heartbeat_rsp_time = time.time() 245 | if not self._connected: 246 | self._connected = True 247 | if self.on_connected: 248 | self._async_call(self.on_connected) 249 | 250 | # Let user has a chance to check message in .sys.heartbeat 251 | if 'result' in msg and self.on_rpc_callback: 252 | self._async_call(lambda: self.on_rpc_callback(msg['method'], msg['result'])) 253 | 254 | elif 'id' in msg and msg['id']: 255 | 256 | # Call result 257 | id = int(msg['id']) 258 | 259 | if self._waiter_lock.acquire(): 260 | if id in self._waiter_map: 261 | q = self._waiter_map[id] 262 | if q: q.put(msg) 263 | self._waiter_lock.release() 264 | else: 265 | # Notification message 266 | if 'method' in msg and 'result' in msg and self.on_rpc_callback: 267 | self._async_call(lambda: self.on_rpc_callback(msg['method'], msg['result'])) 268 | 269 | except Exception as e: 270 | print("_on_data_arrived:", e) 271 | pass 272 | 273 | def _send_hearbeat(self): 274 | msg = {'jsonrpc': '2.0', 275 | 'method': '.sys.heartbeat', 276 | 'params': {'time': time.time()}, 277 | 'id': str(self.next_callid())} 278 | json_str = self._pack(msg) 279 | self._send_request(json_str) 280 | 281 | def _alloc_wait_queue(self): 282 | self._waiter_lock.acquire() 283 | if self._call_wait_queue: 284 | q = self._call_wait_queue 285 | self._call_wait_queue = None 286 | else: 287 | q = queue.Queue() 288 | self._waiter_lock.release() 289 | return q 290 | 291 | def _free_wait_queue(self, q): 292 | self._waiter_lock.acquire() 293 | if not self._call_wait_queue: 294 | self._call_wait_queue = q 295 | else: 296 | del q 297 | self._waiter_lock.release() 298 | 299 | def call(self, method, params, timeout=6): 300 | # print "call", method, params, timeout 301 | callid = self.next_callid() 302 | if timeout: 303 | q = self._alloc_wait_queue() 304 | 305 | self._waiter_lock.acquire() 306 | self._waiter_map[callid] = q 307 | self._waiter_lock.release() 308 | 309 | msg = {'jsonrpc': '2.0', 310 | 'method': method, 311 | 'params': params, 312 | 'id': str(callid)} 313 | 314 | # print "SEND", msg 315 | json_str = self._pack(msg) 316 | self._send_request(json_str) 317 | 318 | if timeout: 319 | ret = {} 320 | try: 321 | r = q.get(timeout=timeout) 322 | q.task_done() 323 | except qEmpty: 324 | r = None 325 | 326 | self._waiter_lock.acquire() 327 | self._waiter_map[callid] = None 328 | self._waiter_lock.release() 329 | self._free_wait_queue(q) 330 | 331 | if r: 332 | if 'result' in r: 333 | ret['result'] = r['result'] 334 | 335 | if 'error' in r: 336 | ret['error'] = r['error'] 337 | 338 | return ret if ret else {'error': {'error': -1, 'message': "timeout"}} 339 | else: 340 | return {'result': True} 341 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | from __future__ import unicode_literals 5 | 6 | try: 7 | basestring 8 | except NameError: 9 | basestring = str 10 | from collections import namedtuple 11 | import datetime as dt 12 | import pandas as pd 13 | import numpy as np 14 | 15 | long_nan = 9223372036854775807 16 | 17 | def is_long_nan(v): 18 | if v == long_nan: 19 | return True 20 | else: 21 | return False 22 | 23 | 24 | def to_nan(x): 25 | if is_long_nan(x): 26 | return np.nan 27 | else: 28 | return x 29 | 30 | 31 | def _to_date(row): 32 | date = int(row['DATE']) 33 | return pd.datetime(year=date // 10000, month=date // 100 % 100, day=date % 100) 34 | 35 | 36 | def _to_datetime(row): 37 | date = int(row['DATE']) 38 | time = int(row['TIME']) // 1000 39 | return pd.datetime(year=date // 10000, month=date // 100 % 100, day=date % 100, 40 | hour=time // 10000, minute=time // 100 % 100, second=time % 100) 41 | 42 | 43 | def _to_dataframe(cloumset, index_func=None, index_column=None): 44 | df = pd.DataFrame(cloumset) 45 | for col in df.columns: 46 | if df.dtypes.loc[col] == np.int64: 47 | df.loc[:, col] = df.loc[:, col].apply(to_nan) 48 | if index_func: 49 | df.index = df.apply(index_func, axis=1) 50 | elif index_column: 51 | df.index = df[index_column] 52 | del df.index.name 53 | 54 | return df 55 | 56 | 57 | def _error_to_str(error): 58 | if error: 59 | if 'message' in error: 60 | return str(error['error']) + "," + error['message'] 61 | else: 62 | return str(error['error']) + "," 63 | else: 64 | return "," 65 | 66 | 67 | def to_obj(class_name, data): 68 | try: 69 | if isinstance(data, (list, tuple)): 70 | result = [] 71 | for d in data: 72 | result.append(namedtuple(class_name, list(d.keys()))(*list(d.values()))) 73 | return result 74 | 75 | elif type(data) == dict: 76 | result = namedtuple(class_name, list(data.keys()))(*list(data.values())) 77 | return result 78 | else: 79 | return data 80 | except Exception as e: 81 | print(class_name, data, e) 82 | return data 83 | 84 | 85 | def to_date_int(date): 86 | if isinstance(date, basestring): 87 | t = dt.datetime.strptime(date, "%Y-%m-%d") 88 | date_int = t.year * 10000 + t.month * 100 + t.day 89 | return date_int 90 | elif isinstance(date, (int, np.integer)): 91 | return date 92 | else: 93 | return -1 94 | 95 | 96 | def to_time_int(time): 97 | if isinstance(time, basestring): 98 | t = dt.datetime.strptime(time, "%H:%M:%S") 99 | time_int = t.hour * 10000 + t.minute * 100 + t.second 100 | return time_int 101 | elif isinstance(time, (int, np.integer)): 102 | return time 103 | else: 104 | return -1 105 | 106 | 107 | def extract_result(cr, data_format="", index_column=None, class_name=""): 108 | """ 109 | format supports pandas, obj. 110 | """ 111 | 112 | err = _error_to_str(cr['error']) if 'error' in cr else None 113 | if 'result' in cr and cr['result'] is not None: 114 | if data_format == "pandas": 115 | if index_column: 116 | return (_to_dataframe(cr['result'], None, index_column), err) 117 | # if 'TIME' in cr['result']: 118 | # return (_to_dataframe(cr['result'], _to_datetime), err) 119 | # elif 'DATE' in cr['result']: 120 | # return (_to_dataframe(cr['result'], _to_date), err) 121 | else: 122 | return (_to_dataframe(cr['result']), err) 123 | 124 | elif data_format == "obj" and cr['result'] and class_name: 125 | r = cr['result'] 126 | if isinstance(r, (list, tuple)): 127 | result = [] 128 | for d in r: 129 | result.append(namedtuple(class_name, list(d.keys()))(*list(d.values()))) 130 | elif isinstance(r, dict): 131 | result = namedtuple(class_name, list(r.keys()))(*list(r.values())) 132 | else: 133 | result = r 134 | 135 | return (result, err) 136 | else: 137 | return (cr['result'], err) 138 | else: 139 | return (None, err) 140 | --------------------------------------------------------------------------------