Convert Figma logo to code with AI

tensortrade-org logotensortrade

An open source reinforcement learning framework for training, evaluating, and deploying robust trading agents.

7,118
1,321
7,118
49

Top Related Projects

37,246

A toolkit for developing and comparing reinforcement learning algorithms.

43,218

Ray is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.

A fork of OpenAI Baselines, implementations of reinforcement learning algorithms

12,177

A standard API for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)

20,638

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)

Quick Overview

TensorTrade is an open-source Python framework for building, training, evaluating, and deploying robust trading algorithms using reinforcement learning. It provides a modular architecture for composing trading environments, allowing users to define custom instruments, action schemes, reward functions, and trading logic.

Pros

  • Flexible and modular design, allowing for easy customization of trading environments
  • Integration with popular machine learning libraries like TensorFlow and PyTorch
  • Supports multiple asset classes and exchange types
  • Provides built-in performance metrics and visualization tools

Cons

  • Steep learning curve for users new to reinforcement learning or algorithmic trading
  • Limited documentation and examples for advanced use cases
  • May require significant computational resources for complex trading strategies
  • Still in active development, which may lead to breaking changes in future versions

Code Examples

  1. Creating a simple trading environment:
from tensortrade.env import TradingEnvironment
from tensortrade.feed.core import Stream, DataFeed
from tensortrade.data import CSVDataset
from tensortrade.oms.instruments import ExchangePair
from tensortrade.oms.exchanges import SimulatedExchange
from tensortrade.oms.services.execution.simulated import execute_order

dataset = CSVDataset("path/to/data.csv")
feed = DataFeed([
    Stream.source(dataset.data, ['open', 'high', 'low', 'close']).rename("USD-BTC")
])

exchange = SimulatedExchange("simulated", service=execute_order)(
    [ExchangePair("USD", "BTC")]
)

env = TradingEnvironment(
    feed=feed,
    portfolio=Portfolio("USD", [
        ExchangePair("USD", "BTC")
    ]),
    action_scheme="managed-risk",
    reward_scheme="risk-adjusted",
    window_size=20
)
  1. Training a trading agent using Stable Baselines3:
from stable_baselines3 import PPO

model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=10000)
  1. Evaluating the trained model:
from tensortrade.env.default.renderers import PlotlyTradingChart

obs = env.reset()
done = False
while not done:
    action, _states = model.predict(obs)
    obs, rewards, done, info = env.step(action)

env.render(renderer="plotly")

Getting Started

To get started with TensorTrade, follow these steps:

  1. Install TensorTrade:
pip install tensortrade
  1. Import necessary modules:
from tensortrade.env import TradingEnvironment
from tensortrade.feed.core import DataFeed, Stream
from tensortrade.oms.exchanges import SimulatedExchange
from tensortrade.oms.services.execution.simulated import execute_order
from tensortrade.oms.instruments import ExchangePair
  1. Create a simple trading environment and run a random agent:
import numpy as np

# Create a simple environment (assuming you have data)
env = TradingEnvironment(
    feed=your_data_feed,
    portfolio=your_portfolio,
    action_scheme="managed-risk",
    reward_scheme="simple",
    window_size=10
)

# Run a random agent
for _ in range(100):
    action = env.action_space.sample()
    state, reward, done, info = env.step(action)
    if done:
        env.reset()

For more detailed examples and documentation, refer to the official TensorTrade documentation.

Competitor Comparisons

37,246

A toolkit for developing and comparing reinforcement learning algorithms.

Pros of Gym

  • Broader scope, supporting a wide range of reinforcement learning environments
  • Larger community and more extensive documentation
  • Well-established standard in the RL research community

Cons of Gym

  • Not specifically designed for financial trading environments
  • Requires more setup and customization for trading-specific tasks
  • Less focus on features tailored to algorithmic trading

Code Comparison

Gym:

import gym
env = gym.make('CartPole-v1')
observation = env.reset()
for _ in range(1000):
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)

TensorTrade:

from tensortrade.env import TradingEnvironment
from tensortrade.feed import DataFeed
env = TradingEnvironment(feed=DataFeed(), window_size=20)
state = env.reset()
for _ in range(1000):
    action = env.action_space.sample()
    state, reward, done, info = env.step(action)

TensorTrade is specifically designed for financial trading environments, offering built-in support for common trading operations and data structures. Gym, on the other hand, provides a more general-purpose framework for reinforcement learning tasks across various domains. While Gym requires more customization for trading-specific applications, it benefits from a larger ecosystem and broader applicability in RL research.

43,218

Ray is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.

Pros of Ray

  • More versatile and general-purpose distributed computing framework
  • Larger community and ecosystem with extensive documentation
  • Better scalability for large-scale machine learning and AI applications

Cons of Ray

  • Steeper learning curve due to its broader scope
  • May be overkill for simpler trading applications
  • Less focused on financial trading compared to TensorTrade

Code Comparison

Ray example:

import ray

@ray.remote
def f(x):
    return x * x

futures = [f.remote(i) for i in range(4)]
print(ray.get(futures))

TensorTrade example:

from tensortrade.env import TradingEnvironment
from tensortrade.feed import DataFeed
from tensortrade.oms import OrderManagementSystem

env = TradingEnvironment(
    feed=DataFeed(),
    oms=OrderManagementSystem()
)

Ray is a distributed computing framework that can be used for various applications, including machine learning and AI. TensorTrade is specifically designed for building and training trading agents. While Ray offers more flexibility and scalability, TensorTrade provides a more focused approach for financial trading applications. The code examples demonstrate Ray's distributed computing capabilities and TensorTrade's trading-specific environment setup.

A fork of OpenAI Baselines, implementations of reinforcement learning algorithms

Pros of Stable-baselines

  • Broader scope, supporting various reinforcement learning algorithms and environments
  • More mature project with extensive documentation and examples
  • Larger community and more frequent updates

Cons of Stable-baselines

  • Not specifically tailored for trading environments
  • May require more setup and customization for trading-specific tasks
  • Steeper learning curve for users focused solely on trading applications

Code Comparison

TensorTrade example:

from tensortrade.env import TradingEnvironment
from tensortrade.features import TAIndicator
from tensortrade.rewards import RiskAdjustedReturns

env = TradingEnvironment(
    instruments=['BTC-USD'],
    features=[TAIndicator('close', 'rsi')],
    reward_scheme=RiskAdjustedReturns()
)

Stable-baselines example:

from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from custom_trading_env import TradingEnvironment

env = DummyVecEnv([lambda: TradingEnvironment()])
model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=10000)

The code comparison shows that TensorTrade provides a more streamlined setup for trading environments, while Stable-baselines requires custom environment creation but offers more flexibility in algorithm choice and training process.

12,177

A standard API for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)

Pros of Gymnasium

  • Broader scope, supporting a wide range of reinforcement learning environments
  • More active development and larger community support
  • Better documentation and examples for various use cases

Cons of Gymnasium

  • Not specifically tailored for financial trading environments
  • May require additional customization for trading-specific tasks
  • Steeper learning curve for users focused solely on trading applications

Code Comparison

TensorTrade:

from tensortrade.env import TradingEnvironment
from tensortrade.feed import DataFeed
from tensortrade.oms import OrderManagementSystem

env = TradingEnvironment(
    feed=DataFeed(),
    oms=OrderManagementSystem()
)

Gymnasium:

import gymnasium as gym

env = gym.make('CartPole-v1')
observation, info = env.reset(seed=42)
for _ in range(1000):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)

TensorTrade is more focused on trading environments, while Gymnasium provides a general-purpose framework for reinforcement learning tasks. TensorTrade offers built-in components for trading systems, whereas Gymnasium requires custom implementation for trading-specific environments.

20,638

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)

Pros of Lean

  • More mature and actively maintained project with a larger community
  • Supports multiple asset classes (stocks, forex, crypto, options)
  • Provides a comprehensive backtesting and live trading framework

Cons of Lean

  • Steeper learning curve due to its complexity
  • Requires C# knowledge for advanced customization
  • Less focused on reinforcement learning compared to TensorTrade

Code Comparison

Lean (C#):

public class MyAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        SetStartDate(2020, 1, 1);
        SetCash(100000);
        AddEquity("AAPL");
    }
}

TensorTrade (Python):

from tensortrade.env import TradingEnvironment

env = TradingEnvironment(
    instruments=['AAPL'],
    start_date='2020-01-01',
    base_instrument='USD'
)

Both repositories aim to provide algorithmic trading frameworks, but they differ in their approach and target audience. Lean offers a more comprehensive solution for professional traders and quants, supporting multiple asset classes and providing a robust backtesting engine. TensorTrade, on the other hand, focuses on reinforcement learning applications in trading, making it more accessible for researchers and AI enthusiasts. Lean's maturity and active community support give it an edge in terms of stability and features, while TensorTrade's simplicity and focus on machine learning make it attractive for those exploring AI-driven trading strategies.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

TensorTrade Logo

TensorTrade

Train RL agents to trade. Can they beat Buy-and-Hold?

Tests Documentation Status Apache License Discord Python 3.12+

TensorTrade is an open-source Python framework for building, training, and evaluating reinforcement learning agents for algorithmic trading. The framework provides composable components for environments, action schemes, reward functions, and data feeds that can be combined to create custom trading systems.

Quick Start

# Requires Python 3.12+
python3.12 -m venv tensortrade-env && source tensortrade-env/bin/activate
pip install -e .

# For training with Ray/RLlib (recommended)
pip install -r examples/requirements.txt

# Run training
python examples/training/train_simple.py

Documentation & Tutorials

📚 Tutorial Index — Start here for the complete learning curriculum.

Foundations

Domain Knowledge

Core Components

Training

Advanced Topics

Additional Resources


Research Findings

We conducted extensive experiments training PPO agents on BTC/USD. Key results:

ConfigurationTest P&Lvs Buy-and-Hold
Agent (0% commission)+$239+$594
Agent (0.1% commission)-$650-$295
Buy-and-Hold-$355—

The agent demonstrates directional prediction capability at zero commission. The primary challenge is trading frequency—commission costs currently exceed prediction profits. See EXPERIMENTS.md for methodology and detailed analysis.


Architecture

┌─────────────────────────────────────────────────────────────────┐
│                        TradingEnv                               │
│                                                                 │
│   Observer ──────> Agent ──────> ActionScheme ──────> Portfolio │
│   (features)      (policy)      (BSH/Orders)        (wallets)  │
│       ^                                                  │      │
│       └──────────── RewardScheme <───────────────────────┘      │
│                        (PBR)                                    │
│                                                                 │
│   DataFeed ──────> Exchange ──────> Broker ──────> Trades       │
└─────────────────────────────────────────────────────────────────┘
ComponentPurposeDefault
ActionSchemeConverts agent output to ordersBSH (Buy/Sell/Hold)
RewardSchemeComputes learning signalPBR (Position-Based Returns)
ObserverGenerates observationsWindowed features
PortfolioManages wallets and positionsUSD + BTC
ExchangeSimulates executionConfigurable commission

Training Scripts

ScriptDescription
examples/training/train_simple.pyBasic demo with wallet tracking
examples/training/train_ray_long.pyDistributed training with Ray RLlib
examples/training/train_optuna.pyHyperparameter optimization
examples/training/train_best.pyBest configuration from experiments

Installation

Requirements: Python 3.11 or 3.12

# Create environment
python3.12 -m venv tensortrade-env
source tensortrade-env/bin/activate  # Windows: tensortrade-env\Scripts\activate

# Install
pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

# Verify
pytest tests/tensortrade/unit -v

# Training dependencies (optional)
pip install -r examples/requirements.txt

See ENVIRONMENT_SETUP.md for platform-specific instructions and troubleshooting.

Docker

make run-notebook  # Jupyter
make run-docs      # Documentation
make run-tests     # Test suite

Project Structure

tensortrade/
├── tensortrade/           # Core library
│   ├── env/              # Trading environments
│   ├── feed/             # Data pipeline
│   ├── oms/              # Order management
│   └── data/             # Data fetching
├── examples/
│   ├── training/         # Training scripts
│   └── notebooks/        # Jupyter tutorials
├── docs/
│   ├── tutorials/        # Learning curriculum
│   └── EXPERIMENTS.md    # Research log
└── tests/

Troubleshooting

IssueSolution
"No stream satisfies selector"Update to v1.0.4-dev1+
Ray installation failsRun pip install --upgrade pip first
NumPy version conflictpip install "numpy>=1.26.4,<2.0"
TensorFlow CUDA issuespip install tensorflow[and-cuda]>=2.15.1

Contributing

See CONTRIBUTING.md for guidelines.

Priority areas:

  1. Trading frequency reduction (position sizing, holding periods)
  2. Commission-aware reward schemes
  3. Alternative action spaces

Community


License

Apache 2.0