Files
Stock-trading-programming/README.md
T

5.5 KiB

단타 자동매매 시스템 v3.0

기획서 v3.0 기준 / KIS Open API / Synology NAS Docker AI: Claude Code/Codex headless (장 전 분석 + 장중 점검 + 장 후 피드백)

운영 모드

KIS_MOCK DRY_RUN 동작
true true 신호 확인만 (주문 없음) ← 현재
true false 모의투자 실제 주문 ← 3개월 검증
false false 실거래 ← 조건 충족 후

빠른 시작

# 1. .env 설정
cp .env.example .env
# .env 열어서 KIS 키, Discord Webhook URL 입력

# 2. KIS 연결 테스트
pip install aiohttp python-dotenv
python test_connection.py

# 3. 신호 확인 (DRY_RUN=true)
python app/main.py

# 4. Docker 실행 (NAS)
docker-compose up -d

컨테이너 구성

컨테이너 역할 실행 시간
stockbot-main 매매 프로그램 상시 (09:00~15:00 활성)
stockbot-redis 시세 캐시 상시
stockbot-dashboard Streamlit 모니터링 상시 (포트 8501)
claude-morning 장 전 AI 분석 08:30 (실행 후 종료)
claude-evening 장 후 AI 피드백 15:30 (실행 후 종료)
stockbot-killswitch 긴급 청산 수동 트리거

긴급 청산

docker-compose --profile emergency up kill-switch
# 또는
python kill_switch/kill.py

StockBot Current Status - 2026-06-04

This project is currently a paper-trading scalping bot with an AI training pipeline in observation mode.

Active:

  • Windows Task Scheduler operation for morning, midday, evening, watchdog, and training jobs.
  • Approved ENTRY_START = "09:20" after the 2026-05-28 evening review.
  • Applied MAX_HOLD_MIN = 90.
  • FORCE_EXIT = "14:50" remains unchanged.
  • avoid_sectors filtering is active in runtime entry checks.
  • Re-entry controls are active:
    • TIME/FORCE cooldown is measured from final exit time.
    • TP final exits block same-day ticker re-entry.
    • Re-entry control state is restored from today's DB rows after restart.
  • Duplicate bot startup guards are active:
    • app/main.py uses logs/stockbot.lock.
    • scripts/start_bot.py kills existing app/main.py processes before starting one new bot.
    • scripts/run_morning.ps1 is the only post-morning bot startup owner.
    • /morning must not call /start-bot or scripts/start_bot.py.
  • KIS stability hardening is active:
    • Mode-specific request spacing.
    • Retry for transient KIS timeout/rate-limit price errors.
    • DRY_RUN simulated fills reuse the current quote to reduce extra KIS calls.
  • Daily settlement has a duplicate-summary guard.
  • Entry snapshots for model training.
  • Post-entry snapshots at 1m, 3m, 5m, and 10m.
  • Bot-data export to data/training_dataset.csv.
  • External daily/minute data collection for pretraining.
  • External daily collection falls back to KIS when pykrx same-day data fails.
  • KIS minute collection excludes ETF/ETN by default and collects multiple windows from 09:30 to 14:00.
  • RandomForest-based training engine.
  • Optional AI entry scoring when models/scalping_model.joblib exists.
  • Latest successful training run: 2026-06-02 18:36.
  • Latest training rows: 5,773 total, including 5,720 external pretraining rows and 53 bot-trade rows.
  • Latest metrics: label_stop_loss ROC-AUC 0.896, label_win ROC-AUC 0.715.
  • Latest daily review/proposal files exist through 2026-06-02; 2026-06-03 was a market holiday.

Not active yet:

  • ML model scores do not block buys.
  • ML model scores do not change position size.
  • ML model scores do not override exits.
  • Real-cash trading is not ready until fill, unfilled-order, and partial-fill handling is hardened.

Current operational notes:

  • On 2026-06-04, duplicate morning execution happened before the /morning instruction fix. The next scheduled morning run should show one context run and one scripts/start_bot.py startup.
  • KIS rate limits can still happen when multiple scripts/processes call KIS concurrently.
  • The 2026-06-03 holiday training run logged a failure at the holiday-check step instead of a clean skip; inspect before relying on holiday automation noise level.
  • Keep secrets out of tracked files and remote URLs. Prefer environment variables for Discord webhooks and credentials.

Daily schedule:

Time Task Purpose
08:15 StockBot_Morning Run /morning, build context, then scripts/start_bot.py starts exactly one bot
09:00-15:10 StockBot_Watchdog Check/restart bot every 5 minutes
11:20 StockBot_Midday Midday review and context update
15:30 StockBot_Evening Daily review and proposal report
16:00 StockBot_Training Collect data, export datasets, train model

Useful commands:

python -m pip install -r requirements.txt
powershell -ExecutionPolicy Bypass -File scripts\install_dependencies.ps1
powershell -ExecutionPolicy Bypass -File scripts\setup_scheduler.ps1
python scripts\start_bot.py
powershell -ExecutionPolicy Bypass -File scripts\run_training_pipeline.ps1
python scripts\export_training_dataset.py
python scripts\train_ai_model.py
Get-Content logs\morning.log -Tail 80
Get-Content logs\stockbot.log -Tail 80

Dependency portability:

  • Root requirements.txt includes app/requirements.txt.
  • scripts/download_dependencies.ps1 downloads Windows/Python 3.11 wheels to vendor/wheels.
  • scripts/install_dependencies.ps1 installs from vendor/wheels first, then falls back to online pip.
  • Raspberry Pi needs its own wheelhouse or online install because Windows wheels are not ARM/Linux compatible.