vibetrading-ai-trading-code-generator
by @liuhaonan00
Generate executable Hyperliquid trading strategy code from natural language prompts. Use when a user wants to create automated trading strategies for Hyperliquid exchange based on their trading ideas, technical indicators, or VibeTrading signals. The skill generates complete Python code with proper error handling, logging, and configuration using actual Hyperliquid API wrappers.
clawhub install vibetrading-code-genπ About This Skill
name: vibetrading-code-gen description: Generate executable Hyperliquid trading strategy code from natural language prompts. Use when a user wants to create automated trading strategies for Hyperliquid exchange based on their trading ideas, technical indicators, or VibeTrading signals. The skill generates complete Python code with proper error handling, logging, and configuration using actual Hyperliquid API wrappers. metadata: { "openclaw": { "emoji": "π€", "requires": { "bins": ["python3"] }, "min_python_version": "3.6" } }
VibeTrading Code Generator
Generate executable Hyperliquid trading strategy code from natural language prompts. This skill transforms trading ideas into ready-to-run Python code using actual Hyperliquid API implementations. Generated code includes complete API integration, error handling, logging, and configuration management.
Quick Start
Basic Usage
# Generate a simple RSI strategy
python scripts/strategy_generator.py "Generate a BTC RSI strategy, buy below 30, sell above 70"Generate a grid trading strategy
python scripts/strategy_generator.py "BTC grid trading 50000-60000 10 grids 0.01 BTC per grid"Generate a signal-following strategy
python scripts/strategy_generator.py "ETH trading strategy based on VibeTrading signals, buy on bullish signals, sell on bearish signals"
Output Structure
The generator creates: 1. Strategy Python file - Complete trading strategy class 2. Configuration file - Strategy parameters and settings 3. Usage instructions - How to run and monitor the strategy 4. Requirements file - Python dependencies
Code Validation System
Automatic Code Validation
All generated code is automatically validated and fixed using the built-in validation system:
# Validate generated code
python scripts/code_validator.py generated_strategy.pyValidate and fix automatically
python scripts/code_validator.py generated_strategy.py --fixValidate entire directory
python scripts/code_validator.py strategy_directory/
Validation Steps
The validation system performs these checks:
1. Syntax Validation - Python syntax checking 2. Import Validation - Module import verification 3. Compatibility Checks - Python 3.5+ compatibility 4. Common Issue Detection - Missing imports, encoding issues, etc.
Automatic Fixes
When validation fails, the system automatically fixes common issues:
1. Add missing imports - Add typing imports if type annotations are used
2. Fix encoding declaration - Add # -*- coding: utf-8 -*- if missing
3. Remove incompatible syntax - Remove f-strings and type annotations for Python 3.5 compatibility
4. Fix import paths - Add sys.path modifications for API wrappers
5. Fix logger initialization order - Ensure logger is initialized before API client
6. Remove pathlib usage - Replace with os.path for Python 3.4 compatibility
7. Fix string formatting - Convert f-strings to .format() method
Validation Configuration
The validation system can be configured via command-line arguments:
# Basic validation
python scripts/code_validator.py strategy.pyValidate and fix automatically
python scripts/code_validator.py strategy.py --fixUse specific Python executable
python scripts/code_validator.py strategy.py --python python3.6Validate directory with all files
python scripts/code_validator.py strategies/ --fixMaximum 5 fix iterations
python scripts/code_validator.py strategy.py --fix --max-iterations 5
Validation Rules
The system enforces these rules for generated code:
1. Python 3.5+ Compatibility
- No f-strings (use .format() or % formatting)
- No type annotations (remove or use comments)
- No pathlib (use os.path instead)
- No typing module imports
2. Code Quality
- Proper encoding declaration (# -*- coding: utf-8 -*-)
- Logger initialized before API client
- All imports are resolvable
- No syntax errors
3. Security - API keys loaded from environment variables - No hardcoded credentials - Proper error handling for API calls
4. Performance - Reasonable check intervals (not too frequent) - Efficient data fetching - Proper resource cleanup
Validation Workflow
User Prompt β Code Generation β Validation β Fixes β Final Code
β
If validation fails
β
Apply automatic fixes
β
Re-validate until success
β
Deliver validated code
Validation Failure Handling
When validation fails, the system automatically updates the code with these steps:
1. Error Analysis - Identify the specific validation errors 2. Fix Application - Apply appropriate fixes based on error type 3. Re-validation - Validate again after fixes 4. Iterative Repair - Repeat until code is valid (max 3 iterations) 5. Fallback Strategy - If automatic fixes fail, provide detailed error report and manual fix instructions
Automatic Fix Examples
#### Fix 1: Missing Imports
# Before (error: NameError: name 'List' is not defined)
def calculate_prices(prices: List[float]) -> List[float]:After (automatic fix)
from typing import List, Dict, Optional
def calculate_prices(prices):
#### Fix 2: Encoding Issues
# Before (error: SyntaxError: Non-ASCII character)
Strategy description: Grid trading
After (automatic fix)
-*- coding: utf-8 -*-
Strategy description: Grid trading
#### Fix 3: Python 3.5 Incompatibility
# Before (error: SyntaxError in Python 3.5)
price = f"Current price: {current_price}"After (automatic fix)
price = "Current price: {}".format(current_price)
#### Fix 4: Import Path Issues
# Before (error: ImportError: No module named 'hyperliquid_api')
from hyperliquid_api import HyperliquidClientAfter (automatic fix)
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "api_wrappers"))
from hyperliquid_api import HyperliquidClient
Supported Strategy Types
1. Technical Indicator Strategies
2. Advanced Trading Strategies
3. Signal-Driven Strategies
How It Works
Step 1: Prompt Analysis
The generator analyzes your natural language prompt to identify:Step 2: Template Selection
Based on the analysis, the system selects the most appropriate template from:templates/grid_trading.py - Grid trading strategy templateStep 3: Code Generation
The generator: 1. Fills template parameters with your values 2. Adds proper error handling and logging 3. Includes configuration management 4. Generates complete runnable codeStep 4: Code Validation
The generated code is automatically validated and fixed: 1. Syntax checking - Ensure valid Python syntax 2. Import verification - Check all imports are resolvable 3. Compatibility testing - Verify Python 3.5+ compatibility 4. Automatic fixes - Apply fixes for common issues 5. Re-validation - Validate again after fixes 6. Error reporting - If fixes fail, provide detailed error reportValidation Failure Handling
If validation fails after automatic fixes: 1. Error Analysis Report - Detailed breakdown of remaining issues 2. Manual Fix Instructions - Step-by-step guidance for manual fixes 3. Fallback Template - Option to use a simpler, validated template 4. Support Contact - Instructions for getting helpStep 5: Output Delivery
You receive validated, runnable code including: 1. Validated Python strategy file - Fully tested and fixed 2. Configuration template - Strategy parameters and settings 3. Validation report - Summary of validation results and fixes applied 4. Usage instructions - How to run and monitor the strategy 5. Troubleshooting guide - Common issues and solutions 6. Risk warnings - Important safety informationAPI Integration
The generated code uses mature Hyperliquid API implementations that support:
Trading Operations
Market Data
Account Management
Template System
Template Structure
Each template includes:Available Templates
#### Grid Trading Template
grid_trading.py - Grid trading within price ranges (Python 3.5+ compatible)Configuration Management
Strategy Configuration
Generated strategies include configurable parameters:STRATEGY_CONFIG = {
"symbol": "BTC",
"timeframe": "1h",
"parameters": {
"rsi_period": 14,
"oversold": 30,
"overbought": 70
},
"risk_management": {
"position_size": 0.01,
"stop_loss": 0.05,
"take_profit": 0.10,
"max_drawdown": 0.20
}
}
Environment Setup
# Required environment variables
export HYPERLIQUID_API_KEY="your_api_key_here"
export HYPERLIQUID_ACCOUNT_ADDRESS="your_address_here"
export TELEGRAM_BOT_TOKEN="optional_for_alerts"
Risk Management Features
All generated strategies include:
1. Position Sizing
2. Stop Loss Mechanisms
3. Risk Controls
4. Monitoring & Alerts
Integration with VibeTrading Signals
Generated strategies can integrate with VibeTrading Global Signals:
from vibetrading import get_latest_signalsGet AI-generated signals
signals = get_latest_signals("BTC,ETH")Use signals in trading logic
if signals["BTC"]["sentiment"] == "BULLISH":
strategy.execute_buy("BTC", amount=0.01)
Usage Examples
Example 1: Simple RSI Strategy
Prompt: "Generate a BTC RSI strategy, buy 0.01 BTC when RSI below 30, sell when above 70"Generated Code Features:
Example 2: Grid Trading Strategy
Prompt: "ETH grid trading strategy, price range 3000-4000, 20 grids, 0.1 ETH per grid"Generated Code Features:
Example 3: Signal-Based Strategy
Prompt: "SOL trading strategy based on VibeTrading signals, buy on bullish signals, sell on bearish signals, 10 SOL per trade"Generated Code Features:
Best Practices
1. Start with Paper Trading
2. Risk Management
3. Monitoring & Maintenance
4. Security
Troubleshooting
Common Issues
#### 1. API Connection Errors
# Check API key and account address
echo $HYPERLIQUID_API_KEY
echo $HYPERLIQUID_ACCOUNT_ADDRESSTest API connection
python scripts/test_connection.py
#### 2. Strategy Not Executing Trades
#### 3. Performance Issues
#### 4. Integration Issues with VibeTrading
#### 5. Validation Errors
# Common validation errors and solutions:Error: "SyntaxError: invalid syntax"
Solution: Check for f-strings or type annotations
python scripts/code_validator.py strategy.py --fixError: "ImportError: No module named 'typing'"
Solution: Remove typing imports (Python 3.4 compatibility)
sed -i '' 's/from typing import.*//g' strategy.pyError: "SyntaxError: Non-ASCII character"
Solution: Add encoding declaration
echo -e '# -*- coding: utf-8 -*-\n' | cat - strategy.py > temp && mv temp strategy.pyError: "NameError: name 'List' is not defined"
Solution: Remove type annotations or add typing import
sed -i '' 's/: List//g; s/: Dict//g; s/: Optional//g' strategy.pyManual validation check
python -m py_compile strategy.py
#### 6. Code Generation Failures
Advanced Features
Custom Template Creation
You can create custom templates intemplates/custom/:1. Create a new template file
2. Define template variables with {{variable_name}}
3. Add to template registry in scripts/template_registry.py
4. Test with the generator
Strategy Backtesting
While this generator focuses on live trading, you can: 1. Export generated code to backtesting frameworks 2. Use historical data for strategy validation 3. Add performance metrics and analysisMulti-Strategy Management
For running multiple strategies: 1. Generate separate strategy files 2. Use different configuration files 3. Monitor overall portfolio risk 4. Implement strategy allocation logicSupport & Updates
Getting Help
examples/Updates
This skill will be updated with:Backtesting Integration
Backtest Evaluation Feature
After generating a strategy, you can now evaluate its performance using our integrated backtesting system:
# Generate a strategy
python scripts/strategy_generator.py "BTC grid trading 50000-60000 10 grids 0.01 BTC per grid"Run backtest on the generated strategy
python scripts/backtest_runner.py generated_strategies/btc_grid_trading_strategy.pyRun backtest with custom parameters
python scripts/backtest_runner.py generated_strategies/btc_grid_trading_strategy.py \
--start-date 2025-01-01 \
--end-date 2025-03-01 \
--initial-balance 10000 \
--interval 1h
Backtest Features
The backtesting system provides:
1. Historical Data Simulation - Uses historical price data for realistic testing 2. Performance Metrics - Calculates key metrics: - Total Return (%) - Maximum Drawdown (%) - Sharpe Ratio - Win Rate (%) - Total Trades - Average Trade Duration
3. Risk Analysis - Evaluates strategy risk characteristics 4. Visual Reports - Generates charts and performance reports 5. Comparative Analysis - Compares strategy performance against benchmarks
Backtest Configuration
You can configure backtests with these parameters:
BACKTEST_CONFIG = {
"start_date": "2025-01-01",
"end_date": "2025-03-01",
"initial_balance": 10000, # USDC
"interval": "1h", # 1m, 5m, 15m, 30m, 1h, 4h, 1d
"symbols": ["BTC", "ETH"], # Trading symbols
"commission_rate": 0.001, # 0.1% trading commission
"slippage": 0.001, # 0.1% slippage
}
Backtest Results Example
π Backtest Results for BTC Grid Trading Strategy
================================================
π
Period: 2025-01-01 to 2025-03-01 (60 days)
π° Initial Balance: $10,000.00
π° Final Balance: $11,234.56π Performance Metrics:
β’ Total Return: +12.35%
β’ Max Drawdown: -5.67%
β’ Sharpe Ratio: 1.45
β’ Win Rate: 58.3%
β’ Total Trades: 120
β’ Avg Trade Duration: 12.5 hours
π Trade Analysis:
β’ Winning Trades: 70
β’ Losing Trades: 50
β’ Largest Win: +$245.67
β’ Largest Loss: -$123.45
β’ Avg Win: +$89.12
β’ Avg Loss: -$56.78
β οΈ Risk Assessment:
β’ Risk-Adjusted Return: Good
β’ Drawdown Control: Acceptable
β’ Consistency: Moderate
Backtest Integration in Generated Code
Generated strategies now include backtest compatibility:
# Generated strategy includes backtest method
strategy = GridTradingStrategy(api_key, account_address, config)Run backtest
backtest_results = strategy.run_backtest(
start_date="2025-01-01",
end_date="2025-03-01",
initial_balance=10000
)Generate backtest report
strategy.generate_backtest_report(backtest_results)
Backtest Data Sources
The backtesting system uses:
Backtest Limitations
Important Notes: 1. Past performance β future results - Historical success doesn't guarantee future profits 2. Data quality - Results depend on historical data accuracy 3. Market conditions - Past market conditions may differ from future 4. Execution assumptions - Assumes perfect order execution (configurable slippage) 5. Liquidity assumptions - Assumes sufficient market liquidity
Best Practices: 1. Always backtest with multiple time periods 2. Test different market conditions (bull, bear, sideways) 3. Use realistic commission and slippage settings 4. Start with small position sizes in live trading 5. Monitor strategy performance and adjust as needed
Code Validation Disclaimer
Validation Limitations: While the code validation system automatically fixes common issues, it cannot guarantee: 1. Trading logic correctness - Validation checks syntax, not trading logic 2. Financial performance - No guarantee of profitability 3. API compatibility - Hyperliquid API changes may break generated code 4. Security vulnerabilities - Manual security review is recommended 5. Edge case handling - All possible error conditions may not be covered
Validation Success Criteria: Code is considered "valid" when: 1. No syntax errors 2. All imports are resolvable 3. Python 3.6+ compatible 4. Basic structure is correct
Not Validated:
Quick Reference
Python Version Requirements
# Check Python version
python scripts/check_python_version.pyMinimum: Python 3.6+ (for f-string support)
Basic Usage
# Generate strategy
python scripts/strategy_generator.py "BTC grid trading 50000-60000 10 grids"Run backtest
python scripts/backtest_runner.py generated_strategies/btc_grid_trading_strategy.py
Key Features
1. Python 3.6+ Compatibility - Modern Python features including f-strings 2. Automatic Backtest Integration - Evaluate strategies before live trading 3. Comprehensive Validation - Syntax and compatibility checking 4. Risk Management - Built-in risk controls in all strategiesTrading Disclaimer
Important: Trading cryptocurrencies involves significant risk. Generated strategies should be thoroughly tested before use with real funds. Past performance is not indicative of future results. Always use proper risk management and never trade with money you cannot afford to lose.
The code generator provides tools for strategy creation, but ultimate responsibility for trading decisions and risk management lies with the user.
Validation is not a substitute for: 1. Thorough testing - Always test in simulation first 2. Code review - Have experienced developers review generated code 3. Security audit - Check for vulnerabilities before deployment 4. Performance testing - Test under various market conditions 5. Risk assessment - Evaluate strategy risks independently
Backtesting Limitations: 1. Historical data quality - Results depend on data accuracy 2. Market condition changes - Past conditions may differ from future 3. Execution assumptions - Assumes perfect order execution 4. Liquidity assumptions - Assumes sufficient market liquidity 5. No guarantee of future performance - Past success β future profits
π‘ Examples
Basic Usage
# Generate a simple RSI strategy
python scripts/strategy_generator.py "Generate a BTC RSI strategy, buy below 30, sell above 70"Generate a grid trading strategy
python scripts/strategy_generator.py "BTC grid trading 50000-60000 10 grids 0.01 BTC per grid"Generate a signal-following strategy
python scripts/strategy_generator.py "ETH trading strategy based on VibeTrading signals, buy on bullish signals, sell on bearish signals"
Output Structure
The generator creates: 1. Strategy Python file - Complete trading strategy class 2. Configuration file - Strategy parameters and settings 3. Usage instructions - How to run and monitor the strategy 4. Requirements file - Python dependencies