added working config lib
This commit is contained in:
parent
f8576c0d93
commit
def9bce8dc
33 changed files with 2896 additions and 1485 deletions
|
|
@ -1,45 +1,82 @@
|
|||
/**
|
||||
* Risk management configuration for trading operations
|
||||
* Risk management configuration using envalid
|
||||
*/
|
||||
import { getNumericEnvVar, validateConfig, createConfigLoader } from './core';
|
||||
import { riskConfigSchema, RiskConfig } from './types';
|
||||
import { cleanEnv, str, num, bool } from 'envalid';
|
||||
|
||||
/**
|
||||
* Default risk configuration
|
||||
* Risk configuration with validation and defaults
|
||||
*/
|
||||
const defaultRiskConfig: RiskConfig = {
|
||||
maxDrawdown: 0.05,
|
||||
maxPositionSize: 0.1,
|
||||
maxLeverage: 1,
|
||||
stopLossDefault: 0.02,
|
||||
takeProfitDefault: 0.05
|
||||
};
|
||||
export const riskConfig = cleanEnv(process.env, {
|
||||
// Position Sizing
|
||||
RISK_MAX_POSITION_SIZE: num({ default: 0.1, desc: 'Maximum position size as percentage of portfolio' }),
|
||||
RISK_MAX_PORTFOLIO_EXPOSURE: num({ default: 0.8, desc: 'Maximum portfolio exposure percentage' }),
|
||||
RISK_MAX_SINGLE_ASSET_EXPOSURE: num({ default: 0.2, desc: 'Maximum exposure to single asset' }),
|
||||
RISK_MAX_SECTOR_EXPOSURE: num({ default: 0.3, desc: 'Maximum exposure to single sector' }),
|
||||
|
||||
// Stop Loss and Take Profit
|
||||
RISK_DEFAULT_STOP_LOSS: num({ default: 0.05, desc: 'Default stop loss percentage' }),
|
||||
RISK_DEFAULT_TAKE_PROFIT: num({ default: 0.15, desc: 'Default take profit percentage' }),
|
||||
RISK_TRAILING_STOP_ENABLED: bool({ default: true, desc: 'Enable trailing stop losses' }),
|
||||
RISK_TRAILING_STOP_DISTANCE: num({ default: 0.03, desc: 'Trailing stop distance percentage' }),
|
||||
|
||||
// Risk Limits
|
||||
RISK_MAX_DAILY_LOSS: num({ default: 0.05, desc: 'Maximum daily loss percentage' }),
|
||||
RISK_MAX_WEEKLY_LOSS: num({ default: 0.1, desc: 'Maximum weekly loss percentage' }),
|
||||
RISK_MAX_MONTHLY_LOSS: num({ default: 0.2, desc: 'Maximum monthly loss percentage' }),
|
||||
|
||||
// Volatility Controls
|
||||
RISK_MAX_VOLATILITY_THRESHOLD: num({ default: 0.4, desc: 'Maximum volatility threshold' }),
|
||||
RISK_VOLATILITY_LOOKBACK_DAYS: num({ default: 20, desc: 'Volatility calculation lookback period' }),
|
||||
|
||||
// Correlation Controls
|
||||
RISK_MAX_CORRELATION_THRESHOLD: num({ default: 0.7, desc: 'Maximum correlation between positions' }),
|
||||
RISK_CORRELATION_LOOKBACK_DAYS: num({ default: 60, desc: 'Correlation calculation lookback period' }),
|
||||
|
||||
// Leverage Controls
|
||||
RISK_MAX_LEVERAGE: num({ default: 2.0, desc: 'Maximum leverage allowed' }),
|
||||
RISK_MARGIN_CALL_THRESHOLD: num({ default: 0.3, desc: 'Margin call threshold' }),
|
||||
|
||||
// Circuit Breakers
|
||||
RISK_CIRCUIT_BREAKER_ENABLED: bool({ default: true, desc: 'Enable circuit breakers' }),
|
||||
RISK_CIRCUIT_BREAKER_LOSS_THRESHOLD: num({ default: 0.1, desc: 'Circuit breaker loss threshold' }),
|
||||
RISK_CIRCUIT_BREAKER_COOLDOWN_MINUTES: num({ default: 60, desc: 'Circuit breaker cooldown period' }),
|
||||
|
||||
// Risk Model
|
||||
RISK_MODEL_TYPE: str({
|
||||
choices: ['var', 'cvar', 'expected_shortfall'],
|
||||
default: 'var',
|
||||
desc: 'Risk model type'
|
||||
}),
|
||||
RISK_CONFIDENCE_LEVEL: num({ default: 0.95, desc: 'Risk model confidence level' }),
|
||||
RISK_TIME_HORIZON_DAYS: num({ default: 1, desc: 'Risk time horizon in days' }),
|
||||
});
|
||||
|
||||
/**
|
||||
* Load risk configuration from environment variables
|
||||
*/
|
||||
export function loadRiskConfig(): RiskConfig {
|
||||
const config: RiskConfig = {
|
||||
maxDrawdown: getNumericEnvVar('RISK_MAX_DRAWDOWN', defaultRiskConfig.maxDrawdown),
|
||||
maxPositionSize: getNumericEnvVar('RISK_MAX_POSITION_SIZE', defaultRiskConfig.maxPositionSize),
|
||||
maxLeverage: getNumericEnvVar('RISK_MAX_LEVERAGE', defaultRiskConfig.maxLeverage),
|
||||
stopLossDefault: getNumericEnvVar('RISK_STOP_LOSS_DEFAULT', defaultRiskConfig.stopLossDefault),
|
||||
takeProfitDefault: getNumericEnvVar('RISK_TAKE_PROFIT_DEFAULT', defaultRiskConfig.takeProfitDefault)
|
||||
};
|
||||
// Export typed configuration object
|
||||
export type RiskConfig = typeof riskConfig;
|
||||
|
||||
return validateConfig(config, riskConfigSchema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a dynamic configuration loader for risk management
|
||||
*/
|
||||
export const createRiskConfig = createConfigLoader<typeof defaultRiskConfig>(
|
||||
'risk',
|
||||
riskConfigSchema,
|
||||
defaultRiskConfig
|
||||
);
|
||||
|
||||
/**
|
||||
* Singleton risk configuration
|
||||
*/
|
||||
export const riskConfig = loadRiskConfig();
|
||||
// Export individual config values for convenience
|
||||
export const {
|
||||
RISK_MAX_POSITION_SIZE,
|
||||
RISK_MAX_PORTFOLIO_EXPOSURE,
|
||||
RISK_MAX_SINGLE_ASSET_EXPOSURE,
|
||||
RISK_MAX_SECTOR_EXPOSURE,
|
||||
RISK_DEFAULT_STOP_LOSS,
|
||||
RISK_DEFAULT_TAKE_PROFIT,
|
||||
RISK_TRAILING_STOP_ENABLED,
|
||||
RISK_TRAILING_STOP_DISTANCE,
|
||||
RISK_MAX_DAILY_LOSS,
|
||||
RISK_MAX_WEEKLY_LOSS,
|
||||
RISK_MAX_MONTHLY_LOSS,
|
||||
RISK_MAX_VOLATILITY_THRESHOLD,
|
||||
RISK_VOLATILITY_LOOKBACK_DAYS,
|
||||
RISK_MAX_CORRELATION_THRESHOLD,
|
||||
RISK_CORRELATION_LOOKBACK_DAYS,
|
||||
RISK_MAX_LEVERAGE,
|
||||
RISK_MARGIN_CALL_THRESHOLD,
|
||||
RISK_CIRCUIT_BREAKER_ENABLED,
|
||||
RISK_CIRCUIT_BREAKER_LOSS_THRESHOLD,
|
||||
RISK_CIRCUIT_BREAKER_COOLDOWN_MINUTES,
|
||||
RISK_MODEL_TYPE,
|
||||
RISK_CONFIDENCE_LEVEL,
|
||||
RISK_TIME_HORIZON_DAYS,
|
||||
} = riskConfig;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue