← all repositories
Yvictor/TradingGym

OpenAI Gym, but your agent loses money instead of playing Atari

A reinforcement learning environment where the reward signal is your P&L, and the episode ends when your margin call does.

1.9k stars Python AgentsDomain Apps
TradingGym
Velocity · 7d
+0.6
★ / day
Trend
steady
star history

What it does TradingGym wraps tick-level market data into an OpenAI Gym-style interface: reset(), step(), render(). You feed it a pandas DataFrame of trades, define your observation window and features, and the environment handles position tracking, fee accounting, and reward calculation. It supports both training mode with rolling windows and a separate backtest mode that runs through historical data in one pass.

The interesting bit The project is built for tick data—individual trade prints, not daily bars—which is the messy reality of actual market microstructure. The transaction_details output gives you a full audit trail of every fill, average entry price, and running P&L, which is more than most toy trading simulators bother with.

Key highlights

  • Gym-compatible API: drop in DQN, PPO, or your own janky Q-table
  • Tick-data native, with OHLC support noted as “WIP”
  • Built-in backtest mode with env.backtest_done and env.backtest() loop
  • Configurable fees, max position limits, and custom feature columns
  • render() produces a visual output (GIF referenced, not shown inline)

Caveats

  • All training examples (DQN, policy gradient, actor-critic, A3C+RNN) are marked “WIP”—you bring your own algorithms
  • README has rough edges: typos (“traning”, “cucalate”), and the backtest example contains pd.concate which is not a real pandas method
  • Real-time trading with Interactive Brokers API is aspirational, not implemented

Verdict Useful if you already have clean tick data and want a Gym-shaped harness for strategy prototyping. Skip it if you need turnkey RL algorithms or production-grade execution infrastructure.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.