拉取交易所订单列表时,常常需要分页拉取,但调用的时候希望一次获得完整的数据,这时采用递归的方式
订单json如下所示
{ "datas":{ "totalRow":625, "entrustList":[ { "entrustId":"E6615806999819034624", "type":1, "completeAmount":"0", "price":"0.001874", "availabelAmount":"1047", "sequeueId":6615806999819034624, "completeTotalMoney":"0", "amount":"1047", "createTime":1577331304507, "status":0 }, { "entrustId":"E6615806999282163712", "type":1, "completeAmount":"0", "price":"0.001883", "availabelAmount":"1047", "sequeueId":6615806999282163712, "amount":"1047", "createTime":1577331304379, "status":0 } ], "totalPage":63, "pageSize":10, "pageNum":2 }, "resMsg":{ "code":"1", "message":"success !", "enMessage":"None" } }
python3代码如下
#获取未成交订单 def order_open(self, symbol, **kwargs): body = { 'marketId' : self.market2id[symbol], 'pageIndex' : 1, 'pageSize' : 100, } for key, value in kwargs.items(): body[key] = value params = { 'body' :body, 'timestamp' :str(int(time.time() * 1000)) } params['sign'] = self.signature("GET", params) url = self._web_url + '/oder_open' result = self.http_request(url, method = "GET", params = params) dict = {} for item in result["datas"]["entrustList"]: filter_dict = {} filter_dict['price'] = float(item["price"]) filter_dict['quantity'] = float(item["amount"]) filter_dict['direction'] = 'BUY' if item["type"] == 1 else "SELL" filter_dict['time'] = float(item["createTime"]) dict[item["entrustId"]] = filter_dict if (body['pageIndex'] <= int(result["datas"]["totalRow"] / 100)): dict.update(self.order_open(symbol, pageIndex=body['pageIndex'] + 1)) return dict else: return dict
递归的核心在于这一段
if (body['pageIndex'] <= int(result["datas"]["totalRow"] / 100)): dict.update(self.order_open(symbol, pageIndex=body['pageIndex'] + 1)) return dict else: return dict