Skip to content

navneetprabhakar/fno-conservative-algo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

F&O Conservative Trading Algorithm

A comprehensive algorithmic trading system for Indian F&O markets (NSE) with AI-powered multi-agent orchestration, database-driven prompt configuration, risk management, and real-time pipeline visualization.

🎯 Overview

Conservative trading system focused on:

  • Multi-Agent AI Pipeline β€” 5 coordinated agents (Research β†’ Signal β†’ Risk β†’ Execution β†’ Monitor) with real-time status tracking
  • Database-Driven Prompt Config β€” All agent system messages and prompt templates stored in PostgreSQL with versioning, live editing, and full audit history
  • Risk Management β€” Circuit breakers, position sizing, daily/weekly loss limits
  • Conservative Strategy β€” Capital preservation with controlled growth
  • Backtesting β€” Multi-strategy comparison with historical data
  • Paper Trading β€” Test strategies without real capital

πŸ—οΈ Architecture

Multi-Agent Pipeline

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Agent Coordinator                             β”‚
β”‚       (Orchestrates pipeline with real-time status tracking)      β”‚
β”‚                                                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Research  β”‚β†’ β”‚  Signal   β”‚β†’ β”‚   Risk   β”‚β†’ β”‚  Execution   β”‚  β”‚
β”‚  β”‚  Agent    β”‚  β”‚  Agent    β”‚  β”‚  Agent   β”‚  β”‚  (Order Svc) β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚       ↑                                             ↓            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚Categoricalβ”‚                              β”‚   Monitor    β”‚    β”‚
β”‚  β”‚  Agent    β”‚                              β”‚   Agent      β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                                   β”‚
β”‚  AgentStatusTracker: IDLE β†’ RUNNING β†’ COMPLETED/ERROR per agent  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Strategy Engine    β”‚      β”‚   Risk Management        β”‚
β”‚  - Trend Following   β”‚      β”‚  - Circuit Breaker       β”‚
β”‚  - Mean Reversion    β”‚      β”‚  - Position Sizing       β”‚
β”‚  - Breakout          β”‚      β”‚  - Daily/Weekly Limits   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Order Execution Service                      β”‚
β”‚           (Paper Trading / Live Trading)                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          MCP Market Data Service (Groww API)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Database-Driven Agent Configuration

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        agent_prompt_configs (DB)             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ agent_name β”‚ config_key β”‚ config_value β”‚ β”‚
β”‚  β”‚ RISK_AGENT β”‚ system_msg β”‚ "You are..." β”‚ β”‚
β”‚  β”‚ RISK_AGENT β”‚ prompt_tpl β”‚ "{signal}..."β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚  - Versioned (v1, v2, v3...)                β”‚
β”‚  - Active/inactive flag                      β”‚
β”‚  - ConcurrentHashMap in-memory cache         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓ on update
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      agent_prompt_config_audit (DB)          β”‚
β”‚  - old_value β†’ new_value                     β”‚
β”‚  - changed_by, change_reason                 β”‚
β”‚  - Full audit trail of all changes           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

  • Framework: Spring Boot 3.4.2
  • AI: Spring AI 1.0.0-M5 (OpenAI GPT-4o integration)
  • Database: PostgreSQL
  • Build: Gradle 8.12
  • Java: 21

πŸš€ Getting Started

Prerequisites

# Java 21
export JAVA_HOME=/opt/homebrew/Cellar/openjdk@21/21.0.10/libexec/openjdk.jdk/Contents/Home

# PostgreSQL
brew install postgresql@17
brew services start postgresql@17

Configuration

Edit src/main/resources/application.properties:

# OpenAI API Key (for AI-powered analysis)
spring.ai.openai.api-key=your-openai-api-key-here

# Trading Configuration
trading.mode=semi-auto
trading.capital=50000
trading.instruments=NIFTY,BANKNIFTY

# Risk Management
trading.risk.daily-loss-limit=1500
trading.risk.weekly-loss-limit=3500

Running

./gradlew bootRun
# Server starts on port 8080

On first startup, the AgentPromptSeeder automatically seeds all 5 agent configurations from classpath defaults into the database.

πŸ“Š Key Features

1. Multi-Agent AI Pipeline with Status Tracking

  • AgentStatusTracker β€” In-memory ConcurrentHashMap tracking each agent's state (IDLE/RUNNING/COMPLETED/ERROR), run duration, result summary, and error details
  • AgentCoordinator β€” Wraps each pipeline step with status tracking calls (markRunning() β†’ markCompleted()/markError())
  • Real-time visibility β€” Pipeline state exposed via REST API, consumed by dashboard for live visualization

2. Database-Driven Prompt Configuration

  • AgentPromptConfig entity β€” Stores system_message and prompt_template per agent with versioning
  • AgentPromptConfigAudit entity β€” Full change history with old/new values, change reason, and who made the change
  • AgentPromptConfigService β€” Cached config access (ConcurrentHashMap) with @Transactional updates that deactivate old versions and create new ones
  • AgentPromptSeeder β€” Seeds defaults from classpath .txt files on first startup
  • All 5 agents modified β€” Each agent uses getSystemMessage() and getPromptTemplate() methods that check DB first, fallback to hardcoded defaults

3. AI-Powered Market Research

  • Market sentiment analysis
  • Volatility assessment
  • Trading recommendations
  • Context-aware analysis (includes current holdings)

4. Categorical Stock Analysis

Comprehensive equity analysis across:

  • Market Cap (Large/Mid/Small)
  • Industries (Banking, IT, Pharma, etc.)
  • Tech Sector
  • Recent IPOs

