540 lines
23 KiB
TypeScript
540 lines
23 KiB
TypeScript
/**
|
|
* Strategy simulation — demonstrates full agent cycle with mock data.
|
|
* No RPC connection, Solana wallet, or real funds needed.
|
|
*
|
|
* Usage: npx ts-node scripts/simulate.ts
|
|
*
|
|
* Useful for:
|
|
* - Demo video recording
|
|
* - Verifying strategy logic end-to-end
|
|
* - Showing judges how the agent works
|
|
*/
|
|
import Decimal from "decimal.js";
|
|
|
|
// Simulated funding rates (realistic Drift data from late 2025)
|
|
const MOCK_FUNDING: Record<
|
|
string,
|
|
{ rate: number; premium: number; imbalance: number }
|
|
> = {
|
|
SOL: { rate: 0.000012, premium: 0.008, imbalance: 0.15 },
|
|
BTC: { rate: 0.000018, premium: 0.005, imbalance: 0.12 },
|
|
ETH: { rate: -0.000008, premium: -0.004, imbalance: -0.10 },
|
|
};
|
|
|
|
const MOCK_PRICES: Record<string, number> = {
|
|
SOL: 185.42,
|
|
BTC: 98250.0,
|
|
ETH: 3420.5,
|
|
};
|
|
|
|
const MOCK_LENDING_RATES: Record<string, number> = {
|
|
SOL: 0.032, // 3.2% APY
|
|
BTC: 0.018, // 1.8% APY
|
|
ETH: 0.025, // 2.5% APY
|
|
};
|
|
|
|
function log(section: string, msg: string, data?: Record<string, string>) {
|
|
const ts = new Date().toISOString().split("T")[1].split(".")[0];
|
|
console.log(`[${ts}] ${section} | ${msg}`);
|
|
if (data) {
|
|
for (const [k, v] of Object.entries(data)) {
|
|
console.log(` ${k}: ${v}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function simulate() {
|
|
const initialCapital = new Decimal(20); // $20 USDC hackathon POC
|
|
const assets = ["SOL", "BTC", "ETH"];
|
|
const minFundingAPY = 0.05;
|
|
|
|
console.log("=".repeat(60));
|
|
console.log(" RANGER DELTA-NEUTRAL FUNDING HARVESTER — SIMULATION");
|
|
console.log("=".repeat(60));
|
|
console.log();
|
|
|
|
// ── Step 1: Initialize ──────────────────────────────────────────
|
|
log("INIT", "Starting AI Strategy Agent", {
|
|
mode: "drift-only",
|
|
capital: `$${initialCapital.toFixed(2)} USDC`,
|
|
assets: assets.join(", "),
|
|
maxLeverage: "2.0x",
|
|
healthFloor: "1.10",
|
|
maxDrawdown: "3.0%",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 2: Market Overview ─────────────────────────────────────
|
|
log("MARKET", "=== Market Overview ===");
|
|
for (const asset of assets) {
|
|
const f = MOCK_FUNDING[asset];
|
|
const annualized = f.rate * 24 * 365.25;
|
|
const lendAPY = MOCK_LENDING_RATES[asset];
|
|
log("MARKET", `${asset}:`, {
|
|
price: `$${MOCK_PRICES[asset].toFixed(2)}`,
|
|
"funding APY": `${(annualized * 100).toFixed(2)}%`,
|
|
premium: `${(f.premium * 100).toFixed(3)}%`,
|
|
"long/short imbalance": `${(f.imbalance * 100).toFixed(1)}%`,
|
|
"lending APY": `${(lendAPY * 100).toFixed(1)}%`,
|
|
direction: annualized > 0 ? "positive (shorts collect)" : "negative (longs collect)",
|
|
});
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 3: On-Chain Funding Analysis ───────────────────────────
|
|
log("FUNDING", "=== On-Chain Funding Analysis ===");
|
|
const rankedAssets: Array<{
|
|
asset: string;
|
|
absYield: number;
|
|
perpSide: string;
|
|
spotSide: string;
|
|
confidence: number;
|
|
momentum: string;
|
|
}> = [];
|
|
|
|
for (const asset of assets) {
|
|
const f = MOCK_FUNDING[asset];
|
|
const annualized = f.rate * 24 * 365.25;
|
|
const absYield = Math.abs(annualized);
|
|
const isAttractive = absYield > minFundingAPY;
|
|
|
|
const perpSide = annualized >= 0 ? "short" : "long";
|
|
const spotSide = perpSide === "short" ? "long" : "short";
|
|
const attractiveDir = annualized >= 0 ? "short" : "long";
|
|
const momentum =
|
|
Math.abs(f.premium) > 0.005 ? "rising" : Math.abs(f.premium) > 0.002 ? "flat" : "falling";
|
|
const confidence = Math.min(0.95, 0.5 + Math.abs(f.imbalance) + Math.abs(f.premium) * 5);
|
|
|
|
log("FUNDING", `${asset}:`, {
|
|
annualized: `${(annualized * 100).toFixed(2)}%`,
|
|
"abs yield": `${(absYield * 100).toFixed(2)}%`,
|
|
"attractive?": isAttractive ? `YES (${attractiveDir} perp)` : "NO",
|
|
momentum,
|
|
confidence: `${(confidence * 100).toFixed(0)}%`,
|
|
});
|
|
|
|
if (isAttractive) {
|
|
rankedAssets.push({ asset, absYield, perpSide, spotSide, confidence, momentum });
|
|
}
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 4: Regime Detection ────────────────────────────────────
|
|
const avgRate =
|
|
assets.reduce((s, a) => s + MOCK_FUNDING[a].rate * 24 * 365.25, 0) /
|
|
assets.length;
|
|
const regime =
|
|
avgRate > 0.15 ? "bull" : avgRate < -0.05 ? "bear" : "neutral";
|
|
log("REGIME", `Market regime: ${regime.toUpperCase()}`, {
|
|
"avg funding": `${(avgRate * 100).toFixed(2)}%`,
|
|
adjustment:
|
|
regime === "bull"
|
|
? "higher allocation, shorts collect"
|
|
: regime === "bear"
|
|
? "lower allocation, longs collect"
|
|
: "standard allocation",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 5: Signal Generation ───────────────────────────────────
|
|
log("SIGNALS", "=== Trade Signal Generation ===");
|
|
rankedAssets.sort((a, b) => b.absYield - a.absYield);
|
|
|
|
let idleCapital = initialCapital;
|
|
const positions: Array<{
|
|
asset: string;
|
|
perpSide: string;
|
|
spotSide: string;
|
|
size: Decimal;
|
|
}> = [];
|
|
|
|
const perAssetAllocation = initialCapital.div(rankedAssets.length);
|
|
for (const ranked of rankedAssets) {
|
|
const posSize = Decimal.min(perAssetAllocation, idleCapital);
|
|
if (posSize.lt(5)) continue;
|
|
|
|
log("SIGNALS", `OPEN ${ranked.asset}`, {
|
|
action: "open",
|
|
"spot leg": `${ranked.spotSide} spot on Drift`,
|
|
"perp leg": `${ranked.perpSide} perp on Drift`,
|
|
size: `$${posSize.toFixed(2)}`,
|
|
"funding rate": `${(ranked.absYield * 100).toFixed(2)}% APY`,
|
|
confidence: `${(ranked.confidence * 100).toFixed(0)}%`,
|
|
momentum: ranked.momentum,
|
|
});
|
|
|
|
positions.push({
|
|
asset: ranked.asset,
|
|
perpSide: ranked.perpSide,
|
|
spotSide: ranked.spotSide,
|
|
size: posSize,
|
|
});
|
|
idleCapital = idleCapital.sub(posSize);
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 6: Execution (LST yield stacking for SOL) ─────────────
|
|
log("EXEC", "=== Executing Trades (atomic txs) ===");
|
|
|
|
// JitoSOL staking APY (mirrors lst.ts registry — ~7.2%)
|
|
const JITOSOL_APY = 0.072;
|
|
|
|
for (const pos of positions) {
|
|
const usesLST = pos.perpSide === "short" && pos.asset === "SOL";
|
|
if (usesLST) {
|
|
log("EXEC", `LST atomic entry: ${pos.asset} (JitoSOL collateral)`, {
|
|
tx: "[simulated] cancel_all → swap_USDC→JitoSOL(Jupiter) → short_SOL-PERP",
|
|
collateral: "JitoSOL (spot idx 6, ~7.2% staking APY)",
|
|
size: `$${pos.size.toFixed(2)}`,
|
|
"oracle price": `$${MOCK_PRICES[pos.asset].toFixed(2)}`,
|
|
"base amount": `${pos.size.div(MOCK_PRICES[pos.asset]).toFixed(6)} ${pos.asset}`,
|
|
"slippage cap": "1.0% (100 bps for Jupiter swap)",
|
|
"yield layers": "funding rate + staking APY + lending yield",
|
|
});
|
|
} else {
|
|
log("EXEC", `Atomic cancel+enter: ${pos.asset}`, {
|
|
tx: `[simulated] cancel_all → ${pos.spotSide}_spot → ${pos.perpSide}_perp`,
|
|
size: `$${pos.size.toFixed(2)}`,
|
|
"oracle price": `$${MOCK_PRICES[pos.asset].toFixed(2)}`,
|
|
"base amount": `${pos.size.div(MOCK_PRICES[pos.asset]).toFixed(6)} ${pos.asset}`,
|
|
"slippage cap": "0.5% (50 bps)",
|
|
});
|
|
}
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 6b: LST Yield Boost Breakdown ─────────────────────────
|
|
const lstPositions = positions.filter(
|
|
(p) => p.perpSide === "short" && p.asset === "SOL"
|
|
);
|
|
if (lstPositions.length > 0) {
|
|
log("LST", "=== LST Yield Stacking (JitoSOL) ===");
|
|
for (const pos of lstPositions) {
|
|
const annualStaking = pos.size.mul(JITOSOL_APY);
|
|
const dailyStaking = annualStaking.div(365.25);
|
|
const fundingAPY = MOCK_FUNDING[pos.asset].rate * 24 * 365.25;
|
|
const lendingAPY = MOCK_LENDING_RATES[pos.asset];
|
|
const totalAPY = Math.abs(fundingAPY) + JITOSOL_APY + lendingAPY;
|
|
log("LST", `${pos.asset} triple-yield breakdown`, {
|
|
"① funding rate APY": `${(Math.abs(fundingAPY) * 100).toFixed(2)}%`,
|
|
"② JitoSOL staking APY": `${(JITOSOL_APY * 100).toFixed(1)}%`,
|
|
"③ lending yield APY": `${(lendingAPY * 100).toFixed(1)}%`,
|
|
"total APY": `${(totalAPY * 100).toFixed(2)}%`,
|
|
"daily staking yield": `$${dailyStaking.toFixed(4)}`,
|
|
"annual staking yield": `$${annualStaking.toFixed(2)}`,
|
|
"collateral weight": "0.80 (minimal margin drag vs 0.90 raw SOL)",
|
|
});
|
|
}
|
|
console.log();
|
|
}
|
|
|
|
// ── Step 7: Funding Settlement ──────────────────────────────────
|
|
log("SETTLE", "=== Funding Settlement ===");
|
|
let totalFunding = new Decimal(0);
|
|
let totalLending = new Decimal(0);
|
|
|
|
for (const pos of positions) {
|
|
const f = MOCK_FUNDING[pos.asset];
|
|
const hourlyRate = Math.abs(f.rate);
|
|
const fundingPayment = pos.size.mul(hourlyRate);
|
|
const lendingPayment = pos.size.mul(MOCK_LENDING_RATES[pos.asset] / (24 * 365.25));
|
|
|
|
totalFunding = totalFunding.add(fundingPayment);
|
|
totalLending = totalLending.add(lendingPayment);
|
|
|
|
log("SETTLE", `${pos.asset} funding collected`, {
|
|
"hourly payment": `$${fundingPayment.toFixed(6)}`,
|
|
"lending income": `$${lendingPayment.toFixed(6)}`,
|
|
direction: pos.perpSide === "short" ? "shorts collect from longs" : "longs collect from shorts",
|
|
});
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 8: Risk Check ──────────────────────────────────────────
|
|
log("RISK", "=== Risk Assessment ===", {
|
|
"health ratio": "5.00 (healthy, floor: 1.10)",
|
|
drawdown: "0.00% (max: 3.00%)",
|
|
leverage: "1.0x (max: 2.0x)",
|
|
"net delta": "0.0% (neutral, limit: ±5%)",
|
|
violations: "none",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 9: Yield Summary ───────────────────────────────────────
|
|
const hourlyYield = totalFunding.add(totalLending);
|
|
const dailyYield = hourlyYield.mul(24);
|
|
const annualYield = dailyYield.mul(365.25);
|
|
const apy = annualYield.div(initialCapital).mul(100);
|
|
|
|
log("YIELD", "=== Projected Yield Breakdown ===", {
|
|
"hourly funding": `$${totalFunding.toFixed(6)}`,
|
|
"hourly lending": `$${totalLending.toFixed(6)}`,
|
|
"daily yield": `$${dailyYield.toFixed(4)}`,
|
|
"annual yield": `$${annualYield.toFixed(2)}`,
|
|
"estimated APY": `${apy.toFixed(2)}%`,
|
|
"funding component": `${totalFunding.div(hourlyYield).mul(100).toFixed(1)}%`,
|
|
"lending component": `${totalLending.div(hourlyYield).mul(100).toFixed(1)}%`,
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 10: Cycle Summary ──────────────────────────────────────
|
|
const deployedCapital = initialCapital.sub(idleCapital);
|
|
log("SUMMARY", "=== Agent Cycle Complete ===", {
|
|
mode: "drift-only",
|
|
regime,
|
|
positions: `${positions.length}`,
|
|
"deployed capital": `$${deployedCapital.toFixed(2)}`,
|
|
"idle capital": `$${idleCapital.toFixed(2)}`,
|
|
"total PnL": `$${hourlyYield.toFixed(6)}`,
|
|
"health ratio": "5.00",
|
|
"APY estimate": `${apy.toFixed(2)}%`,
|
|
});
|
|
|
|
console.log();
|
|
console.log("=".repeat(60));
|
|
console.log(" Position Structure:");
|
|
console.log("=".repeat(60));
|
|
console.log(` USDC Deposit ($${initialCapital.toFixed(0)}) → Drift Vault`);
|
|
for (const pos of positions) {
|
|
const base = pos.size.div(MOCK_PRICES[pos.asset]).toFixed(4);
|
|
console.log(` ├── ${pos.asset} basis pair ($${pos.size.toFixed(2)})`);
|
|
console.log(` │ ├── ${pos.spotSide.toUpperCase()} ${base} ${pos.asset} spot on Drift`);
|
|
console.log(` │ └── ${pos.perpSide.toUpperCase()} ${base} ${pos.asset}-PERP on Drift`);
|
|
}
|
|
console.log();
|
|
console.log(" Yield Sources:");
|
|
console.log(` │ Perp funding (bi-directional): ~${totalFunding.div(hourlyYield).mul(apy).toFixed(1)}% APY`);
|
|
console.log(` │ Spot lending yield: ~${totalLending.div(hourlyYield).mul(apy).toFixed(1)}% APY`);
|
|
console.log(` └ Total estimated: ~${apy.toFixed(1)}% APY`);
|
|
console.log();
|
|
console.log(" Next rebalance in 8 hours (aligned with funding settlement)");
|
|
|
|
// ── Step 11: Simulate Cycle 2 — Direction Flip ────────────────
|
|
console.log();
|
|
console.log("=".repeat(60));
|
|
console.log(" CYCLE 2: 8 HOURS LATER — DIRECTION FLIP DETECTED");
|
|
console.log("=".repeat(60));
|
|
console.log();
|
|
|
|
// Simulate funding direction change for SOL
|
|
const MOCK_FUNDING_2: typeof MOCK_FUNDING = {
|
|
SOL: { rate: -0.000015, premium: -0.006, imbalance: -0.18 }, // flipped negative!
|
|
BTC: { rate: 0.000020, premium: 0.006, imbalance: 0.14 },
|
|
ETH: { rate: -0.000010, premium: -0.005, imbalance: -0.12 },
|
|
};
|
|
|
|
log("FUNDING", "=== Funding Rate Update ===");
|
|
for (const asset of assets) {
|
|
const f = MOCK_FUNDING_2[asset];
|
|
const oldF = MOCK_FUNDING[asset];
|
|
const annualized = f.rate * 24 * 365.25;
|
|
const oldAnnualized = oldF.rate * 24 * 365.25;
|
|
const flipped = (annualized > 0) !== (oldAnnualized > 0);
|
|
log("FUNDING", `${asset}:`, {
|
|
"old funding APY": `${(oldAnnualized * 100).toFixed(2)}%`,
|
|
"new funding APY": `${(annualized * 100).toFixed(2)}%`,
|
|
direction: annualized >= 0 ? "positive → short perp" : "negative → long perp",
|
|
"direction changed": flipped ? "YES — REBALANCE NEEDED" : "no",
|
|
});
|
|
}
|
|
console.log();
|
|
|
|
// SOL funding flipped negative → need to reverse: close short perp + long spot, open long perp + short spot
|
|
log("REBALANCE", "SOL funding direction flipped: short → long perp", {
|
|
action: "close old position → open reversed position",
|
|
"old legs": "LONG spot + SHORT perp",
|
|
"new legs": "SHORT spot + LONG perp",
|
|
"trading cost": "$0.013 (2x round-trip @ 0.1%)",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 12: Regime Detection ─────────────────────────────────
|
|
const avgRate2 =
|
|
assets.reduce((s, a) => s + MOCK_FUNDING_2[a].rate * 24 * 365.25, 0) /
|
|
assets.length;
|
|
const regime2 =
|
|
avgRate2 > 0.15 ? "bull" : avgRate2 < -0.05 ? "bear" : "neutral";
|
|
|
|
log("REGIME", `Market regime: ${regime2.toUpperCase()}`, {
|
|
"avg funding": `${(avgRate2 * 100).toFixed(2)}%`,
|
|
"regime change": regime2 !== regime ? `${regime} → ${regime2}` : "none",
|
|
"sizing adjustment":
|
|
regime2 === "neutral"
|
|
? "standard allocation (1.0x)"
|
|
: regime2 === "bear"
|
|
? "long perp boosted (1.2x), short perp reduced (0.8x)"
|
|
: "short perp boosted (1.2x), long perp reduced (0.8x)",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 13: Updated Yield ─────────────────────────────────────
|
|
let totalFunding2 = new Decimal(0);
|
|
for (const asset of assets) {
|
|
const f = MOCK_FUNDING_2[asset];
|
|
const posSize = initialCapital.div(3);
|
|
const hourlyRate = Math.abs(f.rate);
|
|
totalFunding2 = totalFunding2.add(posSize.mul(hourlyRate));
|
|
}
|
|
const dailyYield2 = totalFunding2.add(totalLending).mul(24);
|
|
const annualYield2 = dailyYield2.mul(365.25);
|
|
const apy2 = annualYield2.div(initialCapital).mul(100);
|
|
|
|
log("YIELD", "=== Updated Yield After Rebalance ===", {
|
|
"new hourly funding": `$${totalFunding2.toFixed(6)}`,
|
|
"new daily yield": `$${dailyYield2.toFixed(4)}`,
|
|
"new APY estimate": `${apy2.toFixed(2)}%`,
|
|
"trading cost impact": `~0.2% one-time (amortized over ${(365.25 / 8 * 24).toFixed(0)} cycles/year)`,
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 14: Risk Scenario — Health Check ──────────────────────
|
|
console.log("=".repeat(60));
|
|
console.log(" RISK SCENARIO: HEALTH MONITORING");
|
|
console.log("=".repeat(60));
|
|
console.log();
|
|
|
|
log("HEALTH", "Health check (every 2 min between cycles)", {
|
|
"current health": "5.00",
|
|
floor: "1.10",
|
|
status: "HEALTHY",
|
|
});
|
|
|
|
log("HEALTH", "Simulated adverse price move: SOL -8%", {
|
|
"SOL price": "$185.42 → $170.59",
|
|
"perp PnL": "-$0.53 (short SOL perp)",
|
|
"spot PnL": "+$0.53 (long SOL spot, now reversed)",
|
|
"net PnL": "$0.00 (delta-neutral — price-neutral by design)",
|
|
"new health": "4.85 (still healthy)",
|
|
});
|
|
console.log();
|
|
|
|
log("HEALTH", "Simulated extreme scenario: health drops to 1.04", {
|
|
trigger: "health < 1.05",
|
|
action: "EMERGENCY UNWIND — close all positions atomically",
|
|
"capital preserved": "$19.95 (0.25% slippage + fees)",
|
|
"restart after": "next cycle re-evaluates positions from idle",
|
|
});
|
|
console.log();
|
|
|
|
// ── Step 15: Yield Analytics ────────────────────────────────────
|
|
console.log("=".repeat(60));
|
|
console.log(" YIELD ANALYTICS DASHBOARD");
|
|
console.log("=".repeat(60));
|
|
console.log();
|
|
|
|
const tradingCost = new Decimal("0.013"); // from direction flip
|
|
const borrowCost = new Decimal("0.0002"); // estimated borrow on short spot
|
|
const netYield = hourlyYield.sub(tradingCost.div(24)).sub(borrowCost);
|
|
const netAPY = netYield.mul(24).mul(365.25).div(initialCapital).mul(100);
|
|
|
|
log("ANALYTICS", "=== Yield Breakdown ===", {
|
|
"funding income": `$${totalFunding.toFixed(6)}/hr`,
|
|
"lending income": `$${totalLending.toFixed(6)}/hr`,
|
|
"borrow cost": `-$${borrowCost.toFixed(6)}/hr (short spot ETH)`,
|
|
"trading costs": `-$${tradingCost.toFixed(4)} (amortized)`,
|
|
"net yield": `$${netYield.toFixed(6)}/hr`,
|
|
"net APY": `${netAPY.toFixed(2)}%`,
|
|
"capital utilization": `${deployedCapital.div(initialCapital).mul(100).toFixed(0)}%`,
|
|
"risk-adjusted return": `${netAPY.div(3).toFixed(2)} (APY / max drawdown)`,
|
|
});
|
|
console.log();
|
|
|
|
log("ANALYTICS", "=== Per-Asset Attribution ===");
|
|
for (const pos of positions) {
|
|
const f = MOCK_FUNDING[pos.asset];
|
|
const funding = pos.size.mul(Math.abs(f.rate));
|
|
const lending = pos.size.mul(MOCK_LENDING_RATES[pos.asset] / (24 * 365.25));
|
|
const assetAPY = funding.add(lending).mul(24).mul(365.25).div(pos.size).mul(100);
|
|
log("ANALYTICS", `${pos.asset}:`, {
|
|
"position size": `$${pos.size.toFixed(2)}`,
|
|
direction: `${pos.spotSide} spot + ${pos.perpSide} perp`,
|
|
"funding APY": `${(Math.abs(f.rate) * 24 * 365.25 * 100).toFixed(2)}%`,
|
|
"lending APY": `${(MOCK_LENDING_RATES[pos.asset] * 100).toFixed(1)}%`,
|
|
"total per-asset APY": `${assetAPY.toFixed(2)}%`,
|
|
});
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 16: AI Funding Predictor ──────────────────────────────
|
|
console.log("=".repeat(60));
|
|
console.log(" AI FUNDING RATE PREDICTOR");
|
|
console.log("=".repeat(60));
|
|
console.log();
|
|
|
|
log("AI", "=== Prediction Engine (4-Signal Model) ===");
|
|
for (const asset of assets) {
|
|
const f = MOCK_FUNDING[asset];
|
|
const annualized = f.rate * 24 * 365.25;
|
|
// Simulated prediction (in production, uses real historical data)
|
|
const emaSignal = annualized * 0.95; // EMA follows trend
|
|
const momentumSignal = annualized * 1.1; // momentum projects forward
|
|
const meanReversionSignal = annualized * 0.85; // pull toward mean
|
|
const crossAssetSignal = avgRate * 0.3 + annualized * 0.7; // cross-asset blend
|
|
const predicted = emaSignal * 0.4 + momentumSignal * 0.25 +
|
|
meanReversionSignal * 0.2 + crossAssetSignal * 0.15;
|
|
const confidence = Math.min(0.95, 0.5 + Math.abs(f.imbalance) * 2);
|
|
const strength = Math.abs(predicted) > 0.15 ? "strong" :
|
|
Math.abs(predicted) > 0.08 ? "moderate" : "weak";
|
|
|
|
log("AI", `${asset} prediction:`, {
|
|
"EMA signal": `${(emaSignal * 100).toFixed(2)}% (weight 40%)`,
|
|
"momentum signal": `${(momentumSignal * 100).toFixed(2)}% (weight 25%)`,
|
|
"mean reversion": `${(meanReversionSignal * 100).toFixed(2)}% (weight 20%)`,
|
|
"cross-asset": `${(crossAssetSignal * 100).toFixed(2)}% (weight 15%)`,
|
|
"predicted rate": `${(predicted * 100).toFixed(2)}%`,
|
|
confidence: `${(confidence * 100).toFixed(0)}%`,
|
|
"signal strength": strength,
|
|
direction: predicted >= 0 ? "positive → short perp" : "negative → long perp",
|
|
});
|
|
}
|
|
console.log();
|
|
|
|
// ── Step 17: Monitoring Server ───────────────────────────────
|
|
log("MONITOR", "=== HTTP Monitoring Server ===", {
|
|
"GET /": "Agent info + endpoint listing",
|
|
"GET /status": "Full agent status (positions, PnL, APY, health)",
|
|
"GET /positions": "Detailed position breakdown",
|
|
"GET /yield": "Yield analytics with per-asset attribution",
|
|
"GET /predictions": "AI predictor output for each asset",
|
|
"GET /health": "Health ratio + drawdown check",
|
|
port: "3000 (configurable via MONITOR_PORT)",
|
|
});
|
|
console.log();
|
|
|
|
// ── Final Summary ──────────────────────────────────────────────
|
|
console.log("=".repeat(60));
|
|
console.log(" AGENT CAPABILITIES DEMONSTRATED");
|
|
console.log("=".repeat(60));
|
|
console.log(" 1. Bi-directional funding harvesting (positive + negative)");
|
|
console.log(" 2. Atomic cancel + enter (slippage-protected limit orders)");
|
|
console.log(" 3. Funding direction flip detection + auto-rebalance");
|
|
console.log(" 4. Market regime detection (bull/bear/neutral/volatile)");
|
|
console.log(" 5. Regime-aware position sizing (1.2x favorable, 0.8x adverse)");
|
|
console.log(" 6. Settlement-aware timing (defer trades near settlement)");
|
|
console.log(" 7. Lending yield tracking on spot deposits");
|
|
console.log(" 8. Borrow cost tracking on short spot positions");
|
|
console.log(" 9. Health monitoring (every 2 min) + emergency unwind");
|
|
console.log(" 10. Drift Vault delegate trading (on-chain vault PDA)");
|
|
console.log(" 11. Profit share application + withdrawal risk monitoring");
|
|
console.log(" 12. Per-asset-pair delta neutrality enforcement");
|
|
console.log(" 13. Peak equity drawdown tracking (high-water mark)");
|
|
console.log(" 14. Yield analytics with per-asset attribution");
|
|
console.log(" 15. Cobo MPC wallet integration (production key management)");
|
|
console.log(" 16. Config validation + pre-flight deployment checks");
|
|
console.log(" 17. Realistic Drift fee tier modeling (maker/taker blend)");
|
|
console.log(" 18. AI funding rate predictor (EMA + momentum + mean reversion)");
|
|
console.log(" 19. Cross-asset correlation for funding predictions");
|
|
console.log(" 20. Auto-compounding (reinvest yield into idle capital)");
|
|
console.log(" 21. HTTP monitoring server (/status, /yield, /health)");
|
|
console.log(" 22. Crash recovery via persistent state store");
|
|
console.log(" 23. Telegram alerting (cycle summaries, emergency unwinds)");
|
|
console.log(" 24. RPC rate limiter (token-bucket for Drift/Solana)");
|
|
console.log();
|
|
console.log(" Test coverage: 100 tests across 11 suites");
|
|
console.log(" Total codebase: ~11,500 lines of TypeScript");
|
|
console.log();
|
|
console.log(" Agent running. Press Ctrl+C to stop.");
|
|
}
|
|
|
|
simulate().catch(console.error);
|