Nija

NAMIE - NIJA Adaptive Market Intelligence Engine 🧠

Overview

NAMIE (NIJA Adaptive Market Intelligence Engine) is the highest ROI upgrade to NIJA’s trading system. It multiplies the effectiveness of all existing strategies by:

Architecture

NAMIE consists of four core components that work together:

1. NAMIE Core Engine (namie_core.py)

The brain of the system that performs comprehensive market analysis:

2. Strategy Switcher (namie_strategy_switcher.py)

Intelligent strategy selection and performance tracking:

3. Integration Layer (namie_integration.py)

Easy-to-use API for existing strategies:

4. Existing Components (Unified)

NAMIE unifies and enhances:

Quick Start

Basic Usage

from bot.namie_integration import NAMIEIntegration

# Initialize NAMIE
namie = NAMIEIntegration()

# In your trading loop
for symbol in trading_pairs:
    df = get_price_data(symbol)
    indicators = calculate_indicators(df)
    
    # Get NAMIE intelligence
    signal = namie.analyze(df, indicators, symbol)
    
    # Check if should trade
    if signal.should_trade:
        print(f"✅ Trade {symbol}")
        print(f"   Regime: {signal.regime.value}")
        print(f"   Strategy: {signal.optimal_strategy.value}")
        print(f"   Trend Strength: {signal.trend_strength}/100")
        print(f"   Chop Score: {signal.chop_score:.0f}/100")
        
        # Adjust position size based on NAMIE
        base_size = calculate_position_size(account_balance)
        adjusted_size = namie.adjust_position_size(signal, base_size)
        
        # Get adaptive RSI ranges
        rsi_ranges = namie.get_adaptive_rsi_ranges(signal)
        
        # Execute trade with NAMIE intelligence
        execute_trade(symbol, adjusted_size, rsi_ranges)

One-Line Quick Check

from bot.namie_integration import quick_namie_check

# Single function for quick integration
should_trade, reason, signal = quick_namie_check(df, indicators, "BTC-USD")

if should_trade:
    # Trade approved by NAMIE
    size = base_size * signal.position_size_multiplier
    execute_trade(size)
else:
    print(f"❌ NAMIE blocked: {reason}")

Integration with Existing Strategies

Option 1: APEX v7.1 Integration

from bot.nija_apex_strategy_v71 import NIJAApexStrategyV71
from bot.namie_integration import NAMIEIntegration

class ApexWithNAMIE(NIJAApexStrategyV71):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.namie = NAMIEIntegration()
    
    def analyze_market(self, df, symbol, account_balance):
        # Get base APEX analysis
        analysis = super().analyze_market(df, symbol, account_balance)
        
        # Get NAMIE intelligence
        indicators = self._calculate_indicators(df)
        signal = self.namie.analyze(df, indicators, symbol)
        
        # Enhance with NAMIE
        if analysis['action'] != 'hold':
            # Check NAMIE approval
            should_trade, reason = self.namie.should_enter_trade(
                signal,
                base_entry_score=analysis.get('entry_score', 3),
                base_should_enter=True
            )
            
            if not should_trade:
                analysis['action'] = 'hold'
                analysis['reason'] = reason
            else:
                # Adjust position size
                analysis['position_size'] = self.namie.adjust_position_size(
                    signal,
                    analysis['position_size']
                )
        
        return analysis

Option 2: Trading Strategy Integration

from bot.trading_strategy import TradingStrategy  # Your existing strategy
from bot.namie_integration import NAMIEIntegration

# In your trading strategy class
class YourStrategy(TradingStrategy):
    def __init__(self):
        super().__init__()
        self.namie = NAMIEIntegration()
    
    def check_entry_signal(self, symbol, df):
        # Your existing entry logic
        base_signal = super().check_entry_signal(symbol, df)
        
        # Add NAMIE intelligence
        indicators = self.calculate_indicators(df)
        namie_signal = self.namie.analyze(df, indicators, symbol)
        
        # Combine signals
        if base_signal and namie_signal.should_trade:
            return True, namie_signal
        else:
            return False, None

Configuration

Environment Variables

Add to your .env file:

# NAMIE Configuration
NAMIE_ENABLED=true
NAMIE_RESPECT_DECISIONS=true
NAMIE_MIN_REGIME_CONFIDENCE=0.6
NAMIE_MIN_TREND_STRENGTH=40
NAMIE_MAX_CHOP_SCORE=60

# Strategy Switching
NAMIE_ENABLE_SWITCHER=true
NAMIE_MIN_TRADES_FOR_SWITCH=10
NAMIE_SWITCH_THRESHOLD_WIN_RATE=0.45
NAMIE_MAX_STRATEGY_DRAWDOWN=0.15

Python Configuration

config = {
    # Core NAMIE settings
    'min_regime_confidence': 0.6,  # Minimum confidence to trade (0-1)
    'min_trend_strength': 40,      # Minimum trend score to trade (0-100)
    'max_chop_score': 60,          # Maximum chop before blocking (0-100)
    
    # Strategy switching
    'min_trades_for_switch': 10,           # Min trades before switching
    'switch_threshold_win_rate': 0.45,     # Switch if WR below this
    'switch_threshold_profit_factor': 0.8, # Switch if PF below this
    'max_strategy_drawdown': 0.15,         # 15% max strategy DD
    'min_switch_interval_hours': 4,        # Min hours between switches
    
    # Integration settings
    'respect_namie_decisions': True,       # Let NAMIE block trades
    'override_position_sizing': True,      # Let NAMIE adjust sizes
    'override_entry_thresholds': True,     # Let NAMIE set min entry score
}

namie = NAMIEIntegration(config=config)

Market Regimes

NAMIE classifies markets into three regimes:

RANGING Regime

VOLATILE Regime

Trend Strength Scoring

NAMIE calculates a comprehensive 0-100 trend strength score:

Components (weighted)

  1. ADX Strength (25 points)
    • 50+ ADX = 25 points
    • 40-50 = 22 points
    • 30-40 = 18 points
    • 25-30 = 15 points
  2. EMA Alignment (25 points)
    • Perfect alignment (9>21>50) = 25 points
    • Partial alignment = 15 points
    • No alignment = 5 points
  3. MACD Momentum (20 points)
    • Based on histogram magnitude and direction
  4. Price Momentum (15 points)
    • Based on 10-period price change
  5. Volume Confirmation (15 points)
    • Volume > 1.5x average = 15 points
    • Volume > average = 10 points

Score Categories

Chop Detection

NAMIE detects choppy/sideways markets to prevent losses:

Chop Factors

  1. Low ADX (30 points max)
    • ADX < 15 = 30 points
    • ADX < 20 = 20 points
  2. Price Range Compression (25 points max)
    • Range < 2% = 25 points
    • Range < 5% = 15 points
  3. EMA Convergence (25 points max)
    • EMAs within 0.5% = 25 points
    • EMAs within 1% = 15 points
  4. MACD Weakness (20 points max)
    • Flat MACD = 20 points

Chop Conditions

Strategy Auto-Switching

NAMIE automatically switches strategies based on performance:

Switch Triggers

  1. NAMIE Recommendation: High confidence (>80%) in different strategy
  2. Low Win Rate: Current strategy WR < 45%
  3. Low Profit Factor: Current strategy PF < 0.8
  4. Excessive Drawdown: Current strategy DD > 15%
  5. Severe Chop: Market conditions unsuitable

Performance Tracking

NAMIE tracks per strategy-regime combination:

Scoring System

Strategies are scored 0-100 based on:

NAMIESignal Object

Complete signal returned by NAMIE analysis:

@dataclass
class NAMIESignal:
    timestamp: datetime
    
    # Regime
    regime: MarketRegime  # TRENDING/RANGING/VOLATILE
    regime_confidence: float  # 0-1
    regime_probabilities: RegimeProbabilities  # Bayesian distribution
    
    # Volatility
    volatility_regime: VolatilityRegime
    volatility_cluster: str  # expanding/contracting/stable
    atr_pct: float
    
    # Trend
    trend_strength: int  # 0-100
    trend_strength_category: TrendStrength
    trend_direction: str  # up/down/neutral
    
    # Chop
    chop_condition: ChopCondition
    chop_score: float  # 0-100
    
    # Strategy
    optimal_strategy: TradingStrategy
    strategy_confidence: float  # 0-1
    alternative_strategies: List[Tuple[Strategy, float]]
    
    # Trading Decision
    should_trade: bool
    trade_reason: str
    position_size_multiplier: float
    min_entry_score_required: int
    
    # Metrics
    metrics: Dict

Performance Monitoring

Get Performance Summary

summary = namie.get_performance_summary()

# Summary structure:
{
    'namie_core': {
        'trending': {
            'trades': 45,
            'wins': 28,
            'losses': 17,
            'win_rate': 0.622,
            'total_pnl': 1250.50,
            'avg_pnl_per_trade': 27.79
        },
        'ranging': {...},
        'volatile': {...}
    },
    'strategy_switcher': {
        'by_strategy_regime': {
            'trend_trending': {
                'total_trades': 30,
                'win_rate': 0.667,
                'profit_factor': 2.15,
                'sharpe_estimate': 1.8
            },
            ...
        },
        'current_allocations': {
            'trending': 'trend',
            'ranging': 'mean_reversion',
            'volatile': 'breakout'
        },
        'recent_switches': [...]
    }
}

Expected Performance Improvements

Based on backtesting and live testing:

Win Rate

Risk/Reward Ratio

Drawdown Reduction

Overall ROI

Chop Loss Prevention

Best Practices

1. Start Conservative

config = {
    'min_regime_confidence': 0.7,  # Higher confidence required
    'min_trend_strength': 50,      # Stronger trends only
    'max_chop_score': 50,          # More aggressive chop filtering
}

2. Monitor Performance

# Check NAMIE effectiveness daily
summary = namie.get_performance_summary()

# Compare regime performance
for regime, stats in summary['namie_core'].items():
    print(f"{regime}: WR={stats['win_rate']:.1%}, PnL=${stats['total_pnl']:.2f}")

3. Adjust Thresholds

# If missing good trades, relax thresholds
config['min_trend_strength'] = 40  # Lower threshold
config['max_chop_score'] = 60     # Allow more chop

# If too many losses, tighten thresholds
config['min_regime_confidence'] = 0.75
config['min_trend_strength'] = 60

4. Record All Trades

# Always record trade results for NAMIE learning
namie.record_trade_result(
    signal=namie_signal,
    entry_price=entry,
    exit_price=exit,
    side='long',
    size_usd=position_size,
    commission=fees
)

Troubleshooting

Issue: NAMIE blocking all trades

Solution: Check thresholds are not too strict

# View current signal
signal = namie.analyze(df, indicators, symbol)
print(f"Regime confidence: {signal.regime_confidence:.0%}")
print(f"Trend strength: {signal.trend_strength}/100")
print(f"Chop score: {signal.chop_score:.0f}/100")
print(f"Reason: {signal.trade_reason}")

# Adjust config if needed

Issue: Strategies switching too frequently

Solution: Increase switch cooldown

config['min_switch_interval_hours'] = 8  # Increase to 8 hours
config['min_trades_for_switch'] = 15     # Need more data before switching

Issue: Performance not improving

Solution: Verify integration

# Ensure NAMIE decisions are being respected
namie = NAMIEIntegration(config={
    'respect_namie_decisions': True,
    'override_position_sizing': True,
    'override_entry_thresholds': True,
})

# Check that trades are being recorded
# (NAMIE learns from trade results)

Advanced Usage

Custom Regime Parameters

# Override default regime parameters
from bot.namie_core import get_namie_engine

namie_core = get_namie_engine()

# Adjust TRENDING regime parameters
namie_core.regime_detector.regime_params[MarketRegime.TRENDING]['position_size_multiplier'] = 1.5
namie_core.regime_detector.regime_params[MarketRegime.TRENDING]['min_entry_score'] = 2

Backtesting Integration

# Use NAMIE in backtesting
from bot.namie_integration import NAMIEIntegration

namie = NAMIEIntegration(enable_switcher=True)

for bar in historical_data:
    df = bar.df
    indicators = calculate_indicators(df)
    
    signal = namie.analyze(df, indicators, bar.symbol)
    
    if signal.should_trade and base_strategy.check_entry(bar):
        # Execute backtest trade
        size = base_size * signal.position_size_multiplier
        
        # ... execute trade ...
        
        # Record result
        namie.record_trade_result(signal, entry, exit, side, size)

# Get backtest performance
summary = namie.get_performance_summary()

Support

For issues or questions:

  1. Check this documentation
  2. Review code comments in namie_core.py, namie_strategy_switcher.py, namie_integration.py
  3. Review existing regime detector documentation
  4. Test with paper trading first

Version History


NAMIE - Adaptive Intelligence for Maximum ROI 🧠💎