@backtest-kit/ollama 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # 🤖 @backtest-kit/ollama
2
+
3
+ > Multi-provider LLM inference library for AI-powered trading strategies. Supports 10+ providers including OpenAI, Claude, DeepSeek, Grok, and more with unified API and automatic token rotation.
4
+
5
+ ![bots](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/bots.png)
6
+
7
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tripolskypetr/backtest-kit)
8
+ [![npm](https://img.shields.io/npm/v/@backtest-kit/ollama.svg?style=flat-square)](https://npmjs.org/package/@backtest-kit/ollama)
9
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue)]()
10
+
11
+ Transform technical analysis into trading decisions with multi-provider LLM support, structured output, and built-in risk management.
12
+
13
+ 📚 **[Backtest Kit Docs](https://backtest-kit.github.io/documents/example_02_first_backtest.html)** | 🌟 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
14
+
15
+ ## ✨ Features
16
+
17
+ - 🔌 **10+ LLM Providers**: OpenAI, Claude, DeepSeek, Grok, Mistral, Perplexity, Cohere, Alibaba, Hugging Face, Ollama
18
+ - 🔄 **Token Rotation**: Automatic API key rotation for Ollama (others throw clear errors)
19
+ - 🎯 **Structured Output**: Enforced JSON schema for trading signals (position, price levels, risk notes)
20
+ - 🔑 **Flexible Auth**: Context-based API keys or environment variables
21
+ - ⚡ **Unified API**: Single interface across all providers
22
+ - 📊 **Trading-First**: Built for backtest-kit with position sizing and risk management
23
+ - 🛡️ **Type Safe**: Full TypeScript support with exported types
24
+
25
+ ## 📋 What It Does
26
+
27
+ `@backtest-kit/ollama` provides a unified interface to call multiple LLM providers and receive structured trading signals:
28
+
29
+ | Provider | Function | Base URL |
30
+ |----------|----------|----------|
31
+ | **OpenAI** | `gpt5()` | api.openai.com |
32
+ | **Claude** | `claude()` | api.anthropic.com |
33
+ | **DeepSeek** | `deepseek()` | api.deepseek.com |
34
+ | **Grok** | `grok()` | api.x.ai |
35
+ | **Mistral** | `mistral()` | api.mistral.ai |
36
+ | **Perplexity** | `perplexity()` | api.perplexity.ai |
37
+ | **Cohere** | `cohere()` | api.cohere.ai |
38
+ | **Alibaba** | `alibaba()` | dashscope-intl.aliyuncs.com/compatible-mode/v1 |
39
+ | **Hugging Face** | `hf()` | router.huggingface.co/v1 |
40
+ | **Ollama** | `ollama()` | ollama.com |
41
+
42
+ **Output Schema:**
43
+
44
+ ```typescript
45
+ {
46
+ id: string; // Unique signal ID
47
+ position: "long" | "short"; // Trading direction
48
+ minuteEstimatedTime: number; // Hold duration estimate
49
+ priceStopLoss: number; // Stop loss price
50
+ priceTakeProfit: number; // Take profit price
51
+ note: string; // Risk assessment note
52
+ priceOpen: number; // Entry price
53
+ }
54
+ ```
55
+
56
+ ## 🚀 Installation
57
+
58
+ ```bash
59
+ npm install @backtest-kit/ollama agent-swarm-kit backtest-kit
60
+ ```
61
+
62
+ ## 📖 Usage
63
+
64
+ ### Quick Start - OpenAI
65
+
66
+ ```typescript
67
+ import { gpt5 } from '@backtest-kit/ollama';
68
+ import { commitHistorySetup } from '@backtest-kit/signals';
69
+
70
+ // Build context with technical analysis
71
+ const messages = [
72
+ {
73
+ role: 'system',
74
+ content: 'You are a trading bot. Analyze indicators and return JSON: { position: "long"|"short", priceStopLoss, priceTakeProfit, minuteEstimatedTime, priceOpen, note }'
75
+ }
76
+ ];
77
+
78
+ await commitHistorySetup('BTCUSDT', messages);
79
+
80
+ // Get trading signal from GPT-5
81
+ const signal = await gpt5(messages, 'gpt-4o', process.env.CC_OPENAI_API_KEY);
82
+
83
+ console.log(signal);
84
+ // {
85
+ // id: "abc-123",
86
+ // position: "long",
87
+ // priceStopLoss: 49000,
88
+ // priceTakeProfit: 51000,
89
+ // minuteEstimatedTime: 60,
90
+ // priceOpen: 50000,
91
+ // note: "Strong bullish momentum with RSI oversold recovery"
92
+ // }
93
+ ```
94
+
95
+ ### Multi-Provider Strategy
96
+
97
+ ```typescript
98
+ import { gpt5, claude, deepseek } from '@backtest-kit/ollama';
99
+ import { addStrategy } from 'backtest-kit';
100
+ import { commitHistorySetup } from '@backtest-kit/signals';
101
+
102
+ addStrategy({
103
+ strategyName: 'multi-llm',
104
+ interval: '5m',
105
+ riskName: 'aggressive',
106
+ getSignal: async (symbol) => {
107
+ const messages = [
108
+ {
109
+ role: 'system',
110
+ content: 'Analyze technical data and return trading signal as JSON'
111
+ }
112
+ ];
113
+
114
+ await commitHistorySetup(symbol, messages);
115
+
116
+ // Try multiple providers with fallback
117
+ try {
118
+ return await deepseek(messages, 'deepseek-chat');
119
+ } catch (err) {
120
+ console.warn('DeepSeek failed, trying Claude:', err);
121
+ try {
122
+ return await claude(messages, 'claude-3-5-sonnet-20241022');
123
+ } catch (err2) {
124
+ console.warn('Claude failed, using GPT-5:', err2);
125
+ return await gpt5(messages, 'gpt-4o');
126
+ }
127
+ }
128
+ }
129
+ });
130
+ ```
131
+
132
+ ### Token Rotation (Ollama Only)
133
+
134
+ Ollama supports automatic API key rotation by passing an array:
135
+
136
+ ```typescript
137
+ import { ollama } from '@backtest-kit/ollama';
138
+
139
+ const signal = await ollama(
140
+ messages,
141
+ 'llama3.3:70b',
142
+ ['key1', 'key2', 'key3'] // Rotates through keys
143
+ );
144
+
145
+ // Other providers throw error:
146
+ // "Claude provider does not support token rotation"
147
+ ```
148
+
149
+ ### Custom Logger
150
+
151
+ Enable logging for debugging:
152
+
153
+ ```typescript
154
+ import { setLogger } from '@backtest-kit/ollama';
155
+
156
+ setLogger({
157
+ log: console.log,
158
+ debug: console.debug,
159
+ info: console.info,
160
+ warn: console.warn,
161
+ });
162
+ ```
163
+
164
+ ## 💡 Why Use @backtest-kit/ollama?
165
+
166
+ Instead of manually integrating LLM SDKs:
167
+
168
+ ```typescript
169
+ // ❌ Without ollama (manual work)
170
+ import OpenAI from 'openai';
171
+ import Anthropic from '@anthropic-ai/sdk';
172
+
173
+ const openai = new OpenAI({ apiKey: process.env.OPENAI_KEY });
174
+ const response = await openai.chat.completions.create({
175
+ model: 'gpt-4o',
176
+ messages,
177
+ response_format: { type: 'json_object' }
178
+ });
179
+ const signal = JSON.parse(response.choices[0].message.content);
180
+ // ... manual schema validation
181
+ // ... manual error handling
182
+ // ... no fallback
183
+ ```
184
+
185
+ ```typescript
186
+ // ✅ With ollama (one line)
187
+ const signal = await gpt5(messages, 'gpt-4o');
188
+ ```
189
+
190
+ **Benefits:**
191
+
192
+ - ⚡ Unified API across 10+ providers
193
+ - 🎯 Enforced JSON schema (no parsing errors)
194
+ - 🔄 Built-in token rotation (Ollama)
195
+ - 🔑 Context-based API keys
196
+ - 🛡️ Type-safe TypeScript interfaces
197
+ - 📊 Trading-specific output format
198
+
199
+ ## 🤝 Contribute
200
+
201
+ Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
202
+
203
+ ## 📜 License
204
+
205
+ MIT © [tripolskypetr](https://github.com/tripolskypetr)