How dantelrharrell-debug/nija works
1. What this repo is
- A Python-based multi-broker trading platform with a large bot/ runtime, supporting live trading, paper trading, backtesting, education/demo mode, dashboards, and mobile/backend integrations.
- The codebase is organized as a trading system plus an app/platform shell, not just a single strategy engine.
2. Primary runtime entry points
- The main execution surfaces are main.py, bot/bot_main.py, run_live.py, run_paper_mode.sh, run_dry_run.sh, api_server.py, gateway.py, fastapi_backend.py, and web_server.py.
- These launch different slices of the system: trading runtime, API layer, gateway/API aggregation, and browser-facing UI.
3. Core trading brain
- The core decision logic lives in bot/, core/, and execution/.
- bot/control/, core/strategy_orchestrator.py, core/nija_brain.py, bot/strategy_manager.py, and the strategy modules in bot/strategies/ form the signal-generation and orchestration layer.
- Strategies include momentum, mean reversion, liquidity reversal, apex/NIJA variants, and AI/meta-learning components.
4. Market data → signal → trade flow
- Market inputs come through the market/regime/liquidity stack (bot/market_data_engine.py, bot/market_regime_*, bot/liquidity_*, bot/indicators*).
- Signals are filtered and scored by layers such as bot/smart_filters.py, bot/ai_trade_quality_filter.py, bot/enhanced_entry_scoring.py, and bot/trade_quality_gate.py.
- Approved trades then move into the execution pipeline, where routing, sizing, and broker selection are resolved.
5. Execution and broker boundary
- Execution is split into abstractions and venue-specific adapters: bot/execution_*, bot/execution_router.py, bot/execution_pipeline.py, bot/broker_*, bot/live_broker_adapters.py, bot/broker_adapters.py, and execution/broker_adapter.py.
- Supported broker/venue configs include Kraken, Coinbase, Binance, Alpaca, and OKX under bot/broker_configs/ and config/brokers/.
- The repo has many “patch” modules that harden or repair execution paths, indicating a defensive execution layer with runtime hooks and fail-closed behavior.
6. Risk, safety, and kill-switch architecture
- Risk is not a single module; it is a layered boundary spanning bot/risk/, bot/risk_engine.py, bot/global_risk_engine.py, bot/risk_management.py, bot/pre_trade_risk_engine.py, and many guardrails.
- Safety controls include drawdown protection, position caps, volatility filters, exchange kill switches, emergency liquidation/reset, symbol freezing, and broker/venue cash guards.
- The README’s live-trading rules are enforced by this stack: live mode is gated by consent, broker readiness, balance verification, and audit logging.
7. Capital, portfolio, and account isolation
- Capital allocation and scaling are handled by bot/capital/, bot/capital_*, bot/portfolio_*, bot/position_*, and tier-aware modules like bot/tier_config.py and bot/tier_aware_position_manager.py.
- There is explicit support for multi-account and user isolation: bot/account_isolation_manager.py, bot/user_*, bot/multi_account_*, and config/users/*.
- This means capital is treated as a routed resource with hierarchy, reservation, and per-user/per-account boundaries.
8. Persistence and runtime state
- Persistent state is split across PostgreSQL-style schema migration support (alembic/, database/models.py), Redis/cache (cache/redis_client.py, bot/redis_runtime.py), and file-based operational state in data/.
- The data/ directory stores audit logs, nonce state, profit confirmations, risk analyses, trade attribution, and recovery snapshots.
- This mix suggests a system that keeps both durable business records and fast runtime coordination state.
9. APIs, dashboards, and user-facing surfaces
- The platform exposes multiple API and UI surfaces: api/, api_server.py, api_gateway.py, mobile_api.py, realtime_dashboard_api.py, control_center_api.py, central_dashboard_api.py, and numerous dashboard modules in bot/.
- Browser UI lives in frontend/, bot/templates/, and standalone HTML dashboards such as owner_dashboard.html and NIJA_OPERATOR_DASHBOARD.html.
- The dashboard flow includes trade panels, execution previews, KPI/performance views, and operational control surfaces.
10. Mobile app boundary
- The mobile app is designed as an education-first client, with demo mode default and live mode locked behind consent, broker readiness, and server-side secret handling.
- Mobile assets and wrappers live under mobile/, mobile_backend_server.py, unified_mobile_api.py, and frontend/static/js/ for app-store-style UI logic.
- The mobile frontend is explicitly not the broker boundary; it calls secure backend APIs only.
11. Ops, deployment, and environment model
- Deployment targets include Docker, Compose, Kubernetes (k8s/), Render (render.yaml), Railway (railway.json), and systemd/supervisor configs under deploy/ and deployment/.
- Environment is configuration-heavy: .env.*, config/environment.py, feature flags, broker configs, and user YAML/JSON definitions drive runtime behavior.
- CI/security automation is extensive via GitHub Actions, pre-commit hooks, secret scanning, CodeQL, and threat-modeling workflows.
12. Observability and auditability
- Monitoring is a first-class subsystem: monitoring/ contains Grafana/Prometheus assets, while runtime modules emit logs, audits, and performance reports.
- The system tracks execution journal entries, trade ledger data, profit confirmations, health checks, and compliance logs.
- Architecture-wise, this supports post-trade reconciliation, operator visibility, and safety verification rather than only live execution.
13. How to draw the main boundaries
- A good diagram should separate: external brokers/venues, the execution/risk core, the capital/account layer, the API/dashboard/mobile layer, and persistence/observability.
- The most important cross-cutting constraint is that every trade must pass through readiness, risk, authority, and venue-validation gates before reaching a broker.
- The mobile app and dashboards are control/visibility surfaces; they do not directly own broker secrets or execution authority.
Main entry points: bot_main.py Bot runtime · api_server.py API server · control_compiler.py Control pipe · apex_strategy.py Strategies
Components 23
Runtimes
Process entrypoints and mode launchers.
- Main · Top-level bootstrap and mode selection.
main.py - Bot runtime · Primary trading process orchestrator.
bot/bot_main.py
Decision Core
Strategy orchestration, signal generation, and market interpretation.
- Orchestrator · Coordinates strategy selection and trade lifecycle.
core/strategy_orchestrator.py - NIJA brain · Higher-level trading intelligence and synthesis.
core/nija_brain.py - Control pipe · Compiles control, regime, and signal flow.
bot/control/control_compiler.py - Market data · Market, regime, and liquidity input layer.
bot/market_data_engine.py - Trade gate · Entry scoring and trade quality gating.
bot/trade_quality_gate.py - Strategies · Representative strategy implementations and variants.
bot/strategies/apex_strategy.py
Capital & Safety
Capital routing, account boundaries, and layered risk controls.
- Capital flow · Capital allocation, reservation, and scaling.
bot/capital_orchestration_engine.py - Account isolation · Separates users, accounts, and authority scopes.
bot/account_isolation_manager.py - Global risk · Portfolio-level risk, drawdown, and kill-switch logic.
bot/global_risk_engine.py - Risk engine · Pre-trade and per-trade risk evaluation layer.
bot/risk_engine.py
Execution
Order compilation, routing, and broker/venue adapters.
- Exec pipeline · Validates, compiles, and submits orders.
bot/execution_pipeline.py - Exec router · Selects venue, route, and execution path.
bot/execution_router.py - Broker adapter · Abstract broker boundary used by execution.
execution/broker_adapter.py
Interfaces
API, dashboard, web, and mobile control surfaces.
- API server · Primary backend API entrypoint.
api_server.py - Gateway · Fronts and aggregates platform APIs.
gateway.py - Mobile backend · Secure backend for the mobile client.
mobile_backend_server.py - Web server · Browser-facing UI host.
web_server.py
State & Storage
Durable records and fast runtime coordination state.
- Trade ledger · Trade journals, ledgers, and audit records.
bot/trade_ledger_db.py - Redis · Fast coordination and shared runtime state.
cache/redis_client.py - DB models · Durable business records and migration-backed schema.
database/models.py
External Venues
Broker and venue boundary.
- Broker configs · Broker-specific parameters and constraints.
bot/broker_configs/default_config.py
Connections 23
- Main → Bot runtime: starts · Bootstraps the trading runtime and mode handlers.
- Bot runtime → Orchestrator: drives · Hands off runtime cycles to the strategy orchestrator.
- Orchestrator → Control pipe: compiles · Builds the control and signal flow.
- Control pipe → Market data: ingests · Consumes market, regime, and liquidity inputs.
- Control pipe → Strategies: selects · Routes decisions into representative strategy modules.
- Strategies → NIJA brain: coordinates · Shares higher-level intelligence and strategy synthesis.
- Strategies → Trade gate: filters · Scores and gates candidate trades.
- Trade gate → Risk engine: vetoes · Fails closed before orders reach execution.
- Risk engine → Global risk: escalates · Promotes local checks into portfolio-level safety.
- Global risk → Capital flow: allocates · Constrains and routes capital through hierarchy.
- Capital flow → Account isolation: scopes · Applies per-user and per-account boundaries.
- Risk engine → Exec pipeline: authorizes · Only approved trades may enter execution.
- Exec pipeline → Exec router: routes · Chooses venue and order path.
- Exec router → Broker adapter: adapts · Uses broker abstraction for venue-specific calls.
- Broker adapter → Broker configs: constrains · Applies broker and venue configuration limits.
- Exec pipeline → Trade ledger: records · Persists order and trade lifecycle events.
- Bot runtime → Redis: coordinates · Shares runtime state, locks, and caches.
- Bot runtime → DB models: persists · Writes durable platform state and records.
- API server → Bot runtime: controls · Operator/API actions influence the trading runtime.
- Gateway → API server: fronts · Aggregates backend API capabilities.
- Mobile backend → API server: calls · Mobile client uses secure backend APIs.
- Web server → API server: renders · Browser UI surfaces platform state and controls.
- Web server → Trade ledger: views · Displays execution and performance history.