Strategy Generation

The @vibe Decorator

Every strategy must have exactly ONE function decorated with @vibe. This registers the function as a callback that the engine executes at each tick.

from vibetrading import vibe

@vibe(interval="1m")
def on_tick():
    pass

For live trading, always use interval="1m". Implement frame-skipping for longer intervals:

last_execution_time = None

@vibe(interval="1m")
def strategy():
    global last_execution_time
    current_time = get_current_time()

    manage_risk()

    if last_execution_time and (current_time - last_execution_time).total_seconds() < 300:
        return
    last_execution_time = current_time
    # ... main logic ...

Write Strategies Manually

You can write strategies by hand. A strategy is a Python function decorated with @vibe:

Note: Strategy code uses from vibetrading import vibe, get_price, ... — these symbols are injected at runtime by the backtest engine or live runner. They don't need to be real functions in the package.

AI-Powered Generation

Convenience Function

The simplest way to generate a strategy:

StrategyGenerator Class

For repeated generation or custom configuration:

Closed-Loop: Validate → Fix

The validator produces structured feedback that can be fed back to the LLM:

Closed-Loop: Backtest → Analyze → Regenerate

For iterative improvement based on backtest performance:

Or use vibetrading.strategy.generate() and vibetrading.strategy.analyze() in a loop to iterate manually.

Agent Integration Components

Component
Import
Purpose

generate()

vibetrading.strategy.generate

Generate strategy from natural language

validate()

vibetrading.strategy.validate

Validate generated code

analyze()

vibetrading.strategy.analyze

LLM-powered backtest analysis

StrategyGenerator

vibetrading.strategy.StrategyGenerator

Full generation + validation pipeline

BacktestAnalyzer

vibetrading.strategy.BacktestAnalyzer

LLM-powered backtest analysis (class)

BacktestAnalysisResult

vibetrading.strategy.BacktestAnalysisResult

Structured analysis result

STRATEGY_SYSTEM_PROMPT

vibetrading.strategy.STRATEGY_SYSTEM_PROMPT

Complete system prompt for LLM strategy generation

VIBETRADING_API_REFERENCE

vibetrading.strategy.VIBETRADING_API_REFERENCE

API documentation string

STRATEGY_CONSTRAINTS

vibetrading.strategy.STRATEGY_CONSTRAINTS

Code generation rules

build_generation_prompt()

vibetrading.strategy.build_generation_prompt

Build message list for chat completion

Last updated