【问题标题】:RestAPI filter params JSONRest API 过滤器参数 JSON
【发布时间】:2022-01-26 09:11:41
【问题描述】:

我正在尝试从 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 编码。

【问题讨论】:

    标签: python api urlencode bitmex


    【解决方案1】:

    正如Filtering part 所说

    许多表端点采用过滤器参数。这应该是 JSON

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

    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}

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-01-28
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多