Closing the Sim-to-Real Gap: Inside TorchTrade

How a PyTorch-native framework uses TensorDicts and foundation models to deploy reinforcement learning agents directly from backtesting to live exchanges.

8 min read · TorchTrade/torchtrade

A laboratory testing track representing an offline backtest ending at a sheer cliff edge, with a chaotic real-world highway far below. A mechanical vehicle sits paralyzed at the cliff edge, illustrating the difficulty of deploying reinforcement learning agents to live markets.
The backtest-to-live deployment gap is where most algorithmic trading projects stall.

If you have trained a trading policy on historical data and then tried to run it live, you know the gap. The observation shapes do not match, the account state tracks different fields, and the cost model silently diverges. Bridging backtesting and live trading is where most of the engineering effort goes — and where most projects stall.

TorchTrade (Author/Maintainer), Author/Maintainer · Medium Blog
Key Takeaways

The Chasm Between Simulation and Reality

The hardest part of machine learning in quantitative finance is not training the model. It is surviving the sim-to-real gap. A reinforcement learning agent built in a sterile backtesting environment usually breaks when exposed to live exchange websockets because the data structures and latency profiles change.

A profitable backtest often relies on historical CSV data processing that does not translate to live API socket consumption. This mismatch forces engineers to rewrite execution logic, introducing bugs and subtle divergence between the simulated strategy and the live behavior.

Portrait of BY571, the primary contributor to TorchTrade.

The TensorDict Rosetta Stone

TorchTrade solves this deployment chasm by forcing both offline simulation and live execution to use the exact same data structure: the TensorDict via TorchRL. The architecture of torchtrade/envs/core separates the trading logic from the data source.

The PositionState dataclass tracks the physics of a trade, such as entry price and unrealized PnL. The HistoryTracker acts as the environment's memory. This decoupling ensures that the exact same tensor shapes are passed to the agent whether it is trading 2018 historical data or live Alpaca API data.

The TorchTrade execution pipeline routes identical TensorDict structures to either offline simulation or live exchanges.

def execute_on(self, env: TorchTradeBaseEnv):
    # The exact same logic applies whether env is Offline or Live
    observation = env.reset()
    action = self.policy(observation)
    next_obs, reward, done, info = env.step(action)
    return next_obs

LLMs as Market Actors

The framework moves beyond standard Multi-Layer Perceptrons to use LLMs and time-series foundation models as first-class trading actors. The examples/llm/ directory demonstrates how agents can use models like GPT-4o (via vLLM) and Amazon's Chronos embeddings to interpret market context.

By treating trading as a reasoning task, TorchTrade allows developers to integrate frontier models into a rigid mechanical trading engine, executing trades based on complex contextual understanding rather than just numerical optimization.

A close-up of a complex antique clockwork mechanism. Instead of standard gears, the central timing cylinder is a smooth, glowing glass tube feeding a continuous ribbon of ticker-tape into a mechanical brain, illustrating the integration of time-series foundation models.
Integrating time-series foundation models transforms raw market data into contextual embeddings.

The PyTorch-Native Pivot

TorchTrade represents a shift away from legacy tools like TensorTrade. Older frameworks relied on a fragmented stack of TensorFlow, Gym wrappers, and Pandas DataFrames, which often struggled with the performance demands of high-frequency reinforcement learning.

By building deeply into the PyTorch and TorchRL ecosystem, TorchTrade provides a unified, high-performance pipeline. This native integration is why the project can seamlessly route data from a historical backtest to a live Binance account with zero code changes.

FeatureTorchTradeTensorTrade
Base EcosystemPyTorch / TorchRLTensorFlow / Gym
State ManagementTensorDictPandas / NumPy
Live ExecutionZero-code switchCustom API wrappers
Advanced ActorsNative LLM / Chronos supportStandard RL policies