Documentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-docs/llms.txt
Use this file to discover all available pages before exploring further.
@backtest-kit/ollama is a universal LLM adapter that abstracts provider differences behind a single higher-order function API. Wrap your getSignal logic with deepseek(), claude(), gpt5(), or ollama() and the package handles authentication, structured output enforcement, prompt context injection, and fallback chains — so your strategy code stays focused on trading logic rather than LLM plumbing.
Install
npm install @backtest-kit/ollama agent-swarm-kit backtest-kit
Supported Providers
| Provider | HOF Function | Base URL |
|---|
| OpenAI (GPT-5, GPT-4o) | gpt5() | https://api.openai.com/v1/ |
| Anthropic Claude | claude() | https://api.anthropic.com/v1/ |
| DeepSeek | deepseek() | https://api.deepseek.com/ |
| Grok (xAI) | grok() | https://api.x.ai/v1/ |
| Mistral | mistral() | https://api.mistral.ai/v1/ |
| Perplexity | perplexity() | https://api.perplexity.ai/ |
| Cohere | cohere() | https://api.cohere.ai/compatibility/v1/ |
| Alibaba (Qwen) | alibaba() | https://dashscope-intl.aliyuncs.com/compatible-mode/v1/ |
| Hugging Face | hf() | https://router.huggingface.co/v1/ |
| Ollama (local) | ollama() | http://localhost:11434/ |
| GLM-4 (Zhipu AI) | glm4() | https://open.bigmodel.cn/api/paas/v4/ |
Unified HOF API
Each provider exposes a higher-order function that wraps your signal generation logic. The wrapped function receives the same arguments as the original and injects the LLM context automatically:
import { deepseek, Module, commitPrompt, MessageModel } from '@backtest-kit/ollama';
import { addStrategySchema } from 'backtest-kit';
import { json } from 'agent-swarm-kit';
const signalModule = Module.fromPath('./signal.prompt.cjs');
const getSignal = async () => {
const messages: MessageModel[] = [];
await commitPrompt(signalModule, messages);
const { data } = await json('SignalOutline', messages);
return data;
};
addStrategySchema({
strategyName: 'llm-signal',
interval: '5m',
getSignal: deepseek(getSignal, 'deepseek-chat', process.env.DEEPSEEK_API_KEY),
});
All provider HOFs share the same signature:
provider(fn, model, apiKey?) => fn
Structured Output Enforcement
Define your signal schema with Zod and pass it to addOutline. The package enforces the schema on every LLM response — invalid shapes trigger automatic retries, so your strategy never receives malformed data:
// schema/Signal.schema.ts
import { z } from 'zod';
export const SignalSchema = z.object({
position: z.enum(['long', 'short', 'wait']),
price_open: z.number(),
price_stop_loss: z.number(),
price_take_profit: z.number(),
minute_estimated_time: z.number(),
risk_note: z.string(),
});
Custom validations can enforce trading-specific rules (e.g. SL must be below entry for a long) and are run after schema parsing:
validations: [
{
validate: ({ data }) => {
if (data.position === 'long' && data.price_stop_loss >= data.price_open) {
throw new Error('For LONG, stop_loss must be below price_open');
}
},
},
],
Token Rotation
Pass an array of API keys to any provider HOF for automatic rotation across requests. Useful for high-frequency backtests that would exhaust a single key’s rate limit:
import { ollama } from '@backtest-kit/ollama';
const wrappedFn = ollama(myFn, 'llama3.3:70b', ['key1', 'key2', 'key3']);
Prompt Modules
Load prompts from .cjs files in config/prompt/. Both system and user prompts can be functions that receive trading context automatically:
// config/prompt/signal.prompt.cjs
module.exports = {
system: (symbol, strategyName, exchangeName, frameName, backtest) => [
`You are analyzing ${symbol} on ${exchangeName}`,
`Strategy: ${strategyName}, Timeframe: ${frameName}`,
backtest ? 'Backtest mode' : 'Live mode',
],
user: (symbol) => `Analyze ${symbol} and return trading decision`,
};
The five context arguments — symbol, strategyName, exchangeName, frameName, backtest — are injected automatically from the active execution context. No manual wiring needed.
Prompt modules are cached with memoize from functools-kit, so repeated calls during a backtest do not re-read the file from disk on every tick.
Memoized Execution
For backtest cost reduction, the package caches prompt module evaluations. The same prompt file loaded hundreds of times during a backtest is read and parsed only once. This keeps LLM-powered backtests fast without requiring explicit caching logic in your strategy.
For zero-cost backtesting with LLM strategies, run a local Ollama instance and wrap your signal function with ollama(). Local inference has no API fees or rate limits, so you can backtest across months of data without worrying about costs. Install Ollama, pull a model (ollama pull llama3.3:70b), and point the HOF at http://localhost:11434/.