# Trade Count, OHLCV, & VWAP

## What is this endpoint for?&#x20;

This endpoint retrieves the Trade Count, OHLCV and VWAP history for any instrument on an exchange. The `interval` parameter is suffixed with `s`, `m`, `h` or `d` to specify seconds, minutes, hours or days, respectively. By making use of the `sort` parameter, data can be returned in ascending `asc` (default) or descending `desc` order.

### Endpoint

{% code overflow="wrap" %}

```http
https://<eu|us>.market-api.kaiko.io/v2/data/trades.v1/exchanges/{exchange}/{instrument_class}/{instrument}/aggregations/count_ohlcv_vwap
```

{% endcode %}

### Path Parameters

<table><thead><tr><th width="203">Parameter</th><th>Required?</th><th>Description</th></tr></thead><tbody><tr><td><code>region</code></td><td>Yes</td><td>Choose between <code>eu</code> and <code>us</code>.</td></tr><tr><td><code>exchange</code></td><td>Yes</td><td><p>Exchange <code>code.</code> </p><p><br>See <br><a data-mention href="../../reference-data/basic-tier/exchange-codes">exchange-codes</a></p></td></tr><tr><td><code>instrument_class</code></td><td>Yes</td><td>Instrument <code>class</code>. <br><br>See <br><a data-mention href="../../reference-data/basic-tier/exchange-trading-pair-codes-instruments">exchange-trading-pair-codes-instruments</a></td></tr><tr><td><code>instrument</code></td><td>Yes</td><td>Instrument <code>code</code>.<br><br><a data-mention href="../../reference-data/basic-tier/exchange-trading-pair-codes-instruments">exchange-trading-pair-codes-instruments</a></td></tr></tbody></table>

### Query Parameters

<table><thead><tr><th width="253">Parameter</th><th width="117">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>continuation_token</code></td><td>No</td><td>See <a data-mention href="../../../general/getting-started/pagination">pagination</a></td></tr><tr><td><code>end_time</code></td><td>No</td><td>Ending time in ISO 8601 (exclusive).<br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>interval</code></td><td>No</td><td>The interval parameter is suffixed with <code>s</code>, <code>m</code>, <code>h</code> or <code>d</code> to specify seconds, minutes, hours or days, respectively.<br><br>Any arbitrary value between one second and one day can be used, as long as it sums up to a maximum of 1 day. The suffixes are <code>s</code> (second), <code>m</code> (minute), <code>h</code> (hour) and <code>d</code> (day).<br><br> Default <code>1d</code>.</td></tr><tr><td><code>page_size</code></td><td>No</td><td>See <a data-mention href="../../../general/getting-started/pagination">pagination</a><br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>start_time</code></td><td>No</td><td>Starting time in ISO 8601 (inclusive).<br><br><em>Automatically included in continuation tokens.</em></td></tr><tr><td><code>sort</code></td><td>No</td><td>Return the data in ascending (<code>asc</code>) or descending (<code>desc</code>) order. Default <code>desc</code><br><br><em>Automatically included in continuation tokens.</em></td></tr></tbody></table>

### Fields

| Field       | Description                                                                         |
| ----------- | ----------------------------------------------------------------------------------- |
| `timestamp` | Timestamp at which the interval begins.                                             |
| `count`     | Then number of trades. `0` when no trades reported.                                 |
| `open`      | Opening price of interval. `null` when no trades reported.                          |
| `high`      | Highest price during interval. `null` when no trades reported.                      |
| `low`       | Lowest price during interval. `null` when no trades reported.                       |
| `close`     | Closing price of interval. `null` when no trades reported.                          |
| `volume`    | Volume traded in interval. `0` when no trades reported.                             |
| `price`     | The volume weighted price during the time interval. `null` when no trades reported. |

### Request examples

{% tabs %}
{% tab title="cURL" %}
{% code overflow="wrap" %}

```url
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: <client-api-key>' \
  'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap'
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code overflow="wrap" %}

````python
```python
##### 1. Import dependencies #####
import requests
import pandas as pd

##### 2. Choose the value of the query's parameters #####
# ---- Required parameters ---- #
exchange = "cbse" 
instrument_class = "spot"
pair = "btc-usd" #called "instrument" in the documentation

