---
title: StratoTest
emoji: ๐
colorFrom: purple
colorTo: pink
sdk: docker
app_port: 7860
pinned: true
---
# StratoTest by Team Prahari
### Agentic AI Backtesting Tool โ Describe a strategy in plain English. Get a full backtest in seconds.
[](https://darshanpurohit-stratotest.hf.space/)
[](https://huggingface.co/spaces/Darshanpurohit/StratoTest/)
[](https://fastapi.tiangolo.com/)
[](https://streamlit.io/)
---
---
# TEAM PRAHARI
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Built by three builders. โ
โ Powered by one obsession. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
| ๐งโ๐ป | Builder | GitHub |
|:---:|:---|:---:|
|  | **Chandan Singh** | [](https://github.com/chandan22468) |
|  | **Darshan Purohit** | [](https://github.com/darshanpurohit20) |
|  | **Harsh Redasani** | [](https://github.com/redasaniharsh) |



---
## ๐ Deployed Links
| Interface | URL |
|---|---|
| **Direct UI** | [darshanpurohit-stratotest.hf.space](https://darshanpurohit-stratotest.hf.space/) |
| **UI + Codebase (Hugging Face Space)** | [huggingface.co/spaces/Darshanpurohit/StratoTest](https://huggingface.co/spaces/Darshanpurohit/StratoTest/) |
---
## ๐ง What is StratoTest?
StratoTest is a **multi-agent AI backtesting system**. A user types a trading strategy in plain English. A pipeline of autonomous AI agents converts that description into executable Python code, validates it, runs a realistic historical simulation with friction modelling, generates a performance tearsheet, and then autonomously optimizes the strategy โ all without any further user input.
A **Talkback Validation Agent** acts as a conversational gatekeeper, ensuring queries are complete before expensive computation begins. A **Pinecone RAG Layer** provides a strategy memory store, surfacing historically relevant configurations to auto-fill missing details and improve results over time.
---
## โจ Key Features (USPs)
1. **Self-correcting code generation** โ The agent writes Python strategy code, runs it in a sandbox, and fixes its own errors without user intervention. Not a chatbot โ an agent with a goal.
2. **Autonomous optimization loop** โ The Optimization Agent tries parameter variants, compares results, keeps improvements, and discards regressions. Goal-driven: targets Sharpe > 1.0 and Max Drawdown < 20%.
3. **Lookahead bias detection** โ The Validation Agent checks for future data leakage, a real quantitative finance concern that most tools ignore entirely.
4. **Live agent reasoning panel** โ Every tool call, every decision, every fix is streamed to the UI in real time. Watch the agent think.
5. **Session memory** โ "Test the same strategy but add a volume filter" works because agents remember prior runs.
6. **Friction-realistic results** โ Slippage, commissions, and spread are baked in. Results reflect what you'd actually make, not theoretical maximums.
7. **Explain Agent** โ Post-backtest, ask "Why did it lose in 2022?" and get a narrative explanation with structural fix suggestions.
8. **Talkback Validation Layer** โ Before any compute, the Talkback Agent confirms all required fields (asset, timeframe, date range, strategy intent) โ using RAG to auto-suggest smart defaults from past successful runs.
9. **Pinecone RAG Memory** โ Every successful backtest is embedded and stored. Future queries retrieve semantically similar past configs to guide decisions automatically.
---
## ๐๏ธ System Architecture
### The Multi-Agent Pipeline
```
User Natural Language Input
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TALKBACK AGENT (NEW) โ โ Validates query completeness
โ โ Uses Pinecone RAG for smart defaults
โ Checks: โ Asks user only if RAG can't fill gaps
โ โข asset_name โ
โ โข timeframe โ
โ โข start_date / end_date โ
โ โข strategy_hint โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ enriched, complete query
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ STRATEGY AGENT โ โ LangChain + Claude + Tools
โ โ Writes Python strategy code
โ Tools: โ Self-corrects syntax errors
โ โข validate_syntax โ Loops until sandbox passes
โ โข run_sandbox_test โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ validated strategy code (Python string)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ VALIDATION AGENT โ โ Separate LLM call
โ โ Checks lookahead bias
โ Checks: โ Minimum trade count
โ โข lookahead bias โ Sharpe sanity check
โ โข trade frequency โ Sends PASS or FAIL+reason
โ โข Sharpe sanity โ back to Strategy Agent
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ PASS
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BACKTEST ENGINE โ โ Pure Python, no LLM
โ โ exec() the strategy code
โ Applies: โ Apply friction costs
โ โข slippage โ Calculate all metrics
โ โข commissions โ
โ โข spread โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ metrics dict
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ OPTIMIZATION AGENT โ โ LangChain + Claude + Tools
โ โ Goal: Sharpe > 1.0, DD < 20%
โ Tools: โ ONE tweak per iteration
โ โข tweak_parameter โ Max 4 iterations
โ โข run_full_backtes โ Returns best variant
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ best strategy + all iteration metrics
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TEARSHEET + UI โ โ Plotly charts + Streamlit
โ โ Equity curve
โ Shows: โ Drawdown chart
โ โข Original vs Best โ Monthly returns heatmap
โ โข Iteration table โ Agent reasoning log
โ โข Explain button โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ on-demand
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ EXPLAIN AGENT โ โ Single LLM call, no tools
โ โ Analyzes bad periods
โ Answers: โ Explains WHY it lost money
โ โข "Why 2022 loss?" โ Suggests structural fixes
โ โข "What kills it?" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
### RAG + Talkback Layer (Intelligence Layers)
The existing pipeline is extended with two intelligence layers that prevent silent hallucination and leverage historical strategy memory:
```
BEFORE (Old Flow):
User Query โโโโโโโโโโโโโโโโโโโโโโโโโโโ Deep Agent โ Backtest
AFTER (New Flow):
User Query โ [Talkback Agent] โ [RAG Layer] โ [Validation Gate]
โ
โผ
โโโโโโโโโโโโโดโโโโโโโโโโโโ
โผ โผ
[Complete Query] [Incomplete Query]
โ โ
โ [Ask User + LLM Defaults]
โ โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโ
โผ
Deep Agent Controller
โ
โผ
Backtest Engine
โ
โผ
[Index Result โ Pinecone RAG]
```
| Layer | Name | Role | Technology |
|---|---|---|---|
| Layer 1 | **Pinecone RAG Layer** | Retrieves historically relevant strategy configs from a vector database | Pinecone + Gemini Embeddings |
| Layer 2 | **Talkback Validation Agent** | Validates all required fields; uses RAG to suggest smart defaults; asks user only when necessary | Gemini 2.0 Flash + Pinecone |
#### What Gets Stored in Pinecone
Every successful backtest (win rate > 50%) is embedded and stored. Each vector carries rich metadata:
```json
{
"strategy_id": "uuid-v4",
"asset_name": "RELIANCE.NS",
"asset_label": "Reliance Industries",
"timeframe": "1d",
"start_date": "2022-01-01",
"end_date": "2024-01-01",
"duration_label": "2 years",
"strategy_type": "EMA Cross",
"indicators_used": ["EMA_20", "EMA_50", "ADX"],
"win_rate": 62.5,
"roi": 18.3,
"market_regime": "trending",
"trade_count": 45,
"summary_text": "EMA 20/50 crossover on Reliance Industries daily chart over 2 years trending regime"
}
```
#### Talkback Decision Logic
The Talkback Agent enforces that all 5 required fields are resolved before passing the query to the Deep Agent:
| Field | Example Values | Resolution Path |
|---|---|---|
| `asset_name` | `"Nifty 50"`, `"RELIANCE"`, `"BTC/USD"` | User text โ NER โ RAG suggestion โ User confirm |
| `timeframe` | `"1d"`, `"1h"`, `"15m"`, `"1w"` | User text โ RAG suggestion โ LLM default |
| `start_date` | `"2022-01-01"` | User text โ duration math โ LLM default |
| `end_date` | `"2024-12-31"` or `"today"` | Derived from start + duration |
| `strategy_hint` | `"RSI overbought"`, `"EMA cross"`, `"breakout"` | User intent โ RAG strategy_type โ LLM inference |
Auto-fill threshold: if RAG confidence score > 0.85, the field is filled automatically and the user is notified. Below 0.85, the field goes to the missing list and the Talkback UI presents options.
---
## ๐ Quick Start
### 1. Clone and set up
```bash
git clone https://github.com/darshanpurohit20/Prahari_Stratotest
cd Prahari_Stratotest
cp .env.example .env
```
### 2. Add your API keys
Edit `.env`:
```env
ANTHROPIC_API_KEY=your_anthropic_key_here
PINECONE_API_KEY=your_pinecone_key_here
GEMINI_API_KEY=your_gemini_key_here
```
Get your Anthropic key at: https://console.anthropic.com/
### 3. Install dependencies
```bash
pip install -r requirements.txt
```
### 4. (Optional) Seed Pinecone with sample strategies
```bash
python scripts/seed_pinecone.py
```
### 5. Start the FastAPI backend
```bash
python -m uvicorn main:app --reload --port 8000
```
### 6. Start the Streamlit frontend (new terminal)
```bash
streamlit run frontend/app.py
```
### 7. Open browser
- **Frontend:** http://localhost:8501
- **API Docs:** http://localhost:8000/docs
---
## ๐ Project Structure
```
prahari/
โ
โโโ main.py โ FastAPI app factory + uvicorn runner
โโโ requirements.txt โ All pinned dependencies
โโโ .env.example โ Template for secrets (never commit .env)
โโโ .gitignore
โโโ config.py โ Central config (env vars, constants)
โ
โโโ agent/ โ All LLM agent logic
โ โโโ tools.py โ @tool decorated LangChain tools
โ โโโ strategy_agent.py โ Agent 1: code generation + self-correction
โ โโโ validation_agent.py โ Agent 2: bias + sanity checks
โ โโโ optimization_agent.py โ Agent 3: autonomous parameter tuning
โ โโโ explain_agent.py โ Agent 4: narrative loss explanation
โ โโโ memory.py โ Session context store
โ โโโ pipeline.py โ Master orchestrator
โ โโโ parser.py โ Claude LLM parser
โ โโโ prompts.py โ All system prompts (single source of truth)
โ
โโโ rag/ โ RAG + Talkback intelligence layers (NEW)
โ โโโ embedder.py โ Gemini text-embedding-004 wrapper
โ โโโ indexer.py โ Pinecone upsert + index_completed_strategy()
โ โโโ retriever.py โ Pinecone query + top-K fetch
โ โโโ talkback_agent.py โ Talkback Validation Agent (Gemini 2.0 Flash)
โ โโโ default_generator.py โ LLM Default Generator + static fallbacks
โ โโโ validator.py โ Entity extraction + confidence scoring
โ
โโโ engine/ โ Pure Python backtesting (no LLM)
โ โโโ data.py โ yfinance OHLCV fetcher + indicator calc
โ โโโ backtester.py โ exec() engine + trade simulator
โ โโโ friction.py โ Slippage, commission, spread models
โ โโโ tearsheet.py โ Metrics computation + chart data
โ โโโ strategies/
โ โโโ base.py โ BaseStrategy ABC โ every generated class inherits this
โ โโโ ma_crossover.py โ Strategy 1: MA / EMA Crossover
โ โโโ rsi_reversal.py โ Strategy 2: RSI Reversal
โ โโโ fibonacci_pullback.py โ Strategy 3: Fibonacci Pullback
โ โโโ strategies.py โ Strategies 4โ10
โ
โโโ api/ โ FastAPI REST layer
โ โโโ models/
โ โ โโโ request.py โ Pydantic input models
โ โ โโโ response.py โ Pydantic output models
โ โโโ routes/
โ โโโ backtest.py โ POST /backtest
โ โโโ talkback.py โ POST /talkback/validate, POST /talkback/confirm (NEW)
โ โโโ strategy.py โ GET /strategies, POST /explain
โ โโโ health.py โ GET /health
โ
โโโ scripts/
โ โโโ seed_pinecone.py โ One-time Pinecone seed script for testing
โ
โโโ frontend/
โโโ app.py โ Streamlit UI (single file)
```
---
## ๐ฏ Supported Strategies
### Classic
| # | Strategy |
|---|---|
| 1 | MA / EMA Crossover |
| 2 | RSI Reversal |
| 3 | Fibonacci Pullback |
| 4 | Support / Resistance Bounce |
| 5 | Breakout + Retest |
| 6 | Higher High Higher Low |
### SMC (Smart Money Concepts)
| # | Strategy |
|---|---|
| 7 | Order Block Entry |
| 8 | Fair Value Gap (FVG) |
| 9 | Change of Character (CHoCH) |
| 10 | Break of Structure + Pullback |
---
## ๐ฌ Example Inputs
```
"Buy when 50 EMA crosses above 200 EMA, SL below last swing low, 1:2 RR"
"Buy at bullish order block, SL below OB, 1:3 RR"
"Buy when RSI drops below 30, SL below swing low, 1:2 RR"
"Buy at 0.618 fibonacci level in uptrend, 1:2 RR"
"Buy after break of structure pullback, SL below BOS, 1:3 RR"
"Buy when MACD crosses above signal line and RSI is below 60"
"Buy at Bollinger Band lower band, sell at upper band"
```
---
## ๐ Supported Markets & Tickers
| Market | Example Tickers |
|---|---|
| NSE India | `RELIANCE.NS`, `TCS.NS`, `INFY.NS` |
| BSE India | `RELIANCE.BO` |
| Nifty Index | `^NSEI`, `^NSEBANK` |
| US Stocks | `AAPL`, `TSLA`, `GOOGL` |
| Crypto | `BTC-USD`, `ETH-USD` |
| Forex | `EURUSD=X`, `GBPUSD=X` |
---
## ๐ API Reference
### Core Endpoints
| Method | Route | Description |
|---|---|---|
| `POST` | `/backtest` | Run a full agentic backtest |
| `GET` | `/strategies` | List all supported strategy types |
| `POST` | `/explain` | Ask the Explain Agent about backtest results |
| `GET` | `/health` | Service health check |
### Talkback Endpoints (RAG Layer)
| Method | Route | Description |
|---|---|---|
| `POST` | `/talkback/validate` | Submit a raw query for entity extraction + RAG validation |
| `POST` | `/talkback/confirm` | Submit confirmed/modified fields to get an enriched query ready for the Deep Agent |
#### Example: Validate a query
```bash
curl -X POST http://localhost:8000/talkback/validate \
-H "Content-Type: application/json" \
-d '{"user_query": "RSI strategy on Nifty"}'
```
Response when fields are missing:
```json
{
"status": "incomplete",
"resolved_fields": {
"asset_name": "^NSEI",
"strategy_hint": "RSI"
},
"missing_fields": [
{
"field": "timeframe",
"question": "What timeframe should the backtest use?",
"options": [
{"label": "Daily (1d)", "value": "1d"},
{"label": "Hourly (1h)", "value": "1h"},
{"label": "Weekly (1wk)", "value": "1wk"},
{"label": "Custom โ I'll type it", "value": null}
],
"rag_suggestion": {
"label": "Daily (based on similar Nifty RSI strategies)",
"confidence": 0.91
}
}
]
}
```
---
## ๐ Data Schemas
### BacktestResult
```python
{
"total_trades": int,
"sharpe_ratio": float, # annualised
"max_drawdown_pct": float, # negative, e.g. -18.3
"win_rate_pct": float, # 0โ100
"total_return_pct": float,
"profit_factor": float,
"avg_trade_return_pct": float,
"best_trade_pct": float,
"worst_trade_pct": float,
"equity_curve": list[float], # one per bar
"monthly_returns": dict[str, float], # {"2023-01": 2.3, ...}
"warning": str | None,
"error": str | None,
"traceback": str | None,
}
```
### PipelineResult
```python
{
"success": bool,
"strategy_name": str,
"run_id": str,
"strategy_code": str,
"baseline_metrics": BacktestResult,
"optimized_metrics": BacktestResult,
"all_iterations": list[{
"label": str,
"metrics": BacktestResult,
"change_description": str,
}],
"improvement_pct": float,
"goal_met": bool,
"validation_issues": list[str],
"reasoning_log": list[str],
"stage_failed": str | None,
"error": str | None,
}
```
---
## โ ๏ธ Error Handling
Every agent function returns a result dict with a `"success"` or `"error"` key โ nothing raises unhandled exceptions. Errors are surfaced to the UI gracefully with friendly messages.
| Stage | Fallback |
|---|---|
| Data fetch failure | Return early: `"Could not fetch data for {ticker}"` |
| Strategy Agent fails | Return agent's last error + reasoning log |
| Validation fails | Log warning, continue (sandbox already passed) |
| Backtest fails | Return engine error + strategy code for debug |
| Optimization fails | Return baseline results + log that optimization failed |
| Pinecone unavailable | Skip RAG entirely, proceed with entity extraction only |
| Gemini embedding fails | Use cached zero-vector, skip RAG for this request |
| Pinecone upsert fails | Log warning, return backtest result normally โ non-blocking |
| RAG returns 0 matches | Skip RAG suggestions, rely purely on LLM defaults |
---
## โ Extending the System
The system is designed so new features can be added without modifying existing files.
### Adding a New Strategy
1. Create `engine/strategies/my_strategy.py`
2. Inherit `BaseStrategy`
3. Implement `generate_signals()` and `get_indicators()`
4. Add to `STRATEGY_MAP` in `engine/backtester.py`
5. Add to `GET /strategies` in `api/routes/strategy.py`
### Adding a New Agent
1. Create `agent/new_agent.py`
2. Follow the pattern: `run_new_agent(inputs..., progress_callback) -> dict`
3. Add it as a step in `agent/pipeline.py`
4. Add its system prompt to `agent/prompts.py`
### Adding a New Chart
1. Add `build_X_chart(result: dict) -> go.Figure` to `engine/tearsheet.py`
2. Import and call it in `frontend/app.py`
### Adding Persistence (Redis/DB)
1. Modify `agent/memory.py` โ implement `save_session` and `get_or_create_session` to use Redis instead of the in-memory `_sessions` dict
2. No other files need to change
### Adding a New Market/Ticker Type
1. Nothing to change โ yfinance handles NSE (`.NS`), crypto (`-USD`), and forex (`=X`) automatically
2. Optionally add examples to the UI pill buttons in `frontend/app.py`
---
## ๐งช Testing
### Unit Tests
```bash
# Run all tests
pytest tests/
# Key test cases
# tests/test_talkback.py
```
```python
def test_complete_query_returns_ready():
result = process_query("Backtest Nifty 50 EMA crossover daily from 2022 to 2024")
assert result["status"] == "ready"
assert result["enriched_query"]["asset_name"] == "^NSEI"
def test_missing_timeframe_triggers_talkback():
result = process_query("RSI strategy on Reliance from 2022")
assert result["status"] in ["incomplete", "ready"]
if result["status"] == "incomplete":
assert "timeframe" in result["talkback_options"]
def test_completely_vague_query_returns_all_missing():
result = process_query("run a backtest")
assert result["status"] == "incomplete"
assert len(result["talkback_options"]) >= 3
```
### Integration Test (End-to-End)
```bash
# Start the backend
uvicorn api.main:app --reload
# Test talkback route
curl -X POST http://localhost:8000/talkback/validate \
-H "Content-Type: application/json" \
-d '{"user_query": "RSI strategy on Nifty"}'
# Test engine in isolation (no API key needed)
python -c "
from engine.data import fetch_ohlc
from engine.backtester import run_backtest_from_code
df = fetch_ohlc('AAPL', '90d')
print(df.columns.tolist())
print(f'Data shape: {df.shape}')
"
```
### Seed Pinecone for RAG Testing
```bash
python scripts/seed_pinecone.py
```
---
## ๐ง Environment Variables
```env
# Required
ANTHROPIC_API_KEY=sk-ant-...
PINECONE_API_KEY=...
GEMINI_API_KEY=...
# Optional (with defaults)
DEFAULT_TICKER=AAPL
DEFAULT_PERIOD=2y
MAX_OPTIMIZATION_ITERATIONS=4
SANDBOX_TEST_DAYS=90
MIN_TRADES_FOR_VALID_STRATEGY=3
COMMISSION_PCT=0.001
SLIPPAGE_PCT=0.0005
RAG_AUTO_FILL_THRESHOLD=0.85
RAG_TOP_K=5
```
---
*Team Prahari | Hackathon 2026*