An autonomous AI-powered stock trading agent that executes trades automatically using OpenAI's Agents framework and Alpaca's paper trading API.
This trading agent starts with virtual capital and attempts to grow the portfolio through strategic trading decisions. All trades are executed on Alpaca's paper trading platform using real market data.
Profile: GPT-5 Mini - $50,426.73 portfolio value
- Performance: Modest gains from initial $50,000 virtual capital (+0.85%)
- Recent Activity: Active trading in NVDA, AMZN, MSFT, QQQ, AAPL, and others
- Latest P&L Report:
results/gpt5mini/pnl_gpt-5-mini_2025-08-28.csv
with 101 trades - Detailed Portfolio: See
results/gpt5mini/README-gpt5mini.md
- 101 total trades executed across multiple sessions
- Profitable trades in NVDA (+$46.64), QQQ (+$36.62), XLE (+$39.50)
- Active positions in 11 different assets
- Diversified portfolio with tech, consumer, and ETF exposure
- π€ Autonomous trading using OpenAI GPT models
- π Real-time market data and analysis
- π° Paper trading through Alpaca API
- π Automated portfolio tracking and reporting
- π§ Strategic decision-making with risk management
-
Alpaca Paper Trading Account
- Sign up at Alpaca Markets
- Account starts with $100,000 virtual capital (this implementation uses $50,000)
- Get your API keys from the dashboard
-
OpenAI API Key
- Get API key from OpenAI Platform
- GPT-5 Mini recommended for best rate limits
# Clone repository
git clone https://github.com/matthewchung74/llm_trader.git
cd llm_trader
# Install dependencies
npm install
# Install global dependencies
npm install -g dotenv-cli tsx
Create profile-based environment files:
# .env.gpt5mini (RECOMMENDED - Best rate limits)
OPENAI_API_KEY=your_openai_api_key
ALPACA_API_KEY=your_alpaca_api_key
ALPACA_SECRET_KEY=your_alpaca_secret_key
ALPACA_BASE_URL=https://paper-api.alpaca.markets
# .env.gpt5 (Premium option - may hit rate limits)
OPENAI_API_KEY=your_openai_api_key
ALPACA_API_KEY=your_alpaca_api_key
ALPACA_SECRET_KEY=your_alpaca_secret_key
ALPACA_BASE_URL=https://paper-api.alpaca.markets
ALWAYS use profile-based commands to load API keys correctly:
# GPT-5 Mini (RECOMMENDED)
npx dotenv -e .env.gpt5mini -- npm start
# GPT-5 (Premium)
npx dotenv -e .env.gpt5 -- npm start
# Direct execution
npx dotenv -e .env.gpt5mini -- tsx src/agent.ts
# GPT-5 Mini - Every 30 minutes (RECOMMENDED)
npx dotenv -e .env.gpt5mini -- npm run start:continuous
# GPT-5 Mini - Every hour
npx dotenv -e .env.gpt5mini -- npm run start:continuous:1h
# GPT-5 Mini - Every 4 hours
npx dotenv -e .env.gpt5mini -- npm run start:continuous:4h
# GPT-5 Premium - Every 30 minutes
npx dotenv -e .env.gpt5 -- npm run start:continuous
# Custom interval (2 hours example)
npx dotenv -e .env.gpt5mini -- tsx src/agent.ts --continuous --interval=2
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Test coverage report
npm run test:coverage
# Test agent functionality
npm run test:agents
# Test trading functions
npm run test:trading
# Keep running even after closing terminal
nohup npx dotenv -e .env.gpt5mini -- npm run start:continuous > trading.log 2>&1 &
# View live logs
tail -f trading.log
# Stop background process
pkill -f "tsx src/agent.ts"
The agent automatically:
- β Sleeps when markets are closed (nights, weekends, holidays)
- β Wakes up at 9:30 AM EST each trading day
- β Trades every 30 minutes during market hours (9:30 AM - 4:00 PM EST)
- β Handles timezone conversion automatically
Deploy the trading agent on any machine with Docker:
# Build and run GPT-5 Mini agent (recommended)
docker-compose --profile gpt5mini up --build
# Build and run GPT-5 agent (premium)
docker-compose --profile gpt5 up --build
# Run single trading session (for testing)
docker-compose --profile single up --build
Create your environment files:
# .env.gpt5mini (recommended)
OPENAI_API_KEY=your_openai_api_key
ALPACA_API_KEY=your_alpaca_api_key
ALPACA_SECRET_KEY=your_alpaca_secret_key
ALPACA_BASE_URL=https://paper-api.alpaca.markets
# .env.gpt5 (premium option)
OPENAI_API_KEY=your_openai_api_key
ALPACA_API_KEY=your_alpaca_api_key
ALPACA_SECRET_KEY=your_alpaca_secret_key
ALPACA_BASE_URL=https://paper-api.alpaca.markets
- Data Persistence: Trading data and logs persist in
./results
and./logs
- Auto Restart: Container automatically restarts unless manually stopped
- Health Checks: Built-in monitoring of agent processes
- Multiple Profiles: Separate containers for different AI models
# Build image
docker build -t priced-in .
# Run continuous trading
docker run -d --env-file .env.gpt5mini \
-v $(pwd)/results:/app/results \
-v $(pwd)/logs:/app/logs \
--name trading-bot priced-in
# View logs
docker logs -f trading-bot
# Stop container
docker stop trading-bot
The agent follows a momentum-based trading strategy, focusing on:
- Technical analysis and market trends
- News-driven opportunities
- Risk management and diversification
- Continuous learning from trading results