Skip to content

saksham869/TradeGaurdAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TradeGuard AI

A multi-agent intelligence platform for retail traders.

"Every retail trader is alone in the room. TradeGuard AI puts an AI council next to them."

Next.js TypeScript Azure OpenAI Azure Content Safety Claude License: MIT


What It Does

Retail traders lose money not because markets are hard — but because they act on emotion, incomplete information, and hype while institutions operate with teams of analysts, quant models, and real-time data feeds.

TradeGuard AI closes that gap with a swarm of specialized AI agents, each owning one intelligence domain, running in parallel, and converging into a single coordinated verdict.


The Agent Swarm

User Action / Ticker Query
         │
    ┌────▼─────────┐
    │  AI Router   │   lib/ai/router.ts + Redis cache
    └────┬─────────┘
         │
   ┌─────┴──────────────────────────────────┐
   │                                        │
   ▼                                        ▼
Azure OpenAI GPT-4o                  Claude 3.5 Sonnet
(deep_research → synthesis)          (news_analysis · journal_reflection
   │                                  morning_brief)
   │  fallback ────────────────────────▶   │
   │                                        │
   ▼                                        ▼
Grok Beta                            Perplexity Sonar-large
(hype_detection)                     (news_research · citations)
   │                                        │
   └──────────────────┬─────────────────────┘
                      │
              ┌───────▼────────┐
              │  Redis Cache   │   shared memory across agents
              └───────┬────────┘
                      │
              ┌───────▼──────────────┐
              │  Synthesis Agent     │   Azure GPT-4o aggregates
              │  (Azure GPT-4o)      │   all agent outputs
              └───────┬──────────────┘
                      │
              ┌───────▼──────────────┐
              │ Azure Content Safety │   journal guardian (non-blocking)
              └───────┬──────────────┘
                      │
                 Final Output

Agent responsibilities

Agent Model Task type Role
Research Agent Azure OpenAI GPT-4o deep_research Final synthesis — coordinates all agent outputs
News Agent Claude 3.5 Sonnet news_analysis Impact analysis, retail trap detection
Journal Agent Claude 3.5 Sonnet journal_reflection Behavioral coaching, emotion tagging
Brief Agent Claude 3.5 Sonnet morning_brief Daily pre-market intelligence
Social Agent Grok Beta hype_detection X/social sentiment, FOMO detection
News Research Agent Perplexity Sonar news_research Verified facts with source citations
Safety Guardian Azure Content Safety Screens all journal entries before DB write

Key swarm properties:

  • Agents run via Promise.all — parallel, not sequential
  • Redis cache layer allows agents to reuse each other's results
  • Azure GPT-4o → Claude fallback keeps the swarm alive if Azure is unavailable
  • Azure GPT-4o is the synthesis coordinator — waits for all agents, produces the final verdict

Azure Integration

Two Azure services are load-bearing in the swarm.

Azure OpenAI GPT-4o — Synthesis Agent

lib/ai/router.ts routes deep_research tasks to Azure GPT-4o first, with automatic Claude fallback:

case 'deep_research':
  try {
    responseText = await callAzureOpenAI(prompt, options)
  } catch (azureErr) {
    console.warn('Azure OpenAI unavailable, falling back to Claude:', azureErr)
    responseText = await callClaude(prompt, options)
  }
  break

lib/services/research.service.ts routes the final synthesis step through deep_research, meaning every Research Terminal query closes with Azure GPT-4o synthesizing all three parallel agent outputs into one verdict.

Azure Content Safety — Journal Guardian

Every journal entry is screened before it touches the database (app/api/journal/route.ts):

const safety = await checkContentSafety(rawText)   // non-blocking

await db.journalEntry.create({
  data: { ...entry, riskFlag: safety.flagged }
})
  • Categories checked: Hate · SelfHarm · Sexual · Violence
  • Severity scale: 0 (safe) → 6 (high)
  • riskFlag: true written to DB → warning badge shown in UI
  • Journal always saves regardless — Content Safety never blocks the write

Modules

Intelligence Feed

Real-time market event analysis, delivered before you can react.

  • Polygon API ingests live news on a Vercel cron schedule
  • Claude analyzes each item: what happened, what it means for retail traders, retailTrap flag
  • Output: sentiment score, impact level (LOW / MEDIUM / HIGH / CRITICAL)
  • Live-pushed to all connected clients via Pusher WebSocket

Research Terminal

Institution-grade parallel research on any ticker in under 10 seconds.

T=0s  Yahoo Finance   → live price, volume, EMA, RSI
T=0s  Perplexity      → verified news (last 24h) with source URLs
T=1s  Claude ×3       → technical read · news impact · retail trap (parallel)
T=3s  Azure GPT-4o    → final synthesis of all three agent outputs
T=4s  Result rendered

Supports: US equities, NSE/BSE (India), crypto, forex, ETFs.

Trading Journal

The only trading journal with a behavioral AI coach built in.

  • Free-text entry → Azure Content Safety screens it first (non-blocking)
  • Claude identifies emotion tag: FOMO · REVENGE · DISCIPLINED · FEARFUL · PANIC · GREED
  • Structured AI response: pattern name, likely mistake, one action for tomorrow
  • PsychProfile builds over time: dominant biases, streak tracking, weekly insight
  • riskFlag stored in DB — visible warning badge if Content Safety flags the entry

Tech Stack

Layer Technology
Frontend Next.js 14, React 18, TypeScript, Tailwind CSS, Framer Motion
UI Components shadcn/ui, Lucide Icons, Lightweight Charts
AI — Synthesis Azure OpenAI GPT-4o
AI — Safety Azure Content Safety
AI — Analysis Anthropic Claude 3.5 Sonnet
AI — Social Grok Beta (xAI)
AI — News Perplexity Sonar-large
Database PostgreSQL via Supabase, Prisma ORM
Cache Upstash Redis
Real-time Pusher WebSocket
Auth Clerk
Data Providers Polygon, Yahoo Finance, Alpaca, FMP, StockTwits
Deployment Vercel (crons + edge)

Project Structure

├── app/
│   ├── (dashboard)/
│   │   ├── feed/           # Intelligence Feed
│   │   ├── research/       # Research Terminal
│   │   ├── journal/        # Trading Journal
│   │   ├── watchlist/
│   │   └── settings/
│   └── api/
│       ├── feed/
│       ├── research/[symbol]/
│       ├── journal/
│       └── cron/           # Vercel background jobs
├── lib/
│   ├── ai/
│   │   ├── router.ts               # Agent dispatcher
│   │   ├── azure-openai.ts         # Azure GPT-4o client
│   │   ├── azure-content-safety.ts # Content Safety client
│   │   ├── claude.ts
│   │   ├── grok.ts
│   │   ├── perplexity.ts
│   │   └── prompts.ts
│   ├── services/
│   │   ├── research.service.ts     # Parallel ticker analysis
│   │   ├── feed.service.ts
│   │   ├── journal.service.ts
│   │   └── brief.service.ts
│   └── data/               # Market data providers
└── prisma/
    └── schema.prisma

Setup

1. Install dependencies

pnpm install

2. Configure environment variables

cp .env.local.example .env.local

Fill in all values. The example file has inline comments for every variable explaining where to find it. Required to get started: DATABASE_URL, DIRECT_URL, NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY, ANTHROPIC_API_KEY.

Azure variables (AZURE_OPENAI_*, AZURE_CONTENT_SAFETY_*) are optional locally — both clients degrade gracefully when not configured (Content Safety returns flagged: false, Azure OpenAI falls back to Claude).

3. Generate Prisma client and run migrations

pnpm prisma generate
pnpm prisma migrate dev

4. Run the development server

pnpm dev

Background Jobs (Vercel Crons)

Route Schedule What it does
/api/cron/morning-brief Daily 9:00 AM ET (Mon–Fri) AI-generated pre-market brief
/api/cron/news-ingestion Every 15 min, 9 AM–5 PM ET Ingests and analyzes latest market news
/api/cron/trending-scan Every 30 min Scans StockTwits for unusual ticker activity
/api/cron/cleanup Daily 1:00 AM Expires feed events older than 7 days
/api/cron/regime-refresh Daily 2:30 AM (Mon–Fri) Refreshes ^NSEI and ^GSPC regime snapshots
/api/cron/trader-models Daily 1:00 AM Recomputes TraderModel for active users
/api/cron/mind-directives Daily 3:00 AM (Mon–Fri) Generates Mind Directives for active users

