Skip to content

Repository files navigation

AI Trading Bridge for NinjaTrader 8

Phase 3 LLM-Enhanced Model Integration

This Python program connects NinjaTrader 8 to your trained Stable-Baselines3 Phase 3 model with LLM-enhanced features for automated trading on E-mini Nasdaq-100 (NQ).


Architecture

[NinjaTrader 8] <--TCP:8888--> [Python Bridge] <---> [Phase 3 Model + Adapter]
   AIBridgeV2.cs                 ai_trading_bridge.py   NQ_ts-0040960...zip

Data Flow (Phase 3):

  1. NinjaTrader sends real-time bar data (OHLCV + indicators)
  2. Python builds 261D observation:
    • 220D market features (20 bars Γ— 11 indicators)
    • 8D position state (includes validity flags)
    • 33D LLM features (extended market context)
  3. VecNormalize applies normalization (trained on 40,960 timesteps)
  4. Adapter layer reduces 261D β†’ 228D
  5. Policy network predicts action (0-5)
  6. Python sends action to NinjaTrader
  7. NinjaTrader executes the action

Files Created

models/
β”œβ”€β”€ ai_trading_bridge.py        # Main program
β”œβ”€β”€ config.py                   # Configuration settings (Phase 3)
β”œβ”€β”€ tcp_client.py               # TCP communication
β”œβ”€β”€ observation_builder.py      # Observation construction (261D)
β”œβ”€β”€ model_manager.py            # Model loading & inference (with adapter)
β”œβ”€β”€ llm_features.py             # LLM feature builder (33D features)
β”œβ”€β”€ trade_logger.py             # Performance tracking
β”œβ”€β”€ requirements.txt            # Dependencies
β”œβ”€β”€ README.md                   # This file
β”‚
β”œβ”€β”€ model/                                                          # Phase 3 model directory
β”‚   β”œβ”€β”€ NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42.zip           # βœ… Trained model
β”‚   β”œβ”€β”€ NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42_vecnormalize.pkl  # βœ… VecNormalize
β”‚   └── NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42_metadata.json     # Metadata
β”‚
└── logs/                       # Auto-created
    β”œβ”€β”€ trading_log.txt         # Session logs
    β”œβ”€β”€ actions.csv             # All actions taken
    └── performance.txt         # Statistics

Installation

1. Install Python Dependencies

cd "/mnt/c/Users/javlo/Documents/NinjaTrader 8/bin/Custom/models"
pip install -r requirements.txt

Dependencies:

  • stable-baselines3>=2.0.0 - RL framework
  • sb3-contrib>=2.0.0 - Advanced algorithms (MaskablePPO)
  • numpy>=1.21.0 - Numerical computing
  • gym>=0.21.0 - Environment interface

2. Prepare Required Files (Phase 3)

βœ… Model File (Already Present)

  • File: model/NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42.zip
  • Size: 2.5 MB
  • Type: MaskablePPO with adapter layer (261D β†’ 228D)
  • Trained: 40,960 timesteps
  • Performance: +$216.697 validation, Sharpe 0.48

βœ… VecNormalize Stats File (FOUND!)

  • File: model/NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42_vecnormalize.pkl
  • Size: 28.6 KB
  • Shape: (261,) - Includes LLM features
  • Trained: 40,960 timesteps (Phase 3)

These files are CRITICAL for model operation and are already present!


3. Validate Configuration

Test that everything is configured correctly:

python config.py

Expected output (Phase 3):

======================================================================
 AI Trading Bridge - Configuration Validation
======================================================================
βœ… Configuration valid!

πŸ“ Model path: .../model/NQ_ts-0040960_evt-best_val-+216.697_sharpe-+0.48_seed-42.zip
πŸ“ VecNormalize path: .../model/NQ_ts-0040960..._vecnormalize.pkl
πŸ“ Log directory: .../logs

πŸ”’ Observation shape: (261,)
   - Market features: 220 (20 bars Γ— 11 features)
   - Position features: 8
   - LLM features: 33
   - Base (Phase 2): 228, Extended (Phase 3): 261

🌐 TCP: localhost:8888
πŸ“Š Model type: MaskablePPO
======================================================================

