11 Commits

Author SHA1 Message Date
sami7777 e02a7e3453 Risk management overhaul: prevent 13-shorts blowup scenario
CRITICAL FIXES (would have prevented the $194K crisis):

1. HARD MAX SHORT POSITION CAP (safety.py + executor.py)
   - new config: max_short_positions=4 (hard limit)
   - new config: max_total_positions=8 (hard limit)
   - executor now queries broker.get_positions() for AUTHORITATIVE count
   - BEFORE opening any short, checks broker directly (not just store)
   - validate_trade() now accepts broker_positions and blocks at hard cap
   - This directly prevents the 13-shorts scenario

2. YESTERDAY-CLOSE DRAWDC OWN CIRCUIT BREAKER (safety.py)
   - new config: yesterday_close_drawdown_limit=3%
   - new config: yesterday_close_drawdown_reduction=50%
   - Separate from peak-equity tracking (which is too slow)
   - At $194K from $200K initial = 3% → fires IMMEDIATELY, cuts 50% of positions
   - Tested: check_yesterday_close_drawdown($194K) → triggers with 50% reduction

3. PER-POSITION STOP LOSS DEFAULTS (safety.py + executor.py)
   - new config: stop_loss_default_pct_long=1.0%, short=1.5%
   - new config: stop_loss_max_pct=3.0% (never wider than this)
   - new config: take_profit_default_pct=2.0%
   - NEW: get_default_stop_loss() + get_default_take_profit() methods
   - executor.execute_signal() now ALWAYS sets stop loss (even if GA provides None)
   - Previously stops were often None → exits never triggered
   - Tested: get_default_stop_loss($100, 'short') = $101.50 ✓

4. BROKER/STORE SYNC VALIDATION (safety.py + main_auto.py)
   - new: validate_broker_position_count() detects discrepancies
   - new: _trade_symbol() now passes combined_equity to executor
   - new: _trading_cycle() validates broker vs store BEFORE evaluating new trades
   - Logs warning when broker has positions not tracked in store
   - Blocks new trades if broker position count already at hard cap

5. CRISIS MODE: 10+ LOSING POSITIONS (executor.py check_exits)
   - If 8+ of 10+ positions are losing money → force reduce 50% of ALL positions
   - Catches cascading blowups before drawdown thresholds are hit
   - Logs CRITICAL warning when triggered

6. DEFAULT STOP LOSS ENFORCEMENT (executor.py)
   - _enforce_stop_loss_tightness() was overriding None stops to None
   - Now executor ALWAYS applies safety defaults if GA provides no stop
   - Every new position gets a stop loss on entry

Config changes (auto_config.json):
- Added max_short_positions, max_total_positions
- Added stop_loss_default_pct_long/short, take_profit_default_pct
- Added stop_loss_atr_multiplier, stop_loss_max_pct
- Added yesterday_close_drawdown_limit=3%, yesterday_close_drawdown_reduction=50%

HOW THIS WOULD HAVE HELPED THE $194,940 PORTFOLIO:
- At $194,940 from $200K = 2.53% drawdown from initial
- If yesterday closed at $200K: 2.53% < 3% limit → NOT triggered
- But at open today if equity dropped to $194,000 → 3.00% → TRIGGERS IMMEDIATELY
- Hard cap at 4 shorts: after 4 shorts, executor blocks action 5/6
- Stop losses: each of the 4 shorts would have had 1.5% stop → 2 shorts would
  have been stopped out before they lost further, limiting damage
- Crisis mode: if 8 positions were losing, 50% of all positions closed
2026-04-06 03:21:15 -07:00
sami7777 87add7e797 Increase max concurrent positions to 25 2026-04-03 20:05:50 -07:00
sami7777 8ea1df771b Overhaul trading strategy: profit-first reward, directional diversity, tighter risk
- Reward function: profit is king, big bonus for profitable closes, penalty for losses
- Removed hold penalty (sitting in cash when uncertain is smart)
- Directional diversity: max 3 positions same direction (was 13/13 shorts)
- Tighter SL/TP: 1.0%/1.5% (was 2.5%/5.0%)
- Max 6 concurrent positions (was 250)
- RL: reset brain, live_epsilon 0.12 (was 0.03), hidden_dim 256
- GA: tighter ATR ranges, 150 population, higher mutation
- Min backtest Sharpe 0.3 (was -0.5, allowing losing strategies)
- Closed all 13 bleeding OANDA short positions
2026-04-03 20:04:11 -07:00
sami7777 41471ee816 Merge remote: resolve conflicts, keep combined portfolio helpers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 14:14:12 -07:00
sami7777 a1e985c91a Fix reporting: combine Alpaca + OANDA portfolio, correct initial capital
- Portfolio equity now sums both brokers instead of showing Alpaca only
- Total P&L was showing -$100k because OANDA equity was missing
- Set initial_capital to $200k (Alpaca $100k + OANDA $100k)
- Set target_capital to $250k
- Label forex positions as "units" instead of "shares"

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 14:12:16 -07:00
sami7777 449d535a43 3x instrument expansion: 22 stocks + 13 forex, 2min cycles, 300 daily trades, 15min training 2026-03-27 01:19:20 -07:00
sami7777 c2a2109316 fix: initialize peak_equity from actual portfolio value on startup
Fixes false 50% drawdown circuit breaker that was blocking all trades.
SafetyManager was initialized with peak_equity=initial_capital (200K)
but OANDA account starts at ~100K, triggering immediate halt.

Now syncs peak_equity to actual portfolio value on startup.
2026-03-26 08:23:48 +01:00
sami7777 7be73159a4 Update: Krystie reporter integration and strategy improvements 2026-03-23 00:21:19 +01:00
sami7777 62822a8675 Merge remote main: resolve conflicts keeping scalping strategy changes
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 16:46:28 -07:00
sami7777 82c68fa8b0 Scalping strategy overhaul: bidirectional trading, oil/JPY focus
- Switch to oil stocks (USO, XLE, OXY, CVX, XOM, SLB, HAL, DVN, MPC, VLO)
- Add JPY/USD forex pairs for Japan targeting
- 7-action RL space: long, short, close (was 5 long-only actions)
- Bollinger Band mean-reversion scalp entries both directions
- 5-minute candles with 60-second cycles for scalping
- 35 features (added VWAP, fast RSI, fast ROC for scalping)
- Short position support in backtest, executor, and RL environment
- GA tuned for scalping: tighter SL/TP, shorter hold times

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 16:45:19 -07:00
root 6a35ba0f0f Initial commit: BIGGFISH Autonomous Trading Bot
- GA-evolved trading strategies
- RL agent with DQN
- Dual-market support (stocks via Alpaca, forex via OANDA)
- Automated backtesting and rebalancing
- Telegram notifications
- Safety limits and risk management
2026-03-13 00:32:54 +01:00