아키텍처
프로젝트 구조
moneymax/
├── src/moneymax/
│ ├── main.py # 앱 오케스트레이터
│ ├── settings.py # 설정 로더 (YAML + .env)
│ ├── common/ # 공통 타입 및 유틸리티
│ │ ├── types.py # Asset, Signal, Position, OHLCV 등
│ │ ├── rate_limiter.py # 토큰 버킷 속도 제한
│ │ ├── retry.py # tenacity 기반 재시도
│ │ └── exceptions.py # 예외 계층
│ ├── data/ # 데이터 피드
│ │ ├── crypto_feed.py # Binance/Upbit (ccxt)
│ │ ├── us_stock_feed.py # yfinance + Alpha Vantage
│ │ └── kr_stock_feed.py # pykrx (KRX)
│ ├── analysis/
│ │ ├── technical.py # pandas-ta 기술 지표
│ │ └── sentiment.py # FinBERT 감성 분석
│ ├── strategy/
│ │ ├── base.py # Strategy ABC
│ │ ├── registry.py # 전략 레지스트리
│ │ ├── risk.py # 리스크 매니저
│ │ ├── crypto_scalp.py # 크립토 스캘핑
│ │ ├── crypto_momentum.py # 크립토 모멘텀
│ │ ├── stock_swing.py # 주식 스윙
│ │ └── stock_trend.py # 주식 트렌드
│ ├── portfolio/
│ │ ├── manager.py # 포지션 추적, P&L
│ │ └── ledger.py # JSONL 기록
│ ├── trading/
│ │ ├── base.py # Broker ABC
│ │ ├── paper_trader.py # Paper Trading 시뮬레이터
│ │ ├── binance_broker.py # Binance 실거래
│ │ └── engine.py # 자동매매 엔진
│ └── notification/
│ ├── telegram.py # Telegram Bot 알림
│ ├── discord_bot.py # Discord Bot
│ └── formatters.py # 메시지 포맷터
├── config/
│ ├── settings.yaml # 앱 설정
│ ├── strategies.yaml # 전략 파라미터
│ ├── watchlists.yaml # 감시 종목
│ ├── trading.yaml # 자동매매 설정
│ └── discord.yaml # Discord 채널 설정
├── scripts/
│ ├── auto_trader.py # 자동매매 CLI
│ ├── run_backtest.py # 백테스트 실행
│ └── tune_*.py # 파라미터 튜닝
└── data/ # 실행 중 생성되는 데이터
└── auto_trader.jsonl # 시그널/거래 기록핵심 데이터 타입
Asset
거래 대상 자산을 나타냅니다.
python
@dataclass(frozen=True)
class Asset:
symbol: str # "BTC/USDT"
market: Market # CRYPTO, US_STOCK, KR_STOCK
exchange: Exchange # BINANCE, UPBIT, NYSE, KRXSignal
전략이 생성한 매매 시그널입니다.
python
@dataclass
class Signal:
asset: Asset
action: SignalAction # BUY, SELL, HOLD
strength: SignalStrength # STRONG, MODERATE, WEAK
confidence: float # 0.0 ~ 1.0
strategy_name: str
current_price: float
stop_loss: float | None
take_profit: float | None
reasons: list[str] # 시그널 근거Position
오픈된 포지션을 추적합니다.
python
@dataclass
class Position:
asset: Asset
side: SignalAction # BUY (롱) or SELL (숏)
entry_price: float
quantity: float
stop_loss: float | None
take_profit: float | None
current_price: float
unrealized_pnl: float
is_open: bool컴포넌트 관계도
┌─────────────┐ ┌──────────────┐ ┌───────────┐
│ CryptoFeed │────→│ Technical │────→│ Strategy │
│ (ccxt) │ │ Analyzer │ │ Registry │
└─────────────┘ └──────────────┘ └─────┬─────┘
│
Signal │
↓
┌─────────────┐ ┌──────────────┐ ┌───────────┐
│ Broker │←────│ Risk │←────│ Trading │
│ Paper/Live │ │ Manager │ │ Engine │
└─────────────┘ └──────────────┘ └─────┬─────┘
│
┌──────────────┐ ┌──────┴──────┐
│ Telegram │←────│ Portfolio │
│ Notifier │ │ Manager │
└──────────────┘ └──────┬──────┘
│
┌──────┴──────┐
│ Ledger │
│ (JSONL) │
└─────────────┘Broker 인터페이스
모든 브로커는 동일한 인터페이스를 구현합니다:
python
class Broker(ABC):
async def place_order(asset, action, quantity, price=None) -> str
async def cancel_order(order_id) -> bool
async def get_balance() -> dict[str, float]
async def get_positions() -> list[Position]
async def get_order_status(order_id) -> str| 구현체 | 용도 |
|---|---|
PaperTrader | 시뮬레이션 (가상 잔고, 즉시 체결) |
BinanceBroker | Binance Spot 실거래 (ccxt, market order) |
비동기 아키텍처
모든 I/O 작업은 asyncio로 비동기 처리됩니다:
- CryptoFeed: ccxt async로 OHLCV 및 가격 조회
- TradingEngine: 3개의 asyncio 루프가
gather로 병렬 실행 - TokenBucketRateLimiter: 비동기 속도 제한
- async_retry: tenacity 기반 자동 재시도
python
# TradingEngine의 3개 루프
asyncio.gather(
_strategy_loop(), # 전략 평가 + 주문 실행
_sl_tp_monitor_loop(), # SL/TP 자동 청산
_status_display_loop(), # 콘솔 상태 표시
)