fix: Backtesting #6: Baseline strategies (HODL, full-range, fixed-width) + reporting (#320)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
openhands 2026-02-27 13:08:53 +00:00
parent 33c5244e53
commit 77f0fd82fd
6 changed files with 1095 additions and 6 deletions

View file

@ -2,10 +2,11 @@
pragma solidity ^0.8.19;
import { BacktestKraiken } from "./BacktestKraiken.sol";
import { BaselineStrategies } from "./BaselineStrategies.sol";
import { EventReplayer } from "./EventReplayer.sol";
import { KrAIkenDeployer, KrAIkenSystem } from "./KrAIkenDeployer.sol";
import { MockToken } from "./MockToken.sol";
import { Reporter } from "./Reporter.sol";
import { ShadowPool, ShadowPoolDeployer } from "./ShadowPoolDeployer.sol";
import { StrategyExecutor } from "./StrategyExecutor.sol";
import { IERC20 } from "@openzeppelin/token/ERC20/IERC20.sol";
@ -151,6 +152,14 @@ contract BacktestRunner is Script {
new StrategyExecutor(sys.lm, IERC20(address(mockWeth)), IERC20(address(krk)), sender, recenterInterval, sp.pool, token0isWeth);
sys.lm.setRecenterAccess(address(executor));
// Deploy baseline strategies and initialize with the same capital as KrAIken.
BaselineStrategies baselines =
new BaselineStrategies(sp.pool, MockToken(sp.token0), MockToken(sp.token1), token0isWeth, recenterInterval);
baselines.initialize(initialCapital);
// Deploy Reporter (no broadcast needed it only writes files).
Reporter reporter = new Reporter();
vm.stopBroadcast();
// ------------------------------------------------------------------
@ -187,6 +196,8 @@ contract BacktestRunner is Script {
console2.log("LiquidityMgr: ", address(sys.lm));
console2.log("StrategyExec: ", address(executor));
console2.log("PositionTracker:", address(executor.tracker()));
console2.log("BaselineStrats: ", address(baselines));
console2.log("Reporter: ", address(reporter));
console2.log("Recenter intv: ", recenterInterval, " blocks");
console2.log("Initial capital:", initialCapital, " (mock WETH wei)");
console2.log("token0isWeth: ", token0isWeth);
@ -218,10 +229,35 @@ contract BacktestRunner is Script {
console2.log("\n=== Starting Event Replay ===");
console2.log("Events file: ", eventsFile);
console2.log("Total events: ", totalEvents);
replayer.replay(eventsFile, totalEvents, executor);
replayer.replay(eventsFile, totalEvents, executor, baselines);
// Print final KrAIken strategy summary.
// Print final strategy summaries.
executor.logSummary();
baselines.logFinalSummary();
// Generate comparison report (Markdown + JSON).
// Use fwTotalBlocks as the total replay duration (every unique block processed).
// Approximate period length at ~2 s/block.
uint256 endBlock = executor.tracker().lastNotifiedBlock();
uint256 totalBlocksElapsed = baselines.fwTotalBlocks();
uint256 startBlock = totalBlocksElapsed > 0 && endBlock > totalBlocksElapsed
? endBlock - totalBlocksElapsed
: endBlock;
uint256 periodDays = (totalBlocksElapsed * 2) / 86_400;
reporter.generate(
executor,
baselines,
Reporter.Config({
poolAddress: address(sp.pool),
startBlock: startBlock,
endBlock: endBlock,
initialCapital: initialCapital,
recenterInterval: recenterInterval,
poolLabel: "AERO/WETH 1%",
periodDays: periodDays
})
);
}
} catch { }
}