TRADING_MODE environment variable.
Trading Modes
Simulated Mode
Runs a local trading simulator that mimics exchange behavior without risking real capital. Perfect for:- Testing trading strategies
- Validating AI model decisions
- Development and debugging
- Backtesting with live market data
- Virtual capital (configurable starting balance)
- Real market price data
- Simulated order execution and fills
- Position tracking and P&L calculation
- No real money at risk
Live Mode
Executes real trades on the zkLighter exchange using your API credentials. Use this mode when:- You’ve thoroughly tested your strategy in simulation
- You’re ready to trade with real capital
- You understand the risks and have proper risk management
Configuration
Set the trading mode in your.env file:
Simulated Mode Configuration
When usingTRADING_MODE=simulated, configure the simulator with these variables:
number
default:"10000"
Starting capital for the simulator. This represents your virtual trading balance in the quote currency.
string
default:"USDT"
The quote currency for all positions and P&L calculations. Typically USDT or USDC.
number
default:"10000"
Interval in milliseconds between market data refreshes. Lower values provide more real-time updates but increase API usage.
Live Mode Configuration
When usingTRADING_MODE=live, you must configure Lighter API credentials:
number
default:"2"
Your API key index in the zkLighter account system. Each account can have multiple key indexes.
string (URL)
default:"https://mainnet.zklighter.elliot.ai"
Base URL for the Lighter REST API. Use mainnet for production trading.
Accessing Trading Mode in Code
Use the validated constants fromsrc/env.ts:
Type-Safe Mode Checking
TheTRADING_MODE constant is type-checked as an enum:
Simulator Architecture
The exchange simulator (ExchangeSimulator) provides a high-fidelity trading environment:
Key Features
State Management:- Maintains virtual account balance and positions
- Tracks open orders and execution history
- Calculates unrealized P&L from live market prices
- Persists state to database (rehydrates on restart)
- Validates order parameters (quantity, price, side)
- Simulates order matching and fills
- Applies realistic slippage and fees
- Supports market and limit orders
- Fetches real-time prices from configured data sources
- Updates position values on price changes
- Triggers position refreshes at configured intervals
- Stores all simulated trades in the
Orderstable withstatus="CLOSED" - Tracks open positions as orders with
status="OPEN" - Maintains full audit trail of simulated trading activity
Simulator Initialization
The simulator is bootstrapped once at server startup:api/src/index.ts) and guarded against duplicate initialization using schedulerState.ts.
Switching Modes
To switch between modes:-
Update your
.envfile: -
Restart the application:
-
Verify the mode in logs:
Safety Features
Mode Isolation
Simulated and live modes are completely isolated:- Simulated trades never reach the real exchange
- Live trades never interact with simulator state
- Database records are tagged with execution context
- Position calculations respect mode boundaries
Pre-Deployment Checklist
Before enabling live mode:- Thoroughly tested strategy in simulated mode
- Verified P&L calculations are accurate
- Confirmed risk management rules are working
- Set appropriate position sizing limits
- Configured stop losses and invalidation levels
- Tested with small capital amounts first
- Verified Lighter API credentials are correct
- Set up monitoring and alerts
- Documented your trading rules and expectations
Environment Validation
Validate your configuration before deployment:- Required variables are set for your chosen mode
- API keys are present (for live mode)
- URLs are valid and reachable
- Database connection is working
- Simulator configuration is valid (for simulated mode)
Common Patterns
Conditional Feature Flags
Enable features based on trading mode:Mode-Specific Logging
Add context to logs:Testing Both Modes
Use different.env files for different scenarios:
Troubleshooting
Simulator Not Initializing
Symptoms: No positions showing, orders not executing in simulated mode Solutions:- Check
TRADING_MODE=simulatedis set - Verify database connection (
DATABASE_URL) - Check server logs for initialization errors
- Ensure scheduler bootstrap completed successfully
Live Orders Not Executing
Symptoms: Orders placed but not filled in live mode Solutions:- Verify
TRADING_MODE=liveis set - Check Lighter API credentials are correct
- Confirm
LIGHTER_BASE_URLis reachable - Review exchange API logs for errors
- Check account balance and permissions
Mode Mismatch After Restart
Symptoms: UI shows different mode than expected Solutions:- Verify
.envfile was saved - Restart both API server and frontend
- Clear browser cache and reload
- Check for multiple
.envfiles that might conflict
Best Practices
Development:
- Always use simulated mode during development
- Test edge cases with various capital amounts
- Validate strategy performance over extended periods
- Use realistic refresh intervals (don’t set too low)
Testing:
- Run integration tests in simulated mode
- Verify mode switching doesn’t break features
- Test with both empty and populated order history
- Validate P&L calculations match expectations
Production:
- Never commit live API credentials
- Use environment-specific
.envfiles - Monitor mode in server logs and metrics
- Implement circuit breakers for live trading
- Start with minimal capital to verify behavior

