Spaces:
Running
Running
Deploy Aureon OS (deploy/selfhost stack: nginx edge + fusion API x2 + console + HNC organ + redis + postgres; paper mode) (part 19)
da0ce7b verified Download scripts/validation/verify_margin_system_integration.py from GaryLeckey/aureon-os: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/scripts/validation/verify_margin_system_integration.py
- Command line
-
hf download hf://spaces/GaryLeckey/aureon-os/scripts/validation/verify_margin_system_integration.py
-
curl -L -o verify_margin_system_integration.py https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/scripts/validation/verify_margin_system_integration.py
10.9 kB
| #!/usr/bin/env python3 | |
| """ | |
| Verify that the margin trading system is properly integrated in the main Orca loop. | |
| Tests that: | |
| 1. Margin penny trader is callable from main loop | |
| 2. Real profit calculations account for all fees | |
| 3. Position monitoring works correctly | |
| 4. Real metrics are accessible | |
| """ | |
| import json | |
| import time | |
| from pathlib import Path | |
| from datetime import datetime | |
| def check_state_file(): | |
| """Check the current margin trading state.""" | |
| print("\n" + "="*80) | |
| print("π MARGIN TRADING STATE FILE CHECK") | |
| print("="*80) | |
| repo = Path(__file__).resolve().parents[2] | |
| state_file = repo / "kraken_margin_army_state.json" | |
| if not state_file.exists(): | |
| print("β State file not found!") | |
| return False | |
| with open(state_file) as f: | |
| state = json.load(f) | |
| # Check active trades | |
| active_long = state.get('active_long') | |
| active_short = state.get('active_short') | |
| print(f"\nβ State file exists (last updated: {state.get('last_updated')})") | |
| if active_long: | |
| print(f"\nπ΅ ACTIVE LONG POSITION:") | |
| print(f" Pair: {active_long.get('pair')}") | |
| print(f" Volume: {active_long.get('volume'):.8f}") | |
| print(f" Entry: ${active_long.get('entry_price'):.4f}") | |
| print(f" Leverage: {active_long.get('leverage')}x") | |
| print(f" Entry Fee: ${active_long.get('entry_fee'):.4f}") | |
| print(f" Order ID: {active_long.get('order_id')}") | |
| else: | |
| print("\nπ΅ No active long position") | |
| if active_short: | |
| print(f"\nπ΄ ACTIVE SHORT POSITION:") | |
| print(f" Pair: {active_short.get('pair')}") | |
| print(f" Volume: {active_short.get('volume'):.8f}") | |
| print(f" Entry: ${active_short.get('entry_price'):.4f}") | |
| print(f" Leverage: {active_short.get('leverage')}x") | |
| print(f" Entry Fee: ${active_short.get('entry_fee'):.4f}") | |
| print(f" Order ID: {active_short.get('order_id')}") | |
| else: | |
| print("\nπ΄ No active short position") | |
| # Stats | |
| print(f"\nπ TRADING STATISTICS:") | |
| print(f" Total Trades: {state.get('total_trades')}") | |
| print(f" Winning Trades: {state.get('winning_trades')}") | |
| print(f" Total P&L: ${state.get('total_profit'):.2f}") | |
| return bool(active_long or active_short) | |
| def check_profit_monitor(): | |
| """Verify real_profit_monitor is available and works.""" | |
| print("\n" + "="*80) | |
| print("π° REAL PROFIT MONITOR INTEGRATION CHECK") | |
| print("="*80) | |
| try: | |
| from real_profit_monitor import RealProfitMonitor | |
| monitor = RealProfitMonitor() | |
| print("β RealProfitMonitor imported successfully") | |
| # Test with a sample trade | |
| test_trade = { | |
| 'pair': 'DASHUSD', | |
| 'side': 'buy', | |
| 'volume': 10.0, | |
| 'entry_price': 36.0, | |
| 'current_price': 36.5, | |
| 'leverage': 3, | |
| 'entry_time': time.time() | |
| } | |
| result = monitor.calculate_real_profit(test_trade) | |
| print("\nβ Real Profit Calculation Test:") | |
| print(f" Entry Price: ${result['entry_price']:.4f}") | |
| print(f" Current Price: ${result['current_price']:.4f}") | |
| print(f" Gross P&L: ${result['gross_pnl']:+.4f}") | |
| print(f" Total Fees: ${result['fees']['total']:+.4f}") | |
| print(f" NET P&L: ${result['net_pnl']:+.4f}") | |
| print(f" Is Profitable: {result['is_profitable']}") | |
| print(f" Fee Burden: {result['fee_burden_pct']:.2f}%") | |
| return True | |
| except Exception as e: | |
| print(f"β Error testing RealProfitMonitor: {e}") | |
| return False | |
| def check_margin_trader(): | |
| """Verify margin penny trader is properly integrated.""" | |
| print("\n" + "="*80) | |
| print("π€ MARGIN PENNY TRADER INTEGRATION CHECK") | |
| print("="*80) | |
| try: | |
| from kraken_margin_penny_trader import KrakenMarginArmyTrader | |
| trader = KrakenMarginArmyTrader() | |
| print("β KrakenMarginPennyTrader imported successfully") | |
| # Check if monitor_position method exists | |
| if hasattr(trader, 'monitor_position'): | |
| print("β monitor_position() method available") | |
| else: | |
| print("β monitor_position() method NOT found") | |
| return False | |
| # Check key attributes | |
| print("\nβ Key Components:") | |
| print(f" Active Trade: {bool(trader.active_trade)}") | |
| print(f" Market Data: {bool(trader.market)}") | |
| print(f" Intelligence: {bool(trader.intel)}") | |
| print(f" Stream: {bool(trader.stream)}") | |
| # Check PROFIT_TARGET_USD is set | |
| from kraken_margin_penny_trader import PROFIT_TARGET_USD | |
| print(f" Profit Target: ${PROFIT_TARGET_USD}") | |
| return True | |
| except Exception as e: | |
| print(f"β Error testing KrakenMarginArmyTrader: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def check_orca_integration(): | |
| """Verify margin trader is called from main Orca loop.""" | |
| print("\n" + "="*80) | |
| print("π ORCA KILL CYCLE INTEGRATION CHECK") | |
| print("="*80) | |
| try: | |
| repo = Path(__file__).resolve().parents[2] | |
| orca_candidates = ( | |
| repo / "aureon_radar" / "bots" / "orca_complete_kill_cycle.py", | |
| repo / "aureon" / "bots" / "orca_complete_kill_cycle.py", | |
| ) | |
| orca_file = next((path for path in orca_candidates if path.is_file()), orca_candidates[0]) | |
| if not orca_file.exists(): | |
| print("β Orca file not found") | |
| return False | |
| content = orca_file.read_text() | |
| # Check for margin_penny_trader.tick() call | |
| if 'self.margin_penny_trader.tick()' in content: | |
| print("β margin_penny_trader.tick() found in main loop") | |
| else: | |
| print("β margin_penny_trader.tick() NOT found in main loop") | |
| return False | |
| # Check for session_stats aggregation | |
| if '_hive_all_closed' in content: | |
| print("β Closed trades aggregation found (session_stats)") | |
| else: | |
| print("β Closed trades aggregation NOT found") | |
| return False | |
| # Check for margin interval setting | |
| if 'kraken_margin_interval' in content: | |
| print("β Margin trading interval configured") | |
| else: | |
| print("β Margin trading interval NOT configured") | |
| # Find the tick interval | |
| import re | |
| match = re.search(r'kraken_margin_interval\s*=\s*([\d.]+)', content) | |
| if match: | |
| interval = float(match.group(1)) | |
| print(f" Tick Interval: {interval}s (every {interval}s the margin trader runs)") | |
| return True | |
| except Exception as e: | |
| print(f"β Error checking Orca integration: {e}") | |
| return False | |
| def check_fee_constants(): | |
| """Verify all fee constants are properly set.""" | |
| print("\n" + "="*80) | |
| print("π³ FEE CONSTANTS VERIFICATION") | |
| print("="*80) | |
| try: | |
| from kraken_margin_penny_trader import ( | |
| KRAKEN_OPEN_FEE, | |
| KRAKEN_CLOSE_FEE, | |
| KRAKEN_ROLLOVER_RATE, | |
| KRAKEN_ROLLOVER_INTERVAL, | |
| PROFIT_TARGET_USD | |
| ) | |
| from real_profit_monitor import ( | |
| KRAKEN_OPEN_FEE as RPM_OPEN_FEE, | |
| KRAKEN_CLOSE_FEE as RPM_CLOSE_FEE, | |
| KRAKEN_ROLLOVER_RATE as RPM_ROLLOVER_RATE, | |
| KRAKEN_ROLLOVER_INTERVAL as RPM_ROLLOVER_INTERVAL | |
| ) | |
| print("β Kraken Margin Trader Fees:") | |
| print(f" Open Fee: {KRAKEN_OPEN_FEE*100:.3f}%") | |
| print(f" Close Fee: {KRAKEN_CLOSE_FEE*100:.3f}%") | |
| print(f" Rollover Rate: {KRAKEN_ROLLOVER_RATE*100:.3f}% per {KRAKEN_ROLLOVER_INTERVAL/3600:.0f}h") | |
| print(f" Profit Target: ${PROFIT_TARGET_USD:.2f}") | |
| print("\nβ Real Profit Monitor Fees (should match):") | |
| print(f" Open Fee: {RPM_OPEN_FEE*100:.3f}%") | |
| print(f" Close Fee: {RPM_CLOSE_FEE*100:.3f}%") | |
| print(f" Rollover Rate: {RPM_ROLLOVER_RATE*100:.3f}% per {RPM_ROLLOVER_INTERVAL/3600:.0f}h") | |
| # Verify they match | |
| if (KRAKEN_OPEN_FEE == RPM_OPEN_FEE and | |
| KRAKEN_CLOSE_FEE == RPM_CLOSE_FEE and | |
| KRAKEN_ROLLOVER_RATE == RPM_ROLLOVER_RATE): | |
| print("\nβ Fee constants are synchronized across modules") | |
| else: | |
| print("\nβ οΈ Fee constants DO NOT match - inconsistency detected!") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β Error checking fees: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Run all verification checks.""" | |
| print("\n" + "β"*80) | |
| print("β" + " "*78 + "β") | |
| print("β" + " MARGIN TRADING SYSTEM INTEGRATION VERIFICATION".center(78) + "β") | |
| print("β" + " "*78 + "β") | |
| print("β"*80) | |
| checks = [ | |
| ("State File", check_state_file), | |
| ("Real Profit Monitor", check_profit_monitor), | |
| ("Margin Penny Trader", check_margin_trader), | |
| ("Fee Constants", check_fee_constants), | |
| ("Orca Integration", check_orca_integration), | |
| ] | |
| results = [] | |
| for name, check_func in checks: | |
| try: | |
| result = check_func() | |
| results.append((name, result)) | |
| except Exception as e: | |
| print(f"\nβ CRITICAL ERROR in {name}: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| results.append((name, False)) | |
| # Summary | |
| print("\n" + "="*80) | |
| print("π VERIFICATION SUMMARY") | |
| print("="*80) | |
| for name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f"{status} | {name}") | |
| all_pass = all(r for _, r in results) | |
| print("\n" + "="*80) | |
| if all_pass: | |
| print("π― ALL CHECKS PASSED - System is properly integrated!") | |
| print("\nThe margin trading system is:") | |
| print(" β Properly integrated into the main Orca autonomous loop") | |
| print(" β Monitoring positions every 5 seconds via margin_penny_trader.tick()") | |
| print(" β Calculating REAL profit (accounting for ALL fees)") | |
| print(" β Auto-closing positions when profit target met") | |
| print(" β Real metrics accessible via real_profit_monitor.py") | |
| print("\nTo run the system with live metrics:") | |
| print(" python run_unified_orca.py --autonomous --open-dashboard") | |
| else: | |
| print("β οΈ SOME CHECKS FAILED - See details above") | |
| print("="*80 + "\n") | |
| return all_pass | |
| if __name__ == '__main__': | |
| success = main() | |
| exit(0 if success else 1) | |