131 lines
3.4 KiB
Markdown
131 lines
3.4 KiB
Markdown
# Stock Bot Configuration Library Usage Guide
|
|
|
|
This guide shows how to use the Zod-based configuration system in the Stock Bot platform.
|
|
|
|
## Quick Start
|
|
|
|
```typescript
|
|
import { databaseConfig, loggingConfig, riskConfig, dataProvidersConfig } from '@stock-bot/config';
|
|
|
|
// Access individual values
|
|
console.log(`Database: ${databaseConfig.POSTGRES_HOST}:${databaseConfig.POSTGRES_PORT}`);
|
|
console.log(`Log level: ${loggingConfig.LOG_LEVEL}`);
|
|
console.log(`Max position size: ${riskConfig.RISK_MAX_POSITION_SIZE}`);
|
|
```
|
|
|
|
## Environment Variables
|
|
|
|
All configuration is driven by environment variables. You can set them in:
|
|
- `.env` files
|
|
- System environment variables
|
|
- Docker environment variables
|
|
|
|
### Database Configuration
|
|
```bash
|
|
DB_HOST=localhost
|
|
DB_PORT=5432
|
|
DB_NAME=stockbot
|
|
DB_USER=stockbot
|
|
DB_PASSWORD=your_password
|
|
DB_SSL=false
|
|
DB_POOL_MAX=10
|
|
```
|
|
|
|
### Logging Configuration
|
|
```bash
|
|
LOG_LEVEL=info
|
|
LOG_CONSOLE=true
|
|
LOKI_HOST=localhost
|
|
LOKI_PORT=3100
|
|
LOKI_LABELS=service=market-data-gateway,version=1.0.0
|
|
```
|
|
|
|
### Risk Management Configuration
|
|
```bash
|
|
RISK_MAX_POSITION_SIZE=0.1
|
|
RISK_DEFAULT_STOP_LOSS=0.05
|
|
RISK_DEFAULT_TAKE_PROFIT=0.15
|
|
RISK_CIRCUIT_BREAKER_ENABLED=true
|
|
```
|
|
|
|
### Data Provider Configuration
|
|
```bash
|
|
DEFAULT_DATA_PROVIDER=alpaca
|
|
ALPACA_API_KEY=your_api_key
|
|
ALPACA_API_SECRET=your_api_secret
|
|
ALPACA_ENABLED=true
|
|
POLYGON_ENABLED=false
|
|
```
|
|
|
|
## Advanced Usage
|
|
|
|
### Type Safety
|
|
All configurations are fully typed:
|
|
|
|
```typescript
|
|
import type { DatabaseConfig, LoggingConfig, RiskConfig } from '@stock-bot/config';
|
|
|
|
function setupDatabase(config: DatabaseConfig) {
|
|
// TypeScript knows all the available properties
|
|
return {
|
|
host: config.POSTGRES_HOST,
|
|
port: config.POSTGRES_PORT, // number
|
|
ssl: config.POSTGRES_SSL, // boolean
|
|
};
|
|
}
|
|
```
|
|
|
|
### Environment Detection
|
|
```typescript
|
|
import { getEnvironment, Environment } from '@stock-bot/config';
|
|
|
|
const env = getEnvironment();
|
|
if (env === Environment.Production) {
|
|
// Production-specific logic
|
|
}
|
|
```
|
|
|
|
### Data Provider Helpers
|
|
```typescript
|
|
import { getProviderConfig, getEnabledProviders, getDefaultProvider } from '@stock-bot/config';
|
|
|
|
// Get specific provider
|
|
const alpaca = getProviderConfig('alpaca');
|
|
|
|
// Get all enabled providers
|
|
const providers = getEnabledProviders();
|
|
|
|
// Get default provider
|
|
const defaultProvider = getDefaultProvider();
|
|
```
|
|
|
|
## Configuration Files
|
|
|
|
The library consists of these modules:
|
|
|
|
- **core.ts** - Core utilities and environment detection
|
|
- **database.ts** - Database connection settings
|
|
- **logging.ts** - Logging and Loki configuration
|
|
- **risk.ts** - Risk management parameters
|
|
- **data-providers.ts** - Data provider settings
|
|
|
|
## Benefits of This Approach
|
|
|
|
1. **Zero Configuration Schema** - No complex schema definitions needed
|
|
2. **Automatic Type Inference** - TypeScript types are generated automatically
|
|
3. **Environment Variable Validation** - Invalid values are caught at startup
|
|
4. **Great Developer Experience** - IntelliSense works perfectly
|
|
5. **Production Ready** - Used by many large-scale applications
|
|
|
|
## Migration from Previous System
|
|
|
|
If you're migrating from the old Valibot-based system:
|
|
|
|
```typescript
|
|
// Old way
|
|
const config = createConfigLoader('database', databaseSchema, defaultConfig)();
|
|
|
|
// New way
|
|
import { databaseConfig } from '@stock-bot/config';
|
|
// That's it! No schema needed, no validation needed, no complex setup.
|
|
```
|