一键导入
order-flow-opt
Order flow MAE optimization workflow - extract features, create strategies, run backtests with exhaustion-timed entries to minimize drawdown
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Order flow MAE optimization workflow - extract features, create strategies, run backtests with exhaustion-timed entries to minimize drawdown
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Search and analyze arXiv papers for quantitative trading research. Use when researching academic papers, finding relevant studies, or literature review.
Look up documentation using Context7 for code and API references. Use when finding documentation, API references, or technical guides.
Complete machine learning pipeline for trading: feature engineering, AutoML, deep learning, and financial RL. Use for automated parameter sweeps, feature creation, model training, and anti-leakage validation.
Comprehensive strategy development workflow from ideation to validation. Use when creating trading strategies, running backtests, parameter optimization, or walk-forward validation.
Extract and learn patterns from trading data continuously. Use when identifying recurring patterns, building adaptive models, or continuous improvement workflows.
NautilusTrader algorithmic trading platform for strategy development and live trading. Use when building trading strategies, backtesting, or deploying to Hyperliquid.
| name | order-flow-opt |
| description | Order flow MAE optimization workflow - extract features, create strategies, run backtests with exhaustion-timed entries to minimize drawdown |
Minimize Maximum Adverse Excursion (MAE) in trading strategies using L2 order book exhaustion detection.
/Users/DanBot/Desktop/HyperFrequency/order-flow-opt/
| Command | Action |
|---|---|
/hyperfrequency:order-flow-opt | Interactive mode - show options |
/hyperfrequency:order-flow-opt download BTC | Download L2 data for BTC |
/hyperfrequency:order-flow-opt extract BTC | Extract features for BTC |
/hyperfrequency:order-flow-opt backtest BTC | Run backtest |
/hyperfrequency:order-flow-opt compare BTC | Compare with/without flow |
/hyperfrequency:order-flow-opt test | Quick verification test |
/hyperfrequency:order-flow-opt create MyStrategy | Create strategy template |
When this skill is invoked, follow these steps:
ls -la /Users/DanBot/Desktop/HyperFrequency/data-historical/hyperliquid/l2book/
If no data or stale, offer to download using memory-safe chunked downloader:
cd /Users/DanBot/Desktop/HyperFrequency/scripts/data
python3 datapull_hl_chunked.py --start YYYY-MM-DD --end YYYY-MM-DD --workers 4
cd /Users/DanBot/Desktop/HyperFrequency/order-flow-opt
python3 scripts/extract_features.py --symbol SYMBOL --start YYYY-MM-DD --end YYYY-MM-DD
Create file in order-flow-opt/src/strategies/:
from src.mae_strategy import MAEOptimizedStrategy, MAEStrategyConfig
from dataclasses import dataclass
@dataclass
class MyConfig(MAEStrategyConfig, frozen=True):
instrument_id: str
bar_type: str
# Your indicator params
ema_fast: int = 8
ema_slow: int = 21
class MyStrategy(MAEOptimizedStrategy):
def indicator_signal(self, bar) -> int:
# YOUR INDICATOR - UNCHANGED
# Return: 1=BUY, -1=SELL, 0=FLAT
if self.ema_fast > self.ema_slow:
return 1
elif self.ema_fast < self.ema_slow:
return -1
return 0
def should_exit(self, bar, flow) -> bool:
# YOUR EXIT LOGIC
return self.hit_stop or self.hit_target
cd /Users/DanBot/Desktop/HyperFrequency/order-flow-opt
python3 scripts/run_backtest.py --symbol BTC --start 2024-01-01 --end 2024-06-30
python3 scripts/run_backtest.py --symbol BTC --compare # Compare with/without
Your indicator tells you WHEN to trade. Order flow tells you the OPTIMAL MOMENT within that window.
Signal: BUY (EMA crossed)
├── Bar 1: Imbalance=-0.33 (sellers active) → WAIT
├── Bar 2: Imbalance=-0.20 (sellers weakening) → WAIT
├── Bar 3: Imbalance=-0.07 (sellers exhausting) → WAIT
├── Bar 4: Imbalance=+0.07 (buyers emerging) → WAIT
└── Bar 5: Imbalance=+0.20, Score=0.60 → ENTER NOW ✓
Result: Instead of -2.1% MAE at Bar 1, you get -0.4% MAE at Bar 5.
| Signal | Description | Weight |
|---|---|---|
IMBALANCE_RECOVERY | Adverse imbalance returning to neutral | 0.25 |
IMBALANCE_FLIP | Imbalance flipped favorable (strongest) | 0.20 |
SPREAD_NORMALIZED | Wide spread returning to normal | 0.15 |
DEPTH_RECOVERY | Depleted side recovering | 0.15 |
MOMENTUM_DECAY | Rate of adverse change slowing | 0.15 |
WALL_ABSORBED | Large opposing orders consumed | 0.10 |
Entry triggers when combined score >= 0.5
| Preset | Window | Threshold | Trade-off |
|---|---|---|---|
conservative | 20 bars | 0.6 | Best MAE, more missed |
balanced | 15 bars | 0.5 | Default |
aggressive | 10 bars | 0.35 | Fewer missed |
| Metric | Without Flow | With Flow |
|---|---|---|
| Avg MAE | -1.8% | -0.8% |
| Win Rate | 52% | 58-62% |
| Trade Count | 100% | 60-75% |
| Sharpe | 1.0 | 1.3-1.6 |
| Data | Path |
|---|---|
| L2 Book Raw | /Users/DanBot/Desktop/HyperFrequency/data-historical/hyperliquid/l2book/ |
| Features | /Users/DanBot/Desktop/HyperFrequency/data-historical/features/ |
| OHLCV | /Users/DanBot/Desktop/HyperFrequency/data-historical/hyperliquid/futures/parquet/ |
| Module | /Users/DanBot/Desktop/HyperFrequency/order-flow-opt/ |
| Config | /Users/DanBot/Desktop/HyperFrequency/order-flow-opt/configs/default.yaml |
| File | Purpose |
|---|---|
src/features.py | OrderFlowFeatures (33 features from L2) |
src/exhaustion.py | ExhaustionDetector (6 signal types) |
src/mae_strategy.py | MAEOptimizedStrategy base class |
src/data_loader.py | L2BookLoader (memory-efficient) |
src/example_ema_strategy.py | Complete EMA+Flow example |
cd /Users/DanBot/Desktop/HyperFrequency/order-flow-opt
python3 -c "
from src.features import OrderFlowFeatures
from src.exhaustion import ExhaustionDetector
flow = OrderFlowFeatures(levels=5)
detector = ExhaustionDetector()
# Signal with selling pressure
bids = [(100.0, 50), (99.9, 100)]
asks = [(100.1, 200), (100.2, 300)]
signal = flow.update(bids, asks, 0)
detector.update(signal)
print(f'Signal: imbalance={signal.book_imbalance:.3f}')
# Simulate exhaustion
for i in range(6):
bids = [(100.0, 50*(1+i*0.3)), (99.9, 100*(1+i*0.3))]
asks = [(100.1, 200*(1-i*0.15)), (100.2, 300*(1-i*0.15))]
snap = flow.update(bids, asks, i+1)
detector.update(snap)
result = detector.detect_sell_exhaustion(snap, signal)
status = '*** ENTER ***' if result.ready else ''
print(f'Bar {i+1}: imb={snap.book_imbalance:+.3f}, score={result.exhaustion_score:.2f} {status}')
if result.ready: break
print('Test passed!')
"
if flow.spread_bps > 12:
return # Skip wide spread
if signal == 1 and flow.book_imbalance < -0.35:
return # Skip buying into sellers
if signal == 1:
result = detector.detect_sell_exhaustion(flow, signal_flow)
if result.ready:
execute()
class MyStrategy(MAEOptimizedStrategy):
def indicator_signal(self, bar) -> int:
return your_indicator(bar) # Unchanged
| Issue | Solution |
|---|---|
| No L2 data | Run datapull_hl_chunked.py |
| Memory overflow | Use chunked downloader with --workers 4 |
| Entry window expires | Lower threshold to 0.4 or increase window |
| Too many filtered | Increase max_spread_bps |
| nautilus_trader import error | pip install nautilus_trader |
/datapull - Download market data/backtest - Run backtests/nautilus-trader-hlfix - NautilusTrader patternsAfter successful optimization:
npx @claude-flow/cli@latest memory store \
--key "pattern/orderflow/mae-$(date +%s)" \
--value "MAE reduced from X% to Y% using exhaustion threshold Z" \
--namespace patterns