# ---- Optional parameters ---- #
interval = "1d"  
sort = "desc"
page_size = 100
start_time= "2023-01-01T00:00:00Z"
end_time= "2023-12-31T23:59:59Z"

# ---- API key configuration ---- #
api_key = "YOUR_API_KEY"

##### 3. Get the data #####
# ---- Function to run an API call ---- # 
# Get the data in a dataframe --------- # 

def get_kaiko_data(api_key: str, exchange: str, pair: str, instrument_class: str, start_time: str, end_time: str, interval: str, sort: str, page_size: int):
    headers = {'Accept': 'application/json', 'X-Api-Key': api_key}
    
    url = f'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/{exchange}/{instrument_class}/{pair}/aggregations/count_ohlcv_vwap'
    params = {
        "start_time": start_time,
        "end_time": end_time,
        "sort": sort,
        "page_size": page_size,
        "interval": interval
    }

    try:
        res = requests.get(url, headers=headers, params=params)
        res.raise_for_status() 
        data = res.json()
        if 'data' not in data:
            print("No data returned.")
            return pd.DataFrame() 
        df = pd.DataFrame(data['data'])

        # Handle pagination with continuation token
        while 'next_url' in data:
            res = requests.get(data['next_url'], headers=headers)
            res.raise_for_status()
            data = res.json()
            if 'data' in data:
                df = pd.concat([df, pd.DataFrame(data['data'])], ignore_index=True)
        return df

    except requests.exceptions.RequestException as e:
        print(f"API request error: {e}")
        return pd.DataFrame() 

# ---- Get the data ---- #
df = get_kaiko_data(api_key=api_key, exchange=exchange, pair=pair, instrument_class=instrument_class, start_time=start_time, end_time=end_time ,interval=interval, sort=sort, page_size=page_size)
```
````

{% endcode %}
{% endtab %}

{% tab title="BigQuery" %}
Trade Count and OHLCV can be accessed through Google BigQuery. \
\
To get started, read our [guide](https://docs.kaiko.com/rest-api/data-feeds/level-1-and-level-2-data/level-1-aggregations/broken-reference).
{% endtab %}
{% endtabs %}

### Response example

```json
{
    "query": {
        "page_size": 100,
        "exchange": "cbse",
        "instrument_class": "spot",
        "instrument": "btc-usd",
        "interval": "1d",
        "sort": "desc",
        "aggregation": "count_ohlcv_vwap",
        "data_version": "v1",
        "commodity": "trades",
        "request_time": "2020-11-12T16:55:42.588Z"
    },
    "time": "2020-11-12T16:55:42.710Z",
    "timestamp": 1605200142710,
    "data": [
        {
            "timestamp": 1605139200000,
            "open": "15705.79",
            "high": "16185.87",
            "low": "15446.82",
            "close": "16139.93",
            "volume": "14829.124546730012",
            "price": "15880.01873841608",
            "count": 95111
        },
        {
            "timestamp": 1605052800000,
            "open": "15315.46",
            "high": "16000",
            "low": "15293.04",
            "close": "15705.79",
            "volume": "15123.844197729988",
            "price": "15664.643871798791",
            "count": 114205
        },
    /* ... */
  ],
  "result": "success",
  "continuation_token": "rbd1XbkjMwv2SyUfvJwsqFGmCKzg3WToTvqigui1bejckYnxd9DM1V3v58iqMCdXa4dJSXap6p6fBuvzz32tiHVrv5LC76MyRyYNbZyvSEoVzd1krSWWeXYEtEtR",
  "next_url": "https://<eu|us>.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?continuation_token=rbd1XbkjMwv2SyUfvJwsui1bejckYnxd9DM1V3v58iqMCdXa4dJSXap6p6fBuvzz32tiHVrv5LC76MyRyYNbZyvSEoVzd1krSWWeXYEtEtR",
  "access": {
    "access_range": {
      "start_timestamp": 1546300800000,
      "end_timestamp": 1577836800000
    },
    "data_range": {
      "start_timestamp": 1417391000000,
      "end_timestamp": 1577836800000
    }
  }
}

```
