Home > Back-end >  RestAPI filter params JSON
RestAPI filter params JSON

Time:12-28

I'm trying to get the last data from the bitmex API

Base URI: https://www.bitmex.com/api/v1

I don't really understand how to get the last data (from today) using filters : https://www.bitmex.com/app/restAPI

here is my code:

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)

and the response is :

 ...
{'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}

it's missing a few hours, I tried using endTime, StartTime and Count without success.. I think I need to pass another filter like endtime = now and timestamp.time = now but I don't know how to send a payload or how to url-encode it.

CodePudding user response:

As Filtering part tells

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

These parameters are not keys in the query string, but keys in a dictionnary given in 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)

Example

/trade?symbol=.BVOL24H&filter={"startTime":"2021-12-20","timestamp.time":"12:00:00.000"}

CodePudding user response:

You can add additional parameters to the url with & like below.

'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()}'

  • Related