├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── jrpc_py.py ├── trade_api.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 | # TradeApi 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、使用TradeApi 28 | 29 | 在项目目录,验证`TradeApi`是否能够正常使用。 30 | 31 | ```python 32 | from TradeApi import TradeApi # 这里假设项目目录名为TradeApi, 且存放在工作目录下 33 | 34 | api = TradeApi(addr="tcp://gw.quantos.org:8901") 35 | result, msg = api.login("username", "token") # 示例账户,用户需要改为自己在www.quantos.org上注册的账户 36 | print result 37 | print msg 38 | ``` 39 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | """ 3 | tradeapi defines standard APIs for communicating with 4 | algorithmic trading & executing system. 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 .trade_api import TradeApi 12 | 13 | __all__ = ['TradeApi'] 14 | -------------------------------------------------------------------------------- /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 | from builtins import * 10 | 11 | import zmq 12 | 13 | try: 14 | import queue 15 | except ImportError: 16 | import Queue as queue 17 | import threading 18 | import msgpack 19 | import snappy 20 | import copy 21 | 22 | qEmpty = copy.copy(queue.Empty) 23 | 24 | 25 | def _unpack_msgpack_snappy(str): 26 | if str.startswith(b'S'): 27 | tmp = snappy.uncompress(str[1:]) 28 | # print "SNAPPY: ", len(str), len(tmp) 29 | obj = msgpack.loads(tmp, encoding='utf-8') 30 | elif str.startswith(b'\0'): 31 | obj = msgpack.loads(str[1:], encoding='utf-8') 32 | else: 33 | return None 34 | 35 | return obj 36 | 37 | 38 | def _pack_msgpack_snappy(obj): 39 | # print "pack", obj 40 | tmp = msgpack.dumps(obj, encoding='utf-8') 41 | if len(tmp) > 1000: 42 | return b'S' + snappy.compress(tmp) 43 | else: 44 | return b'\0' + tmp 45 | 46 | 47 | def _unpack_msgpack(str): 48 | return msgpack.loads(str, encoding='utf-8') 49 | 50 | 51 | def _pack_msgpack(obj): 52 | return msgpack.dumps(obj, encoding='utf-8') 53 | 54 | 55 | def _unpack_json(str): 56 | return json.loads(str, encoding='utf-8') 57 | 58 | 59 | def _pack_json(obj): 60 | return json.dumps(obj, encoding='utf-8') 61 | 62 | 63 | class JRpcClient(object): 64 | def __init__(self, data_format="msgpack_snappy"): 65 | self._waiter_lock = threading.Lock() 66 | self._waiter_map = {} 67 | 68 | self._should_close = False 69 | self._next_callid = 0 70 | self._send_lock = threading.Lock() 71 | self._callid_lock = threading.Lock() 72 | 73 | self._last_heartbeat_rsp_time = 0 74 | self._connected = False 75 | 76 | self.on_disconnected = None 77 | self.on_rpc_callback = None 78 | self._callback_queue = queue.Queue() 79 | self._call_wait_queue = queue.Queue() 80 | 81 | self._ctx = zmq.Context() 82 | self._pull_sock = self._ctx.socket(zmq.PULL) 83 | self._pull_sock.bind("inproc://pull_sock") 84 | self._push_sock = self._ctx.socket(zmq.PUSH) 85 | self._push_sock.connect("inproc://pull_sock") 86 | 87 | self._heartbeat_interval = 1 88 | self._heartbeat_timeout = 3 89 | 90 | self._addr = None 91 | 92 | if data_format == "msgpack_snappy": 93 | self._pack = _pack_msgpack_snappy 94 | self._unpack = _unpack_msgpack_snappy 95 | 96 | elif data_format == "msgpack": 97 | self._pack = _pack_msgpack 98 | self._unpack = _unpack_msgpack 99 | 100 | elif data_format == "json": 101 | self._pack = _pack_json 102 | self._unpack = _unpack_json 103 | 104 | else: 105 | assert False, "unknown data_format " + data_format 106 | 107 | t = threading.Thread(target=self._recv_run) 108 | t.setDaemon(True) 109 | t.start() 110 | self._recv_thread = t 111 | 112 | t = threading.Thread(target=self._callback_run) 113 | t.setDaemon(True) 114 | t.start() 115 | self._callback_thread = t 116 | 117 | def __del__(self): 118 | self.close() 119 | 120 | def next_callid(self): 121 | self._callid_lock.acquire() 122 | self._next_callid += 1 123 | callid = self._next_callid 124 | self._callid_lock.release() 125 | return callid 126 | 127 | def set_heartbeat_options(self, interval, timeout): 128 | self._heartbeat_interval = interval 129 | self._heartbeat_timeout = timeout 130 | 131 | def _recv_run(self): 132 | 133 | heartbeat_time = 0 134 | 135 | poller = zmq.Poller() 136 | poller.register(self._pull_sock, zmq.POLLIN) 137 | 138 | remote_sock = None 139 | 140 | while not self._should_close: 141 | 142 | try: 143 | if self._connected and time.time() - self._last_heartbeat_rsp_time > self._heartbeat_timeout: 144 | self._connected = False 145 | if self.on_disconnected: self._async_call(self.on_disconnected) 146 | 147 | if remote_sock and time.time() - heartbeat_time > self._heartbeat_interval: 148 | self._send_hearbeat() 149 | heartbeat_time = time.time() 150 | 151 | socks = dict(poller.poll(500)) 152 | if self._pull_sock in socks and socks[self._pull_sock] == zmq.POLLIN: 153 | cmd = self._pull_sock.recv() 154 | if cmd == b"CONNECT": 155 | # print time.ctime(), "CONNECT " + self._addr 156 | if remote_sock: 157 | poller.unregister(remote_sock) 158 | remote_sock.close() 159 | remote_sock = None 160 | 161 | remote_sock = self._do_connect() 162 | 163 | if remote_sock: 164 | poller.register(remote_sock, zmq.POLLIN) 165 | 166 | elif cmd.startswith(b"SEND:") and remote_sock: 167 | # print time.ctime(), "SEND " + cmd[5:] 168 | remote_sock.send(cmd[5:]) 169 | 170 | if remote_sock and remote_sock in socks and socks[remote_sock] == zmq.POLLIN: 171 | data = remote_sock.recv() 172 | if data: 173 | # if not data.find("heartbeat"): 174 | # print time.ctime(), "RECV", data 175 | self._on_data_arrived(data) 176 | 177 | except zmq.error.Again as e: 178 | # print "RECV timeout: ", e 179 | pass 180 | except Exception as e: 181 | print("_recv_run:", e) 182 | 183 | def _callback_run(self): 184 | while not self._should_close: 185 | try: 186 | r = self._callback_queue.get(timeout=1) 187 | if r: 188 | r() 189 | except qEmpty as e: 190 | pass 191 | except TypeError as e: 192 | if str(e) == "'NoneType' object is not callable": 193 | pass 194 | else: 195 | print("_callback_run {}".format(r), type(e), e) 196 | except Exception as e: 197 | print("_callback_run {}".format(r), type(e), e) 198 | 199 | def _async_call(self, func): 200 | self._callback_queue.put(func) 201 | 202 | def _send_request(self, json): 203 | 204 | try: 205 | self._send_lock.acquire() 206 | self._push_sock.send(b"SEND:" + json) 207 | 208 | finally: 209 | self._send_lock.release() 210 | 211 | def connect(self, addr): 212 | self._addr = addr 213 | self._push_sock.send_string('CONNECT', encoding='utf-8') 214 | 215 | def _do_connect(self): 216 | 217 | client_id = str(random.randint(1000000, 100000000)) 218 | 219 | socket = self._ctx.socket(zmq.DEALER) 220 | identity = (client_id) + '$' + str(random.randint(1000000, 1000000000)) 221 | identity = identity.encode('utf-8') 222 | socket.setsockopt(zmq.IDENTITY, identity) 223 | socket.setsockopt(zmq.RCVTIMEO, 500) 224 | socket.setsockopt(zmq.SNDTIMEO, 500) 225 | socket.setsockopt(zmq.LINGER, 0) 226 | socket.connect(self._addr) 227 | 228 | return socket 229 | 230 | def close(self): 231 | self._should_close = True 232 | self._callback_thread.join() 233 | self._recv_thread.join() 234 | 235 | def _on_data_arrived(self, str): 236 | try: 237 | msg = self._unpack(str) 238 | # print "RECV", msg 239 | 240 | if not msg: 241 | print("wrong message format") 242 | return 243 | 244 | if 'method' in msg and msg['method'] == '.sys.heartbeat': 245 | self._last_heartbeat_rsp_time = time.time() 246 | if not self._connected: 247 | self._connected = True 248 | if self.on_connected: 249 | self._async_call(self.on_connected) 250 | 251 | # Let user has a chance to check message in .sys.heartbeat 252 | if 'result' in msg and self.on_rpc_callback: 253 | self._async_call(lambda: self.on_rpc_callback(msg['method'], msg['result'])) 254 | 255 | elif 'id' in msg and msg['id']: 256 | 257 | # Call result 258 | id = int(msg['id']) 259 | 260 | if self._waiter_lock.acquire(): 261 | if id in self._waiter_map: 262 | q = self._waiter_map[id] 263 | if q: q.put(msg) 264 | self._waiter_lock.release() 265 | else: 266 | # Notification message 267 | if 'method' in msg and 'result' in msg and self.on_rpc_callback: 268 | self._async_call(lambda: self.on_rpc_callback(msg['method'], msg['result'])) 269 | 270 | except Exception as e: 271 | print("_on_data_arrived:", e) 272 | pass 273 | 274 | def _send_hearbeat(self): 275 | msg = {'jsonrpc' : '2.0', 276 | 'method' : '.sys.heartbeat', 277 | 'params' : { 'time': time.time()}, 278 | 'id' : str(self.next_callid())} 279 | json_str = self._pack(msg) 280 | self._send_request(json_str) 281 | 282 | def _alloc_wait_queue(self): 283 | self._waiter_lock.acquire() 284 | if self._call_wait_queue: 285 | q = self._call_wait_queue 286 | self._call_wait_queue = None 287 | else: 288 | q = queue.Queue() 289 | self._waiter_lock.release() 290 | return q 291 | 292 | def _free_wait_queue(self, q): 293 | self._waiter_lock.acquire() 294 | if not self._call_wait_queue: 295 | self._call_wait_queue = q 296 | else: 297 | del q 298 | self._waiter_lock.release() 299 | 300 | def call(self, method, params, timeout=6): 301 | # print "call", method, params, timeout 302 | callid = self.next_callid() 303 | if timeout: 304 | q = self._alloc_wait_queue() 305 | 306 | self._waiter_lock.acquire() 307 | self._waiter_map[callid] = q 308 | self._waiter_lock.release() 309 | 310 | msg = {'jsonrpc' : '2.0', 311 | 'method' : method, 312 | 'params' : params, 313 | 'id' : str(callid) } 314 | 315 | # print "SEND", msg 316 | json_str = self._pack(msg) 317 | self._send_request(json_str) 318 | 319 | if timeout: 320 | ret = {} 321 | try: 322 | r = q.get(timeout=timeout) 323 | q.task_done() 324 | except qEmpty: 325 | r = None 326 | 327 | self._waiter_lock.acquire() 328 | self._waiter_map[callid] = None 329 | self._waiter_lock.release() 330 | self._free_wait_queue(q) 331 | 332 | if r: 333 | if 'result' in r: 334 | ret['result'] = r['result'] 335 | 336 | if 'error' in r: 337 | ret['error'] = r['error'] 338 | 339 | return ret if ret else {'error': {'error': -1, 'message': "timeout"}} 340 | else: 341 | return {'result': True} 342 | -------------------------------------------------------------------------------- /trade_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 json 7 | import pandas as pd 8 | from . import utils 9 | 10 | 11 | class EntrustOrder(object): 12 | def __init__(self, security, action, price, size): 13 | self.security = security 14 | self.action = action 15 | self.price = price 16 | self.size = size 17 | 18 | 19 | def set_log_dir(log_dir): 20 | try: 21 | import jrpc 22 | if log_dir: 23 | jrpc.set_log_dir(log_dir) 24 | else: 25 | jrpc.set_log_dir("") 26 | except Exception as e: 27 | print("Exception", e) 28 | 29 | 30 | class TradeApi(object): 31 | def __init__(self, addr, use_jrpc=False, prod_type="jzts"): 32 | """ 33 | use_jrpc: 34 | True -- Use jrcp_client of C version, for jzts only 35 | False -- Use pure python version 36 | prod_type: 37 | "jaqs" -- jrpc_msgpack_wth_snappy 38 | "jzts" -- jrpc_msgpack 39 | """ 40 | 41 | self._remote = None 42 | if prod_type == "jzts": 43 | try: 44 | if use_jrpc: 45 | import jrpc 46 | self._remote = jrpc.JsonRpcClient() 47 | else: 48 | from . import jrpc_py 49 | self._remote = jrpc_py.JRpcClient(data_format="msgpack") 50 | except Exception as e: 51 | print("Exception", e) 52 | 53 | if not self._remote: 54 | from . import jrpc_py 55 | self._remote = jrpc_py.JRpcClient(data_format="msgpack") 56 | 57 | else: 58 | from . import jrpc_py 59 | self._remote = jrpc_py.JRpcClient(data_format="msgpack_snappy") 60 | 61 | self._remote.on_rpc_callback = self._on_rpc_callback 62 | self._remote.on_disconnected = self._on_disconnected 63 | self._remote.on_connected = self._on_connected 64 | self._remote.connect(addr) 65 | 66 | self._ordstatus_callback = None 67 | self._taskstatus_callback = None 68 | self._internal_order_callback = None 69 | self._trade_callback = None 70 | self._on_connection_callback = None 71 | self._connected = False 72 | self._username = "" 73 | self._password = "" 74 | self._strategy_id = 0 75 | self._strategy_selected = False 76 | self._data_format = "default" 77 | 78 | def __del__(self): 79 | self._remote.close() 80 | 81 | def _on_rpc_callback(self, method, data): 82 | #print "_on_rpc_callback:", method, data 83 | 84 | if method == "oms.orderstatus_ind": 85 | if self._data_format == "obj": 86 | data = utils.to_obj("Order", data) 87 | 88 | if self._ordstatus_callback: 89 | self._ordstatus_callback(data) 90 | 91 | elif method == "oms.taskstatus_ind": 92 | if self._data_format == "obj": 93 | data = utils.to_obj("TaskStatus", data) 94 | 95 | if self._taskstatus_callback: 96 | self._taskstatus_callback(data) 97 | 98 | elif method == "oms.trade_ind": 99 | if self._data_format == "obj": 100 | data = utils.to_obj("Trade", data) 101 | 102 | if self._trade_callback: 103 | self._trade_callback(data) 104 | 105 | elif method == "oms.internal_order_ind": 106 | if self._data_format == "obj": 107 | data = utils.to_obj("QuoteOrder", data) 108 | 109 | if self._internal_order_callback: 110 | self._internal_order_callback(data) 111 | 112 | def _on_disconnected(self): 113 | print("TradeApi: _on_disconnected") 114 | self._connected = False 115 | self._strategy_selected = False 116 | if self._on_connection_callback: 117 | self._on_connection_callback(False) 118 | 119 | def _on_connected(self): 120 | print("TradeApi: _on_connected") 121 | self._connected = True 122 | self._do_login() 123 | self._do_use_strategy() 124 | if self._on_connection_callback: 125 | self._on_connection_callback(True) 126 | 127 | def _check_session(self): 128 | if not self._connected: 129 | return (False, "no connection") 130 | 131 | if self._strategy_selected: 132 | return (True, "") 133 | 134 | r, msg = self._do_login() 135 | if not r: return (r, msg) 136 | if self._strategy_id: 137 | return self._do_use_strategy() 138 | else: 139 | return (r, msg) 140 | 141 | def set_data_format(self, format): 142 | self._data_format = format 143 | 144 | def set_connection_callback(self, callback): 145 | self._on_connection_callback = callback 146 | 147 | def set_ordstatus_callback(self, callback): 148 | self._ordstatus_callback = callback 149 | 150 | def set_trade_callback(self, callback): 151 | self._trade_callback = callback 152 | 153 | def set_task_callback(self, callback): 154 | self._taskstatus_callback = callback 155 | 156 | def set_quoteorder_callback(self, callback): 157 | self._internal_order_callback = callback 158 | 159 | def _get_format(self, format, default_format): 160 | if format: 161 | return format 162 | elif self._data_format != "default": 163 | return self._data_format 164 | else: 165 | return default_format 166 | 167 | def login(self, username, password, format=""): 168 | self._username = username 169 | self._password = password 170 | return self._do_login(format=format) 171 | 172 | def _do_login(self, format=""): 173 | # Shouldn't check connected flag here. ZMQ is a mesageq queue! 174 | # if !self._connected : 175 | # return (False, "-1,no connection") 176 | 177 | if self._username and self._password: 178 | rpc_params = {"username": self._username, 179 | "password": self._password} 180 | 181 | cr = self._remote.call("auth.login", rpc_params) 182 | f = self._get_format(format, "") 183 | if f != "obj" and f != "": 184 | f = "" 185 | return utils.extract_result(cr, data_format=f, class_name="UserInfo") 186 | else: 187 | return (False, "-1,empty username or password") 188 | 189 | def logout(self): 190 | rpc_params = {} 191 | 192 | cr = self._remote.call("auth.logout", rpc_params) 193 | return utils.extract_result(cr) 194 | 195 | def close(self): 196 | self._remote.close() 197 | 198 | def use_strategy(self, strategy_id): 199 | if strategy_id: 200 | self._strategy_id = strategy_id 201 | return self._do_use_strategy() 202 | else: 203 | # Query 204 | rpc_params = {"account_id": 0} 205 | 206 | cr = self._remote.call("auth.use_strategy", rpc_params) 207 | r, msg = utils.extract_result(cr) 208 | self._strategy_selected = r 209 | 210 | return (r, msg) 211 | 212 | def _do_use_strategy(self): 213 | if self._strategy_id: 214 | rpc_params = {"account_id": self._strategy_id} 215 | 216 | cr = self._remote.call("auth.use_strategy", rpc_params) 217 | r, msg = utils.extract_result(cr) 218 | self._strategy_selected = r 219 | 220 | return (r, msg) 221 | else: 222 | return (False, "-1,no strategy_id was specified") 223 | 224 | def confirm_internal_order(self, task_id, confirmed): 225 | """ 226 | return (result, message) 227 | if result is None, message contains error information 228 | """ 229 | 230 | r, msg = self._check_session() 231 | if not r: return (None, msg) 232 | 233 | rpc_params = {"task_id" : task_id, 234 | "confirmed": confirmed} 235 | 236 | cr = self._remote.call("oms.confirm_internal_order", rpc_params) 237 | return utils.extract_result(cr) 238 | 239 | def order(self, security, price, size, algo="", algo_param={}, userdata=""): 240 | """ 241 | return (result, message) 242 | if result is None, message contains error information 243 | """ 244 | 245 | r, msg = self._check_session() 246 | if not r: return (None, msg) 247 | 248 | rpc_params = {"security" : security, 249 | "price" : price, 250 | "size" : int(size), 251 | "algo" : algo, 252 | "algo_param" : json.dumps(algo_param), 253 | "user" : self._username, 254 | "userdata" : userdata} 255 | 256 | cr = self._remote.call("oms.order", rpc_params) 257 | return utils.extract_result(cr) 258 | 259 | def place_order(self, security, action, price, size, algo="", algo_param={}, userdata=""): 260 | """ 261 | return (result, message) 262 | if result is None, message contains error information 263 | """ 264 | 265 | r, msg = self._check_session() 266 | if not r: return (None, msg) 267 | 268 | rpc_params = { "security" : security, 269 | "action" : action, 270 | "price" : price, 271 | "size" : int(size), 272 | "algo" : algo, 273 | "algo_param" : json.dumps(algo_param), 274 | "user" : self._username, 275 | "userdata" : userdata} 276 | 277 | cr = self._remote.call("oms.place_order", rpc_params) 278 | return utils.extract_result(cr) 279 | 280 | def batch_order(self, orders, algo="", algo_param={}, userdata=""): 281 | """ 282 | orders format: 283 | [ {"security": "000001.SZ", "action": "Buy", "price": 10.0, "size" : 100}, ... ] 284 | return (result, message) 285 | if result is None, message contains error information 286 | """ 287 | 288 | if not orders or not isinstance(orders, (list, tuple)): 289 | return (None, "empty order") 290 | 291 | if isinstance(orders[0], EntrustOrder): 292 | tmp = [] 293 | for o in orders: 294 | tmp.append({"security": o.security, 295 | "price" : o.price, 296 | "size" : int(o.size)}) 297 | 298 | orders = tmp 299 | 300 | r, msg = self._check_session() 301 | if not r: return (None, msg) 302 | 303 | rpc_params = {"orders" : orders, 304 | "algo" : algo, 305 | "algo_param" : json.dumps(algo_param), 306 | "user" : self._username, 307 | "userdata" : userdata} 308 | 309 | cr = self._remote.call("oms.batch_order", rpc_params) 310 | return utils.extract_result(cr) 311 | 312 | def place_batch_order(self, orders, algo="", algo_param={}, userdata=""): 313 | """ 314 | orders format: 315 | [ {"security": "000001.SZ", "action": "Buy", "price": 10.0, "size" : 100}, ... ] 316 | return (result, message) 317 | if result is None, message contains error information 318 | """ 319 | 320 | if not orders or not isinstance(orders, (list, tuple)): 321 | return (None, "empty order") 322 | 323 | if isinstance(orders[0], EntrustOrder): 324 | tmp = [] 325 | for o in orders: 326 | tmp.append({"security": o.security, 327 | "action" : o.action, 328 | "price" : o.price, 329 | "size" : int(o.size)}) 330 | 331 | orders = tmp 332 | 333 | r, msg = self._check_session() 334 | if not r: return (None, msg) 335 | 336 | rpc_params = {"orders" : orders, 337 | "algo" : algo, 338 | "algo_param" : json.dumps(algo_param), 339 | "user" : self._username, 340 | "userdata" : userdata} 341 | 342 | cr = self._remote.call("oms.place_batch_order", rpc_params) 343 | return utils.extract_result(cr) 344 | 345 | def cancel_order(self, task_id): 346 | """ 347 | return (result, message) 348 | if result is None, message contains error information 349 | """ 350 | 351 | r, msg = self._check_session() 352 | if not r: return (None, msg) 353 | 354 | rpc_params = {"task_id": task_id} 355 | 356 | cr = self._remote.call("oms.cancel_order", rpc_params) 357 | return utils.extract_result(cr) 358 | 359 | def query_account(self, format=""): 360 | """ 361 | return pd.dataframe 362 | """ 363 | r, msg = self._check_session() 364 | if not r: return (None, msg) 365 | 366 | rpc_params = {} 367 | 368 | data_format = self._get_format(format, "pandas") 369 | if data_format == "pandas": 370 | rpc_params["format"] = "columnset" 371 | 372 | cr = self._remote.call("oms.query_account", rpc_params) 373 | 374 | return utils.extract_result(cr, data_format=data_format, class_name="Account") 375 | 376 | def query_position(self, mode="all", securities="", format=""): 377 | """ 378 | securities: seperate by "," 379 | return pd.dataframe 380 | """ 381 | 382 | r, msg = self._check_session() 383 | if not r: return (None, msg) 384 | 385 | rpc_params = {"mode" : mode, 386 | "security" : securities} 387 | 388 | data_format = self._get_format(format, "pandas") 389 | if data_format == "pandas": 390 | rpc_params["format"] = "columnset" 391 | 392 | cr = self._remote.call("oms.query_position", rpc_params) 393 | 394 | return utils.extract_result(cr, data_format=data_format, class_name="Position") 395 | 396 | def query_net_position(self, mode="all", securities="", format=""): 397 | """ 398 | securities: seperate by "," 399 | return pd.dataframe 400 | """ 401 | 402 | r, msg = self._check_session() 403 | if not r: return (None, msg) 404 | 405 | rpc_params = {"mode" : mode, 406 | "security" : securities} 407 | 408 | data_format = self._get_format(format, "pandas") 409 | if data_format == "pandas": 410 | rpc_params["format"] = "columnset" 411 | 412 | cr = self._remote.call("oms.query_net_position", rpc_params) 413 | 414 | return utils.extract_result(cr, data_format=data_format, class_name="NetPosition") 415 | 416 | def query_repo_contract(self, format=""): 417 | """ 418 | securities: seperate by "," 419 | return pd.dataframe 420 | """ 421 | 422 | r, msg = self._check_session() 423 | if not r: return (None, msg) 424 | 425 | rpc_params = {} 426 | 427 | cr = self._remote.call("oms.query_repo_contract", rpc_params) 428 | 429 | return utils.extract_result(cr, data_format=self._get_format(format, "pandas"), class_name="RepoContract") 430 | 431 | def query_task(self, task_id=-1, format=""): 432 | """ 433 | task_id: -1 -- all 434 | return pd.dataframe 435 | """ 436 | 437 | r, msg = self._check_session() 438 | if not r: return (None, msg) 439 | 440 | rpc_params = {"task_id": task_id} 441 | 442 | data_format = self._get_format(format, "pandas") 443 | if data_format == "pandas": 444 | rpc_params["format"] = "columnset" 445 | 446 | cr = self._remote.call("oms.query_task", rpc_params) 447 | 448 | return utils.extract_result(cr, data_format=data_format, class_name="Task") 449 | 450 | def query_order(self, task_id=-1, format=""): 451 | """ 452 | task_id: -1 -- all 453 | return pd.dataframe 454 | """ 455 | 456 | r, msg = self._check_session() 457 | if not r: return (None, msg) 458 | 459 | rpc_params = {"task_id": task_id} 460 | 461 | data_format = self._get_format(format, "pandas") 462 | if data_format == "pandas": 463 | rpc_params["format"] = "columnset" 464 | 465 | cr = self._remote.call("oms.query_order", rpc_params) 466 | 467 | return utils.extract_result(cr, data_format=data_format, class_name="Order") 468 | 469 | def query_trade(self, task_id=-1, format=""): 470 | """ 471 | task_id: -1 -- all 472 | return pd.dataframe 473 | """ 474 | 475 | r, msg = self._check_session() 476 | if not r: return (None, msg) 477 | 478 | rpc_params = {"task_id": task_id} 479 | 480 | data_format = self._get_format(format, "pandas") 481 | if data_format == "pandas": 482 | rpc_params["format"] = "columnset" 483 | 484 | cr = self._remote.call("oms.query_trade", rpc_params) 485 | 486 | return utils.extract_result(cr, data_format=data_format, class_name="Trade") 487 | 488 | def query_portfolio(self, format=""): 489 | """ 490 | return pd.dataframe 491 | """ 492 | 493 | r, msg = self._check_session() 494 | if not r: return (None, msg) 495 | 496 | rpc_params = {} 497 | 498 | data_format = self._get_format(format, "pandas") 499 | if data_format == "pandas": 500 | rpc_params["format"] = "columnset" 501 | 502 | cr = self._remote.call("pms.query_portfolio", rpc_params) 503 | 504 | return utils.extract_result(cr, index_column="security", data_format=data_format, class_name="NetPosition") 505 | 506 | def goal_portfolio(self, positions, algo="", algo_param={}, userdata=""): 507 | """ 508 | positions format: 509 | [ {"security": "000001.SZ", "ref_price": 10.0, "size" : 100}, ...] 510 | return (result, message) 511 | if result is None, message contains error information 512 | """ 513 | 514 | r, msg = self._check_session() 515 | if not r: return (False, msg) 516 | 517 | if type(positions) is pd.core.frame.DataFrame : 518 | tmp = [] 519 | for i in range(0, len(positions)): 520 | tmp.append ({'security': positions.index[i], 521 | 'ref_price': float(positions['ref_price'][i]), 522 | "size" : int(positions['size'][i])}) 523 | positions = tmp 524 | 525 | rpc_params = {"positions" : positions, 526 | "algo" : algo, 527 | "algo_param" : json.dumps(algo_param), 528 | "user" : self._username, 529 | "userdata" : userdata} 530 | 531 | cr = self._remote.call("pms.goal_portfolio", rpc_params) 532 | return utils.extract_result(cr) 533 | 534 | def basket_order(self, orders, algo="", algo_param={}, userdata=""): 535 | """ 536 | orders format: 537 | [ {"security": "000001.SZ", "ref_price": 10.0, "inc_size" : 100}, ...] 538 | return (result, message) 539 | if result is None, message contains error information 540 | """ 541 | 542 | r, msg = self._check_session() 543 | if not r: return (False, msg) 544 | 545 | if type(orders) is pd.core.frame.DataFrame : 546 | tmp = [] 547 | for i in range(0, len(orders)): 548 | tmp.append ({'security': orders.index[i], 549 | 'ref_price': float(orders['ref_price'][i]), 550 | 'inc_size' : int(orders['inc_size'][i])}) 551 | orders = tmp 552 | 553 | rpc_params = {"orders": orders, 554 | "algo": algo, 555 | "algo_param": json.dumps(algo_param), 556 | "user": self._username, 557 | "userdata": userdata} 558 | 559 | cr = self._remote.call("pms.basket_order", rpc_params) 560 | return utils.extract_result(cr) 561 | 562 | def stop_portfolio(self): 563 | """ 564 | return (result, message) 565 | if result is None, message contains error information 566 | """ 567 | 568 | r, msg = self._check_session() 569 | if not r: return (False, msg) 570 | 571 | rpc_params = {} 572 | 573 | cr = self._remote.call("pms.stop_portfolio", rpc_params) 574 | return utils.extract_result(cr) 575 | 576 | def query_universe(self, format=""): 577 | 578 | r, msg = self._check_session() 579 | if not r: return (None, msg) 580 | 581 | rpc_params = {} 582 | data_format = self._get_format(format, "pandas") 583 | if data_format == "pandas": 584 | rpc_params["format"] = "columnset" 585 | 586 | cr = self._remote.call("oms.query_universe", rpc_params) 587 | 588 | return utils.extract_result(cr, data_format=data_format, class_name="UniverseItem") 589 | 590 | def set_heartbeat(self, interval, timeout): 591 | self._remote.set_hearbeat_options(interval, timeout) 592 | print("heartbeat_interval =", self._remote._heartbeat_interval, ", heartbeat_timeout =", 593 | self._remote._heartbeat_timeout) 594 | -------------------------------------------------------------------------------- /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: 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 | --------------------------------------------------------------------------------