fix: Backtesting #5: Position tracking + P&L metrics (#319)

- Add PositionTracker.sol: tracks position lifecycle (open/close per
  recenter), records tick ranges, liquidity, entry/exit blocks/timestamps,
  token amounts (via LiquidityAmounts math), fees (proportional to
  liquidity share), IL (LP exit value − HODL value at exit price), and
  net P&L per position. Aggregates total fees, cumulative IL, net P&L,
  rebalance count, Anchor time-in-range, and capital efficiency accumulators.
  Logs with [TRACKER][TYPE] prefix; emits cumulative P&L every 500 blocks.

- Modify StrategyExecutor.sol: add IUniswapV3Pool + token0isWeth to
  constructor (creates PositionTracker internally), call
  tracker.notifyBlock() on every block for time-in-range, and call
  tracker.recordRecenter() on each successful recenter. logSummary()
  now delegates to tracker.logFinalSummary().

- Modify BacktestRunner.s.sol: pass sp.pool and token0isWeth to
  StrategyExecutor constructor; log tracker address.

- forge fmt: reformat all backtesting scripts and affected src/test files
  to project style (number_underscore=thousands, multiline_func_header=all).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
openhands 2026-02-27 11:23:18 +00:00
parent a491ecb7d9
commit cfcf750084
12 changed files with 666 additions and 244 deletions

View file

@ -1,9 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.19;
import { UniswapHelpers } from "../../src/helpers/UniswapHelpers.sol";
import { IUniswapV3Factory } from "@uniswap-v3-core/interfaces/IUniswapV3Factory.sol";
import { IUniswapV3Pool } from "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
import { UniswapHelpers } from "../../src/helpers/UniswapHelpers.sol";
struct ShadowPool {
IUniswapV3Factory factory;
@ -27,14 +27,7 @@ library ShadowPoolDeployer {
* @param sqrtPriceX96 Initial sqrt price (Q64.96).
* @return sp ShadowPool struct with factory, pool, token0, token1 addresses.
*/
function deploy(
address tokenA,
address tokenB,
uint160 sqrtPriceX96
)
internal
returns (ShadowPool memory sp)
{
function deploy(address tokenA, address tokenB, uint160 sqrtPriceX96) internal returns (ShadowPool memory sp) {
sp.factory = UniswapHelpers.deployUniswapFactory();
address poolAddr = sp.factory.createPool(tokenA, tokenB, SHADOW_FEE);