# Intraday Margin Trading Automation

Complete automated system for HK/US intraday margin trading with LongPort SDK.

## Architecture

```
8:30 Beijing → hk_intraday_scanner.py → TOP3 candidates → QQ
9:30 Beijing → hk_intraday_monitor.py → entry signals → auto order → QQ
15:45 Beijing → hk_intraday_close.py → close all system positions → QQ

21:00 Beijing → us_intraday_scanner.py → TOP3 candidates → QQ
21:30 Beijing → us_intraday_monitor.py → entry signals → auto order → QQ
3:45 Beijing → us_intraday_close.py → close all system positions → QQ
```

## Scoring Formula

```
score = min(ADR% / 4, 1) × 40 + min(VolumeRatio / 2, 1) × 30 + min(TurnoverRate / 2, 1) × 30
```

- ADR%: Average Daily Range (近5日高低价差百分比)
- VolumeRatio: LongPort CalcIndex.VolumeRatio
- TurnoverRate: LongPort CalcIndex.TurnoverRate

Score > 60 = excellent, 40-60 = good, < 40 = not ideal

## Entry Signals (5-min SMA)

**做多条件:**
- current > SMA5 > SMA10
- current > previous close (上涨趋势)

**做空条件:**
- current < SMA5 < SMA10
- current < previous close (下跌趋势)

## Position Sizing

```python
buying_power = account.buy_power  # HKD or USD
position_size = buying_power * 0.25  # 25% per trade
shares = int(position_size / current_price / 100) * 100  # HK: round to 100
shares = int(position_size / current_price)  # US: round to 1
```

## Stop Loss / Take Profit

```python
atr = sum(max(h-l, abs(h-pc), abs(l-pc)) for ...) / n  # 5-min ATR

# 做多
stop_loss = max(min(lows[-5:]), entry - atr * 2)
take_profit = entry + atr * 3

# 做空
stop_loss = min(max(highs[-5:]), entry + atr * 2)
take_profit = entry - atr * 3
```

盈亏比 = 3:2 = 1.5:1

## Position Tracking (CRITICAL)

Entries tracked in `~/.hermes/trading/{hk,us}_intraday_entries.json`:

```json
{
  "3690.HK": {
    "side": "buy",
    "entry_price": 66.10,
    "stop_loss": 65.85,
    "take_profit": 66.77,
    "shares": 100,
    "order_id": "3686893095794171904",
    "time": "2026-06-25T09:45:00"
  }
}
```

## Safety Rules

1. **ONLY CLOSE SYSTEM-OPENED POSITIONS** — verify `order_id` exists before closing
2. **NEVER touch user's manual positions** (UNH, RGTI, 3416.HK, etc.)
3. **Day trade only** — close all at 15:45 HK / 3:45 US Beijing
4. **Single trade max** — 25% of buying power
5. **Stop loss mandatory** — 2× ATR from entry

## Cron Jobs

| Job | Schedule (EDT) | Schedule (Beijing) | Script |
|-----|----------------|-------------------|--------|
| HK Scanner | `30 20 * * 1-5` | 8:30 | hk_intraday_scanner.py |
| HK Monitor | `*/15 9-15 * * 1-5` | 21:15-3:45 | hk_intraday_monitor.py |
| HK Close | `45 15 * * 1-5` | 3:45 | hk_intraday_close.py |
| US Scanner | `0 9 * * 1-5` | 21:00 | us_intraday_scanner.py |
| US Monitor | `*/15 21-23,0-3 * * 1-5` | 9:00-15:45 | us_intraday_monitor.py |
| US Close | `45 3 * * 2-6` | 3:45 | us_intraday_close.py |

## Pitfalls

- **Period enum**: Use `Period.Min_5` not `Period.Min5` (underscore required)
- **buy_power**: `account.buy_power` not `account.available_cash`
- **SecurityQuote**: Use `q.last_done`, `q.prev_close`, `q.high`, `q.low`, `q.open` — no `change_rate` attribute
- **Entry file path**: `~/.hermes/trading/` not `~/.hermes/skills/...`
