Reinforcement learning on Bentrix: a trading environment over a workbench, with the venue's own fills and costs, trained from your own program in any of nine languages.
What the Gym is
The Gym turns a workbench into a reinforcement-learning environment — the shape every learning library expects. Your program, the agent, repeats one loop:
- it observes the market and its account,
- it chooses an action — flat, long or short,
- Bentrix carries the action out on the workbench's venue, moves the clock to the next candle's close, and answers a reward and the next observation.
An episode is one run of that loop over a stretch of history; training is many episodes, the agent changing how it chooses after each one.
01Why train here
- The fills are the venue's. Every action is an order on the same simulated venue a person trades on: the spread, the commission, the swap, the volume step, the stops level. A policy cannot learn to exploit a fill that does not exist.
- No look-ahead, by construction. An observation is built only from candles closed at the clock. The next candle does not exist yet when the agent chooses — not by discipline, but because the clock has not served it.
- Every episode is repeatable. The same seed and the same actions give the same deals, and the environment answers a ledger hash that proves it. A result can be checked by running it again.
- Any language. The environment is part of Bentrix itself, so the same eight calls work from Python, JavaScript, C#, Go, Java, C++, Julia, R and Rust. In Python it is also a ready Gymnasium environment for Stable-Baselines3, CleanRL and the rest.
02What you need
- Bentrix open on this computer, with a laboratory workbench of its own for training. Each new episode rewinds the workbench to its start and undoes its deals — so never train on a workbench whose trades you want to keep. A laboratory to practise in makes one in a minute.
- The SDK for your language, installed and connected: start in your language.
- A pairing with the lab scope (the default for a program).
03The calls
| Call | What it does |
|---|---|
env_make | makes an environment over a workbench and answers its id, its observation and its actions |
env_reset | starts an episode and answers the first observation |
env_step | takes one action and answers the reward, the next observation and whether the episode ended |
run_episode | resets and takes a whole list of actions in one call |
env_act, env_observe | the same loop on a paper or real workbench, whose clock is the market's |
env_close | lets the environment go — the workbench and its deals stay |
extract_dataset | writes labelled candles for supervised learning instead |
04Where to begin
Read how one step works, then run your first episode. The guides after it beat a simple rule, train a policy, and move to Python libraries.
The Gym is a laboratory instrument. Nothing an agent does here is ranked or published, and a reward is a training signal, not a score of anyone.
How the environment works
What one step is, what an observation holds, the three kinds of action, the eight rewards, episodes and seeds, every setting.
- How one step worksWhat happens between the action your agent sends and the reward it gets back — the order, the clock, the reward and the end of an episode.
- What an observation holdsThe two parts of every observation — the last candles as log ratios to the last close, and the account — with a worked example and why they are scaled this way.
- The three kinds of actionDiscrete, continuous and order actions — what each number means, how a size becomes lots, and what a stop and a target do.
- The eight rewardsEvery reward the environment computes, with its formula, what it teaches an agent, and which to start with.
- Episodes and seedsThe three kinds of episode, how a seed chooses where one starts, a curriculum of lengthening episodes, your own sampler, and the ledger hash that proves a run.
- Every settingEvery argument of env_make, env_reset, env_step, run_episode, env_act, env_observe and env_close, with its default and what it answers.
Step by step, in nine languages
Each guide is a whole program in Python, JavaScript, C#, Go, Java, C++, Julia, R and Rust.
- Your first episodeMake an environment, run one whole episode, add up its reward, and prove with the ledger hash that the same seed gives the same deals.
- A rule to beatRead the observation, act on it with a simple momentum rule, and compare it with always-long on the same episodes — the baseline every learned policy must beat.
- Train a policyThe program learns its own parameter: it tries thresholds on training episodes, keeps the best, then tests it on episodes it never saw — training and testing in nine languages, no library.
- Whole episodes in one callrun_episode takes a list of actions and answers every step's reward, refusal and equity at once — the fast way to score a policy decided in advance.
With a Python library
Gymnasium, Stable-Baselines3 and a loop of your own, on the same environment.
- With Gymnasium and Stable-Baselines3The environment as a Gymnasium environment, trained with PPO from Stable-Baselines3, then evaluated on seeds it never saw — one Python program, start to end.
- Your own learner, in a page of PythonTabular Q-learning written from scratch with NumPy — a state from the observation, an action-value table, exploration, the update rule — trained and tested on the environment.
The engine in your own program
Bentrix's replay clock and venue inside your own Python program: your own environment, orders and legs, several timeframes, fills, snapshots and branching, many environments, a strategy, datasets, and checking the engine yourself.
- The engine in your own programBentrix's replay clock and venue inside your own Python process — no window, no server — opened with bentrix.local.Local, stepped a candle at a time, traded and read with the same tools an agent calls.
- An environment of your ownA Gymnasium environment written by you on Bentrix's engine — your observation, your actions, your reward, your episodes — with none of the ready-made environment's choices, in about ninety lines.
- Orders, legs and exposureEvery order operation from a program on the engine — several positions at once, adding, reducing, closing one or all, reversing, moving stops, trailing, pending orders — and net and gross exposure computed from the book.
- Several timeframes without look-aheadRead many intervals of one or several markets cut at one instant with bars_window — which candles are closed, which one is still forming — and a short test that shows no price from the future can reach an observation.
- Slippage, latency and spreadMake a workbench whose fills are worse than the recording's — slippage that is fixed, random from a seed or scaled to the market's move, a delay before a market order fills, a wider or session-dependent spread — and see that the same seed gives the same fills.
- Snapshots and branchingKeep a workbench's whole state — its clock, positions, orders and account — in memory, come back to it in a millisecond, try several continuations from one moment, and pick a workbench up again in another sitting exactly where it was left.
- Many environments at onceStep several environments together — one workbench each, with its own account, clock, seed and fill conditions — inside one program, or run several programs side by side over one set of recordings.
- A strategy in Python, candle by candlebentrix.backtest — write a strategy with on_bar, and the engine calls it at every closed candle with a context that buys, sells, modifies and closes on the workbench's own venue; one market or several on one clock.
- Datasets and a fast first passbentrix.data and bentrix.vector — a workbench's candles as NumPy columns, a check of how each day is held, triple-barrier labels, walk-forward and purged splits, a vectorised first pass over a signal, and its reconciliation with the engine.
- Checking the engine yourselfDo not take the venue on trust — check a fill against the ticks, a stop against the bid, the money against the arithmetic and a run against its repeat, in twenty lines; then run the sixteen-check battery from a program or a terminal.
- What the engine supports for an autonomous agentOne list for a team building its own trading agent — what Bentrix does for you today, what you build yourself on top of it, and what this version does not do — with the page that shows each.
Further
A live market, a labelled dataset, and how to evaluate a policy honestly.
- On a live marketRun a trained policy on a paper workbench — or, with your permission, a real one — where the market's clock moves and nothing can be rewound.
- A labelled dataset for supervised learningextract_dataset writes a workbench's closed candles with triple-barrier labels — did a trade reach its target, its stop, or neither — as a CSV for any machine-learning library.
- Evaluating a policy honestlyWhy a policy's training reward misleads, how to split episodes, how many seeds to use, why the interquartile mean, and the baselines to beat.