exp14: full portfolio simulation with $114, equal vs optimized allocation exp15: grid search ws=[32,48,64,96,128] × leverage=[1,2] per asset → Sharpe-optimal params found and deployed in config Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
151 lines
5.6 KiB
Python
151 lines
5.6 KiB
Python
"""Exp15b: Portfolio sim with Sharpe-optimal params on $114.
|
|
|
|
Uses max-Sharpe criterion (not max-PnL) from grid search.
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from run import run_portfolio, ASSETS, TOTAL_PORTFOLIO
|
|
from remizov_trader.core import signals
|
|
from common.metrics import save_results
|
|
|
|
# Sharpe-optimal params from exp15 grid search /wait analysis
|
|
SHARPE_OPTIMAL = {
|
|
"ETH-USD": {"ws": 64, "leverage": 2, "weight": 0.147, "tf": "2h"},
|
|
"SOL-USD": {"ws": 128, "leverage": 2, "weight": 0.100, "tf": "2h"},
|
|
"BTC-USD": {"ws": 32, "leverage": 2, "weight": 0.224, "tf": "4h"},
|
|
"DOGE-USD": {"ws": 48, "leverage": 2, "weight": 0.118, "tf": "4h"},
|
|
"ADA-USD": {"ws": 64, "leverage": 2, "weight": 0.127, "tf": "4h"},
|
|
"XRP-USD": {"ws": 64, "leverage": 2, "weight": 0.085, "tf": "4h"},
|
|
"DOT-USD": {"ws": 32, "leverage": 2, "weight": 0.127, "tf": "4h"},
|
|
}
|
|
|
|
# PnL-optimal (from grid search Phase 2)
|
|
PNL_OPTIMAL = {
|
|
"ETH-USD": {"ws": 64, "leverage": 2, "weight": 0.164, "tf": "2h"},
|
|
"SOL-USD": {"ws": 128, "leverage": 2, "weight": 0.111, "tf": "2h"},
|
|
"BTC-USD": {"ws": 32, "leverage": 2, "weight": 0.249, "tf": "4h"},
|
|
"DOGE-USD": {"ws": 48, "leverage": 2, "weight": 0.131, "tf": "4h"},
|
|
"ADA-USD": {"ws": 32, "leverage": 2, "weight": 0.113, "tf": "4h"},
|
|
"XRP-USD": {"ws": 96, "leverage": 2, "weight": 0.092, "tf": "4h"},
|
|
"DOT-USD": {"ws": 32, "leverage": 2, "weight": 0.141, "tf": "4h"},
|
|
}
|
|
|
|
# Current baseline
|
|
CURRENT = {
|
|
"ETH-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "2h"},
|
|
"SOL-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "2h"},
|
|
"BTC-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "4h"},
|
|
"DOGE-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "4h"},
|
|
"ADA-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "4h"},
|
|
"XRP-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "4h"},
|
|
"DOT-USD": {"ws": 64, "leverage": 2, "weight": round(1/7, 4), "tf": "4h"},
|
|
}
|
|
|
|
|
|
def print_result(name, r):
|
|
print(f"\n {'='*78}")
|
|
print(f" {name}")
|
|
print(f" {'='*78}")
|
|
print(f" Capital: ${r['total_capital']:.0f} → ${r['final_equity']:.2f} "
|
|
f"(PnL: ${r['total_pnl']:+.2f}, {r['total_ret']:+.1f}%)")
|
|
print(f" Total trades: {r['total_trades']}")
|
|
|
|
stats = r["asset_stats"]
|
|
if not stats:
|
|
print(" No trades!")
|
|
return
|
|
|
|
print(f"\n {'Asset':<10s} {'WS':>4s} {'Lev':>4s} {'Wt%':>5s} "
|
|
f"{'Init$':>7s} {'Final$':>8s} {'PnL$':>9s} "
|
|
f"{'Sharpe':>7s} {'WR%':>5s} {'Trds':>5s} {'$/mo':>8s}")
|
|
print(f" {'-'*78}")
|
|
|
|
sorted_stats = sorted(stats.items(), key=lambda x: x[1]["pnl"], reverse=True)
|
|
for ticker, s in sorted_stats:
|
|
print(f" {ticker:<10s} {s['ws']:>4d} {s['leverage']:>3d}x {s['weight']*100:>4.1f}% "
|
|
f"${s['initial']:>6.2f} ${s['final']:>7.2f} ${s['pnl']:>+8.2f} "
|
|
f"{s['sharpe']:>7.2f} {s['wr']:>4.1f}% {s['trades']:>5d} ${s['monthly_pnl']:>+7.2f}")
|
|
|
|
total_monthly = sum(s["monthly_pnl"] for s in stats.values())
|
|
print(f" {'-'*78}")
|
|
print(f" {'TOTAL':<10s} {'':>4s} {'':>4s} {'100%':>5s} "
|
|
f"${r['total_capital']:>6.0f} ${r['final_equity']:>7.2f} "
|
|
f"${r['total_pnl']:>+8.2f} "
|
|
f"{'':>7s} {'':>5s} {r['total_trades']:>5d} ${total_monthly:>+7.2f}")
|
|
|
|
|
|
def main():
|
|
results_dir = Path(__file__).parent / "results"
|
|
|
|
# Fetch data
|
|
price_cache = {}
|
|
for symbol, ticker, tf in ASSETS:
|
|
print(f"Fetching {ticker} ({tf})...", end=" ", flush=True)
|
|
try:
|
|
prices, dates = signals.fetch_prices_ccxt(symbol, tf, "max")
|
|
if prices is not None:
|
|
price_cache[ticker] = (prices, dates, tf)
|
|
print(f"{len(prices)} bars")
|
|
else:
|
|
print("SKIP")
|
|
except Exception as e:
|
|
print(f"ERROR: {e}")
|
|
|
|
configs = [
|
|
("A: CURRENT (equal, ws=64, 2x)", CURRENT),
|
|
("B: SHARPE-OPTIMAL (max Sharpe, Sharpe/DD weights)", SHARPE_OPTIMAL),
|
|
("C: PNL-OPTIMAL (max PnL, Sharpe/DD weights)", PNL_OPTIMAL),
|
|
]
|
|
|
|
results = {}
|
|
for name, params in configs:
|
|
print(f"\nRunning: {name}...")
|
|
r = run_portfolio(params, price_cache)
|
|
results[name] = r
|
|
print_result(name, r)
|
|
|
|
# Comparison
|
|
print(f"\n{'='*75}")
|
|
print("COMPARISON: A vs B vs C")
|
|
print(f"{'='*75}")
|
|
names = list(results.keys())
|
|
print(f" {'Metric':<16s}", end="")
|
|
for n in names:
|
|
label = n.split(":")[0] + ":" + n.split(":")[1][:15]
|
|
print(f" {label:>18s}", end="")
|
|
print()
|
|
print(f" {'-'*70}")
|
|
|
|
for label, k in [("Final $", "final_equity"), ("PnL $", "total_pnl"),
|
|
("Return %", "total_ret"), ("Trades", "total_trades")]:
|
|
print(f" {label:<16s}", end="")
|
|
for n in names:
|
|
v = results[n][k]
|
|
if k in ("final_equity", "total_pnl"):
|
|
print(f" ${v:>16.2f}", end="")
|
|
elif k == "total_ret":
|
|
print(f" {v:>16.1f}%", end="")
|
|
else:
|
|
print(f" {v:>17d}", end="")
|
|
print()
|
|
|
|
# Monthly PnL comparison
|
|
print(f"\n {'$/mo':<16s}", end="")
|
|
for n in names:
|
|
stats = results[n]["asset_stats"]
|
|
total_monthly = sum(s["monthly_pnl"] for s in stats.values())
|
|
print(f" ${total_monthly:>16.2f}", end="")
|
|
print()
|
|
|
|
# Save
|
|
path = save_results(results, results_dir, "exp15b_sharpe_optimal")
|
|
print(f"\nSaved: {path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|