web / pmct /backtest.py
LinJennyLin's picture
Deploy SharpFlow (nav + example wallets + Polymarket copy)
d4c51f2 verified
Raw History Blame Contribute Delete
6.58 kB
"""Event-driven copy-trade backtest against a single target wallet.
We replay the target's normalized ledger in time order and mirror it with OUR
bankroll, sizing each entry by conviction (sizing.py). Exits mirror the target:
when the target trims/redeems X% of their position, we trim the same % of ours.
Fill model (documented approximations — this is research, not a fill simulator):
* entry fill = target's price * (1 + slippage) # we pay up to get in
* exit fill = target's exit price * (1 - slippage) # we give up edge to exit
* redemptions use payout-per-share (usdc/size) as the exit price
* copy_delay_sec is folded into `slippage_bps` rather than modeled tick-by-tick
(modeling it exactly needs CLOB prices-history per token; left as a TODO).
Outputs an equity curve and summary stats, and can run a flat-sizing baseline
so you can see whether conviction sizing actually helped.
"""
from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
import numpy as np
import pandas as pd
from .sizing import SizingConfig, position_fraction
@dataclass
class BacktestResult:
equity_curve: pd.DataFrame # ts, equity, cash, invested
final_equity: float
total_return: float
max_drawdown_pct: float
n_entries: int
n_exits: int
closed_win_rate: float
summary: dict
def to_summary(self) -> dict:
return {
"final_equity": round(self.final_equity, 2),
"total_return": round(self.total_return, 4),
"max_drawdown_pct": round(self.max_drawdown_pct, 4),
"n_entries": self.n_entries,
"n_exits": self.n_exits,
"closed_win_rate": round(self.closed_win_rate, 4),
**self.summary,
}
def _trailing_avg(values: list[float], window: int = 20) -> float:
if not values:
return 0.0
return float(np.mean(values[-window:]))
def run(ledger: pd.DataFrame, bankroll: float, sizing: SizingConfig,
slippage_bps: float = 150, fee_bps: float = 0,
flat_sizing: bool = False) -> BacktestResult:
"""`flat_sizing=True` ignores conviction and stakes a constant
max_frac_per_trade on every entry (baseline for comparison)."""
slip = slippage_bps / 1e4
fee = fee_bps / 1e4
cash = bankroll
our_shares: dict = defaultdict(float) # asset -> shares WE hold
tgt_shares: dict = defaultdict(float) # asset -> shares TARGET holds
market_cost: dict = defaultdict(float) # market -> USD we've committed
last_price: dict = defaultdict(float) # asset -> last seen price
asset_market: dict = {} # asset -> market id
entry_cost: dict = defaultdict(float) # asset -> our cost basis (for win/loss)
realized: list = [] # per-closed-lot pnl
target_buy_usdc: list = []
curve = []
n_entries = n_exits = 0
def mark_equity(ts):
invested = sum(sh * last_price.get(a, 0.0) for a, sh in our_shares.items())
curve.append({"ts": ts, "equity": cash + invested,
"cash": cash, "invested": invested})
for row in ledger.itertuples(index=False):
side, typ = row.side, row.type
asset, market = row.asset, row.market
price = float(row.price) if row.price else last_price.get(asset, 0.0)
size = float(row.size)
last_price[asset] = price or last_price.get(asset, 0.0)
asset_market[asset] = market
if side == "BUY":
tgt_shares[asset] += size
target_buy_usdc.append(float(row.usdc))
rolling = _trailing_avg(target_buy_usdc)
exposure_frac = market_cost[market] / bankroll
if flat_sizing:
frac = max(0.0, min(sizing.max_frac_per_trade,
sizing.max_frac_per_market - exposure_frac))
conv = float("nan")
else:
frac, conv = position_fraction(
float(row.usdc), rolling, price, exposure_frac, sizing)
fill = price * (1 + slip)
stake = min(frac * bankroll, cash)
if stake <= 0 or fill <= 0:
mark_equity(row.ts)
continue
stake *= (1 - fee)
shares = stake / fill
cash -= stake
our_shares[asset] += shares
entry_cost[asset] += stake
market_cost[market] += stake
n_entries += 1
elif side == "SELL" or typ in ("REDEEM", "CLAIM"):
held = our_shares.get(asset, 0.0)
tgt_held = tgt_shares.get(asset, 0.0)
if held > 0 and size > 0 and tgt_held > 0:
frac_exit = min(1.0, size / tgt_held)
shares_out = held * frac_exit
exit_price = (float(row.usdc) / size) if typ in ("REDEEM", "CLAIM") else price
exec_price = exit_price * (1 - slip)
proceeds = shares_out * exec_price * (1 - fee)
cost_out = entry_cost.get(asset, 0.0) * (shares_out / held if held else 0)
cash += proceeds
our_shares[asset] -= shares_out
entry_cost[asset] -= cost_out
market_cost[asset_market.get(asset, market)] = max(
0.0, market_cost.get(market, 0.0) - cost_out)
realized.append(proceeds - cost_out)
n_exits += 1
tgt_shares[asset] = max(0.0, tgt_held - size)
mark_equity(row.ts)
if not curve:
empty = pd.DataFrame(columns=["ts", "equity", "cash", "invested"])
return BacktestResult(empty, bankroll, 0.0, 0.0, 0, 0, 0.0, {"note": "no trades"})
eq_df = pd.DataFrame(curve)
equity = eq_df["equity"].values
running_max = np.maximum.accumulate(equity)
max_dd = float(((running_max - equity) / running_max).max())
final_equity = float(equity[-1])
closed_wr = float(np.mean([r > 0 for r in realized])) if realized else 0.0
return BacktestResult(
equity_curve=eq_df,
final_equity=final_equity,
total_return=(final_equity - bankroll) / bankroll,
max_drawdown_pct=max_dd,
n_entries=n_entries,
n_exits=n_exits,
closed_win_rate=closed_wr,
summary={
"bankroll": bankroll,
"sizing": "flat" if flat_sizing else "conviction",
"realized_pnl": round(float(sum(realized)), 2),
"open_lots": int(sum(1 for v in our_shares.values() if v > 1e-9)),
},
)