Rest API 过滤器参数 JSON

RestAPI filter params JSON

我正在尝试从 bitmex 获取最后的数据 API

基本 URI:https://www.bitmex.com/api/v1

我不太明白如何使用过滤器获取最后的数据(从今天开始):https://www.bitmex.com/app/restAPI

这是我的代码:

from datetime import date

import requests
import json
import pandas as pd



today = date.today()
d1 = today.strftime("%Y-%m-%d")
#print("d1 =", d1)

def parser():

 today = date.today()

 # yy/dd/mm
 d1 = today.strftime("%Y-%m-%d")
 # print("d1 =", d1)
 return f'https://www.bitmex.com/api/v1/trade?symbol=.BVOL24H&startTime={d1}&timestamp.time=12:00:00.000&columns=price'


# Making a get request
response = requests.get(parser()).json()
# print(response)

for elem in response:
  print(elem)

响应是:

 ...
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:05:00.000Z', 'price': 2.02}
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:10:00.000Z', 'price': 2.02}
{'symbol': '.BVOL24H', 'timestamp': '2021-12-27T08:15:00.000Z', 'price': 2.02}

它少了几个小时,我尝试使用 endTime、StartTime 和 Count 但没有成功.. 我想我需要传递另一个过滤器,如 endtime = now 和 timestamp.time = now 但我不知道如何发送有效负载或如何 url-encode 它。

您可以使用 & 向 url 添加其他参数,如下所示。

'https://www.bitmex.com/api/v1/trade?symbol=.BVOL24H&startTime={d1}&timestamp.time=12:00:00.000&columns=price&endTime={date.today()}&timestamp.time={date.today()}'

Filtering part所述

Many table endpoints take a filter parameter. This is expected to be JSON

这些参数不是查询字符串中的键,而是 filter key

中给出的字典中的键
url = "https://www.bitmex.com/api/v1/trade"
filters = {
    'startTime': date(2021, 12, 20).strftime("%Y-%m-%d"),
    'timestamp.time': '12:00:00.000'
}
params = {
    'symbol': '.BVOL24H',
    'filter': json.dumps(filters),
}
response = requests.get(url, params=params)
for elem in response.json():
    print(elem)

示例

/trade?symbol=.BVOL24H&filter={%22startTime%22:%222021-12-20%22,%22timestamp.time%22:%2212:00:00.000%22}