All pages
Powered by GitBook
1 of 1

Loading...

Value at risk calculation

What is this endpoint for?

This endpoints helps you calculate Value at Risk (VaR) and stressed VaR. Computed by a proprietary and thoroughly backtested methodology that accounts for the idiosyncrasies of crypto market structure. By convention, it's a forecasting VaR, i.e., the prediction of the potential loss for the next day. It includes a mixture parameter that can provide either the VaR, the stressed VaR, or a combination of the two (see the stress_parameter below).

Endpoint

Parameters

Parameter
Required
Description

Fields

Parameter
Description

Request examples

Response example

Yes

Quantities list of base asset in the portfolio. Must match the number of bases. The order of quantities and their respective bases must match in the request.

risk_level

Yes

The Value at Risk confidence level. Min: 0.90 (included) Max: 1 (excluded)

start_time

Yes

First fixing of the calculation in ISO 8601 (inclusive).

end_time

Yes

Last fixing of the calculation in ISO 8601 (inclusive).

stress_parameter

No

Float between 0 and 1 that controls the blend between VaR and Stressed VaR.

It is 0 for VaR and 1 for Stressed VaR. It equals 0 by default.

reporting_currency

No

This allows you to express the final risk metrics in a specific fiat currency.

By default, expressed in USD.

sources

No

boolean. If true, returns all pair prices which were used to compute the Value at Risk. Default: false

The reference price per asset. (showing only when sources is set to be true)

date

The date of the reference price. (showing only when sources is set to be true)

bases

Yes

List of portfolio base components. See Asset codes

Total must match quantities below. The order of bases and their respective quantities must match in the request.

quote

Yes

The fiat currency. Asset codes

var_time

The time at which the VaR is computed.

value_at_risk

Composed of two fields: value and risk_level (the Value at Risk estimator at the specified risk_level.)

pair

The constituent pair. (showing only when sources is set to be true)

quantities

ref_price

https://us.market-api.kaiko.io/v2/data/analytics.v2/value_at_risk
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: <client-api-key>' \
'https://us.market-api.kaiko.io/v2/data/analytics.v2/value_at_risk?bases=eth,btc,ltc&quantities=3,2,5&quote=usd&risk_level=0.95&start_time=2025-12-01T00:00:00.000Z&end_time=2025-12-05T00:00:00.000Z&stress_parameter=0.5'
##### 1. Import dependencies #####
import requests
import pandas as pd

##### 2. Choose the value of the query's parameters #####
# ---- Required parameters ---- #
bases = "btc,eth"
quote = "usd"
quantities = "1,10"
risk_level = "0.95"
start_time = "2021-12-01T00:00:00.000Z"
end_time = "2022-01-31T00:00:00.000Z"

# ---- Optional parameters ---- #
sources = None

# ---- 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, bases: str, quote: str, quantities: str, risk_level: str, start_time: str, end_time: str, sources: bool = None):
    headers = {'Accept': 'application/json', 'X-Api-Key': api_key}
    
    url = f'https://us.market-api.kaiko.io/v2/data/analytics.v2/value_at_risk'
    params = {
        "bases": bases,
        "quote": quote,
        "quantities": quantities,
        "risk_level": risk_level,
        "start_time": start_time,
        "end_time": end_time,
        "sources": sources
    }

    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:
            next_url = data['next_url']
            if next_url is None:
                break
            res = requests.get(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, bases=bases, quote=quote, quantities=quantities, risk_level=risk_level, start_time=start_time, end_time=end_time, sources=sources)
print (df)
{
    "query": {
        "bases": "eth,btc,ltc",
        "quote": "usd",
        "quantities": [
            3,
            2,
            5
        ],
        "exchanges": "",
        "risk_level": 0.95,
        "sources": false,
        "data_version": "v2",
        "commodity": "analytics",
        "request_time": "2026-04-01T14:02:10.954Z",
        "reporting_currency": ""
    },
    "time": "2026-04-01T14:02:11.316Z",
    "timestamp": 1775052131316,
    "data": [
        {
            "var_time": 1764547200000,
            "value_at_risk": {
                "value": 14051.3051028357,
                "risk_level": 0.95
            }
        },
        {
            "var_time": 1764633600000,
            "value_at_risk": {
                "value": 13896.656231419009,
                "risk_level": 0.95
            }
        },
        {
            "var_time": 1764720000000,
            "value_at_risk": {
                "value": 14243.618215415132,
                "risk_level": 0.95
            }
        },
        {
            "var_time": 1764806400000,
            "value_at_risk": {
                "value": 14366.155261745589,
                "risk_level": 0.95
            }
        },
        {
            "var_time": 1764892800000,
            "value_at_risk": {
                "value": 14317.703400730708,
                "risk_level": 0.95
            }
        }
    ],
    "start_date": "2021-12-01T00:00:00.000Z",
    "access": {
        "access_range": {
            "start_timestamp": 1763683200,
            "end_timestamp": null
        },
        "data_range": {
            "start_timestamp": null,
            "end_timestamp": null
        }
    }
}