admin管理员组

文章数量:1291217

I'm trying to fetch historical candles from dydx APis V3 (run until 28/10/2024) and V4 (started on 16/12/2025).

On API V4 the function getCandles does not seem to take into account the arguments fromISO & toIso as the answer from the API is always the last available candles to today.

Same for API V3 sending back only the last 35 available candles to its sunset on 28/10/2024

Example of requestion 100 1-hour-candles from API V4 with and without FromIso argument

import requests
from datetime import datetime, timezone
import json

def test_v4_api():
    base_url = "/v4"
    endpoint = "/candles/perpetualMarkets/BTC-USD"
    url = f"{base_url}{endpoint}"
    
    # Test 1: Date récente (20/01/2025)
    print("Test 1: Date récente")
    test_date = datetime(2025, 1, 1, 0, 0, tzinfo=timezone.utc)
    params1 = {
        'resolution': '1HOUR',
        'limit': 100,
        'fromISO': test_date.strftime("%Y-%m-%dT%H:%M:%S.000Z")
    }
    
    print(f"\nParamètres: {params1}")
    response1 = requests.get(url, params=params1)
    print(f"Status code: {response1.status_code}")
    print("Headers reçus:", json.dumps(dict(response1.headers), indent=2))
    
    if response1.status_code == 200:
        data = response1.json()
        candles = data.get('candles', [])
        print(f"Nombre de candles reçues: {len(candles)}")
        if candles:
            timestamps = [datetime.strptime(candle['startedAt'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
                        for candle in candles]
            timestamps.sort()
            print(f"Premier: {timestamps[0]}")
            print(f"Dernier: {timestamps[-1]}")
    
    # Test 2: Sans paramètres de date
    print("\nTest 2: Sans paramètres de date")
    params2 = {
        'resolution': '1HOUR',
        'limit': 100
    }
    
    print(f"\nParamètres: {params2}")
    response2 = requests.get(url, params=params2)
    print(f"Status code: {response2.status_code}")
    print("Headers reçus:", json.dumps(dict(response2.headers), indent=2))
    
    if response2.status_code == 200:
        data = response2.json()
        candles = data.get('candles', [])
        print(f"Nombre de candles reçues: {len(candles)}")
        if candles:
            timestamps = [datetime.strptime(candle['startedAt'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
                        for candle in candles]
            timestamps.sort()
            print(f"Premier: {timestamps[0]}")
            print(f"Dernier: {timestamps[-1]}")

if __name__ == "__main__":
    test_v4_api()

Answer below:

Test 1: Date récente

Paramètres: {'resolution': '1HOUR', 'limit': 100, 'fromISO': '2025-01-01T00:00:00.000Z'}
Status code: 200
Headers reçus: {
  "Date": "Sat, 01 Feb 2025 14:12:33 GMT",
  "Content-Type": "application/json; charset=utf-8",
  "Transfer-Encoding": "chunked",
  "Connection": "keep-alive",
  "x-powered-by": "Express",
  "x-request-id": "92cec52d-2d6c-4421-b71b-5e43de3b096b",
  "access-control-allow-origin": "*",
  "surrogate-control": "no-store",
  "Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate",
  "pragma": "no-cache",
  "expires": "0",
  "ratelimit-remaining": "98",
  "ratelimit-reset": "1738419157294",
  "ratelimit-limit": "100",
  "etag": "W/\"7bf9-dmZ5qqLeE/Y+EJkavTNItexIYs8\"",
  "x-response-time": "563.442",
  "cf-cache-status": "DYNAMIC",
  "Server": "cloudflare",
  "CF-RAY": "90b28979cc30d081-CDG",
  "Content-Encoding": "br"
}
Nombre de candles reçues: 100
Premier: 2025-01-28 11:00:00+00:00
Dernier: 2025-02-01 14:00:00+00:00

Test 2: Sans paramètres de date

Paramètres: {'resolution': '1HOUR', 'limit': 100}
Status code: 200
Headers reçus: {
  "Date": "Sat, 01 Feb 2025 14:12:34 GMT",
  "Content-Type": "application/json; charset=utf-8",
  "Transfer-Encoding": "chunked",
  "Connection": "keep-alive",
  "x-powered-by": "Express",
  "x-request-id": "23eac39a-6b53-4952-aa9c-2f777e8fe703",
  "access-control-allow-origin": "*",
  "surrogate-control": "no-store",
  "Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate",
  "pragma": "no-cache",
  "expires": "0",
  "ratelimit-remaining": "97",
  "ratelimit-reset": "1738419157293",
  "ratelimit-limit": "100",
  "etag": "W/\"7bf9-dmZ5qqLeE/Y+EJkavTNItexIYs8\"",
  "x-response-time": "19.264",
  "cf-cache-status": "DYNAMIC",
  "Server": "cloudflare",
  "CF-RAY": "90b2897f0aaa02cf-CDG",
  "Content-Encoding": "br"
}
Nombre de candles reçues: 100
Premier: 2025-01-28 11:00:00+00:00
Dernier: 2025-02-01 14:00:00+00:00

Am I badly using this function? Is there an alternative way to fetch historical data besides funding levels?

  "startedAt": "string",
  "ticker": "string",
  "resolution": "1MIN",
  "low": "string",
  "high": "string",
  "open": "string",
  "close": "string",
  "baseTokenVolume": "string",
  "usdVolume": "string",
  "trades": 0.1,
  "startingOpenInterest": "string",

Thank you for your help !

本文标签: python 3xGet historical candles from dydx API V3 amp V4Stack Overflow