Conservative strategy:

  • 60-70% Large Cap
  • 20-30% Mid Cap
  • 0-10% Small Cap

5. Risk Management

  • Circuit Breaker with daily/weekly limits
  • Position Sizing based on risk
  • Emergency Stop functionality
  • Real-time monitoring

6. Trading Pipeline

Multi-agent coordination:

  1. Research Agent β†’ Market analysis
  2. Signal Agent β†’ Entry/exit signals with confidence scoring
  3. Risk Agent β†’ Hard rule checks + AI-enhanced assessment
  4. Execution β†’ Order placement (paper or live)
  5. Monitor Agent β†’ Position monitoring and exit signals

7. Backtesting

  • Multiple strategies (Trend Following, Mean Reversion, Breakout)
  • Historical performance analysis
  • Multi-strategy comparison

8. Historical Data Management

  • Instrument sync from Groww API with DB persistence
  • Historical candle data storage with DB-first lookup
  • Rate-limited API calls with exponential backoff

πŸ”Œ API Endpoints

Dashboard

  • GET /api/dashboard/overview β€” Main metrics
  • GET /api/dashboard/positions β€” Current positions
  • GET /api/dashboard/trades β€” Trade history
  • GET /api/dashboard/metrics β€” Trading metrics

AI Agents

  • POST /api/dashboard/agents/research β€” Market research
  • POST /api/dashboard/agents/categorical-analysis β€” Stock analysis
  • POST /api/dashboard/agents/pipeline β€” Run full trading pipeline
  • GET /api/dashboard/agents/decisions β€” Recent agent decisions

Agent Pipeline Status

  • GET /api/dashboard/agents/status β€” Real-time pipeline state (all agent statuses)

Agent Prompt Configuration

  • GET /api/dashboard/agents/configs β€” All active configs
  • GET /api/dashboard/agents/configs/{agentName} β€” Configs for a specific agent
  • PUT /api/dashboard/agents/configs/{agentName}/{configKey} β€” Update config (body: {value, reason, changedBy})
  • GET /api/dashboard/agents/configs/audit β€” Recent 50 audit entries
  • GET /api/dashboard/agents/configs/audit/{agentName} β€” Audit trail for specific agent

Controls

  • POST /api/dashboard/emergency-stop β€” Emergency halt
  • POST /api/dashboard/resume-trading β€” Resume trading
  • POST /api/dashboard/mode/{mode} β€” Set mode (semi-auto/full-auto)

Backtesting

  • POST /api/dashboard/backtest/{symbol}?days=180&capital=50000&strategy=TREND_FOLLOWING
  • POST /api/dashboard/backtest/compare/{symbol}?days=180&capital=50000 β€” Multi-strategy comparison

Instruments

  • GET /api/dashboard/instruments β€” Tradable instruments
  • GET /api/dashboard/instruments/search?query=NIFTY β€” Search instruments
  • POST /api/dashboard/instruments/sync β€” Sync from Groww API

πŸ“ Project Structure

src/main/java/com/navneet/fnotrading/
β”œβ”€β”€ agent/
β”‚   β”œβ”€β”€ config/              # AgentPromptConfigService, AgentPromptSeeder
β”‚   β”œβ”€β”€ coordinator/         # AgentCoordinator (pipeline orchestration)
β”‚   β”œβ”€β”€ monitor/             # TradeMonitorAgent
β”‚   β”œβ”€β”€ research/            # MarketResearchAgent, CategoricalAnalysisAgent
β”‚   β”œβ”€β”€ risk/                # RiskAssessmentAgent
β”‚   β”œβ”€β”€ signal/              # SignalGenerationAgent
β”‚   └── status/              # AgentStatusTracker (pipeline state tracking)
β”œβ”€β”€ backtest/                # Backtesting engine
β”œβ”€β”€ config/                  # TradingConfig, RiskConfig, McpClientConfig
β”œβ”€β”€ dashboard/
β”‚   β”œβ”€β”€ controller/          # DashboardController (REST endpoints)
β”‚   └── service/             # MetricsService, ReportService, PerformanceTrackingService
β”œβ”€β”€ execution/               # OrderExecutionService, PaperTradingService, PositionManager
β”œβ”€β”€ market/                  # TechnicalIndicatorService, market models
β”œβ”€β”€ mcp/                     # Groww API integration (McpMarketDataService, McpOrderService)
β”œβ”€β”€ persistence/
β”‚   β”œβ”€β”€ entity/              # Trade, Signal, Instrument, AgentPromptConfig, AgentDecision, etc.
β”‚   └── repository/          # JPA repositories
β”œβ”€β”€ risk/                    # RiskManagementService, CircuitBreakerService, PositionSizingService
└── strategy/                # StrategyEngine (Trend, MeanReversion, Breakout)

🚦 Status

  • βœ… Core trading engine
  • βœ… Multi-agent system with 5 AI agents
  • βœ… Agent pipeline status tracking (real-time)
  • βœ… Database-driven prompt configuration with versioning & audit
  • βœ… AI-powered research & categorical analysis
  • βœ… Risk management & circuit breaker
  • βœ… Paper trading
  • βœ… Backtesting (single & multi-strategy)
  • βœ… Historical candle data persistence
  • βœ… Instrument sync & management
  • βœ… Dashboard REST API (30+ endpoints)
  • ⏳ Live trading (testing in paper mode)

πŸ”— Related Projects


Version: 1.1.0 Author: Navneet Prabhakar Last Updated: 2026-02-12

About

Conservative F&O algorithmic trading system with AI agents - Spring Boot 3 + Spring AI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages