Market LabDocs
Scripting V2 - PythonPlain Python Files

Built-In Functions

Native Market Lab study helpers available through ctx.study in Python V2.

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

Use retained candle history with sma and ema:

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:

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

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

ctx.study.cvd

CVD consumes MMT vd records, not normal OHLCV candles:

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.

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

Orderbook Functions

Use the latest normalized orderbook snapshot:

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

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

ctx.study.depth

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

ctx.study.imbalance

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

ctx.study.slippage

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

ctx.study.vamp

{
    "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 and Orderbook for the accepted input shapes.

On this page