Defi Trading Engine
by @avmw2025
DeFi Trading Engine - Autonomous DeFi trading bot with self-improving review system for OpenClaw agents. Use when setting up DeFi trading, crypto trading bot...
clawhub install defi-trading-engineπ About This Skill
name: defi-trading-engine description: DeFi Trading Engine - Autonomous DeFi trading bot with self-improving review system for OpenClaw agents. Use when setting up DeFi trading, crypto trading bot, automated trading, Base chain trading, Bankr integration, trading engine, self-improving bot, or trading strategy execution.
DeFi Trading Engine
Autonomous DeFi trading bot with self-improving review system. Scans for opportunities, executes trades, logs performance, and learns from mistakes.
When to Use
Apply this skill when:
Architecture
Self-Improvement Loop:
scan β evaluate β execute β log β review β patch params β repeat
Components:
1. Token Scanner (scan-tokens.py) β Finds trading opportunities
2. Risk Manager (risk-manager.py) β Enforces position limits and risk rules
3. Trade Executor (trade-executor.py) β Executes trades via Bankr CLI
4. Daily Review (daily-review.py) β Analyzes performance and suggests improvements
5. Config File (trading-config.json) β Central configuration for all parameters
Quick Start
1. Setup
Create workspace:
mkdir -p ~/trading-bot/{trades,reviews}
cd ~/trading-bot
Copy skill scripts:
cp ~/.openclaw/skills/defi-trading-engine/scripts/* .
2. Configure
Create trading-config.json:
{
"risk": {
"max_position_size_usd": 40,
"take_profit_pct": 4,
"stop_loss_pct": 8,
"max_active_positions": 5,
"max_daily_trades": 8,
"cooldown_minutes": 30,
"max_drawdown_pct": 15
},
"strategy": {
"type": "momentum_swing",
"entry_signal": "volume_spike_and_price_up",
"exit_signal": "take_profit_or_stop_loss",
"timeframe": "15min"
},
"bankr": {
"chain": "base",
"wallet": "trading-wallet",
"slippage_pct": 1.5
},
"data_sources": {
"use_coingecko_trending": true,
"use_dexscreener": true,
"min_liquidity_usd": 50000,
"min_volume_24h_usd": 100000
}
}
3. Setup Bankr (if needed)
See references/bankr-setup.md for Bankr CLI setup.
4. Run the Trading Loop
Manual execution:
# 1. Scan for opportunities
python3 scan-tokens.py --output candidates.json2. Review candidates
cat candidates.json3. Execute a trade (after risk check)
python3 trade-executor.py --symbol SOL --action buy --amount 404. Run daily review
python3 daily-review.py
Automated loop (cron):
# Run scanner every 30 minutes
*/30 * * * * cd ~/trading-bot && python3 scan-tokens.py --output candidates.jsonRun daily review at 23:00
0 23 * * * cd ~/trading-bot && python3 daily-review.py
Core Scripts
scan-tokens.py
Scans for trading opportunities using free APIs.
Data Sources:
Output (candidates.json):
[
{
"symbol": "SOL",
"name": "Solana",
"price": 145.5,
"volume_24h": 2800000000,
"volume_spike_ratio": 1.8,
"price_change_1h_pct": 2.5,
"price_change_24h_pct": 5.2,
"liquidity_usd": 850000000,
"score": 8.5,
"signals": ["trending", "volume_spike", "momentum_up"]
}
]
Usage:
python3 scan-tokens.py --output candidates.json --min-score 7.0
risk-manager.py
Enforces risk limits before every trade. Acts as the gatekeeper.
Checks:
Usage:
python3 risk-manager.py --action check --symbol SOL --amount 40
Exit Codes:
0 β Trade approved1 β Trade denied (prints reason)Example Output:
β
Risk check passed
- Position size: $40 (limit: $40)
- Active positions: 3 (limit: 5)
- Daily trades: 5 (limit: 8)
- Cooldown: OK (35 minutes since last trade)
- Drawdown: 8.5% (limit: 15%)
trade-executor.py
Executes trades via Bankr CLI (or generic DEX interface).
Supported Actions:
buy β Market buysell β Market selllimit_buy β Limit order buylimit_sell β Limit order sellset_stop_loss β Stop-loss orderset_take_profit β Take-profit orderUsage:
# Market buy
python3 trade-executor.py --symbol SOL --action buy --amount 40Sell with stop-loss
python3 trade-executor.py --symbol SOL --action sell --stop-loss-pct 8
Trade Log (trades/YYYY-MM-DD.json):
[
{
"timestamp": "2026-03-13T15:45:00Z",
"symbol": "SOL",
"action": "buy",
"amount_usd": 40,
"price": 145.5,
"quantity": 0.275,
"tx_hash": "0xabc123...",
"status": "success",
"take_profit_price": 151.32,
"stop_loss_price": 133.86
}
]
daily-review.py
Analyzes trade history, calculates P&L, identifies weaknesses, and suggests parameter adjustments.
Metrics Calculated:
Output (reviews/review-YYYY-MM-DD.md):
# Trading Review β 2026-03-13
Performance Summary
Top Performers
1. SOL: +$18.50 (+12.7%) 2. LINK: +$12.20 (+8.1%)Worst Performers
1. UNI: -$8.50 (-5.7%)Pattern Analysis
Recommended Adjustments
1. Increase min_liquidity_usd from $50k to $100k (low liquidity trades underperformed)
2. Add volatility filter (skip trades when VIX > 30)
3. Tighten stop-loss to 6% (avg loss exceeds target)Next Actions
[ ] Update trading-config.json with new parameters
[ ] Backtest on last 30 days with new rules
[ ] Monitor performance for 1 week before further changes
Usage:
python3 daily-review.py --start-date 2026-03-01 --end-date 2026-03-13
Configuration Reference
Risk Parameters
| Parameter | Default | Purpose |
|-----------|---------|---------|
| max_position_size_usd | 40 | Max $ per trade |
| take_profit_pct | 4 | Exit when +4% gain |
| stop_loss_pct | 8 | Exit when -8% loss |
| max_active_positions | 5 | Max concurrent positions |
| max_daily_trades | 8 | Max trades per day |
| cooldown_minutes | 30 | Wait time between trades |
| max_drawdown_pct | 15 | Stop trading if down 15% |
Strategy Parameters
| Parameter | Options | Purpose |
|-----------|---------|---------|
| type | momentum_swing, mean_reversion, dca, asymmetric | Strategy type |
| entry_signal | volume_spike_and_price_up, oversold, breakout | Entry condition |
| exit_signal | take_profit_or_stop_loss, reversal, time_based | Exit condition |
| timeframe | 5min, 15min, 1h, 4h | Trading timeframe |
Bankr Integration
| Parameter | Default | Purpose |
|-----------|---------|---------|
| chain | base | EVM chain (base, ethereum, polygon) |
| wallet | trading-wallet | Bankr wallet name |
| slippage_pct | 1.5 | Max acceptable slippage |
Strategy Templates
See references/strategies.md for detailed strategy implementations:
1. DCA (Dollar-Cost Averaging) β Buy fixed amount on schedule 2. Momentum Swing β Ride short-term momentum with tight stops 3. Mean Reversion β Buy dips, sell rallies 4. Asymmetric Bets β Small positions on high-upside opportunities
Risk Management Rules
The risk manager enforces these rules:
Position Sizing
Position size β€ max_position_size_usd
Active Position Limit
count(open_positions) < max_active_positions
Daily Trade Limit
count(trades_today) < max_daily_trades
Cooldown Period
time_since_last_trade β₯ cooldown_minutes
Max Drawdown Circuit Breaker
if current_drawdown β₯ max_drawdown_pct:
halt_all_trading()
send_alert()
When max drawdown is hit, all trading stops until manually reset.
Self-Improvement Process
The bot learns from performance:
1. Daily Review
Run daily-review.py to analyze trades.
2. Pattern Recognition Identify which setups worked:
3. Parameter Adjustment
Update trading-config.json based on findings:
4. Backtest Changes Test new parameters on historical data (manual or automated).
5. Monitor Run new parameters for 7 days, then review again.
Cycle: Weekly reviews β Parameter tweaks β Monitor β Repeat
Safety Features
β DO:
β DON'T:
Monitoring & Alerts
Track bot health:
Check active positions:
jq '.[] | select(.status == "open")' trades/*.json
Check today's P&L:
python3 daily-review.py --start-date $(date +%Y-%m-%d) --end-date $(date +%Y-%m-%d)
Alert on max drawdown:
# Add to cron (every hour)
python3 risk-manager.py --action check_drawdown && echo "Trading halted: max drawdown exceeded"
Troubleshooting
Problem: Risk manager denies all trades
Solution: Check trading-config.json limits. May have hit daily trade limit or max drawdown.
Problem: Trades execute but P&L is negative
Solution: Run daily-review.py to identify losing patterns. Tighten entry filters or adjust stop-loss.
Problem: Bankr CLI errors
Solution: Check wallet balance, network connection, and gas fees. See references/bankr-setup.md.
Problem: Scanner returns no candidates
Solution: Lower min_score threshold or relax liquidity filters.
Advanced Features
Paper Trading Mode
Test strategies without real funds:
{
"mode": "paper",
"paper_balance_usd": 1000
}
All trades simulate execution, no real transactions.
Multi-Strategy Support
Run multiple strategies in parallel:
{
"strategies": [
{
"name": "momentum",
"allocation_pct": 60,
"config": { ... }
},
{
"name": "mean_reversion",
"allocation_pct": 40,
"config": { ... }
}
]
}
Backtesting
Test parameters on historical data (requires historical price data):
python3 backtest.py --start 2026-01-01 --end 2026-03-01 --config trading-config.json
*(Backtest script not included β implement based on your data source)*
Resources
references/bankr-setup.mdreferences/strategies.mdVersion: 1.0 Last Updated: 2026-03-13 Security Note: Store API keys and wallet private keys securely. Never commit to Git.
β‘ When to Use
π‘ Examples
1. Setup
Create workspace:
mkdir -p ~/trading-bot/{trades,reviews}
cd ~/trading-bot
Copy skill scripts:
cp ~/.openclaw/skills/defi-trading-engine/scripts/* .
2. Configure
Create trading-config.json:
{
"risk": {
"max_position_size_usd": 40,
"take_profit_pct": 4,
"stop_loss_pct": 8,
"max_active_positions": 5,
"max_daily_trades": 8,
"cooldown_minutes": 30,
"max_drawdown_pct": 15
},
"strategy": {
"type": "momentum_swing",
"entry_signal": "volume_spike_and_price_up",
"exit_signal": "take_profit_or_stop_loss",
"timeframe": "15min"
},
"bankr": {
"chain": "base",
"wallet": "trading-wallet",
"slippage_pct": 1.5
},
"data_sources": {
"use_coingecko_trending": true,
"use_dexscreener": true,
"min_liquidity_usd": 50000,
"min_volume_24h_usd": 100000
}
}
3. Setup Bankr (if needed)
See references/bankr-setup.md for Bankr CLI setup.
4. Run the Trading Loop
Manual execution:
# 1. Scan for opportunities
python3 scan-tokens.py --output candidates.json2. Review candidates
cat candidates.json3. Execute a trade (after risk check)
python3 trade-executor.py --symbol SOL --action buy --amount 404. Run daily review
python3 daily-review.py
Automated loop (cron):
# Run scanner every 30 minutes
*/30 * * * * cd ~/trading-bot && python3 scan-tokens.py --output candidates.jsonRun daily review at 23:00
0 23 * * * cd ~/trading-bot && python3 daily-review.py
π Tips & Best Practices
Problem: Risk manager denies all trades
Solution: Check trading-config.json limits. May have hit daily trade limit or max drawdown.
Problem: Trades execute but P&L is negative
Solution: Run daily-review.py to identify losing patterns. Tighten entry filters or adjust stop-loss.
Problem: Bankr CLI errors
Solution: Check wallet balance, network connection, and gas fees. See references/bankr-setup.md.
Problem: Scanner returns no candidates
Solution: Lower min_score threshold or relax liquidity filters.