AI Trading Agent
An intelligent trading agent that uses AI models to make automated trading decisions on the Lighter Protocol. The system includes a backend API, trading-agent dashboard, and AI-powered trading logic.
Prerequisites
Bun runtime
PostgreSQL database
OpenRouter API account
Lighter Protocol API keys
Setup Instructions
1. Install Dependencies
2. Environment Variables
Create or update the
.env file in the root directory with the following variables:3. Required API Keys
OpenRouter API Key
Sign up at OpenRouter
Generate an API key from your dashboard
Replace
your_openrouter_api_key_here in the .env fileDatabase Setup
Install and start PostgreSQL
Create a database named
ai_trading_agentUpdate the
DATABASE_URL with your actual database credentialsRun Prisma migrations:
Lighter Protocol API Keys
Create API keys for your trading accounts
Note the API key index (typically starts from 0, 1, 2, etc.)
Store these keys in the database through the Models table (see Database Configuration below)
4. Database Configuration
The trading agents are configured through the
Models table in the database. Each model requires:name: Unique identifier for the trading agentopenRoutermodelName: The AI model name from OpenRouter (e.g., "anthropic/claude-3.5-sonnet")lighterApiKey: Your Lighter Protocol private API keyaccountIndex: Your Lighter Protocol account indexinvocationCount: Number of times the agent has been invoked (starts at 0)Example database entry:
5. Configuration Files
Trading Configuration
Update
config/trading.config.ts if needed:API_KEY_INDEX: Set to match your Lighter Protocol API key index (default: 2)BASE_URL: Lighter Protocol API endpoint (default: mainnet)Market Configuration
The
config/markets.config.ts contains supported trading pairs and market information.Running the Application
Start the Backend API Server
The API server will start on
http://localhost:3000Start the trading-agent Dashboard
The trading-agent will start on
http://localhost:5173Run the Trading Agent
This starts the AI trading agent that will execute trades based on the configured models.
API Endpoints
GET /performance - Get portfolio performance dataGET /invocations - Get recent AI model invocations and trading decisionsProject Structure
Security Notes
Never commit API keys to version control
Store sensitive keys in environment variables or secure key management
Use different API keys for development and production
Regularly rotate your API keys
Monitor your trading activity and set appropriate limits
Troubleshooting
Common Issues
Database Connection Error: Verify your PostgreSQL is running and
DATABASE_URL is correctOpenRouter API Error: Check your API key and account credits
Lighter Protocol Error: Verify your API keys and account permissions
Import Path Errors: Run
bunx prisma generate to regenerate the Prisma clientLogs and Monitoring
Check the console output for trading decisions and API calls
Monitor the trading-agent dashboard for performance metrics
Review database logs for any data inconsistencies
Development
This project was created using
bun init and uses Bun as the JavaScript runtime for optimal performance.1 like · 12 views · Oct 31, 2025
Developed an AI trading agent for Lighter Protocol using AI models.