using System; using System.Collections.Generic; namespace NT8.Core.Risk { /// /// Represents different risk modes that can be applied to strategies. /// public enum RiskMode { /// /// Standard, normal risk settings. /// Standard, /// /// Conservative risk settings, lower exposure. /// Conservative, /// /// Aggressive risk settings, higher exposure. /// Aggressive, /// /// Emergency flatten mode, no new trades, close existing. /// EmergencyFlatten } /// /// Represents a time window for trading restrictions. /// public class TradingTimeWindow { /// /// Gets the start time of the window. /// public TimeSpan StartTime { get; private set; } /// /// Gets the end time of the window. /// public TimeSpan EndTime { get; private set; } /// /// Initializes a new instance of the class. /// /// The start time of the window. /// The end time of the window. public TradingTimeWindow(TimeSpan startTime, TimeSpan endTime) { StartTime = startTime; EndTime = endTime; } } /// /// Represents the configuration for advanced risk management. /// public class AdvancedRiskConfig { /// /// Gets the maximum weekly loss limit. /// public double WeeklyLossLimit { get; private set; } /// /// Gets the trailing drawdown limit. /// public double TrailingDrawdownLimit { get; private set; } /// /// Gets the maximum exposure allowed across all strategies. /// public double? MaxCrossStrategyExposure { get; private set; } /// /// Gets the duration of the cooldown period after a risk breach. /// public TimeSpan CooldownDuration { get; private set; } /// /// Gets the maximum correlated exposure across instruments. /// public double? MaxCorrelatedExposure { get; private set; } /// /// Gets the list of allowed trading time windows. /// public List TradingTimeWindows { get; private set; } /// /// Initializes a new instance of the class. /// /// The maximum weekly loss limit. /// The trailing drawdown limit. /// The maximum exposure allowed across all strategies. /// The duration of the cooldown period after a risk breach. /// The maximum correlated exposure across instruments. /// The list of allowed trading time windows. public AdvancedRiskConfig( double weeklyLossLimit, double trailingDrawdownLimit, double? maxCrossStrategyExposure, TimeSpan cooldownDuration, double? maxCorrelatedExposure, List tradingTimeWindows) { WeeklyLossLimit = weeklyLossLimit; TrailingDrawdownLimit = trailingDrawdownLimit; MaxCrossStrategyExposure = maxCrossStrategyExposure; CooldownDuration = cooldownDuration; MaxCorrelatedExposure = maxCorrelatedExposure; TradingTimeWindows = tradingTimeWindows ?? new List(); } } /// /// Represents the current state of advanced risk management. /// public class AdvancedRiskState { /// /// Gets the current weekly PnL. /// public double WeeklyPnL { get; private set; } /// /// Gets the date of the start of the current weekly tracking period. /// public DateTime WeekStartDate { get; private set; } /// /// Gets the current trailing drawdown. /// public double TrailingDrawdown { get; private set; } /// /// Gets the highest point reached in equity or PnL. /// public double PeakEquity { get; private set; } /// /// Gets the list of active strategies. /// public List ActiveStrategies { get; private set; } /// /// Gets the exposure by symbol. /// public Dictionary ExposureBySymbol { get; private set; } /// /// Gets the correlated exposure. /// public double CorrelatedExposure { get; private set; } /// /// Gets the last time the state was updated. /// public DateTime LastStateUpdate { get; private set; } /// /// Initializes a new instance of the class. /// /// The current weekly PnL. /// The date of the start of the current weekly tracking period. /// The current trailing drawdown. /// The highest point reached in equity or PnL. /// The list of active strategies. /// The exposure by symbol. /// The correlated exposure. /// The last time the state was updated. public AdvancedRiskState( double weeklyPnL, DateTime weekStartDate, double trailingDrawdown, double peakEquity, List activeStrategies, Dictionary exposureBySymbol, double correlatedExposure, DateTime lastStateUpdate) { WeeklyPnL = weeklyPnL; WeekStartDate = weekStartDate; TrailingDrawdown = trailingDrawdown; PeakEquity = peakEquity; ActiveStrategies = activeStrategies ?? new List(); ExposureBySymbol = exposureBySymbol ?? new Dictionary(); CorrelatedExposure = correlatedExposure; LastStateUpdate = lastStateUpdate; } } /// /// Represents the exposure of a single strategy. /// public class StrategyExposure { private readonly object _lock = new object(); /// /// Gets the unique identifier for the strategy. /// public string StrategyId { get; private set; } /// /// Gets the current net exposure (longs - shorts) for the strategy. /// public double NetExposure { get; private set; } /// /// Gets the gross exposure (absolute sum of longs and shorts) for the strategy. /// public double GrossExposure { get; private set; } /// /// Gets the number of open positions for the strategy. /// public int OpenPositions { get; private set; } /// /// Initializes a new instance of the class. /// /// The unique identifier for the strategy. public StrategyExposure(string strategyId) { if (strategyId == null) throw new ArgumentNullException("strategyId"); StrategyId = strategyId; NetExposure = 0; GrossExposure = 0; OpenPositions = 0; } /// /// Updates the strategy's exposure. /// /// The change in net exposure. /// The change in gross exposure. /// The change in open positions. public void Update(double netChange, double grossChange, int positionsChange) { lock (_lock) { NetExposure = NetExposure + netChange; GrossExposure = GrossExposure + grossChange; OpenPositions = OpenPositions + positionsChange; } } /// /// Resets the strategy exposure. /// public void Reset() { lock (_lock) { NetExposure = 0; GrossExposure = 0; OpenPositions = 0; } } } }