import os import sys # Ensure kis_bot root is in path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from database import TradeDB from kis_trader.backtest.tail_backtest_common import load_tail_candles_by_code from kis_trader.engine.tail_engine import run_tail_backtest, run_tail_backtest_rust_experimental, get_tail_defaults_from_db def test_rust_tail(): db = TradeDB() try: # Load recent env params engine_params = get_tail_defaults_from_db(db) print(f"Engine params: {engine_params}") # Load candles start_date = "2026-08-07" end_date = "2026-08-07" print(f"Loading candles from {start_date} to {end_date}...") candles_by_code, _, _ = load_tail_candles_by_code(db, start_date, end_date, 3) print(f"Loaded {len(candles_by_code)} symbols.") # Create Python engine py_trades = run_tail_backtest( candles_by_code=candles_by_code, params=engine_params, ) print(f"[Python] Found {len(py_trades)} trades.") # Create Rust engine rs_trades = run_tail_backtest_rust_experimental( codes_candles=candles_by_code, params=engine_params, ) print(f"[Rust] Found {len(rs_trades)} trades.") # Sort both rs_trades.sort(key=lambda x: (x["code"], x["entry_time"])) py_trades.sort(key=lambda x: (x["code"], x["entry_time"])) if len(rs_trades) != len(py_trades): print(f"!!! COUNT MISMATCH !!! Rust: {len(rs_trades)}, Python: {len(py_trades)}") # Compare first 5 print("\n--- Python Trades ---") for i, t in enumerate(py_trades[:5]): print(f"{i}: {t}") print("\n--- Rust Trades ---") for i, t in enumerate(rs_trades[:5]): print(f"{i}: {t}") # Check diff print("\n--- Differences ---") py_simple = [{"code": t["code"], "entry_time": t["entry_time"], "entry": t["entry"], "exit": t["exit"]} for t in py_trades] rs_simple = [{"code": t["code"], "entry_time": t["entry_time"], "entry": t["entry"], "exit": t["exit"]} for t in rs_trades] diff_count = 0 for pt in py_simple: if pt not in rs_simple: print(f"Python has trade not in Rust: {pt}") diff_count += 1 if diff_count > 10: break for rt in rs_simple: if rt not in py_simple: print(f"Rust has trade not in Python: {rt}") diff_count += 1 if diff_count > 20: break if diff_count == 0: print("100% Match!") finally: db.close() if __name__ == "__main__": test_rust_tail()