File size: 5,887 Bytes
d8ee9a4
 
 
 
 
 
 
 
fa1c25e
d8ee9a4
 
b07e8e2
fa1c25e
89faf8b
 
717bee1
 
 
 
 
46c6e13
717bee1
 
46c6e13
717bee1
89faf8b
717bee1
 
 
 
 
 
 
 
89faf8b
717bee1
 
 
 
46c6e13
717bee1
89faf8b
 
717bee1
89faf8b
717bee1
 
89faf8b
 
717bee1
 
89faf8b
717bee1
 
b07e8e2
717bee1
 
fa1c25e
 
717bee1
a85dc8d
717bee1
 
 
fa1c25e
717bee1
89faf8b
 
717bee1
 
46c6e13
717bee1
 
89faf8b
717bee1
 
46c6e13
 
717bee1
46c6e13
717bee1
 
 
 
46c6e13
 
 
 
 
 
 
717bee1
 
 
 
 
46c6e13
717bee1
46c6e13
 
717bee1
 
46c6e13
717bee1
 
 
 
 
 
 
 
 
 
b07e8e2
 
89faf8b
 
 
b07e8e2
46c6e13
 
 
 
b07e8e2
 
89faf8b
 
46c6e13
 
 
 
 
89faf8b
 
46c6e13
 
b07e8e2
 
46c6e13
 
717bee1
46c6e13
 
 
717bee1
46c6e13
 
 
89faf8b
46c6e13
b07e8e2
717bee1
46c6e13
 
 
e7e76f5
 
89faf8b
46c6e13
 
89faf8b
 
 
717bee1
89faf8b
 
717bee1
89faf8b
 
 
 
 
 
46c6e13
 
 
 
89faf8b
 
 
46c6e13
 
 
 
 
 
 
 
 
 
 
 
89faf8b
 
 
 
 
 
 
 
 
 
 
 
46c6e13
 
 
89faf8b
b07e8e2
 
 
 
 
46c6e13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
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

```bash
# Clone the repository
git clone https://github.com/Samikshacode934/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