123 lines
4.2 KiB
Markdown
123 lines
4.2 KiB
Markdown
# Kim Jong-bong (KJB) Strategy - Full System Design
|
|
|
|
**Date**: 2026-02-19
|
|
**Status**: Approved
|
|
|
|
## Overview
|
|
|
|
Implement the Kim Jong-bong short-term trading strategy from `quant.md` as a full system:
|
|
backtesting, daily signal generation, portfolio management, and frontend dashboard.
|
|
|
|
This strategy is fundamentally different from existing strategies (Quality, MultiFactor,
|
|
ValueMomentum) which are periodic-rebalancing based. KJB is signal-based daily trading
|
|
with individual position management, stop-losses, and trailing stops.
|
|
|
|
## Trading Rules (from quant.md)
|
|
|
|
### Universe
|
|
- Market cap rank: 1-30 (large caps)
|
|
- Daily trading value >= 200 billion KRW
|
|
|
|
### Entry Signals (all must be true)
|
|
- Relative Strength vs KOSPI > 100 (stock outperforming market over 10 trading days)
|
|
- AND at least one of:
|
|
- Box breakout (close > 20-day high)
|
|
- Large bullish candle (daily return >= 5% AND volume >= 1.5x 20-day average)
|
|
|
|
### Exit Rules
|
|
- Stop-loss: -3% from entry price
|
|
- Take-profit 1: +5% → sell 50% of position
|
|
- Take-profit 2: +10% → sell remaining
|
|
- Trailing stop: when +5%, move stop to entry price; when +10%, move stop to +5%
|
|
|
|
### Portfolio Rules
|
|
- Cash reserve: 30% (investable = total capital x 0.7)
|
|
- Max positions: 10
|
|
- Position size: equal weight (investable / max_positions)
|
|
|
|
## Architecture
|
|
|
|
### 1. Strategy Layer
|
|
|
|
**`KJBStrategy(BaseStrategy)`** - `services/strategy/kjb.py`
|
|
- Compatible with existing strategy pattern (returns `StrategyResult`)
|
|
- Used for stock ranking by KJB criteria
|
|
- Scores based on: relative strength, breakout proximity, volume trend
|
|
|
|
**`KJBSignalGenerator`** - `services/strategy/kjb.py`
|
|
- Separate class for daily signal generation
|
|
- Input: stock ticker + price data
|
|
- Output: buy/sell signals with entry, target, stop-loss prices
|
|
- Used by: DailyBacktestEngine, daily scheduler job
|
|
|
|
### 2. Daily Backtest Engine
|
|
|
|
**`DailyBacktestEngine`** - `services/backtest/daily_engine.py`
|
|
- New engine alongside existing `BacktestEngine`
|
|
- Daily simulation loop (not rebalance-based)
|
|
- Each day: check entry signals for universe, check exit conditions for positions
|
|
- Uses `TradingPortfolio` for position management
|
|
|
|
**`TradingPortfolio`** - `services/backtest/trading_portfolio.py`
|
|
- Separate class from existing `VirtualPortfolio`
|
|
- Supports: `enter_position()`, `exit_position()`, `partial_exit()`
|
|
- Per-position tracking: stop_loss, target_price, trailing_stop
|
|
- Cash reserve enforcement (30%)
|
|
- Transaction recording with reasons
|
|
|
|
### 3. Signal System
|
|
|
|
**DB Model: `Signal`** - `models/signal.py`
|
|
- Fields: date, ticker, signal_type (buy/sell/partial_sell), entry_price,
|
|
target_price, stop_loss_price, reason, status (active/executed/expired)
|
|
|
|
**Scheduler Job: `kjb_daily_signal_job`** - `jobs/kjb_signal_job.py`
|
|
- Runs daily at 18:00 KST (after price data collection)
|
|
- Generates signals for all universe stocks
|
|
- Saves to DB
|
|
|
|
### 4. API Endpoints
|
|
|
|
**Signal API** - `api/signal.py`
|
|
- `GET /api/signal/kjb/today` - today's buy/sell signals
|
|
- `GET /api/signal/kjb/history` - historical signals
|
|
- `GET /api/signal/kjb/positions` - active positions with P&L
|
|
|
|
**Strategy API additions:**
|
|
- `POST /api/strategy/kjb` - run KJB ranking
|
|
|
|
**Backtest additions:**
|
|
- Existing backtest API works with strategy_type="kjb"
|
|
|
|
### 5. Frontend
|
|
|
|
- KJB signal dashboard page
|
|
- Today's signals cards (buy/sell recommendations)
|
|
- Active positions table (entry price, current price, P&L, stop-loss level)
|
|
- Portfolio value chart
|
|
|
|
## Files to Create
|
|
|
|
| File | Purpose |
|
|
|------|---------|
|
|
| `services/strategy/kjb.py` | KJBStrategy + KJBSignalGenerator |
|
|
| `services/backtest/daily_engine.py` | DailyBacktestEngine |
|
|
| `services/backtest/trading_portfolio.py` | TradingPortfolio |
|
|
| `models/signal.py` | Signal DB model |
|
|
| `schemas/signal.py` | Signal Pydantic schemas |
|
|
| `api/signal.py` | Signal API endpoints |
|
|
| `jobs/kjb_signal_job.py` | Daily signal scheduler job |
|
|
| Alembic migration | signals table |
|
|
| Frontend pages | KJB dashboard |
|
|
|
|
## Files to Modify
|
|
|
|
| File | Change |
|
|
|------|--------|
|
|
| `services/backtest/engine.py` | Add "kjb" to `_create_strategy()` |
|
|
| `models/__init__.py` | Register Signal model |
|
|
| `api/__init__.py` | Register signal router |
|
|
| `schemas/backtest.py` | Add "kjb" to strategy_type enum |
|
|
| `jobs/scheduler.py` | Register KJB signal job |
|
|
| `api/strategy.py` | Add KJB strategy endpoint |
|