If you see errors:

  • Missing model file β†’ Ensure Phase 3 model exists in model/ subdirectory
  • Missing VecNormalize file β†’ This is CRITICAL (must match model)

Testing

Test 1: TCP Connection

Test TCP connection to NinjaTrader:

python tcp_client.py

Before running:

  1. Start NinjaTrader 8
  2. Load NQ 1-minute chart
  3. Add AIBridgeV2 strategy to chart
  4. Enable strategy (green arrow)

Expected output:

πŸ”Œ Connecting to NinjaTrader @ localhost:8888...
βœ… Connected to NinjaTrader successfully!

Waiting for messages (Ctrl+C to stop)...

πŸ“¨ Received: bar
   Close: 21450.25
   ATR: 45.5

πŸ“€ Sending test action: HOLD (0)

If connection fails:

  • Check NinjaTrader Output Window for "TCP Server started"
  • Ensure AIBridgeV2 strategy is enabled
  • Check no firewall blocking port 8888

Test 2: Observation Builder

Test observation construction:

python observation_builder.py

Expected output (Phase 3):

1. Adding first bar...
   Window size: 1/20
⚠️  LLM feature calculation failed (using base obs only): single positional indexer is out-of-bounds
βœ… Observation shape: (261,)
   First 11 features (bar 1): [21450.25  1234.  21448. ...]
   Position features (8): [0. 1. 0. 0. 0. 1. 0. 0.]
   LLM features (33): shape=(33,), sample=[0. 0. 0. 0. 0.]

2. Adding 19 more bars...
   Window size: 20/20
βœ… Full observation shape: (261,)
   Market features (220): min=0.00, max=21470.00
   Position features (8): [0. 1. 0. 0. 0. 1. 0. 0.]
   LLM features (33): min=0.00, max=0.00

Note: LLM feature warnings are normal during warmup (needs 60+ bars for multi-timeframe calculations).


Test 3: Model Loading

Test model loading and predictions:

python model_manager.py

Expected output (Phase 3):

======================================================================
 Loading Phase 3 AI Model
======================================================================

πŸ“ Loading model: .../model/NQ_ts-0040960...zip
   ⚠️  MaskablePPO detected - trying standard loader first
   ⚠️  Standard loading failed (expected for Phase 3)
   Trying custom loader (handles adapter architecture)...
   πŸ”§ Using custom loader for pickle compatibility...
   Detecting network architecture from weights...
   Architecture: pi=[512, 256, 128], vf=[512, 256, 128]
   πŸ“ Adapter detected: torch.Size([228, 261])
   πŸ“ Phase 3 model - extracting adapter (261D β†’ 228D)
   βœ… Policy weights loaded (228D input)
   βœ… Adapter extracted: torch.Size([228, 261])
βœ… Adapter layer created in ModelManager
βœ… Model loaded with custom loader: MaskablePPO

πŸ“ Loading VecNormalize: .../model/NQ_ts-0040960..._vecnormalize.pkl
βœ… VecNormalize loaded
   Running mean shape: (261,)
   Running var shape: (261,)

βœ… Model ready for predictions!

2. Testing prediction with dummy observation...
   Dummy observation shape: (261,)
βœ… Prediction successful!
   Action: 4 (ENABLE_TRAIL)

If model loading fails:

  • Check Phase 3 model file exists in model/ subdirectory
  • Check VecNormalize file matches model (261D)
  • Adapter architecture is required for Phase 3 (automatic)

Test 4: Trade Logger

Test logging functionality:

python trade_logger.py

Expected output:

1. Logging sample bars and actions...
[2025-11-07T10:30:00.000Z] πŸ“Š Bar #1 | Close=21450.00 | Vol=1200 | ATR=45.00
[2025-11-07T10:30:00.000Z] 🎯 Action 1: BUY

... (more bars) ...

─────────────────────────────────────────────────────────────────
 Performance Summary (Live)
─────────────────────────────────────────────────────────────────
 Runtime: 0s
 Bars Processed: 15
 Actions Sent: 15

 Action Distribution:
   βœ… HOLD            (0):   12 ( 80.0%) [Expected: 86.5%]
   βœ… BUY             (1):    1 (  6.7%) [Expected: 5.0%]
   ...

Running the Live System

