An autonomous, LLM-powered trading agent that adapts to social sentiment and makes trading decisions using AI.
MAHORAGA scrapes StockTwits for trending stocks, uses OpenAI to analyze signals and research opportunities, then executes trades through Alpaca. It's designed as a starting point for building your own agentic trading strategies.
- LLM-Powered Analysis - OpenAI evaluates signals and decides what to buy/sell
- 24/7 Sentiment Monitoring - Scrapes StockTwits trending stocks
- Position Research - AI continuously evaluates held positions
- Automatic Risk Management - Stop-loss, take-profit, position limits, kill switch
- Real-Time Dashboard - Monitor positions, signals, research, and costs
- Paper Trading Mode - Test safely before going live
- MCP Server Architecture - Extensible tool-based design
- Cost Tracking - Monitor your OpenAI spend in real-time
- Node.js 18+
- Alpaca account (free, paper trading supported)
- OpenAI API key (required for agentic features)
git clone https://github.com/ygwyg/MAHORAGA.git
cd mahoraga
npm install
cd dashboard && npm install && cd ..Create a .dev.vars file in the project root:
ALPACA_API_KEY=your_alpaca_key
ALPACA_API_SECRET=your_alpaca_secret
ALPACA_PAPER=true
OPENAI_API_KEY=your_openai_key
KILL_SWITCH_SECRET=any_random_string_hereImportant: Always start with
ALPACA_PAPER=trueuntil you understand how the system works.
npm run db:migratenpm run devThe server runs at http://localhost:8787
In a new terminal:
node agent-v1.mjsIn a new terminal:
cd dashboard
npm run devOpen http://localhost:5173 in your browser.
- Create a free account at alpaca.markets
- Go to Paper Trading > API Keys
- Click Generate New Keys
- Copy both the key and secret
Start with paper trading. Switch to live only after thorough testing.
The agent uses OpenAI for signal analysis and position management:
- Create an account at platform.openai.com
- Add billing and credits ($10 is plenty to start)
- Go to API Keys > Create new secret key
- Add to
.dev.vars:OPENAI_API_KEY=sk-your_key
Estimated costs: ~$0.50-2/day depending on trading activity (using gpt-4o-mini)
Edit agent-config.json (created on first run) or use the dashboard:
| Setting | Default | Description |
|---|---|---|
max_positions |
3 | Maximum stocks to hold at once |
max_position_value |
2000 | Maximum $ per position |
take_profit_pct |
8 | Auto-sell at this % profit |
stop_loss_pct |
4 | Auto-sell at this % loss |
min_sentiment_score |
0.3 | Minimum bullish sentiment to consider |
min_analyst_confidence |
0.6 | Minimum LLM confidence to trade |
min_volume |
10 | Minimum message volume to consider |
position_size_pct_of_cash |
20 | Max % of cash per position |
llm_model |
gpt-4o-mini | OpenAI model for analysis |
┌─────────────────────────────────────────────────────────────────┐
│ AGENT (agent-v1.mjs) │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ ┌─────────────────────────────────┐ │
│ │ StockTwits │ │ LLM Analysis (OpenAI) │ │
│ │ Sentiment │────▶│ • Signal research │ │
│ │ │ │ • Position management │ │
│ └─────────────────┘ │ • Buy/sell decisions │ │
│ └──────────────┬──────────────────┘ │
└─────────────────────────────────────────┼───────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ MCP SERVER (Cloudflare Workers) │
├─────────────────────────────────────────────────────────────────┤
│ Policy Engine → Approval Tokens → Order Execution │
└──────────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────┐
│ Alpaca │
│ Broker │
└─────────────┘
Data Gathering (runs 24/7)
- Fetches trending stocks from StockTwits
- Calculates sentiment score (bullish vs bearish messages)
- LLM researches top signals: analyzes if sentiment is real, looks for red flags
Trading Loop (market hours only)
- Checks existing positions against stop-loss/take-profit rules
- LLM analyzes each position: should we hold or sell?
- LLM evaluates buy opportunities from researched signals
- Executes trades through MCP server
The agent uses OpenAI for two key decisions:
Signal Research - For each trending stock:
Is the sentiment justified? Is it too late? Any red flags?
→ Returns: BUY / SKIP / WAIT with confidence score
Position Analysis - For each held position:
Is sentiment still supportive? Signs of exhaustion?
→ Returns: HOLD / SELL with reasoning
Only BUY signals with confidence >= min_analyst_confidence (default 0.6) are executed.
All orders go through a two-step process:
- Preview (
orders-preview) - Validates against policy, returns approval token - Submit (
orders-submit) - Executes with valid token
This prevents accidental trades and enforces risk limits.
| Feature | Description |
|---|---|
| Paper Trading | Default mode - no real money at risk |
| Kill Switch | Emergency halt for all trading |
| Position Limits | Max positions and $ per position |
| Daily Loss Limit | Stops trading after 2% daily loss |
| Cooldown Period | 30-minute pause after losses |
| Approval Tokens | Orders expire after 5 minutes |
| LLM Confidence Gate | Trades require minimum confidence |
| No Margin | Cash-only trading |
| No Shorting | Long positions only |
| Service | Cost | Notes |
|---|---|---|
| StockTwits | Free | No API key required |
| Alpaca (paper) | Free | Practice trading |
| Alpaca (live) | Free | Commission-free stocks |
| OpenAI | ~$0.50-2/day | Using gpt-4o-mini |
The dashboard shows real-time LLM cost tracking.
mahoraga/
├── agent-v1.mjs # Trading agent - COPY AND MODIFY THIS
├── .dev.vars # API keys (DO NOT COMMIT)
├── agent-config.json # Runtime config (DO NOT COMMIT)
├── agent-logs.json # Activity logs (DO NOT COMMIT)
├── wrangler.toml # Cloudflare Workers config
├── package.json
│
├── src/ # MCP Server
│ ├── index.ts # Entry point
│ ├── durable-objects/
│ │ └── trading-agent.ts # DO version of agent (optional)
│ ├── mcp/
│ │ └── agent.ts # MCP tool definitions
│ ├── policy/
│ │ ├── engine.ts # Trade validation logic
│ │ ├── config.ts # Policy configuration
│ │ └── approval.ts # Token generation/validation
│ ├── providers/
│ │ ├── alpaca/ # Alpaca API client
│ │ ├── llm/ # OpenAI integration
│ │ └── technicals.ts # Technical indicators
│ └── storage/
│ └── d1/ # Database queries
│
├── dashboard/ # React dashboard
│ ├── src/
│ │ ├── App.tsx # Main dashboard
│ │ └── components/
│ └── package.json
│
└── migrations/ # Database migrations
Make sure the MCP server is running:
npm run devCheck your .dev.vars file has correct Alpaca and OpenAI keys.
The agent only trades during market hours (9:30 AM - 4:00 PM ET, Mon-Fri). It gathers data 24/7 but won't execute trades when markets are closed.
- Check
min_analyst_confidence- LLM might not be confident enough (try 0.5) - Check
min_sentiment_score- might be too high (try 0.2) - Check
max_positions- might already be at limit - Check research results in dashboard - see what the LLM is thinking
- Check logs in dashboard or
agent-logs.json
- Reduce polling frequency: increase
data_poll_interval_ms - Use cheaper model: set
llm_modeltogpt-4o-mini - The dashboard shows real-time cost tracking
Copy agent-v1.mjs and modify it. The file has clearly marked sections:
┌─────────────────────────────────────────────────────────────────┐
│ SECTION 1: DATA SOURCE (customize this) │
│ - StockTwitsAgent class │
│ - Add your own: news APIs, custom signals, etc. │
├─────────────────────────────────────────────────────────────────┤
│ SECTION 2: TRADING STRATEGY (customize this) │
│ - runTradingLogic() method │
│ - Change buy/sell rules, add technical indicators, etc. │
├─────────────────────────────────────────────────────────────────┤
│ SECTION 3: HARNESS (probably don't touch) │
│ - MCP connection, execution, dashboard API │
│ - Modify only if you know what you're doing │
└─────────────────────────────────────────────────────────────────┘
The MCP server provides these tools for your agents:
| Tool | Description |
|---|---|
accounts-get |
Get account balance and status |
positions-list |
List current positions |
positions-close |
Close a position |
orders-preview |
Preview order and get approval token |
orders-submit |
Submit approved order |
orders-list |
List recent orders |
market-clock |
Check if market is open |
market-quote |
Get stock quote |
technicals-get |
Get technical indicators (RSI, MACD, etc.) |
catalog-list |
List all available tools |
- Add more data sources: News APIs, SEC filings, custom signals
- Enhance LLM prompts: Add technical analysis, fundamentals, news context
- Multi-source confirmation: Require 2+ sources to agree before trading
- Options trading: The MCP server supports options via Alpaca
- Custom indicators: Use the
technicals-getMCP tool
Join our Discord for help, discussion, and sharing strategies:
This software is provided for educational and informational purposes only. Nothing in this repository constitutes financial, investment, legal, or tax advice.
By using this software, you acknowledge and agree that:
- All trading and investment decisions are made at your own risk
- Markets are volatile and you can lose some or all of your capital
- No guarantees of performance, profits, or outcomes are made
- The authors, contributors, and maintainers are not responsible for any financial losses, damages, or other consequences resulting from the use of this software
- You are solely responsible for your own trades and investment decisions
- This software may contain bugs, errors, or behave unexpectedly
- Past performance (real or simulated) does not guarantee future results
If you do not fully understand the risks involved in trading or investing, you should not use this software.
No member, contributor, or operator of this project shall be held liable for losses of any kind. This software is provided "as is" without warranty of any kind, express or implied.
Always start with paper trading (ALPACA_PAPER=true) and never risk money you cannot afford to lose.
MIT License - Free for personal and commercial use. See LICENSE for full terms.