z1norhc
z1norhc

Reputation: 13

Python - API request does not refresh

import requests
from time import sleep

r = requests.get('https://testnet.bitmex.com/api/v1/trade?symbol=.BVOL24H&count=1&columns=price&reverse=true')
data = r.json()
volatility = r.json()[0]['price']


def loop():
    while True:
        print(data)
        print(volatility)
        sleep(10)


loop()

I'm trying to use this data for my project but when I make this API request it keeps returning the same value.

The API refreshes its data every 5 minutes. (12:35:00, 12:40:00 etc.)

How do I make sure my function always has the most recent data?

Upvotes: 1

Views: 939

Answers (1)

Sharku
Sharku

Reputation: 1092

You need to request new data in each run of the loop.

import requests
from time import sleep

def loop():
    while True:
        r = requests.get('https://testnet.bitmex.com/api/v1/trade?symbol=.BVOL24H&count=1&columns=price&reverse=true')
        data = r.json()
        volatility = r.json()[0]['price']
        print(data)
        print(volatility)
        sleep(10)


loop()

Upvotes: 3

Related Questions