# mukul2345/anytrading architecture

> Architecture diagram of the GitHub repository mukul2345/anytrading, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mukul2345/anytrading
- Repository: https://github.com/mukul2345/anytrading

## Overview

1. **What this repo is**
   - `AnyTrading` is a small RL trading environment library built on the OpenAI Gym API. Its main product is a set of discrete-action market simulators for **FOREX** and **stocks**, not a trading strategy or model.

2. **Primary package boundary**
   - The functional code lives under `gym_anytrading/`.
   - Everything else is supporting material: `datasets/` for sample market data, `examples/` for notebooks, and `docs/` for rendered outputs and documentation assets.
   - The two `*.egg-info/` directories and `setup.py` indicate it is packaged for installation as a Python library.

3. **Core abstraction: `TradingEnv`**
   - `gym_anytrading/envs/trading_env.py` is the architectural center.
   - It defines the shared Gym environment lifecycle: `seed`, `reset`, `step`, `render`, `render_all`, and `close`.
   - It also defines the common environment state: `df`, `prices`, `signal_features`, `window_size`, `action_space`, `observation_space`, `history`, and the internal ticks that control episode progression.

4. **Data-processing contract**
   - `TradingEnv` is intentionally abstract around market specifics.
   - Subclasses must implement `_process_data`, `_calculate_reward`, `_update_profit`, and `max_possible_profit`.
   - This is the main extension boundary: the base class owns the episode mechanics, while subclasses own market-specific data extraction and economics.

5. **Concrete market environments**
   - `gym_anytrading/envs/forex_env.py` implements `ForexEnv`.
   - `gym_anytrading/envs/stocks_env.py` implements `StocksEnv`.
   - Both inherit from `TradingEnv`, but differ in fee model and market semantics.

6. **FOREX-specific behavior**
   - `ForexEnv` introduces `frame_bound`, `unit_side`, and `trade_fee`.
   - It models a currency pair and supports starting from either the left or right side of the pair, which affects how profit is interpreted.

7. **Stocks-specific behavior**
   - `StocksEnv` uses `frame_bound` plus asymmetric transaction costs:
     - `trade_fee_bid_percent`
     - `trade_fee_ask_percent`
   - This makes buy and sell operations costed differently from the FOREX environment.

8. **Simplified RL interface**
   - The repo deliberately reduces trading actions to `Sell=0` and `Buy=1`, and positions to `Short=0` and `Long=1`.
   - That simplification is a key architecture choice: it keeps the state/action space small so the environment is easier to train against and easier to reason about in diagrams.

9. **Runtime data flow**
   - Raw market data starts as a pandas `DataFrame` loaded from CSV.
   - `_process_data` converts that frame into:
     - `prices` for reward/profit accounting
     - `signal_features` for observations
   - At each `step`, the agent’s action updates position/profit/reward, advances the current tick, and appends to `history`.

10. **Observation and episode windowing**
   - Observations are sliding windows over `signal_features`.
   - `window_size` determines how many ticks are exposed to the agent at each step.
   - `frame_bound` slices the usable data range, so the environment only runs over a selected segment of the full dataset.

11. **Sample data subsystem**
   - `gym_anytrading/datasets/data/` contains bundled CSVs:
     - `FOREX_EURUSD_1H_ASK.csv`
     - `STOCKS_GOOGL.csv`
   - `gym_anytrading/datasets/utils.py` and the dataset package expose these as ready-to-use inputs for the environments.
   - This lets the repo ship with runnable examples without requiring external data sources.

12. **Examples and documentation**
   - `examples/` contains notebooks that demonstrate training and evaluation, including SB3-based A2C/PPO usage and quantstats reporting.
   - `README.md` is effectively the primary architectural documentation and usage guide.
   - `README.ipynb` and `docs/` appear to contain generated or notebook-based documentation artifacts.

13. **Packaging/runtime expectations**
   - The library is meant to be installed into a Python RL stack with Gym/Gymnasium, pandas, numpy, and plotting tooling.
   - The examples show `gym.make('forex-v0')` / `gym.make('stocks-v0')`, so the package likely registers environment IDs at import time through its module initialization.

14. **Diagram-worthy takeaway**
   - The clearest architecture diagram is a three-layer flow:
     - **Data layer**: CSV datasets and user-supplied DataFrames
     - **Environment layer**: `TradingEnv` plus `ForexEnv`/`StocksEnv`
     - **Consumer layer**: RL agents, notebooks, and visualization/reporting
   - The main boundary is between the reusable environment engine and the market-specific subclasses.

