Skip to content

feat: autonomous trading toolkit — risk guardrails, PnL tracking, strategy runner#5

Open
dolepee wants to merge 1 commit intoCreator-Bid:mainfrom
dolepee:qdee-trading-toolkit
Open

feat: autonomous trading toolkit — risk guardrails, PnL tracking, strategy runner#5
dolepee wants to merge 1 commit intoCreator-Bid:mainfrom
dolepee:qdee-trading-toolkit

Conversation

@dolepee
Copy link
Copy Markdown

@dolepee dolepee commented Feb 12, 2026

Summary

Complete agent-first trading toolkit for autonomous Clawlett agents, built on top of the CoW Protocol swap engine. All features integrate cleanly with the new CoW presign/batch/fill flow.

  • Risk guardrails on swap.js: max balance usage cap per trade + ETH reserve protection
  • JSON output mode on swap.js: --json flag for machine-readable quote/execute responses (includes orderUid, CoW explorer links)
  • Trade history logger: automatic PnL tracking per swap with CoW order UIDs, CSV export, reset
  • Strategy runner: DCA, limit order, and rebalance strategies with proper CoW async order handling
  • README: updated for CoW Protocol — contracts, env vars, examples

What's included

File Change Lines
swap.js Risk guards + JSON mode + auto trade logging +219/-62
strategy.js New: DCA, limit, rebalance strategy runner +457
trade-history.js New: PnL tracker with JSON/CSV export +227
README.md Updated for CoW Protocol + new feature docs +103

Risk Guardrails (swap.js)

# Cap each trade to 10% of balance
node swap.js --from ETH --to USDC --amount 5 --max-balance-usage-pct 10 --execute

# Keep ETH reserve for gas (accounts for WETH wrapping)
node swap.js --from ETH --to USDC --amount 0.5 --min-safe-eth-reserve 0.01 --execute

Configurable via env (MAX_BALANCE_USAGE_PCT, MIN_SAFE_ETH_RESERVE) or CLI flags.

JSON Output (swap.js)

node swap.js --from ETH --to USDC --amount 0.1 --json
# Returns: { mode, from, to, quote: { amountOut, fee, minReceive, slippagePct, expiresIn } }

node swap.js --from ETH --to USDC --amount 0.1 --json --execute
# Returns: { mode, status, orderUid, explorer, from, to, amountOut, newBalance }

Handles all CoW order states: fulfilled, expired, cancelled, timeout.

PnL Tracking (trade-history.js)

Every executed swap is automatically logged with CoW order UIDs. Tracks stablecoin inflows/outflows for realized PnL.

node trade-history.js              # Human-readable PnL summary
node trade-history.js --json       # Machine-readable
node trade-history.js --export trades.csv  # Includes CoW explorer links

Strategy Runner (strategy.js)

All strategies handle CoW's async order lifecycle (presign → batch → fill) with appropriate timeouts (35 min for executions, 60s for quotes).

# DCA into ETH every hour
node strategy.js --strategy dca --from USDC --to ETH --amount 10 --interval 3600

# Rebalance: maintain 60/40 ETH/USDC (reads balances, computes drift, swaps)
node strategy.js --strategy rebalance --tokens ETH,USDC --target 60,40 --threshold 5

# Limit order: sell when price target met
node strategy.js --strategy limit --from ETH --to USDC --amount 0.1 --min-out 280

# Dry run any strategy
node strategy.js --dry-run --strategy dca --from USDC --to AERO --amount 5

The rebalance strategy reads actual Safe balances, gets USDC-denominated prices via CoW quotes, computes portfolio drift against target allocation, and executes the swap if drift exceeds the threshold.

Why

Autonomous agents competing in the Clawlett challenge need:

  1. Safety rails to prevent oversized trades and gas starvation
  2. Machine-readable output to parse results without brittle log scraping
  3. PnL tracking to measure competition performance
  4. Strategy primitives (DCA, limit, rebalance) as building blocks for custom logic

Scope

Non-breaking: all existing swap.js behavior preserved. New features are opt-in via flags. README updated to reflect CoW Protocol migration.

Supersedes #2 and #3 (consolidated into this PR).

@dolepee dolepee force-pushed the qdee-trading-toolkit branch from 3c0d4e7 to 7d79bb9 Compare February 13, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant