Convert Figma logo to code with AI

tensortrade-org logotensortrade

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

4,499
1,017
4,499
51

Top Related Projects

34,643

A toolkit for developing and comparing reinforcement learning algorithms.

33,421

Ray is a unified framework for scaling AI and Python applications. 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

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

9,713

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

34,643

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.

33,421

Ray is a unified framework for scaling AI and Python applications. 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.

An API standard 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.

9,713

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: Trade Efficiently with Reinforcement Learning

Build Status Documentation Status Apache License Discord Python 3.11



TensorTrade is still in Beta, meaning it should be used very cautiously if used in production, as it may contain bugs.

TensorTrade is an open source Python framework for building, training, evaluating, and deploying robust trading algorithms using reinforcement learning. The framework focuses on being highly composable and extensible, to allow the system to scale from simple trading strategies on a single CPU, to complex investment strategies run on a distribution of HPC machines.

Under the hood, the framework uses many of the APIs from existing machine learning libraries to maintain high quality data pipelines and learning models. One of the main goals of TensorTrade is to enable fast experimentation with algorithmic trading strategies, by leveraging the existing tools and pipelines provided by numpy, pandas, gym, keras, and tensorflow.

Every piece of the framework is split up into re-usable components, allowing you to take advantage of the general use components built by the community, while keeping your proprietary features private. The aim is to simplify the process of testing and deploying robust trading agents using deep reinforcement learning, to allow you and I to focus on creating profitable strategies.

The goal of this framework is to enable fast experimentation, while maintaining production-quality data pipelines.

Read the documentation.

Guiding principles

Inspired by Keras' guiding principles.

  • User friendliness. TensorTrade is an API designed for human beings, not machines. It puts user experience front and center. TensorTrade follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear and actionable feedback upon user error.

  • Modularity. A trading environment is a conglomeration of fully configurable modules that can be plugged together with as few restrictions as possible. In particular, exchanges, feature pipelines, action schemes, reward schemes, trading agents, and performance reports are all standalone modules that you can combine to create new trading environments.

  • Easy extensibility. New modules are simple to add (as new classes and functions), and existing modules provide ample examples. To be able to easily create new modules allows for total expressiveness, making TensorTrade suitable for advanced research and production use.

Getting Started

You can get started testing on Google Colab or your local machine, by viewing our many examples

Installation

TensorTrade requires Python >= 3.11.9 for all functionality to work as expected. You can install TensorTrade both as a pre-packaged solution by running the default setup command.

pip install tensortrade

You can then alternatively install TensorTrade directly from the master code repository, pulling directly from the latest commits. This will give you the latest features\fixes, but it is highly untested code, so proceed at your own risk.

pip install git+https://github.com/tensortrade-org/tensortrade.git

Alternatively you can clone\download the repository in your local environment an manually install the requirements, either the "base" ones, or the ones that also include requirements to run the examples in the documentation.

pip install -r requirements.txt
pip install -r examples/requirements.txt

Docker

To run the commands below, ensure Docker is installed. Visit https://docs.docker.com/install/ for more information.

Run Jupyter Notebooks

To run a jupyter notebook in your browser, execute the following command and visit the http://127.0.0.1:8888/?token=... link printed to the command line.

make run-notebook

Build Documentation

To build the HTML documentation, execute the following command.

make run-docs

Run Test Suite

To run the test suite, execute the following command.

make run-tests

Support

You can ask questions and join the development discussion:

You can also post bug reports and feature requests in GitHub issues. Make sure to read our guidelines first.

Contributors

Contributions are encouraged and welcomed. This project is meant to grow as the community around it grows. Let me know on Discord in the #suggestions channel if there is anything that you would like to see in the future, or if there is anything you feel is missing.

Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub

https://github.com/notadamking/tensortrade/graphs/contributors