Prerequisites Checklist

Before running live trading:

  • βœ… Python dependencies installed
  • βœ… NQ_o1.zip model file present
  • βœ… phase2_position_mgmt_final_vecnorm.pkl file present βœ…
  • βœ… All tests passed (tcp, observation, model, logger)
  • βœ… NinjaTrader 8 running
  • βœ… NQ 1-minute chart loaded
  • βœ… AIBridgeV2 strategy compiled (F5)
  • βœ… AIBridgeV2 strategy enabled on chart
  • βœ… Using Sim101 account (NOT live account!)

Step 1: Start NinjaTrader

  1. Open NinjaTrader 8
  2. Connect to data feed
  3. Load chart: Instruments β†’ NQ 09-25 β†’ 1 Minute
  4. Add strategy: Strategies β†’ AIBridgeV2
  5. Enable strategy (green arrow at top)
  6. Verify in Output Window:
    ═══════════════════════════════════════════════════════════════
      AIBridgeV2 Starting... (Phase 2 Model Support)
      Integer Actions: 0-5 | ATR-based SL/TP | Trailing Stops
    ═══════════════════════════════════════════════════════════════
    βœ“ TCP Server started - Waiting for Python client connection...
    

Step 2: Run Python Bridge

Open a terminal and run:

cd "/mnt/c/Users/javlo/Documents/NinjaTrader 8/bin/Custom/models"
python ai_trading_bridge.py

Expected startup sequence:

======================================================================
 AI Trading Bridge - Phase 2 Model
 Initializing...
======================================================================

1. Validating configuration...
βœ… Configuration valid

2. Initializing components...
βœ… Observation builder ready
βœ… Model manager ready
βœ… Logger ready
βœ… TCP client ready

3. Loading AI model...

======================================================================
 Loading Phase 2 AI Model
======================================================================
βœ… Model loaded: MaskablePPO
βœ… VecNormalize loaded

4. Connecting to NinjaTrader...
πŸ”Œ Connecting to NinjaTrader @ localhost:8888...
βœ… Connected to NinjaTrader successfully!

======================================================================
βœ… All systems ready!
======================================================================

======================================================================
 AI Trading Bridge - RUNNING
 Press Ctrl+C to stop
======================================================================

πŸ“Š NinjaTrader Configuration:
   Symbol: NQ 09-25
   Tick Size: 0.25
   Max Position: 10

Step 3: Monitor Live Trading

NinjaTrader Output:

[2025-11-07 10:30:00] πŸ“Š Bar data sent: Close=$21450.25, ...
βœ… Action sent to NinjaTrader
[2025-11-07 10:30:00] πŸ“¨ Received Phase 2 action: 0
[2025-11-07 10:30:00] Action 0: HOLD - No action taken

Python Console Output:

[2025-11-07T10:30:00.000Z] πŸ“Š Bar #1 | Close=21450.25 | Vol=1234 | ATR=45.5
[2025-11-07T10:30:00.000Z] 🎯 Action 0: HOLD
πŸ“€ Sending: HOLD (action=0)

[2025-11-07T10:31:00.000Z] πŸ“Š Bar #2 | Close=21452.75 | Vol=1456 | ATR=45.2
   ⏳ Building window: 2/20 bars

[2025-11-07T10:49:00.000Z] πŸ“Š Bar #20 | Close=21478.50 | Vol=1889 | ATR=46.1
[2025-11-07T10:49:00.000Z] 🎯 Action 1: BUY
πŸ“€ Sending: BUY (action=1)

When a trade executes:

[2025-11-07T10:49:00.000Z] 🎯 Action 1: BUY
   βœ… Order filled: AI_LONG @ 21478.50 x 1
   πŸ“ Position: LONG @ 21478.50

[2025-11-07T10:52:00.000Z] 🎯 Action 3: MOVE_TO_BE
   βœ… MOVE_TO_BE executed | New SL: 21478.75 | PnL: $600.00 | Count: 1

[2025-11-07T10:55:00.000Z] 🎯 Action 4: ENABLE_TRAIL
   βœ… ENABLE_TRAIL activated | SL: 21533.00 | Profit: $1,390.00 (1.05R)

