quant-gym / README.md
Astocoder's picture
update changes
e7e76f5
metadata
title: Quant-Gym
emoji: ๐Ÿ“ˆ
colorFrom: blue
colorTo: green
sdk: docker
pinned: false

Quant-Gym: Financial Analysis Environment for AI Agents

An OpenEnv-compliant environment that tests AI agents on financial data analysis, market sentiment, and trading strategy evaluation.

๐ŸŽฏ Overview

Quant-Gym is a benchmark environment where AI agents can practice:

  • Fetching real-time market data
  • Analyzing financial news sentiment
  • Executing buy/sell trades
  • Evaluating trading strategies with risk metrics

This is a research benchmark for evaluating AI reasoning in financial contexts, not a trading tool.

๐Ÿ“Š Environment Tasks

Task Description Difficulty
Task 1 Fetch current market price for AAPL Easy
Task 2 Analyze news headlines and recommend Buy/Sell/Hold with explanation Medium
Task 3 Backtest a trading strategy (momentum/mean reversion) with Sharpe ratio & drawdown Hard

๐Ÿ—๏ธ API Endpoints

Endpoint Method Description
/ GET Welcome message
/health GET Health check
/metadata GET Environment metadata
/schema GET Action/observation schemas
/reset POST Reset environment to initial state
/step POST Execute an action
/state GET Get current environment state
/tasks GET List all available tasks
/docs GET Interactive API documentation

๐Ÿ”ง Installation

Prerequisites

  • Python 3.10+
  • Docker (for containerized deployment)

Local Setup

# Clone the repository
git clone https://github.com/Astocoder/quant-gym-openenv.git
cd quant-gym-openenv

# Install dependencies
pip install -r requirements.txt

# Set up Hugging Face token for LLM features (create .env file)
echo 'HF_TOKEN=your_hf_token_here' > .env

# Start the server
python -m uvicorn server.app:app --host 0.0.0.0 --port 8000 --reload



๐ŸŽฎ Action Schema
The agent can take the following actions:

json
{
  "type": "BUY | SELL | GET_PRICE | BACKTEST | GET_NEWS",
  "amount": 10,
  "explanation": "RSI indicates oversold condition",
  "strategy": "momentum"
}
Action Examples
Action	Description
{"type": "GET_PRICE"}	Get current stock price
{"type": "BUY", "amount": 10}	Buy 10 shares
{"type": "SELL", "amount": 5}	Sell 5 shares
{"type": "GET_NEWS", "explanation": "your analysis"}	Get news with analysis
{"type": "BACKTEST", "strategy": "momentum"}	Backtest momentum strategy


๐Ÿ‘๏ธ Observation Schema
The environment returns:

json
{
  "timestamp": "step_5",
  "price": 155.00,
  "balance": 8500.00,
  "holdings": 10,
  "portfolio_value": 10050.00,
  "last_news": {
    "headline": "Apple announces new AI chip",
    "sentiment": "positive"
  },
  "backtest_results": {
    "sharpe_ratio": 1.35,
    "max_drawdown": 0.12,
    "total_return": 0.18
  }
}



๐Ÿƒ Running the Baseline Agent
bash
# Set your Hugging Face token
export HF_TOKEN="your_hf_token_here"

# Run inference
python inference.py
Expected Output
text
[INFO] Starting Quant-Gym Inference
[START] task=quant-gym env=quant-gym model=gpt-3.5-turbo
[STEP] step=1 action=BUY 5 reward=0.15 done=false error=null
[STEP] step=2 action=GET_PRICE reward=0.05 done=false error=null
[STEP] step=3 action=SELL 5 reward=0.20 done=false error=null
...
[END] success=true steps=10 score=0.650 rewards=...


๐Ÿณ Docker Deployment
Build and run with Docker:

bash
# Build the image
docker build -t quant-gym .

# Run the container
docker run -p 7860:7860 quant-gym
Then access the API at http://localhost:7860

๐ŸŒ Hugging Face Space
Live demo: https://huggingface.co/spaces/Astocoder/quant-gym


๐Ÿ“ Project Structure
text
quant-gym-openenv/
โ”œโ”€โ”€ Dockerfile              # Container configuration
โ”œโ”€โ”€ inference.py            # Baseline agent script
โ”œโ”€โ”€ models.py               # Pydantic schemas
โ”œโ”€โ”€ openenv.yaml  
โ”œโ”€โ”€ pre_validation.sh          # OpenEnv configuration
โ”œโ”€โ”€ pyproject.toml          # Python project config
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ”œโ”€โ”€ README.md               # This file
โ”œโ”€โ”€ task1_grader.py         # Price fetch grader
โ”œโ”€โ”€ task2_grader.py         # News analysis grader
โ”œโ”€โ”€ task3_grader.py         # Backtest grader
โ”œโ”€โ”€ server/
โ”‚   โ”œโ”€โ”€ app.py              # FastAPI server
โ”‚   โ”œโ”€โ”€ environment.py      # Trading logic
โ”‚   โ””โ”€โ”€ data/
โ”‚       โ”œโ”€โ”€ prices.csv      # Market data
โ”‚       โ””โ”€โ”€ news.json       # News headlines
โ””โ”€โ”€ graders/                # Backup grader folder
    โ”œโ”€โ”€ task1_grader.py
    โ”œโ”€โ”€ task2_grader.py
    โ””โ”€โ”€ task3_grader.py


๐Ÿ” Environment Variables
Variable	Description	Default
HF_TOKEN	Hugging Face API token	None (optional)
API_BASE_URL	LLM API endpoint	None (judge provides)
API_KEY	LLM API key	None (judge provides)
BASE_URL	Quant-Gym API URL	http://localhost:8000


๐Ÿ“ˆ Evaluation Criteria
OpenEnv Compliance: Full implementation of step()/reset()/state() APIs

Task Completion: All 3 tasks return scores between 0.0-1.0

Reward Function: Partial progress signals for meaningful learning

Reproducibility: Static data ensures consistent results


๐Ÿ’ก Unique Innovation
Unlike traditional trading environments that only measure profit, Quant-Gym rewards explanation quality:

Agents must explain their reasoning for each trade

Graders evaluate financial terminology, logical reasoning, and detail

Promotes transparent, auditable AI decision-making



โš ๏ธ Disclaimer
This is a research benchmark environment for evaluating AI agent reasoning. It does not provide financial advice or real trading recommendations. All data is for simulation purposes only.


๐Ÿ“„ License
MIT License - See LICENSE file for details.

Built with: Python, FastAPI, OpenEnv, Hugging Face, Docker