How mukul2345/anytrading works
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: trading_env.py Trading env · __init__.py Env init · README.md README · forex_env.py Forex env
Components 16
Python package
Installable library code and environment implementation.
- Package init · Package entry point; likely exposes the library and environment registration.
gym_anytrading/__init__.py - Env init · Environment package entry point.
gym_anytrading/envs/__init__.py - Trading env · Abstract Gym trading environment defining episode flow, state, observation windows, and shared mechanics.
gym_anytrading/envs/trading_env.py - Forex env · Concrete foreign-exchange environment with currency-pair semantics and fee model.
gym_anytrading/envs/forex_env.py - Stocks env · Concrete stock-market environment with asymmetric bid/ask transaction costs.
gym_anytrading/envs/stocks_env.py - Setup · Python package installation metadata and dependency entry point.
setup.py
Market data
Bundled datasets and data-loading helpers used by the environments.
- Datasets init · Dataset package entry point for bundled market data.
gym_anytrading/datasets/__init__.py - Dataset utils · Utilities for accessing or preparing packaged datasets.
gym_anytrading/datasets/utils.py - EURUSD CSV · Bundled FOREX sample data.
gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv - GOOGL CSV · Bundled stock sample data.
gym_anytrading/datasets/data/STOCKS_GOOGL.csv
Usage examples
Notebooks and rendered outputs showing training, evaluation, and reporting.
- Training notebook · Example notebook for SB3 A2C/PPO training and evaluation.
examples/SB3_a2c_ppo.ipynb - Quantstats notebook · Example notebook for performance analysis and reporting.
examples/SB3_a2c_quantstats.ipynb - Report HTML · Rendered quantstats report output.
examples/SB3_a2c_quantstats.html
Docs
Primary documentation and generated documentation assets.
- README · Primary usage guide and architectural documentation.
README.md - Notebook doc · Notebook-form documentation source or rendering artifact.
README.ipynb - Docs assets · Rendered documentation outputs and supporting assets.
docs
Connections 22
- 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.