# UnifiedTradingModal

A unified modal component that replaces both `BacktestModal` and `BotConfigModal`. This component handles three different modes:

- **backtest**: Run backtests with multiple tickers
- **createBot**: Create a new trading bot (optionally from a backtest)
- **updateBot**: Update an existing bot's configuration

## Features

### ✅ **Unified Interface**
- Single component for all trading configuration needs
- Mode-specific form fields and validation
- Consistent UI/UX across all use cases

### ✅ **Advanced Configuration**
- **Advanced Parameters**: Collapsible section with cooldown periods, position limits, trading options
- **Risk Management**: Complete `RiskManagement` configuration with preset levels (Conservative, Moderate, Aggressive)
- **Synth API Integration**: AI-powered probabilistic forecasts and risk assessment

### ✅ **Smart Defaults**
- Context-aware initialization based on mode
- Automatic data loading and form population
- Preset risk management configurations

## Usage Examples

### 1. Backtest Mode
```tsx
import { UnifiedTradingModal } from '../../components/organism'

<UnifiedTradingModal
  showModal={showModal}
  closeModal={() => setShowModal(false)}
  mode="backtest"
  setBacktests={setBacktests}
  showLoopSlider={true} // Optional: enable loop optimization
/>
```

### 2. Create Bot Mode
```tsx
<UnifiedTradingModal
  showModal={showModal}
  closeModal={() => setShowModal(false)}
  mode="createBot"
/>
```

### 3. Create Bot from Backtest
```tsx
<UnifiedTradingModal
  showModal={showModal}
  closeModal={() => setShowModal(false)}
  mode="createBot"
  backtest={selectedBacktest} // Initialize from backtest
/>
```

### 4. Update Bot Mode
```tsx
<UnifiedTradingModal
  showModal={showModal}
  closeModal={() => setShowModal(false)}
  mode="updateBot"
  existingBot={{
    identifier: bot.identifier,
    config: bot.config
  }}
/>
```

## Migration Guide

### From BacktestModal
```tsx
// Old
<BacktestModal
  showModal={showModal}
  closeModal={closeModal}
  setBacktests={setBacktests}
  showLoopSlider={true}
/>

// New
<UnifiedTradingModal
  mode="backtest"
  showModal={showModal}
  closeModal={closeModal}
  setBacktests={setBacktests}
  showLoopSlider={true}
/>
```

### From BotConfigModal
```tsx
// Old
<BotConfigModal
  showModal={showModal}
  mode="create"
  backtest={backtest}
  onClose={closeModal}
/>

// New
<UnifiedTradingModal
  mode="createBot"
  showModal={showModal}
  closeModal={closeModal}
  backtest={backtest}
/>
```

## Risk Management Features

The component includes a comprehensive Risk Management section with:

- **Preset Levels**: Conservative, Moderate, Aggressive configurations
- **Kelly Criterion**: Position sizing based on edge and odds
- **Expected Utility**: Risk-adjusted decision making
- **Probability Thresholds**: Adverse and favorable signal validation
- **Liquidation Risk**: Maximum acceptable liquidation probability

All risk management parameters use the official `RiskManagement` type from the API and integrate seamlessly with the backend trading logic. 