[2025-11-07T10:56:00.000Z] πŸ“Š Bar #27 | Close=21548.25
   ↑ Trail SL updated: 21548.25 | Profit: $1,695.00

Step 4: Monitor Performance

Every 10 bars, statistics are printed:

─────────────────────────────────────────────────────────────────
 Performance Summary (Live)
─────────────────────────────────────────────────────────────────
 Runtime: 23m 45s
 Bars Processed: 142
 Actions Sent: 142

 Action Distribution:
   βœ… HOLD            (0):  122 ( 85.9%) [Expected: 86.5%]
   βœ… BUY             (1):    7 (  4.9%) [Expected: 5.0%]
   βœ… SELL            (2):    6 (  4.2%) [Expected: 5.0%]
   βœ… MOVE_TO_BE      (3):    4 (  2.8%) [Expected: 2.0%]
   βœ… ENABLE_TRAIL    (4):    2 (  1.4%) [Expected: 1.0%]
   βœ… DISABLE_TRAIL   (5):    1 (  0.7%) [Expected: 0.5%]

 Position Management Usage: 4.9% [Target: ~13.5%]
─────────────────────────────────────────────────────────────────

Indicators:

  • βœ… Green checkmark: Distribution within 5% of expected
  • ⚠️ Yellow warning: Distribution 5-10% off expected
  • ❌ Red X: Distribution >10% off expected

Step 5: Stop Trading

Graceful Shutdown:

  1. Press Ctrl+C in Python terminal
  2. Python will:
    • Print final statistics
    • Disconnect from NinjaTrader cleanly
    • Close log files
    • Exit

Final Output:

⚠️  Shutdown signal received...

======================================================================
 Final Session Statistics
======================================================================
 Session Duration: 1h 23m 45s
 Total Bars: 142
 Total Actions: 142

 Action Distribution:
   HOLD            (0):  122 ( 85.9%)
   BUY             (1):    7 (  4.9%)
   SELL            (2):    6 (  4.2%)
   MOVE_TO_BE      (3):    4 (  2.8%)
   ENABLE_TRAIL    (4):    2 (  1.4%)
   DISABLE_TRAIL   (5):    1 (  0.7%)

 Position Management Usage: 4.9%
 Expected PM Usage: 13.5%
 ⚠️  PM usage outside expected range

======================================================================
βœ… Shutdown complete
======================================================================

Log Files

All logs are saved in models/logs/:

trading_log.txt

Complete session log with timestamps:

======================================================================
 AI Trading Bridge Session Started
 Time: 2025-11-07 10:30:00
======================================================================

2025-11-07T10:30:00 | Action 0: HOLD
2025-11-07T10:49:00 | Action 1: BUY
2025-11-07T10:52:00 | Action 3: MOVE_TO_BE
...

actions.csv

Structured action log (importable to Excel):

timestamp,bar_index,action,action_name,close_price,atr,position
2025-11-07T10:30:00,1,0,HOLD,21450.25,45.5,0
2025-11-07T10:49:00,20,1,BUY,21478.50,46.1,0
2025-11-07T10:52:00,23,3,MOVE_TO_BE,21485.00,45.8,1

performance.txt

Session summaries and statistics


Troubleshooting

Problem: "Model file not found"

Solution: Ensure NQ_o1.zip exists in models/ directory


Problem: "VecNormalize file not found"

Solution: This is CRITICAL. You must:

  1. Re-run your training script
  2. VecNormalize stats already present: phase2_position_mgmt_final_vecnorm.pkl
  3. Copy file to models/ directory

Without this file, model predictions will be incorrect!


Problem: "Connection refused"

Solution:

  1. Ensure NinjaTrader is running
  2. Ensure AIBridgeV2 strategy is enabled on chart
  3. Check Output Window for "TCP Server started"
  4. Try reloading the strategy

Problem: "Invalid observation shape"

Solution:

  1. Check that NinjaTrader sends all 8 indicators
  2. Verify timestamp format is ISO 8601
  3. Run python observation_builder.py to test

Problem: "PM usage outside expected range"

Possible causes:

  • Model still warming up (wait for more data)
  • Market conditions different from training
  • Position state not updating correctly from NinjaTrader

Not necessarily an error - monitor over longer period


Advanced Configuration

Change Model Type

If you trained with a different algorithm:

Edit config.py:

MODEL_TYPE = "DQN"  # Options: PPO, MaskablePPO, DQN, A2C, SAC

Supported Algorithms:

  • "PPO" - Proximal Policy Optimization (stable-baselines3)
  • "MaskablePPO" - PPO with action masking (sb3-contrib) ← Current model
  • "DQN" - Deep Q-Network (stable-baselines3)
  • "A2C" - Advantage Actor-Critic (stable-baselines3)
  • "SAC" - Soft Actor-Critic (stable-baselines3)

Note: MaskablePPO requires sb3-contrib package


Adjust Logging Verbosity

Edit config.py:

VERBOSE = True           # Print detailed output
SHOW_BAR_DATA = True     # Print every bar
SHOW_OBSERVATIONS = False # Print observation arrays (noisy)
SHOW_PREDICTIONS = True   # Print model predictions
STATS_UPDATE_FREQUENCY = 10  # Print stats every N bars

Change TCP Port

If port 8888 is in use:

  1. Edit config.py:
TCP_PORT = 9999  # Or any available port
  1. Edit AIBridgeV2.cs in NinjaTrader:
ServerPort = 9999  // Match Python port
  1. Recompile strategy (F5)

Performance Expectations

Based on Phase 2 model training:

Metric Expected Value
Sharpe Ratio ~21.71
Returns +11%
PM Usage ~13.5% of actions
HOLD Actions ~86.5%
Entry Actions (BUY/SELL) ~10%
PM Actions (3,4,5) ~3.5%

Note: Performance may vary based on:

  • Market conditions
  • Data feed quality
  • Execution slippage
  • Actual vs simulated commissions

Safety Recommendations

⚠️ IMPORTANT SAFETY RULES

  1. ALWAYS test in Sim101 account first - NEVER use live account initially
  2. Run for minimum 1 week in simulation before considering live
  3. Monitor action distribution - if significantly different from expected, investigate
  4. Check log files daily for errors or unusual behavior
  5. Set up alerts for disconnections or errors
  6. Have a manual override plan to close positions if needed

πŸ›‘ STOP TRADING IF:

  • PM usage consistently >20% or <5%
  • Action distribution deviates >20% from expected
  • Model predictions become erratic
  • TCP connection becomes unstable
  • Unexpected errors in logs

Support

For issues:

  1. Check this README troubleshooting section
  2. Review log files in models/logs/
  3. Test individual components (tcp_client.py, model_manager.py, etc.)
  4. Check NinjaTrader Output Window for errors

Model-specific questions:

  • Refer to your model training documentation
  • Check Phase 2 model evaluation results
  • Verify VecNormalize stats are from correct training run

Quick Reference

Start Trading

# Terminal 1: Start NinjaTrader, enable AIBridgeV2
# Terminal 2:
cd "/mnt/c/Users/javlo/Documents/NinjaTrader 8/bin/Custom/models"
python ai_trading_bridge.py

Stop Trading

Ctrl+C in Python terminal

View Logs

tail -f logs/trading_log.txt     # Real-time log
cat logs/actions.csv              # Action history

Test Components

python config.py              # Validate configuration
python tcp_client.py          # Test TCP connection
python observation_builder.py # Test observation builder
python model_manager.py       # Test model loading
python trade_logger.py        # Test logger

Version Information

  • Python Bridge Version: 2.0 (Phase 3)
  • Compatible with: NinjaTrader 8, AIBridgeV2 strategy
  • Model: Phase 3 LLM-Enhanced (Stable-Baselines3 + Adapter)
  • Observation Space: (261,) - 220 market + 8 position + 33 LLM features
  • Action Space: Discrete(6) - Actions 0-5
  • Model Architecture: MaskablePPO with 261D β†’ 228D adapter layer
  • Training: 40,960 timesteps, gymnasium framework

Created: November 7, 2025 Last Updated: November 26, 2025 (Phase 3 Migration)


⚠️ DISCLAIMER: This software is for educational and testing purposes only. Trading involves risk. Always test thoroughly in simulation before live trading. Past performance does not guarantee future results.

RL_Agent

About

This is the agent that will run with the models created by the RL_Trainer

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages