Running and Monitoring
This section covers building, running, and monitoring your trading agent in development and production environments.
Logger Setup
The logger provides formatted, colored console output with timestamps to help you monitor your trading agent's activity.
Create the logger utility file:
touch src/utils/logger.tsAdd the following content to src/utils/logger.ts:
// src/utils/logger.ts
import pino from 'pino';
/**
* Create a Pino logger with pretty formatting
* @param logLevel - Logging level (debug, info, warn, error)
* @returns Configured logger instance
*/
export function createLogger(logLevel: string = 'info') {
return pino({
// Set minimum log level
level: logLevel,
// Configure pretty printing for human-readable output
transport: {
target: 'pino-pretty', // Use pino-pretty for formatting
level: logLevel,
options: {
colorize: true, // Add colors to log output
translateTime: 'SYS:standard', // Format timestamps as readable dates
ignore: 'pid,hostname', // Don't show process ID or hostname
},
},
// Custom serializers for error objects
serializers: {
err: pino.stdSerializers.err, // Pretty-print error stack traces
},
});
}Build and Run
Development Mode
For quick testing and iteration during development:
# Run directly with ts-node (no compilation step needed)
npx ts-node src/index.tsThis is slower but provides instant feedback without a build step.
Production Mode
For production deployments:
# Build with TypeScript compiler (compiles to JavaScript)
pnpm run build
# Run the compiled JavaScript (faster than ts-node)
pnpm start
# or directly:
node dist/src/index.jsThe compiled JavaScript runs faster and is more suitable for production environments.
Expected Output
When running successfully, you should see:
[13:45:01] INFO: Starting Trading Agent
[13:45:02] INFO: Wallet initialized: 0x7b2f9e4a3c8d1f6b5a2e7d9c4b8a3e1f6c5d2a9b
[13:45:02] INFO: O2Client initialized successfully
[13:45:02] INFO: Trading agent initialized successfully
[13:45:02] INFO: Starting scheduler for ETH/USDC with 2.5s interval
[13:45:03] INFO: Reference price: 2500.50
[13:45:03] INFO: Adjusted prices - Buy: 2750550000000, Sell: 2250450000000 (±10%)
[13:45:03] INFO: Order size: 40000000 ETH
[13:45:03] INFO: Buy order placed (234ms) - Price: 2750550000000, Qty: 40000000
[13:45:03] INFO: Order created: 0x1234567890abcdef...
[13:45:04] INFO: Sell order placed (198ms) - Price: 2250450000000, Qty: 40000000
[13:45:04] INFO: Order created: 0xfedcba0987654321...
[13:45:06] INFO: Reference price: 2501.25
[13:45:06] INFO: Adjusted prices - Buy: 2751375000000, Sell: 2251125000000 (±10%)
...Conclusion
Congrats you now have a complete trading agent that:
- Fetches real-time prices from Bitget
- Places automated buy and sell orders on o2
Key Reminders:
- Start on testnet with small orders to validate your setup
- Monitor logs to ensure orders are filling successfully
- Keep your session private key secure
For additional resources, refer to the o2 API Documentation and the Fee Structure to understand trading costs.