Algo Trade Bot'un mimari yapısı, modül bağımlılıkları ve veri akışı.
┌─────────────────────────────────────────────────────────────────────────────┐
│ ALGO TRADE BOT │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ DATA │───→│ MODELS │───→│ STRATEGY │───→│ BROKER │ │
│ │ PIPELINE │ │ PIPELINE │ │ ENGINE │ │ ADAPTER │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │ │
│ ↓ ↓ ↓ ↓ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ data/raw/ │ │ artifacts/ │ │ backtest/ │ │ Alpaca │ │
│ │ (parquet) │ │ registry/ │ │ live/ │ │ Paper │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
common/
│
┌──────────────────┼──────────────────┐
│ │ │
↓ ↓ ↓
interfaces/ data/ storage
│ │
┌──────────┼──────────┐ │
│ │ │ │
↓ ↓ ↓ ↓
broker/ models/ strategy/ research/
│ │ │ │
│ │ │ │
│ ↓ ↓ │
│ backtest/ ←───┘ │
│ │ │
│ ↓ │
└────→ live/ ←────────────────┘
│
↓
bot/ (legacy)
Temel altyapı katmanı. Tüm modüller bu katmana bağımlıdır.
common/
├── config.py # Pydantic Settings - merkezi konfigürasyon
├── logger.py # Loguru wrapper - structured logging
├── storage.py # Parquet/CSV I/O
├── time.py # Timestamp utilities
├── types.py # Type aliases
└── secrets.py # Credential management
interfaces/
├── broker.py # BaseBroker ABC
├── model.py # ModelInterface ABC
└── strategy.py # StrategyInterface ABC
Veri ingestion ve preprocessing katmanı.
data/
├── ingest.py # Binance OHLCV fetcher
├── coingecko.py # CoinGecko alternative
├── features.py # 37 technical features
└── labels.py # Target label generation
Flow:
Binance API → Raw OHLCV → Features (37) → Labels (7)
Makine öğrenmesi modeli eğitim ve yönetim katmanı.
models/
├── sklearn_baseline.py # LogisticRegression pipeline
└── calibration.py # Platt/Isotonic calibration
research/
├── dataset.py # ML table builder
├── splits.py # Walk-forward CV
├── train.py # Training orchestrator
├── evaluate.py # Metrics computation
└── registry.py # Model versioning
Flow:
Features + Labels → Dataset → Walk-Forward Split → Train → Calibrate → Registry
Trading execution katmanı.
strategy/
├── signal_to_trade.py # Probability → Signal
├── position_sizing.py # Share calculation
└── risk.py # Risk management
backtest/
├── engine.py # Event-driven engine
├── broker_sim.py # Simulated broker
├── costs.py # Transaction costs
├── execution.py # Fill timing
├── metrics.py # Performance metrics
└── report.py # Report generation
broker/
├── alpaca_broker.py # Basic implementation
├── alpaca_paper.py # Production broker
└── auth.py # Authentication
Canlı trading execution katmanı.
live/
├── runner.py # Main orchestrator
├── decision.py # Decision engine
├── state.py # State dataclasses
├── state_store.py # Persistence
├── scheduler.py # APScheduler
├── alerts.py # Notification system
├── monitoring.py # Logging
└── reconciliation.py # Broker sync
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Binance │────→│ ingest.py │────→│ btc_1h. │
│ API │ │ │ │ parquet │
└──────────────┘ └──────────────┘ └──────────────┘
│
┌──────────────┐ │
│ features.py │←──────────┘
│ │
└──────────────┘
│
↓
┌──────────────┐
│ labels.py │
│ │
└──────────────┘
│
↓
┌──────────────┐
│ dataset.py │
│ (X, y, meta)│
└──────────────┘
│
↓
┌──────────────┐
│ splits.py │
│ walk-forward │
└──────────────┘
│
↓
┌──────────────┐ ┌──────────────┐
│ train.py │────→│ registry/ │
│ │ │ artifacts │
└──────────────┘ └──────────────┘
┌──────────────┐ ┌──────────────┐
│ registry/ │────→│ Model + │
│ model.joblib│ │ Calibrator │
└──────────────┘ └──────────────┘
│
┌──────────────┐ │
│ btc_1h. │ │
│ parquet │ │
└──────────────┘ │
│ │
↓ ↓
┌─────────────────────────────────────┐
│ engine.py │
│ │
│ for each bar: │
│ 1. Compute features (past only) │
│ 2. Model predict │
│ 3. Generate signal │
│ 4. Risk check │
│ 5. Execute pending order │
│ 6. Create new order (for t+1) │
│ │
└─────────────────────────────────────┘
│
↓
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ equity_curve │ │ trades. │ │ report. │
│ .csv │ │ csv │ │ md/json │
└──────────────┘ └──────────────┘ └──────────────┘
┌─────────────────────────────────────────────────────────────┐
│ LiveRunner │
│ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ Scheduler │ │
│ │ (APScheduler) │ │
│ │ │ │
│ │ Every hour at :01 (after bar close + delay) │ │
│ └────────────────────────────────────────────────────────┘ │
│ │ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ Decision Engine │ │
│ │ │ │
│ │ 1. broker.get_bars() → Latest OHLCV │ │
│ │ 2. build_features() → Feature vector │ │
│ │ 3. model.predict_proba() → Raw probability │ │
│ │ 4. calibrator.transform() → Calibrated prob │ │
│ │ 5. probability_to_signal() → Signal │ │
│ │ │ │
│ └────────────────────────────────────────────────────────┘ │
│ │ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ Risk Manager │ │
│ │ │ │
│ │ • Max drawdown check (10%) │ │
│ │ • Position size check (20%) │ │
│ │ • Cooldown check │ │
│ │ │ │
│ └────────────────────────────────────────────────────────┘ │
│ │ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ Order Execution │ │
│ │ │ │
│ │ broker.place_order(symbol, qty, side) │ │
│ │ │ │
│ └────────────────────────────────────────────────────────┘ │
│ │ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ State Store & Logging │ │
│ │ │ │
│ │ • state.json (positions, equity) │ │
│ │ • decisions.jsonl (audit trail) │ │
│ │ • alerts.log (notifications) │ │
│ │ │ │
│ └────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Security Layers │
├─────────────────────────────────────────────────────────────┤
│ │
│ Layer 1: Credential Management │
│ ├── Environment variables (not in code) │
│ ├── .env file (gitignored) │
│ └── Credential masking in logs │
│ │
│ Layer 2: Paper Mode Enforcement │
│ ├── URL validation (must contain "paper") │
│ ├── Hard-coded check in auth.py │
│ └── Exception on live URL attempt │
│ │
│ Layer 3: Risk Limits │
│ ├── Max position size (20% equity) │
│ ├── Max drawdown kill switch (10%) │
│ └── Cooldown periods │
│ │
│ Layer 4: State Integrity │
│ ├── Atomic save operations │
│ ├── Reconciliation with broker │
│ └── File locking (single instance) │
│ │
└─────────────────────────────────────────────────────────────┘
artifacts/
├── registry/ # Model versiyonlama
│ └── sklearn_baseline/
│ └── {YYYYMMDD_HHMMSS}/
│ ├── model.joblib # Trained model
│ ├── calibrator.joblib # Fitted calibrator
│ ├── config.json # Training config
│ ├── metrics.json # Evaluation metrics
│ └── feature_list.json # Feature names
│
├── backtest/ # Backtest outputs
│ ├── equity_curve.csv # Time series
│ ├── trades.csv # Trade log
│ ├── report.json # Metrics (programmatic)
│ └── report.md # Metrics (readable)
│
├── reports/ # Training metrics
│ ├── metrics.json
│ └── metrics.md
│
└── live/ # Live trading state
├── state.json # Current state
├── decisions.jsonl # Decision audit
├── alerts.log # Alert history
└── runner.lock # Instance lock
Environment Variables (highest priority)
↓
.env file
↓
config.py defaults (lowest priority)
Örnek:
ALGO_TRADER_ALPACA_API_KEY → Settings.ALPACA_API_KEY
ALGO_TRADER_DATA_DIR → Settings.DATA_DIR
┌─────────────────────────────────────────────────────────────┐
│ Monitoring Stack │
├─────────────────────────────────────────────────────────────┤
│ │
│ Logging: │
│ ├── Loguru (structured, colored) │
│ ├── Level: DEBUG → INFO → WARNING → ERROR → CRITICAL │
│ └── Output: Console + File │
│ │
│ Decision Tracking: │
│ ├── decisions.jsonl (append-only) │
│ ├── Fields: timestamp, signal, prob, action, order_id │
│ └── Retention: Indefinite (audit trail) │
│ │
│ Alerting: │
│ ├── Console (always) │
│ ├── File (alerts.log) │
│ └── Telegram (optional, configurable) │
│ │
│ Metrics (per backtest/live session): │
│ ├── Total Return, CAGR │
│ ├── Sharpe, Sortino, Calmar ratios │
│ ├── Max Drawdown │
│ ├── Win Rate, Profit Factor │
│ └── Trade statistics │
│ │
└─────────────────────────────────────────────────────────────┘