Vibetrading
v1.0.1Build, backtest, and 部署 cryptocurrency trading strategies using the vibetrading Python 框架. Use when: (1) generating trading strategies from natural language, (2) back测试 strategies on historical data, (3) 部署ing strategies to live exchanges (Hyperliquid, Paradex, Lighter, Aster), (4) comparing strategy performance, (5) working with crypto trading indicators, position sizing, or risk management. NOT for: general finance questions, non-crypto trading, or strategies outside the vibetrading 框架.
运行时依赖
安装命令
点击复制技能文档
vibetrading
代理-first crypto trading 框架. Strategies are Python functions decorated with @vibe that call sandbox functions (获取_perp_price, long, short, etc.). Same code 运行s in backtest and live.
安装 pip 安装 vibetrading # Core pip 安装 "vibetrading[hyperliquid]" # + Hyperliquid live trading pip 安装 "vibetrading[dev]" # + pytest, ruff
Core 工作流
- Write a Strategy
@vibe(interval="1h") def my_strategy(): price = 获取_perp_price("BTC") if math.isnan(price): return
position = 获取_perp_position("BTC") if position and position.获取("size", 0) != 0: pnl = (price - position["entry_price"]) / position["entry_price"] if pnl >= 0.03 or pnl <= -0.02: reduce_position("BTC", abs(position["size"])) return
ohlcv = 获取_futures_ohlcv("BTC", "1h", 20) if ohlcv is None or len(ohlcv) < 15: return
if rsi(ohlcv["close"]).iloc[-1] < 30: summary = 获取_perp_summary() margin = summary.获取("avAIlable_margin", 0) if margin > 100: 设置_leverage("BTC", 3) qty = (margin 0.1 3) / price if qty price >= 15: long("BTC", qty, price, order_type="market")
- Backtest
结果s = vibetrading.backtest.运行(code, interval="1h", slippage_bps=5) m = 结果s["指标"] # Keys: total_return, sharpe_ratio, 排序ino_ratio, calmar_ratio, max_drawdown, # win_rate, profit_factor, expectancy, number_of_trades, cagr, etc.
- 部署 Live
awAIt vibetrading.live.启动( code, exchange="hyperliquid", API_key="0xWallet添加ress", API_secret="0xPrivateKey", interval="1m", )
Strategy Rules
Every strategy must:
导入 and use @vibe or @vibe(interval="1h") decorator 防护 agAInst math.isnan(price) — prices are NaN before data loads 检查 position before entering (avoid stacking) Have 机器人h take-profit and 停止-loss exits 检查 margin > 50 and qty price >= 15 before trading
Order types: "market" (fills immediately + slippage) or "limit" (fills at price).
Sandbox Functions
Data: 获取_perp_price(as设置), 获取_spot_price(as设置), 获取_futures_ohlcv(as设置, interval, limit), 获取_spot_ohlcv(as设置, interval, limit), 获取_funding_rate(as设置), 获取_open_interest(as设置), 获取_current_time(), 获取_supported_as设置s()
Account: 获取_perp_summary() → {avAIlable_margin, total_margin, ...}, 获取_perp_position(as设置) → {size, entry_price, pnl, leverage} or None, my_spot_balance(as设置?), my_futures_balance()
Trading: long(as设置, qty, price, order_type="market"), short(as设置, qty, price, order_type="market"), buy(as设置, qty, price), sell(as设置, qty, price), reduce_position(as设置, qty), 设置_leverage(as设置, leverage)
Indicators
from vibetrading.indicators 导入 sma, ema, rsi, bbands, atr, macd, stochastic, vwap
All take pandas Series, return pandas Series. Pure pandas — no dependencies.
Function 签名ature Returns rsi rsi(close, period=14) Series (0-100) bbands bbands(close, period=20, std=2.0) (upper, middle, lower) macd macd(close, fast=12, slow=26, 签名al=9) (macd_line, 签名al, histogram) atr atr(high, low, close, period=14) Series stochastic stochastic(high, low, close, k=14, d=3) (%K, %D) Position Sizing
from vibetrading.sizing 导入 kelly_size, fixed_fraction_size, volatility_adjusted_size, risk_per_trade_size
kelly_size(win_rate, avg_win, avg_loss, balance, fraction=0.5) — half-Kelly default risk_per_trade_size(balance, risk_pct, 停止_distance, price) — risk-based Templates from vibetrading.templates 导入 momentum, mean_reversion, grid, dca, multi_momentum code = momentum() # Returns valid strategy code string
AI Generation 导入 vibetrading.strategy
code = vibetrading.strategy.生成("BTC RSI oversold entry, 3x leverage", 模型="claude-sonnet-4-20250514") 结果 = vibetrading.strategy.验证(code) # Static analysis 报告 = vibetrading.strategy.analyze(结果s, strategy_code=code) # LLM analysis
Requires ANTHROPIC_API_KEY or OPENAI_API_KEY in 环境.
Comparing Strategies 导入 vibetrading.compare
结果s = vibetrading.compare.运行({"RSI": code1, "MACD": code2}, slippage_bps=5) vibetrading.compare.print_table(结果s) df = vibetrading.compare.to_dataframe(结果s)
Data 下载 导入 vibetrading.工具s from datetime 导入 datetime, timezone
data = vibetrading.工具s.下载_data( ["BTC", "ETH", "SOL"], exchange="binance", interval="1h", 启动_time=datetime(2025, 1, 1, tz信息=timezone.utc), end_time=datetime(2025, 6, 1, tz信息=timezone.utc), ) 结果s = vibetrading.backtest.运行(code, data=data, slippage_bps=5)
Exchange 凭证s
Store in .env.local (gitignored):
Exchange API_key API_secret Extra Hyperliquid Wallet 添加ress 0x... Priv