# Built-In Functions (/scripting-v2/built-ins)



`ctx.study` exposes the same Rust calculations used by Market Lab's built-in study commands. Pass normal Python dictionaries and lists; helpers return dictionaries and lists.

Return blocks below are compact type sketches, not executable Python expressions.

## Candle Functions [#candle-functions]

Use retained candle history with `sma` and `ema`:

```python
candles = history.source("btc@candles@binancef:timeframe=60")
sma = ctx.study.sma(candles, {"field": "c", "window": 20})
ema = ctx.study.ema(candles, {"field": "c", "window": 20})
```

Both return:

```python
{
    "latest": float | None,
    "previous": float | None,
    "points": list[float | None],
}
```

`ema` is seeded with the SMA of the first complete window.

## `ctx.study.cvd` [#ctxstudycvd]

CVD consumes MMT `vd` records, not normal OHLCV candles:

```python
SELECTOR = "btc@vd@hyperliquidf@mmt:timeframe=60,bucket=1"
result = ctx.study.cvd(
    history.source(SELECTOR),
    {"bucket": 1},
)
```

It also accepts one current VD record.

```python
{
    "latest": float | None,
    "previous": float | None,
    "delta": float,
    "bucket": int,
    "points": [
        {"t": int | None, "delta": float, "cumulative": float},
    ],
}
```

## Orderbook Functions [#orderbook-functions]

Use the latest normalized orderbook snapshot:

```python
book = history.source("btc@orderbook@bulkf:depth=20", 0)
if book is None:
    return

spread = ctx.study.spread(book)
depth = ctx.study.depth(book, {"levels": 20})
imbalance = ctx.study.imbalance(book, {"depth": 20})
slippage = ctx.study.slippage(
    book,
    {"side": "buy", "notional": 100000},
)
vamp = ctx.study.vamp(book, {"dollar_depth": 250000})
```

## `ctx.study.spread` [#ctxstudyspread]

```python
{
    "best_bid": float,
    "best_ask": float,
    "spread_abs": float,
    "spread_bps": float,
    "mid": float,
}
```

## `ctx.study.depth` [#ctxstudydepth]

```python
{
    "bid_base": float,
    "ask_base": float,
    "bid_quote": float,
    "ask_quote": float,
    "total_quote": float,
}
```

## `ctx.study.imbalance` [#ctxstudyimbalance]

```python
{
    "bid_volume": float,
    "ask_volume": float,
    "imbalance": float,
}
```

## `ctx.study.slippage` [#ctxstudyslippage]

```python
{
    "avg_fill_price": float,
    "best_price": float,
    "slippage_abs": float,
    "slippage_bps": float,
    "levels_consumed": int,
}
```

## `ctx.study.vamp` [#ctxstudyvamp]

```python
{
    "ask_vwap": float,
    "bid_vwap": float,
    "vamp": float,
    "ask_levels_consumed": int,
    "bid_levels_consumed": int,
    "max_reachable_quote_ask": float,
    "max_reachable_quote_bid": float,
    "complete": bool,
}
```

See [Candles](/scripting-v2/data-types/candles) and [Orderbook](/scripting-v2/data-types/orderbook) for the accepted input shapes.
