Bentrix Docs
HomePricingDashboard

The Python SDK

import bentrix, connect, and every tool is a method — with types, jobs, streams and the in-process engine beside it.

The client is the package bentrix (Python 3.9 or later). It finds the Bentrix running on this computer through the agents folder Bentrix writes at every start, pairs with it under the name you give — the name the person sees in ☰ › Settings › Agents, where the pairing's scopes are set — and talks to it over MCP. Nothing is typed: no address, no key.

01What you need

Python 3.9 or later, and Bentrix open on the same computer.

02Install

Two ways, the same package either way.

From PyPI. The SDK is the package bentrix on PyPI, its version the application's:

pip install bentrix

From its source on your computer. Its source is github.com/bentrixhq/bentrix-python, under the MIT licence; a clone of it is the SDK on your computer:

git clone https://github.com/bentrixhq/bentrix-python
pip install ./bentrix-python

The client needs nothing else. The engine's modules — bentrix.data, bentrix.gym, bentrix.forecast, bentrix.vector and bentrix.live — need NumPy, and the Gym's spaces Gymnasium (from a clone, pip install "./bentrix-python[engine]" alike):

pip install "bentrix[engine]"    # NumPy
pip install "bentrix[gym]"       # NumPy and Gymnasium

Check it:

python -c "import bentrix; print(bentrix.CATALOGUE_VERSION)"

03Connect

import bentrix

b = bentrix.connect(name="my-agent")     # finds the running Bentrix and pairs; the name shows in Settings › Agents
state = b.workspace.get_state(depth="benches")
bench = state["laboratories"][0]["benches"][0]["uri"]
home, owner, _, name = bench.removeprefix("bentrix://").split("/")
book = b.trade.book(home, owner, name)
print(len(book["positions"]), "positions open")

04How a call looks

Every tool of the catalogue is a method, grouped by its domain: b.workspace, b.chart, b.study, b.trade … in snake case. A method takes the tool's arguments as Python arguments — the required ones by position, the optional ones by keyword, and answers the tool's answer as a dict. The whole list is at the end of this page; each method links to its tool's full entry — the arguments with their types and defaults, the scope a pairing must grant, and the plan.

05When Bentrix says no

A tool that runs and refuses raises bentrix.ToolError, carrying the product's own sentence (and, in machine, the kind: busy, expired, insufficient_scope …); a request the server refuses before running anything raises bentrix.RpcError. bentrix.NotRunning and bentrix.PairingRefused say there was nothing to connect to, or that Bentrix said no. The client also checks, when it connects, that the catalogue it was generated from is the one the running Bentrix serves: a different major version is refused before any call, so a program never calls a tool with arguments that no longer fit.

Busy is waited out, not raised. Bentrix answers one program at most 20 calls a second (a burst of 60), and answers a call past that busy (-40001, with the wait in retryAfterMs) before running anything. The client waits that long and sends the call again — so an order is never sent twice by it — up to ten times, each wait at most five seconds, and only then raises the refusal: bentrix._runtime.BUSY (-40001), BUSY_RETRIES (10) and BUSY_WAIT_MAX_S (5).

06Long work and streams

A tool that starts long work — a strategy run, a batch, a question put to the person in Bentrix's own window — answers a job; in Python a bentrix.Job, whose wait() returns the result once it ends (it reports awaiting_person while the person has a decision to make). A stream (bars as they close, ticks, quotes, deals, the Journal) is followed with wait_for, which answers what came after the cursor it is given and the cursor to give next.

07Beyond MCP: the engine in your program

The same package carries Bentrix's laboratory engine for a program that runs without the application open — the replay clock and the venue through Bentrix's C library (bentrix.backtest, bentrix.data), a reinforcement-learning environment in the common Gym shape (bentrix.gym), and the @study decorator for a Python indicator Bentrix draws like a built-in (bentrix.study). See Python and the Gym.

Each part of it has a page of its own:

ModuleWhat it isPage
bentrix.localLocal: a workspace opened in your process — tools by name, the clock stepped a close at a time, snapshotsThe engine in your own program, snapshots and branching
bentrix.gymTradingEnv, make_vec, evaluate, iqm, growing, Draw, registerWith Gymnasium and Stable-Baselines3, many environments at once
your own environment over LocalA Gymnasium environment with your observation, actions and rewardAn environment of your own
bentrix.backtestStrategy, Context, Bar, run: a callback per closed candleA strategy in Python, candle by candle
bentrix.databars, quality, segments, triple_barrier, walk_forward, purged_kfold, indicator, to_arrowDatasets and a fast first pass
bentrix.vectorfrom_signals, reconcile: a vectorised pass, set beside the engineDatasets and a fast first pass
bentrix.liveRemote, LiveEnv, Model, Retrain, serve, run, parity: the same code on a paper or a real workbenchOn a live market
bentrix.trackidentity, log_run: a run's tags in MLflow and Weights & BiasesThe engine in your own program
python -m bentrixtape, backtest, battery from a terminalA strategy in Python, candle by candle, checking the engine yourself

The orders, the fill model and the checks are the same tools as over MCP: orders, legs and exposure, slippage, latency and spread, several timeframes without look-ahead, checking the engine yourself. For a team building its own agent, what the engine supports is the whole list on one page.

The engine is Bentrix's own library, libbentrix, installed with Bentrix — on Windows in %LOCALAPPDATA%\Programs\BENTRIX — where the package finds it by itself; BENTRIX_FFI (the file) or BENTRIX_FFI_DIR (its folder) names another. It runs for a computer signed in to Bentrix with Plus or Pro; with Free, a program reaches Bentrix through MCP, as above, with the application open.

Your first program

Save this as a file, with Bentrix open on this computer. It connects, lists your laboratories and their workbenches, and prints the selected workbench’s markets.

import bentrix

client = bentrix.connect(name="my-agent")

# Every laboratory, and the workbenches in it.
state = client.workspace.get_state(depth="benches")
for lab in state["laboratories"]:
    print("laboratory:", lab["name"])
    for bench in lab["benches"]:
        print("  workbench:", bench["uri"])

Run it

python my_agent.py

The first run pairs the program: it appears in ☰ › Settings › Agents under the name it gave (my-agent), with the scopes read, lab and paper. If it says Bentrix is not running, open Bentrix and run it again.

Next

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed