TensorTrade Documentation

repository·master·Indexed 27 days ago

https://github.com/tensortrade-org/tensortrade

An open-source Python framework for building, training, and evaluating reinforcement learning (RL) agents for algorithmic trading. It features a modular architecture with composable components including TradingEnv, ActionSchemes, RewardSchemes, Observers, Portfolios, and Exchanges. The framework supports integration with Ray/RLlib for distributed training and Optuna for hyperparameter optimization, requiring Python 3.11 or 3.12.

Tokens
40.8K
Snippets
107
Records
195
Agent score
89%

What's inside TensorTrade

  1. Overview of TensorTrade

    master

    TensorTrade is an open-source Python framework designed for building, training, evaluating, and deploying trading algorithms using reinforcement learning. It is built to be highly composable and extensible, allowing users to scale from simple single-CPU strategies to complex investment strategies on HPC clusters.

    The framework leverages existing machine learning ecosystems including numpy, pandas, gym, keras, and tensorflow to maintain high-quality data pipelines and learning models. It is structured around reusable, standalone modules such as exchanges, feature pipelines, action schemes, reward schemes, trading agents, and performance reports.

  2. Use slippage models in TensorTrade OMS

    master

    The tensortrade.oms.services.slippage package provides services for modeling slippage within the Order Management System (OMS). Slippage models are used to simulate the difference between the expected price of a trade and the price at which the trade is actually executed.

    This package includes:

    • slippage_model: The base interface/class for defining slippage behavior.
    • random_slippage_model: A specific implementation that applies random slippage to trades.
  3. Understand the TensorTrade workflow components

    master

    TensorTrade combines trading environments with Reinforcement Learning (RL) agents. The core components are:

    TermMeaningIn TensorTrade
    AgentThe learner/decision makerA neural network
    EnvironmentWhere the agent actsSimulated market
    StateWhat the agent seesMarket data (prices, indicators)
    ActionWhat the agent doesBuy, sell, or hold
    RewardFeedback signalProfit or loss

    The training loop follows these steps:

    1. Agent observes the State (market data).
    2. Agent performs an Action (BUY, SELL, or HOLD).
    3. The Environment updates based on the action.
    4. The Agent receives a Reward (profit/loss).
    5. The Agent adjusts its strategy based on the reward.
  4. Understand the Informer component

    master

    The Informer component is responsible for delivering contextual environment information following each step execution in a TensorTrade environment. It provides metadata or state information that is not necessarily part of the observation or reward, but is useful for monitoring or logging.

    In the default environment implementation, the Informer delivers the following information in the step functions:

    • portfolio
    • broker
    • net_worth
  5. Understand the TensorTrade Architecture Layers

    master

    TensorTrade is organized into three functional layers:

    1. Data Layer: Uses a DataFeed (OHLCV + Derived Features) and a Stream API for reactive data processing.
    2. Environment Layer: A Gym-compatible TradingEnv consisting of:
      • Observer: Converts DataFeed into a state.
      • ActionScheme: Maps agent actions to orders.
      • RewardScheme: Calculates the learning signal (reward).
    3. Execution Layer: The OMS which simulates real trading via an Exchange (price/commission), Broker (execution), and Portfolio (wallet management).
  6. Understand the Three Pillars of TensorTrade

    master

    TensorTrade integrates three distinct domains to enable reinforcement learning for trading:

    1. Reinforcement Learning (RL): The mechanism where an agent learns through trial and error by taking actions in an environment to maximize rewards.
    2. Trading Domain: The simulation of market mechanics, including an Order Management System (OMS) that manages portfolios, exchanges, and commissions.
    3. Data Engineering: The process of transforming raw OHLCV (Open, High, Low, Close, Volume) data into meaningful, scale-invariant features for the agent to learn from.

    TensorTrade acts as the bridge connecting these three layers.

  7. Understand the TradingEnv architecture

    master

    A TradingEnv is a reinforcement learning environment that follows the OpenAI gym.Env specification. It is built using highly composable components that allow you to customize how an agent interacts with the market. When the reset() method of a TradingEnv is called, all child components are also reset to their default values to prepare for a new episode.

    The core components of a TradingEnv are:

    • ActionScheme: Interprets and applies the agent's actions to the environment.
    • RewardScheme: Computes the reward for each time step based on agent performance.
    • Observer: Generates the next observation for the agent.
    • Stopper: Determines whether or not the episode is over.
    • Informer: Generates monitoring information at each time step.
    • Renderer: Renders a view of the environment and its interactions.
  8. Use the tensortrade.oms.wallets package

    master

    The tensortrade.oms.wallets package provides the core wallet management functionality for the Order Management System (OMS). It is composed of several submodules that handle different aspects of asset tracking and management:

    • tensortrade.oms.wallets.ledger: Manages the accounting and transaction history.
    • tensortrade.oms.wallets.portfolio: Manages collections of assets and holdings.
    • tensortrade.oms.wallets.wallet: Provides the primary interface for managing individual wallet balances and assets.