Main entry points: [`gym_anytrading/envs/trading_env.py`](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/trading_env.py) (Trading env), [`gym_anytrading/envs/__init__.py`](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/__init__.py) (Env init), [`README.md`](https://github.com/mukul2345/anytrading/blob/main/README.md) (README), [`gym_anytrading/envs/forex_env.py`](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/forex_env.py) (Forex env)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_package["Python package"]
  node_node_init["Package init<br/>[__init__.py]"]
  node_node_env_init["Env init<br/>[__init__.py]"]
  node_node_trading_env["Trading env<br/>base gym env<br/>[trading_env.py]"]
  node_node_forex_env["Forex env<br/>market env<br/>[forex_env.py]"]
  node_node_stocks_env["Stocks env<br/>market env<br/>[stocks_env.py]"]
  node_node_setup["Setup<br/>packaging<br/>[setup.py]"]
end

subgraph group_group_data["Market data"]
  node_node_dataset_init["Datasets init<br/>[__init__.py]"]
  node_node_dataset_utils["Dataset utils<br/>helpers<br/>[utils.py]"]
  node_node_forex_csv["EURUSD CSV<br/>csv dataset"]
  node_node_stocks_csv["GOOGL CSV<br/>csv dataset<br/>[STOCKS_GOOGL.csv]"]
end

subgraph group_group_usage["Usage examples"]
  node_node_ex_nb["Training notebook<br/>example notebook<br/>[SB3_a2c_ppo.ipynb]"]
  node_node_ex_qn["Quantstats notebook<br/>analysis notebook"]
  node_node_ex_html["Report HTML<br/>report output"]
end

subgraph group_group_docs["Docs"]
  node_node_readme["README<br/>docs<br/>[README.md]"]
  node_node_readme_ipynb["Notebook doc<br/>[README.ipynb]"]
  node_node_docs["Docs assets<br/>documentation"]
end

node_node_init -->|"exports"| node_node_env_init
node_node_init -->|"exports"| node_node_dataset_init
node_node_env_init -->|"exposes"| node_node_trading_env
node_node_env_init -->|"exposes"| node_node_forex_env
node_node_env_init -->|"exposes"| node_node_stocks_env
node_node_forex_env -->|"inherits"| node_node_trading_env
node_node_stocks_env -->|"inherits"| node_node_trading_env
node_node_trading_env -->|"consumes data"| node_node_dataset_utils
node_node_dataset_utils -->|"loads"| node_node_forex_csv
node_node_dataset_utils -->|"loads"| node_node_stocks_csv
node_node_readme -->|"documents"| node_node_trading_env
node_node_readme -->|"documents"| node_node_forex_env
node_node_readme -->|"documents"| node_node_stocks_env
node_node_readme_ipynb -.->|"source of"| node_node_readme
node_node_ex_nb -->|"uses"| node_node_forex_env
node_node_ex_nb -->|"uses"| node_node_stocks_env
node_node_ex_qn -->|"renders"| node_node_ex_html
node_node_ex_qn -->|"analyzes"| node_node_trading_env
node_node_setup -->|"packages"| node_node_init
node_node_setup -->|"packages"| node_node_env_init
node_node_setup -->|"packages"| node_node_dataset_init
node_node_docs -.->|"supports"| node_node_readme

click node_node_init "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/__init__.py"
click node_node_env_init "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/__init__.py"
click node_node_trading_env "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/trading_env.py"
click node_node_forex_env "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/forex_env.py"
click node_node_stocks_env "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/stocks_env.py"
click node_node_dataset_init "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/__init__.py"
click node_node_dataset_utils "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/utils.py"
click node_node_forex_csv "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv"
click node_node_stocks_csv "https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/data/STOCKS_GOOGL.csv"
click node_node_readme "https://github.com/mukul2345/anytrading/blob/main/README.md"
click node_node_readme_ipynb "https://github.com/mukul2345/anytrading/blob/main/README.ipynb"
click node_node_docs "https://github.com/mukul2345/anytrading/tree/main/docs"
click node_node_ex_nb "https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_ppo.ipynb"
click node_node_ex_qn "https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_quantstats.ipynb"
click node_node_ex_html "https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_quantstats.html"
click node_node_setup "https://github.com/mukul2345/anytrading/blob/main/setup.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_init,node_node_env_init,node_node_trading_env,node_node_forex_env,node_node_stocks_env,node_node_setup toneBlue
class node_node_dataset_init,node_node_dataset_utils,node_node_forex_csv,node_node_stocks_csv toneAmber
class node_node_ex_nb,node_node_ex_qn,node_node_ex_html toneMint
class node_node_readme,node_node_readme_ipynb,node_node_docs toneRose
```

## Components

### Python package

Installable library code and environment implementation.

- [Package init](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/__init__.py): module · Package entry point; likely exposes the library and environment registration. · `gym_anytrading/__init__.py`
- [Env init](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/__init__.py): module · Environment package entry point. · `gym_anytrading/envs/__init__.py`
- [Trading env](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/trading_env.py): base gym env · Abstract Gym trading environment defining episode flow, state, observation windows, and shared mechanics. · `gym_anytrading/envs/trading_env.py`
- [Forex env](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/forex_env.py): market env · Concrete foreign-exchange environment with currency-pair semantics and fee model. · `gym_anytrading/envs/forex_env.py`
- [Stocks env](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/envs/stocks_env.py): market env · Concrete stock-market environment with asymmetric bid/ask transaction costs. · `gym_anytrading/envs/stocks_env.py`
- [Setup](https://github.com/mukul2345/anytrading/blob/main/setup.py): packaging · Python package installation metadata and dependency entry point. · `setup.py`

### Market data

Bundled datasets and data-loading helpers used by the environments.

- [Datasets init](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/__init__.py): module · Dataset package entry point for bundled market data. · `gym_anytrading/datasets/__init__.py`
- [Dataset utils](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/utils.py): helpers · Utilities for accessing or preparing packaged datasets. · `gym_anytrading/datasets/utils.py`
- [EURUSD CSV](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv): csv dataset · Bundled FOREX sample data. · `gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv`
- [GOOGL CSV](https://github.com/mukul2345/anytrading/blob/main/gym_anytrading/datasets/data/STOCKS_GOOGL.csv): csv dataset · Bundled stock sample data. · `gym_anytrading/datasets/data/STOCKS_GOOGL.csv`

### Usage examples

Notebooks and rendered outputs showing training, evaluation, and reporting.

- [Training notebook](https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_ppo.ipynb): example notebook · Example notebook for SB3 A2C/PPO training and evaluation. · `examples/SB3_a2c_ppo.ipynb`
- [Quantstats notebook](https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_quantstats.ipynb): analysis notebook · Example notebook for performance analysis and reporting. · `examples/SB3_a2c_quantstats.ipynb`
- [Report HTML](https://github.com/mukul2345/anytrading/blob/main/examples/SB3_a2c_quantstats.html): report output · Rendered quantstats report output. · `examples/SB3_a2c_quantstats.html`

### Docs

Primary documentation and generated documentation assets.

- [README](https://github.com/mukul2345/anytrading/blob/main/README.md): docs · Primary usage guide and architectural documentation. · `README.md`
- [Notebook doc](https://github.com/mukul2345/anytrading/blob/main/README.ipynb): notebook · Notebook-form documentation source or rendering artifact. · `README.ipynb`
- [Docs assets](https://github.com/mukul2345/anytrading/tree/main/docs): documentation · Rendered documentation outputs and supporting assets. · `docs`

## Connections

- Package init → Env init: exports · Package surface points to the environment subsystem.
- Package init → Datasets init: exports · Package surface points to bundled datasets.
- Env init → Trading env: exposes · Environment package exposes the base environment.
- Env init → Forex env: exposes · Environment package exposes the FOREX environment.
- Env init → Stocks env: exposes · Environment package exposes the stocks environment.
- Forex env → Trading env: inherits · FOREX environment extends the shared trading engine.
- Stocks env → Trading env: inherits · Stocks environment extends the shared trading engine.
- Trading env → Dataset utils: consumes data · Base environment operates on prepared market data and features.
- Dataset utils → EURUSD CSV: loads · Dataset utilities provide bundled FOREX data.
- Dataset utils → GOOGL CSV: loads · Dataset utilities provide bundled stock data.
- README → Trading env: documents · README explains the core environment contract and lifecycle.
- README → Forex env: documents · README covers FOREX-specific behavior and usage.
- README → Stocks env: documents · README covers stock-specific behavior and usage.
- Notebook doc → README: source of · Notebook documentation source mirrors the main README.
- Training notebook → Forex env: uses · Example training notebook runs the trading environments.
- Training notebook → Stocks env: uses · Example training notebook runs the trading environments.
- Quantstats notebook → Report HTML: renders · Notebook produces the HTML quantstats report.
- Quantstats notebook → Trading env: analyzes · Reporting notebook evaluates environment-driven trading results.
- Setup → Package init: packages · Setup installs the library package and its modules.
- Setup → Env init: packages · Setup includes the environment subsystem.
- Setup → Datasets init: packages · Setup includes the dataset subsystem.
- Docs assets → README: supports · Documentation assets complement the main README.