All cron routes require a Bearer token matching CRON_SECRET. Generate one with openssl rand -hex 32 and set it in both .env.local and Vercel dashboard.


ULTRA — V7 Mind Engine (current)

Phase 2 adds a proprietary behavioral intelligence layer that fuses market regime with each trader's own verified statistics to produce a personalized daily directive.

Architecture

                   ┌─────────────────────────────────────────┐
                   │           Mind Engine (V7)              │
                   │                                         │
 Market Data ──►  │  Gaussian HMM (regime-service/Python)   │
                   │  ↓ BULL_TREND | BEAR_TREND | CHOP | CRISIS │
                   │                                         │
 Trade History ►  │  TraderModel (pure TS, zero AI)         │
                   │  ↓ winRate, expectancyR, statsBySetup   │
                   │  ↓ 4 behavioral flags (n≥5 threshold)  │
                   │                                         │
 Journals ──────► │  Mind Directive (AI synthesis)          │
                   │  ↓ headline + reads + EV + oneRule     │
                   └─────────────────────────────────────────┘
                            │
                   ┌────────▼────────┐
                   │  Live Copilot   │  ← fused with TraderModel flags
                   │  + Directive EV │    and statsAfterLoss
                   └─────────────────┘

New in V7

  • Regime Classifier — Gaussian HMM on 5y daily returns, 4-state labels, 6h Redis cache, heuristic fallback. Python microservice in regime-service/.
  • TraderModel — pure TypeScript stat engine. Computes winRate, expectancyR, profitFactor, statsBySetup/Regime/Hour, statsAfterLoss/Win, convictionCalib, and 4 behavioral flags. Calibrates at ≥20 trades AND ≥10 journals.
  • Mind Directive — daily AI synthesis of regime + TraderModel → JSON directive with headline, market read, personal read, EV assessment, greenlight/avoid setups, size guidance, and ONE imperative rule. PRO-gated; FREE users see blurred preview + upgrade CTA.
  • Copilot × Mind Fusion — behavioral agent receives top 2 flags + statsAfterLoss + today's EV for hyper-personalized real-time warnings.
  • Trade Log (/trades) — R-multiple tracking, setup tags, conviction stars, close modal with live P&L preview.
  • Mind Page (/mind) — three-column layout: directive + history, full TraderModelCard, regime panels.

Behavioral Flags

Flag Trigger
OVERSIZE_AFTER_WINS Avg position size after 2+ consecutive wins > 1.3× baseline (n≥5)
REVENGE_FAST_REENTRY ≥3 same-setup re-entries within 15 min of a loss
LATE_DAY_LEAK Last-hour expectancyR < overall expectancyR − 0.3R (n≥8)
CONVICTION_INVERTED Win rate at 5★ < win rate at ≤2★ (both n≥5)

Honesty Guarantees (V7)

  • Every AI output labeled with actual model used
  • Every statistic shows sample size: 58% (n=31)
  • TraderModel shows CALIBRATING when <20 trades or <10 journals
  • Approximations labeled (approx) — regime from heuristic fallback
  • App never places trades; disclaimer on every analysis surface

Roadmap

V1 — Current

  • Intelligence Feed with retail trap detection
  • Research Terminal — 4-agent parallel analysis
  • Trading Journal with behavioral AI coach
  • Azure OpenAI GPT-4o — synthesis agent
  • Azure Content Safety — journal guardian with riskFlag
  • Real-time Pusher feed
  • PsychProfile — emotion tracking over time

V7 — Mind Engine (shipped)

  • Gaussian HMM regime classifier (Python microservice)
  • TraderModel — 4 behavioral flags, zero AI
  • Mind Directive — daily AI directive, PRO-gated
  • Copilot × Mind Fusion — flags injected into behavioral agent
  • Trade Log with R-multiple tracking
  • /mind page — three-column layout
  • Extended demo seed — 30 closed trades, all flags firing

V8 — Execution Intelligence (next)

  • Broker webhook integration (Zerodha Kite, IBKR)
  • Pre-trade checklist gate — blocks entry until all rules verified
  • Emergency intervention — full-screen modal when psychState = TILT
  • Position sizing calculator respecting TraderModel size guidance
  • Streak-based regime alerts (3+ consecutive regime changes)

License

MIT / Proprietary

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors