| | |
| | | CODE_LIST_WANT = "want" |
| | | CODE_LIST_PAUSE_BUY = "pause_buy" |
| | | CODE_LIST_MUST_BUY = "must_buy" |
| | | CODE_LIST_GREEN = "green" |
| | | # 类型 |
| | | API_TYPE_TRADE = "trade" # 交易 |
| | | API_TYPE_TRADE_STATE = "trade_state" # 交易状态 |
| | |
| | | raise Exception(f"读取内容超时: request_id={request_id}") |
| | | finally: |
| | | pass |
| | | else: |
| | | try: |
| | | # 如果不是阻塞请求需要释放客户端 |
| | | ClientSocketManager.release_client(client[0]) |
| | | except: |
| | | pass |
| | | |
| | | return None |
| | | |
| | |
| | | # blocking是否阻塞进程 |
| | | def trade_order(direction, code, volume, price, price_type=2, blocking=True): |
| | | print("客户端", ClientSocketManager.socket_client_dict) |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, |
| | | {"type": API_TYPE_TRADE, "trade_type": 1, |
| | | "direction": direction, |
| | | "code": code, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": f"order_{code}_{round(time.time() * 1000)}"}) |
| | | request_id, client = __request( |
| | | ClientSocketManager.CLIENT_TYPE_TRADE if direction == 1 else ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_TRADE, "trade_type": 1, |
| | | "direction": direction, |
| | | "code": code, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": f"order_{code}_{round(time.time() * 1000)}"}) |
| | | |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | |
| | | |
| | | |
| | | # 刷新交易数据 |
| | | def refresh_trade_data(type, blocking=True): |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, |
| | | def refresh_trade_data(type, blocking=True, client_type=ClientSocketManager.CLIENT_TYPE_COMMON): |
| | | request_id, client = __request(client_type, |
| | | {"type": API_TYPE_REFRESH_TRADE_DATA, "ctype": type, |
| | | "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | |
| | | |
| | | # 保存正在运行的数据 |
| | | def sell_rule(operate, data={}, blocking=True): |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_SELL_RULE, "operate": operate, "data": data, |
| | | "sinfo": f"cb_{API_TYPE_SELL_RULE}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking, timeout=10) |
| | |
| | | |
| | | # 获取代码持仓信息 |
| | | def get_code_position_info(code, blocking=True): |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE, |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_SELL, |
| | | {"type": API_TYPE_GET_CODE_POSITION_INFO, "code": code, |
| | | "sinfo": f"cb_{API_TYPE_GET_CODE_POSITION_INFO}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | | |
| | | def common_request(params={}, blocking=True): |
| | | def common_request(params={}, blocking=True, client_type=ClientSocketManager.CLIENT_TYPE_COMMON): |
| | | data = {"type": API_TYPE_COMMON_REQUEST, |
| | | "sinfo": f"cb_{API_TYPE_COMMON_REQUEST}_{round(time.time() * 1000)}"} |
| | | if params: |
| | | for k in params: |
| | | data[k] = params[k] |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_COMMON, data) |
| | | request_id, client = __request(client_type, data) |
| | | return __read_response(client, request_id, blocking, timeout=10) |
| | | |
| | | |
| | | ###############可转债################# |
| | | def trade_order_for_cb(direction, code, volume, price, price_type=2, blocking=True): |
| | | """ |
| | | 交易 |
| | | :param direction: 1-买 2-卖 |
| | | :param code: 代码 |
| | | :param volume: 量 |
| | | :param price: 价格 |
| | | :param price_type: 价格类型 |
| | | :param blocking: 是否阻塞 |
| | | :return: |
| | | """ |
| | | request_id, client = __request( |
| | | ClientSocketManager.CLIENT_TYPE_TRADE_CB, |
| | | {"type": API_TYPE_TRADE, "trade_type": 1, |
| | | "direction": direction, |
| | | "code": code, |
| | | "volume": volume, |
| | | "price_type": price_type, |
| | | "price": price, "sinfo": f"order_{code}_{round(time.time() * 1000)}"}) |
| | | |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | | |
| | | def get_position_for_cb(code=None, blocking=True): |
| | | """ |
| | | 获取持仓 |
| | | :param code: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, |
| | | {"type": API_TYPE_GET_CODE_POSITION_INFO, "code": code, |
| | | "sinfo": f"cb_{API_TYPE_GET_CODE_POSITION_INFO}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | | |
| | | def refresh_trade_data_for_cb(refresh_type, blocking=True): |
| | | """ |
| | | 刷新交易数据 |
| | | :param refresh_type: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, |
| | | {"type": API_TYPE_REFRESH_TRADE_DATA, "ctype": refresh_type, |
| | | "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | | |
| | | def get_account_money_for_cb(blocking=True): |
| | | """ |
| | | 获取账户资金情况 |
| | | :param code: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | return common_request_for_cb({"ctype": "get_account_money"}, blocking) |
| | | |
| | | |
| | | def refresh_trade_data_for_cb(type_, blocking=True): |
| | | """ |
| | | 刷新可转债交易数据 |
| | | :param code: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, |
| | | {"type": API_TYPE_REFRESH_TRADE_DATA, "ctype": type_, |
| | | "sinfo": f"cb_{API_TYPE_REFRESH_TRADE_DATA}_{round(time.time() * 1000)}"}) |
| | | return __read_response(client, request_id, blocking) |
| | | |
| | | |
| | | def common_request_for_cb(params, blocking=True): |
| | | """ |
| | | 通用请求 |
| | | :param params: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | data = {"type": API_TYPE_COMMON_REQUEST, |
| | | "sinfo": f"cb_{API_TYPE_COMMON_REQUEST}_{round(time.time() * 1000)}"} |
| | | if params: |
| | | for k in params: |
| | | data[k] = params[k] |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_CB, data) |
| | | return __read_response(client, request_id, blocking, timeout=10) |
| | | |
| | | |
| | | def common_request_for_low_suction(params, blocking=True): |
| | | """ |
| | | 通用请求 |
| | | :param params: |
| | | :param blocking: |
| | | :return: |
| | | """ |
| | | data = {"type": API_TYPE_COMMON_REQUEST, |
| | | "sinfo": f"cb_{API_TYPE_COMMON_REQUEST}_{round(time.time() * 1000)}"} |
| | | if params: |
| | | for k in params: |
| | | data[k] = params[k] |
| | | request_id, client = __request(ClientSocketManager.CLIENT_TYPE_TRADE_LOW_SUCTION, data) |
| | | return __read_response(client, request_id, blocking, timeout=10) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | d = {"id": "123123"} |
| | | print(d.pop("id")) |
| | | pass |