Function addOptimizer

  • Registers an optimizer configuration in the framework.

    The optimizer generates trading strategies by:

    • Collecting data from multiple sources across training periods
    • Building LLM conversation history with fetched data
    • Generating strategy prompts using getPrompt()
    • Creating executable backtest code with templates

    The optimizer produces a complete .mjs file containing:

    • Exchange, Frame, Strategy, and Walker configurations
    • Multi-timeframe analysis logic
    • LLM integration for signal generation
    • Event listeners for progress tracking

    Parameters

    • optimizerSchema: IOptimizerSchema

      Optimizer configuration object

      Schema configuration for optimizer registration. Defines how to collect data, generate strategies, and create executable code.

      • Optionalcallbacks?: Partial<IOptimizerCallbacks>

        Optional lifecycle callbacks for monitoring.

      • getPrompt: (symbol: string, messages: MessageModel[]) => string | Promise<string>

        Function to generate strategy prompt from conversation history. Called after all sources are processed for each training range.

      • Optionalnote?: string

        Optional description of this optimizer configuration.

      • optimizerName: string

        Unique identifier for this optimizer. Used to retrieve optimizer instance from registry.

      • rangeTest: IOptimizerRange

        Testing time range for strategy validation. Used in generated Walker to evaluate strategy performance.

      • rangeTrain: IOptimizerRange[]

        Array of training time ranges. Each range generates a separate strategy variant for comparison.

      • source: Source<any>[]

        Array of data sources for strategy generation. Each source contributes to the LLM conversation context.

      • Optionaltemplate?: Partial<IOptimizerTemplate>

        Optional custom template overrides. If not provided, uses defaults from OptimizerTemplateService.

    Returns void

    // Basic optimizer with single data source
    addOptimizer({
    optimizerName: "llm-strategy-generator",
    rangeTrain: [
    {
    note: "Bull market period",
    startDate: new Date("2024-01-01"),
    endDate: new Date("2024-01-31"),
    },
    {
    note: "Bear market period",
    startDate: new Date("2024-02-01"),
    endDate: new Date("2024-02-28"),
    },
    ],
    rangeTest: {
    note: "Validation period",
    startDate: new Date("2024-03-01"),
    endDate: new Date("2024-03-31"),
    },
    source: [
    {
    name: "historical-backtests",
    fetch: async ({ symbol, startDate, endDate, limit, offset }) => {
    // Fetch historical backtest results from database
    return await db.backtests.find({
    symbol,
    date: { $gte: startDate, $lte: endDate },
    })
    .skip(offset)
    .limit(limit);
    },
    user: async (symbol, data, name) => {
    return `Analyze these ${data.length} backtest results for ${symbol}:\n${JSON.stringify(data)}`;
    },
    assistant: async (symbol, data, name) => {
    return "Historical data analyzed successfully";
    },
    },
    ],
    getPrompt: async (symbol, messages) => {
    // Generate strategy prompt from conversation
    return `"Analyze ${symbol} using RSI and MACD. Enter LONG when RSI < 30 and MACD crosses above signal."`;
    },
    callbacks: {
    onData: (symbol, strategyData) => {
    console.log(`Generated ${strategyData.length} strategies for ${symbol}`);
    },
    onCode: (symbol, code) => {
    console.log(`Generated ${code.length} characters of code for ${symbol}`);
    },
    onDump: (symbol, filepath) => {
    console.log(`Saved strategy to ${filepath}`);
    },
    onSourceData: (symbol, sourceName, data, startDate, endDate) => {
    console.log(`Fetched ${data.length} rows from ${sourceName} for ${symbol}`);
    },
    },
    });