File size: 2,515 Bytes
c49b21b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
derivatives.py – Derivatives endpoints for CoinDesk API client.

- list_markets(): List all available derivatives markets.
- get_latest_futures(symbol=None): Fetch the latest futures data, optionally for a symbol.
- get_futures_historical(days, limit=None): Retrieve futures historical data over N days.
- list_options(symbol=None): List available options or option chain for a given asset.
- get_options_historical(symbol, start, end=None, limit=None): Fetch options historical data over a timeframe.
"""

from client import BaseClient

class DerivativesClient(BaseClient):
    def list_markets(self):
        """
        List all available derivatives markets.
        """
        return self._get("derivatives/markets")

    def get_latest_futures(self, symbol=None):
        """
        Get the most recent futures data. If `symbol` is provided, returns data for that symbol.

        :param symbol: Futures symbol, e.g., "BTC-USD" (optional).
        """
        path = "derivatives/futures"
        if symbol:
            path += f"/{symbol}"
        return self._get(path)

    def get_futures_historical(self, days, limit=None):
        """
        Fetch historical futures data for the past `days` days.

        :param days: Number of days of history to retrieve.
        :param limit: Maximum number of records to return (optional).
        """
        params = {"days": days}
        if limit is not None:
            params["limit"] = limit
        return self._get("derivatives/futures/historical", params=params)

    def list_options(self, symbol=None):
        """
        List all available options or get the option chain for a symbol.

        :param symbol: Asset symbol for option chain, e.g., "BTC-USD" (optional).
        """
        path = "derivatives/options"
        if symbol:
            path += f"/{symbol}"
        return self._get(path)

    def get_options_historical(self, symbol, start, end=None, limit=None):
        """
        Fetch historical options data for a symbol over a timeframe.

        :param symbol: Asset symbol, e.g., "BTC-USD".
        :param start: ISO8601 start datetime string.
        :param end: ISO8601 end datetime string (optional).
        :param limit: Maximum number of records to return (optional).
        """
        params = {"start": start}
        if end:
            params["end"] = end
        if limit is not None:
            params["limit"] = limit
        return self._get(f"derivatives/options/{symbol}/historical", params=params)