The NIJA Live Execution + Backtesting Engine provides a comprehensive framework for:
bot/unified_backtest_engine.py)Comprehensive backtesting engine with:
bot/live_execution_tracker.py)Real-time trade tracking with:
nija_execution_cli.py)Unified command-line interface for:
Dependencies are already in requirements.txt:
pip install -r requirements.txt
# Run backtest on BTC-USD with historical data
python nija_execution_cli.py backtest \
--symbol BTC-USD \
--data data/BTC-USD_1h.csv \
--initial-balance 10000 \
--days 90 \
--output results/backtest_btc.json
Options:
--symbol: Trading symbol (e.g., BTC-USD, ETH-USD)--data: Path to historical OHLCV CSV file--initial-balance: Starting balance (default: $10,000)--commission: Commission rate (default: 0.001 = 0.1%)--slippage: Slippage rate (default: 0.0005 = 0.05%)--days: Number of days to backtest (from end of data)--output: Output file for results (JSON format)--strategy: Strategy to use (apex_v71, apex_v72, enhanced)# Monitor live trading execution
python nija_execution_cli.py live \
--balance 10000 \
--max-daily-loss 5.0 \
--max-drawdown 12.0 \
--export-csv
Options:
--balance: Current account balance--data-dir: Directory for storing tracking data (default: ./data/live_tracking)--max-daily-loss: Maximum daily loss % before circuit breaker (default: 5%)--max-drawdown: Maximum drawdown % for alerts (default: 12%)--export-csv: Export trades to CSV# Generate report from backtest and live data
python nija_execution_cli.py report \
--backtest results/backtest_btc.json \
--live data/live_tracking \
--format text
Options:
--backtest: Path to backtest results JSON--live: Path to live tracking data directory--output: Output file path--format: Report format (text, json, html)# Compare backtest vs live performance
python nija_execution_cli.py compare \
--backtest results/backtest_btc.json \
--live data/live_tracking
The engine tracks elite-tier performance metrics:
| Metric | Description | Elite Target |
|---|---|---|
| Profit Factor | Total wins รท Total losses | 2.0 - 2.6 |
| Win Rate | % of winning trades | 58% - 62% |
| Sharpe Ratio | Risk-adjusted returns | > 1.8 |
| Sortino Ratio | Downside risk-adjusted returns | > 1.8 |
| Max Drawdown | Maximum peak-to-trough decline | < 12% |
| Avg Win | Average winning trade | ~0.9% - 1.5% |
| Avg Loss | Average losing trade | -0.4% to -0.7% |
| Risk:Reward | Avg win รท Avg loss | 1:1.8 - 1:2.5 |
| Expectancy | Expected profit per trade | +$0.45 - $0.65 per $1 risked |
from bot.unified_backtest_engine import UnifiedBacktestEngine
from bot.nija_apex_strategy_v71 import NIJAApexStrategyV71
# Create backtest engine
engine = UnifiedBacktestEngine(
initial_balance=10000.0,
commission_pct=0.001,
slippage_pct=0.0005
)
# Initialize strategy
strategy = NIJAApexStrategyV71(broker_client=None)
# Run backtest loop (integrate with strategy signals)
# ... (see example in unified_backtest_engine.py)
# Calculate and print results
results = engine.calculate_metrics()
results.print_summary()
from bot.live_execution_tracker import LiveExecutionTracker
# Initialize tracker
tracker = LiveExecutionTracker(
initial_balance=10000.0,
max_daily_loss_pct=5.0,
max_drawdown_pct=12.0
)
# Record trade entry (when opening position)
tracker.record_entry(
trade_id="BTC-001",
symbol="BTC-USD",
side="long",
entry_price=50000.0,
size=0.1,
stop_loss=49000.0,
take_profit=52000.0,
commission=5.0
)
# Record trade exit (when closing position)
tracker.record_exit(
trade_id="BTC-001",
exit_price=51000.0,
exit_reason="take_profit",
commission=5.1
)
# Get performance snapshot
snapshot = tracker.get_performance_snapshot(current_balance=10090.0)
print(f"Win Rate: {snapshot.win_rate*100:.1f}%")
print(f"Total P&L: ${snapshot.realized_pnl_total:+.2f}")
# Step 1: Run backtest on historical data
python nija_execution_cli.py backtest \
--symbol BTC-USD \
--data data/BTC-USD_1h.csv \
--initial-balance 10000 \
--days 180 \
--output results/backtest_6months.json
# Step 2: Review backtest results
# Check if metrics meet elite targets (Profit Factor > 2.0, Win Rate 58-62%, etc.)
# Step 3: If backtest looks good, start live trading with small capital
# Monitor with live tracker
# Step 4: Compare backtest vs live after 30 days
python nija_execution_cli.py compare \
--backtest results/backtest_6months.json \
--live data/live_tracking
# Morning: Check current status
python nija_execution_cli.py live --balance <current_balance>
# Throughout day: Live tracker runs automatically (integrated with bot)
# End of day: Generate daily report
python nija_execution_cli.py report \
--live data/live_tracking \
--format text
# Generate monthly performance report
python nija_execution_cli.py report \
--backtest results/backtest.json \
--live data/live_tracking \
--format json \
--output reports/monthly_$(date +%Y%m).json
# Future: Convert to PDF/HTML for investor presentation
The live tracker includes automatic circuit breakers:
Example alert:
================================================================================
๐จ CIRCUIT BREAKER: Daily loss limit exceeded!
Daily P&L: $-520.00 (-5.2%)
Limit: -5.0%
ACTION REQUIRED: Stop trading for today
================================================================================
Before opening positions, the engine validates:
Real-time monitoring of:
CSV files should have these columns:
timestamp,open,high,low,close,volume
2024-01-01 00:00:00,50000.0,50500.0,49800.0,50200.0,100.5
2024-01-01 01:00:00,50200.0,50800.0,50100.0,50600.0,120.3
...
Alternative column names are supported:
time, date instead of timestampStored in data/live_tracking/ (configurable):
tracker_state.json: Current state with all tradestrades_YYYYMMDD.csv: Daily trade exportsbot/execution_engine.py):
tracker.record_entry() when opening positionstracker.record_exit() when closing positionsbot/broker_integration.py):
bot/trading_strategy.py):
bot/dashboard_server.py):
Issue: ModuleNotFoundError: No module named 'pandas'
# Solution: Install dependencies
pip install -r requirements.txt
Issue: FileNotFoundError: data/BTC-USD_1h.csv
# Solution: Ensure historical data file exists
# Download or generate OHLCV data for backtesting
Issue: Live tracker shows no trades
# Solution: Ensure tracker is integrated with execution engine
# Check that record_entry() and record_exit() are being called
Track these metrics to ensure the engine meets investor-grade standards:
โ Backtesting
โ Live Trading
โ Reporting
For issues or questions:
Version: 1.0 Last Updated: January 28, 2026 Author: NIJA Trading Systems