Architecture
How the system works under the hood.
System Overview
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MAHORAGA HARNESS (Durable Object) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββ βββββββββββββ βββββββββββββ βββββββββββββββββββ β
β β StockTwitsβ β Reddit β β Twitter β β Crypto Momentum β β
β β Sentiment β β (4 subs) β β Confirm β β (24/7) β β
β βββββββ¬ββββββ βββββββ¬ββββββ βββββββ¬ββββββ ββββββββββ¬βββββββββ β
β βββββββββββββββ΄ββββββββββββββ΄βββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ β
β β Signal Cache β β
β β (Persistent) β β
β ββββββββββ¬βββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ β
β β LLM Research β β
β β (OpenAI) β β
β ββββββββββ¬βββββββββ β
β β β
β βΌ β
β βββββββββββββββββββ β
β β Trading Logic β β
β β + Staleness β β
β β + Options β β
β ββββββββββ¬βββββββββ β
βββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββ
β Alpaca β
β Broker β
βββββββββββββββ
The Harness runs entirely on Cloudflare Workers with persistent state stored in Durable Object storage. It executes via an alarm every 30 seconds and survives restarts.
The MCP server provides tools for external integrations if needed.
Agent Loop
The agent runs continuously with two main phases:
Data Gathering (24/7)
- Fetch trending stocks from StockTwits API
- Calculate sentiment scores (bullish vs bearish messages)
- Filter by minimum sentiment and volume thresholds
- Send top signals to LLM for research
Trading (Market Hours Only)
- Check existing positions for stop-loss/take-profit
- Ask LLM to analyze each position (HOLD/SELL)
- Look for buy opportunities from researched signals
- Execute trades via MCP server
LLM Decision Making
Signal Research
For each trending stock, the LLM receives:
- Symbol and current price
- Sentiment score and volume
- Source breakdown (bullish/bearish counts)
It returns:
{
"verdict": "BUY" | "SKIP" | "WAIT",
"confidence": 0.0-1.0,
"reasoning": "explanation",
"red_flags": ["concerns"],
"catalysts": ["positive factors"]
}
Position Analysis
For each held position, the LLM receives:
- Symbol, shares, P&L percentage
- Current sentiment (if still trending)
- Take profit/stop loss targets
It returns:
{
"action": "HOLD" | "SELL",
"confidence": 0.0-1.0,
"reasoning": "explanation"
}
Order Flow
All orders go through a two-step process for safety:
1. Preview
Call orders-preview with order details. The policy engine validates:
- Kill switch not active
- Not in cooldown period
- Within position limits
- Within daily loss limits
- Sufficient buying power
Returns an approval token (valid 5 minutes).
2. Submit
Call orders-submit with the approval token. The order is executed through Alpaca.
MCP Tools
The MCP server at /mcp exposes tools for external integrations (e.g., Claude Desktop). Key tools:
| Category | Tools |
|---|---|
| Account | accounts-get, portfolio-get |
| Positions | positions-list, positions-close |
| Orders | orders-preview, orders-submit, orders-list, orders-cancel |
| Market Data | market-clock, market-quote, market-movers |
| Technicals | technicals-get, signals-get |
| Risk | risk-status, killswitch-enable, killswitch-disable |
For daily operation, use the /agent/* HTTP endpoints instead (see Configuration).
Project Structure
mahoraga/
βββ wrangler.jsonc # Cloudflare config
βββ .dev.vars # Local secrets (gitignored)
βββ src/
β βββ index.ts # Entry point & routing
β βββ durable-objects/
β β βββ mahoraga-harness.ts # Trading agent (customize this!)
β βββ mcp/
β β βββ agent.ts # MCP tool definitions
β βββ policy/ # Trade validation & risk
β βββ providers/
β β βββ alpaca/ # Alpaca broker client
β β βββ llm/ # LLM providers (OpenAI, AI SDK)
β β βββ social/ # StockTwits, Reddit scrapers
β βββ storage/ # D1 and KV storage
β βββ schemas/ # Config schemas
βββ dashboard/ # React dashboard
βββ docs/ # This documentation
βββ migrations/ # D1 database migrations