Skip to main content

A simple trading system for backtesting Model Based RL strategies

Project description

build Downloads PyPi Version Codacy Badge

DeepTrade

Deeptrade is a backtesting system and library designed to test and evaluate machine learning based strategies.

Getting Started

Prerequisites

DeepTrade relies on python 3.8 or higher and Pytorch 1.9.0 or higher.

We recommend using a conda environment to manage dependencies. You can create a new environment with the following command:

conda create --name deeptrade-env python=3.10
conda activate deeptrade-env

Installation

Standard Installation

pip install deeptrade

Development Installation

If you want to modify the library, clone the repository and setup a development environment:

git clone https://github.com/AOS55/deeptrade.git
pip install -e .

Running Tests

To test the library, either run pytest at root or specify test directories from root with:

python -m pytest tests/core
python -m pytest tests/instruments

Usage

The core idea of DeepTrade is to backtest machine learning trading strategies based on either synthetic or real data. Backtesting is split into 2 datasets, training data, available at the start of the theoretical trading period and backtest data used to evaluate the strategy which is where you started the strategy from. The following provides an overview of the basic components of the library, examples of various backtests are provided in the notebooks directory.

The train-backtest split is shown below:

Train/Backtest split

The classical Markov Decision Process (MDP) is used to model the trading environment. The environment is defined by the following components:

  • Environment: the trading environment represents the world the agent interacts with, $p(s'|s, a)$. This is responsible for providing the agent with observations, rewards, and other information about the state of the environment. The environment is defined by the gymnasium interface. These include:

    • SingleInstrument-v0: A single instrument trading environment designed for a simple single asset portfolio.
    • MultiInstrument-v0: A multi-instrument trading environment designed to hold a multiple asset portfolio.

    Each of the trading environments have the following key components:

    • Market data: either generated synthetically or from a real dataset. Data is queried at time $t$ which is updated by a size period each time around the env-agent loop.
    • Account: represents the portfolio consisting of:
      • Margin: the amount of cash available.
      • Positions: the quantity of the asset held.

    The observation of the environment is a numpy array consisting of:

    • returns, $r_{t-\tau:t}$ from the asset price, usually log returns over window $\tau$.
    • position, position of the portfolio in the asset.
    • margin, the amount of cash available.
  • Agent: The agent, $\pi(a|s)$, is the decision maker that interacts with the environment. The agent is responsible for selecting actions based on observations from the environment. Model Based RL (MBRL) agents are provided along with classical systematic trading strategies. These include:

    • MBRL agents
    • Systematic agents
      • HoldAgent: A simple buy and hold strategy.
      • EWMACAgent: Exponential Weighted Moving Average Crossover, momentum based trend following.
      • BreakoutAgent: Breakout strategy, based on the high and low of the previous n periods.

The overall environment-agent loop is shown below:

Agent/Env loop

Environment

The following is a basic example of how to instantiate an environment with deeptrade.env:

import gymnasium as gym
import deeptrade.env

env = gym.make("SingleInstrument-v0")

obs, info = env.reset()
truncated, terminated = False, False
while not truncated or not terminated:
    action = env.action_space.sample()
    obs, reward, truncated, info = env.step(action)
    print(f"Reward: {reward}")

Contributing

Please read the CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.

Citing

If you use this project in your research, please consider citing it with:

@misc{deeptrade,
  author = {DeepTrade},
  title = {DeepTrade: A Model Based Reinforcement Learning System for Trading},
  year = {2024},
  publisher = {GitHub},
  journal = {GitHub Repository},
  howpublished = {\url{https://github.com./AOS55/deeptrade}},
}

Disclaimer

DeepTrade is for educational and research purposes and should is used for live trading entirely at your own risk.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deeptrade_mbrl-0.1.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

deeptrade_mbrl-0.1.0-py3-none-any.whl (81.1 kB view details)

Uploaded Python 3

File details

Details for the file deeptrade_mbrl-0.1.0.tar.gz.

File metadata

  • Download URL: deeptrade_mbrl-0.1.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.2

File hashes

Hashes for deeptrade_mbrl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3d216866449a05069fcb82ed8578fca85ae0c26bd40a969106d8781544c2d46
MD5 020070ed4fd528153df6d8eda0ed9465
BLAKE2b-256 6e195bf40f4b8f2acc029aa6612bbff14bf000281adab98eb6ea3306384e78a1

See more details on using hashes here.

File details

Details for the file deeptrade_mbrl-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for deeptrade_mbrl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0721e72f002fe76e05163f1b65bc6d6f81536cfe8308ea27b05b0ab3c796972
MD5 6e80ae02e813b5032431d8fb813af011
BLAKE2b-256 f31a586f17d5138287c931aa1c6588cd96443da21a7d5e8a87b5ee914f3fbb79

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page