Nija

High Leverage Improvements Guide

Overview

This guide covers three critical improvements to NIJA that provide high-leverage value:

  1. CodeQL + Security Hardening - Automated security scanning and best practices
  2. 5-Year Multi-Regime Backtesting - Comprehensive historical validation
  3. 30-Day Live Paper Trading - Real-world strategy validation before deployment

1️⃣ CodeQL + Security Hardening

What It Does

Automated security scanning integrated into the CI/CD pipeline to detect vulnerabilities before they reach production.

Components

Setup

Security scanning is automatically enabled once the workflows are merged to main.

View Security Results:

GitHub Repository → Security Tab → Code scanning alerts

Manual Security Scan:

# Install security tools
pip install safety bandit

# Run dependency scan
safety check

# Run Bandit security linting
bandit -r . --exclude ./archive,./venv,./mobile,./frontend

# Generate reports
bandit -r . -f json -o bandit-report.json
safety check --json --output safety-report.json

Security Best Practices

See SECURITY_HARDENING_GUIDE.md for complete details:

Schedule

2️⃣ 5-Year Multi-Regime Backtesting

What It Does

Comprehensive backtesting across 5 years of historical data with analysis broken down by market regime (bull/bear/ranging/volatile).

Why It Matters

Usage

# Run 5-year backtest on BTC-USD
python run_5year_backtest.py \
  --symbol BTC-USD \
  --years 5 \
  --initial-balance 10000 \
  --strategy APEX_V71 \
  --output results/5year_backtest_btc.json

# Run on ETH-USD
python run_5year_backtest.py \
  --symbol ETH-USD \
  --years 5 \
  --output results/5year_backtest_eth.json

# Custom parameters
python run_5year_backtest.py \
  --symbol BTC-USD \
  --years 3 \
  --initial-balance 50000 \
  --commission 0.0006 \
  --slippage 0.0003 \
  --output results/custom_backtest.json

Output

The script generates a comprehensive JSON report with:

Overall Performance:

Regime Analysis: For each market regime (bull/bear/ranging/volatile):

Monte Carlo Simulation:

Statistical Significance:

Example Output

================================================================================
5-YEAR MULTI-REGIME BACKTEST SUMMARY
================================================================================

📊 BACKTEST DETAILS
Symbol: BTC-USD
Strategy: APEX_V71
Period: 2021-01-28 to 2026-01-28 (5 years)
Initial Balance: $10,000.00

💰 OVERALL PERFORMANCE
Final Balance: $45,230.00
Total Return: 352.30%
Total Trades: 1,247
Win Rate: 61.2%
Profit Factor: 2.35
Sharpe Ratio: 1.92
Max Drawdown: 8.45%

📈 REGIME ANALYSIS

BULL Market:
  Duration: 450 days
  Trades: 387
  Win Rate: 64.3%
  Return: 145.20%
  Profit Factor: 2.68
  Max DD: 5.20%

BEAR Market:
  Duration: 380 days
  Trades: 312
  Win Rate: 55.8%
  Return: 42.10%
  Profit Factor: 1.95
  Max DD: 8.45%

RANGING Market:
  Duration: 620 days
  Trades: 428
  Win Rate: 62.4%
  Return: 118.60%
  Profit Factor: 2.42
  Max DD: 6.30%

VOLATILE Market:
  Duration: 380 days
  Trades: 120
  Win Rate: 58.3%
  Return: 46.40%
  Profit Factor: 2.15
  Max DD: 7.80%

🎲 MONTE CARLO SIMULATION (1000 runs)
Expected Return: 352.30% ± 45.20%
95% Confidence: [275.40%, 428.60%]
Expected Sharpe: 1.92

📊 STATISTICAL SIGNIFICANCE
Sample Size: 1,247 trades
P-Value: 0.0001
Conclusion: Strategy shows statistically significant edge

================================================================================

Data Requirements

The script expects historical OHLCV data in data/ directory:

If data file not found, the script automatically generates synthetic data for demonstration.

To use real data:

  1. Download historical data from exchange or data provider
  2. Save as CSV in data/ directory
  3. Ensure proper column names and datetime format

3️⃣ 30-Day Live Paper Trading

What It Does

Live paper trading system that runs for 30 days to validate strategy performance in real market conditions without risking capital.

Why It Matters

Usage

Day 1-30: Record Daily Metrics

Run this once per day (set up as cron job):

# Record today's performance
python run_30day_paper_trading.py --record-daily

# Specify custom data directory
python run_30day_paper_trading.py --record-daily --data-dir data/paper_jan2026

Weekly Reports

# Generate week 1 report
python run_30day_paper_trading.py --weekly-report 1

# Generate week 2 report
python run_30day_paper_trading.py --weekly-report 2

# And so on...

Final 30-Day Report

# Generate comprehensive 30-day report
python run_30day_paper_trading.py --final-report

# Compare to backtest
python run_30day_paper_trading.py \
  --compare-backtest results/5year_backtest_btc.json \
  --final-report

Daily Output

============================================================
DAILY PAPER TRADING REPORT - 2026-01-28
============================================================

💰 BALANCE
Starting: $10,000.00
Ending:   $10,234.50
P&L:      +$234.50 (+2.35%)

📊 TRADING
Trades:   8
Wins:     5 (62.5%)
Losses:   3
Best:     +$125.30
Worst:    -$45.20

📍 POSITIONS
Open:     3
Exposure: $3,045.20 (29.7% of balance)
============================================================

Weekly Summary

📊 WEEK 1 REPORT
Period: 2026-01-21 to 2026-01-27
Return: +12.45%
Trades: 47
Win Rate: 59.6%
Profit Factor: 2.28
Sharpe: 1.85
Max DD: 3.20%

30-Day Final Report

================================================================================
30-DAY PAPER TRADING FINAL REPORT
================================================================================

📅 Period: 2026-01-01 to 2026-01-30
💰 Return: +48.50%
📊 Trades: 187
✅ Win Rate: 61.5%
📈 Sharpe: 1.94
📉 Max DD: 6.80%
================================================================================

Automated Monitoring

Set up daily cron job:

# Edit crontab
crontab -e

# Add daily execution at market close (5pm EST = 22:00 UTC)
0 22 * * * cd /path/to/Nija && python run_30day_paper_trading.py --record-daily >> logs/paper_trading.log 2>&1

# Weekly report on Sundays at 9am
0 9 * * 0 cd /path/to/Nija && python run_30day_paper_trading.py --weekly-report $(($(date +\%W) % 4 + 1)) >> logs/paper_weekly.log 2>&1

Performance Alerts

The system automatically generates alerts for:

Alerts are saved in data/paper_trading_30day/alerts.json and logged.

Comparison to Backtest

python run_30day_paper_trading.py \
  --compare-backtest results/5year_backtest_btc.json

Output:

================================================================================
PAPER vs BACKTEST COMPARISON
================================================================================

WIN_RATE:
  Backtest: 0.612
  Paper:    0.615
  Diff:     +0.003

SHARPE_RATIO:
  Backtest: 1.920
  Paper:    1.940
  Diff:     +0.020

MAX_DRAWDOWN:
  Backtest: 8.450
  Paper:    6.800
  Diff:     -1.650

✅ EXCELLENT - Paper trading matches backtest expectations
================================================================================

Integration Workflow

Week 1: Security Hardening

  1. Merge security workflows to main
  2. Review and fix any security alerts
  3. Implement security best practices
  4. Set up alert notifications

Week 2: Run 5-Year Backtests

  1. Download historical data for top trading pairs
  2. Run comprehensive backtests
  3. Analyze results by regime
  4. Document findings and expected performance

Week 3-6: 30-Day Paper Trading

  1. Start paper trading with live bot
  2. Record daily metrics (automated via cron)
  3. Generate weekly reports
  4. Compare to backtest expectations
  5. Adjust strategy if needed

Week 7: Go Live Decision

  1. Review 30-day paper trading results
  2. Compare vs backtest
  3. Assess risk metrics
  4. Make go/no-go decision
  5. If go: Start with small capital
  6. If no-go: Refine strategy and repeat

Metrics to Track

Security Metrics

Backtest Metrics

Paper Trading Metrics

Success Criteria

Security

Backtesting

Paper Trading

Troubleshooting

Security Scans Failing

Backtest Issues

Paper Trading Issues

Next Steps After Validation

Once all three components are validated:

  1. Scale Testing
    • Run backtests on additional symbols
    • Extend paper trading to more trading pairs
    • Test with larger position sizes
  2. Live Deployment
    • Start with minimum capital
    • Monitor closely for first week
    • Gradually increase capital as confidence builds
    • Implement circuit breakers
  3. Continuous Improvement
    • Keep security scans running
    • Quarterly backtest updates
    • Monthly paper trading validation
    • Regular performance reviews

Resources

Support

For issues or questions:

  1. Check relevant documentation files
  2. Review error logs
  3. Create GitHub issue with details
  4. Tag with appropriate label (security/backtest/paper-trading)