@backtest-kit/ollama 0.0.1 → 0.0.2
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 +206 -205
- package/build/index.cjs +2588 -36
- package/build/index.mjs +2588 -37
- package/package.json +13 -6
- package/types.d.ts +787 -1
package/README.md
CHANGED
|
@@ -1,205 +1,206 @@
|
|
|
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
|
-

|
|
6
|
-
|
|
7
|
-
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
-
[](https://npmjs.org/package/@backtest-kit/ollama)
|
|
9
|
-
[]()
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
import {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
//
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
import {
|
|
100
|
-
import {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
import
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
// ... manual
|
|
182
|
-
// ...
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
-
|
|
196
|
-
-
|
|
197
|
-
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+

|
|
6
|
+
|
|
7
|
+
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
+
[](https://npmjs.org/package/@backtest-kit/ollama)
|
|
9
|
+
[]()
|
|
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()` | cloud.ollama.com |
|
|
41
|
+
| **Z.ai** | `glm4()` | api.z.ai/api/paas/v4 |
|
|
42
|
+
|
|
43
|
+
**Output Schema:**
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
{
|
|
47
|
+
id: string; // Unique signal ID
|
|
48
|
+
position: "long" | "short"; // Trading direction
|
|
49
|
+
minuteEstimatedTime: number; // Hold duration estimate
|
|
50
|
+
priceStopLoss: number; // Stop loss price
|
|
51
|
+
priceTakeProfit: number; // Take profit price
|
|
52
|
+
note: string; // Risk assessment note
|
|
53
|
+
priceOpen: number; // Entry price
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 🚀 Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install @backtest-kit/ollama agent-swarm-kit backtest-kit
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 📖 Usage
|
|
64
|
+
|
|
65
|
+
### Quick Start - OpenAI
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { gpt5 } from '@backtest-kit/ollama';
|
|
69
|
+
import { commitHistorySetup } from '@backtest-kit/signals';
|
|
70
|
+
|
|
71
|
+
// Build context with technical analysis
|
|
72
|
+
const messages = [
|
|
73
|
+
{
|
|
74
|
+
role: 'system',
|
|
75
|
+
content: 'You are a trading bot. Analyze indicators and return JSON: { position: "long"|"short", priceStopLoss, priceTakeProfit, minuteEstimatedTime, priceOpen, note }'
|
|
76
|
+
}
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
await commitHistorySetup('BTCUSDT', messages);
|
|
80
|
+
|
|
81
|
+
// Get trading signal from GPT-5
|
|
82
|
+
const signal = await gpt5(messages, 'gpt-4o', process.env.CC_OPENAI_API_KEY);
|
|
83
|
+
|
|
84
|
+
console.log(signal);
|
|
85
|
+
// {
|
|
86
|
+
// id: "abc-123",
|
|
87
|
+
// position: "long",
|
|
88
|
+
// priceStopLoss: 49000,
|
|
89
|
+
// priceTakeProfit: 51000,
|
|
90
|
+
// minuteEstimatedTime: 60,
|
|
91
|
+
// priceOpen: 50000,
|
|
92
|
+
// note: "Strong bullish momentum with RSI oversold recovery"
|
|
93
|
+
// }
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Multi-Provider Strategy
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { gpt5, claude, deepseek } from '@backtest-kit/ollama';
|
|
100
|
+
import { addStrategy } from 'backtest-kit';
|
|
101
|
+
import { commitHistorySetup } from '@backtest-kit/signals';
|
|
102
|
+
|
|
103
|
+
addStrategy({
|
|
104
|
+
strategyName: 'multi-llm',
|
|
105
|
+
interval: '5m',
|
|
106
|
+
riskName: 'aggressive',
|
|
107
|
+
getSignal: async (symbol) => {
|
|
108
|
+
const messages = [
|
|
109
|
+
{
|
|
110
|
+
role: 'system',
|
|
111
|
+
content: 'Analyze technical data and return trading signal as JSON'
|
|
112
|
+
}
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
await commitHistorySetup(symbol, messages);
|
|
116
|
+
|
|
117
|
+
// Try multiple providers with fallback
|
|
118
|
+
try {
|
|
119
|
+
return await deepseek(messages, 'deepseek-chat');
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.warn('DeepSeek failed, trying Claude:', err);
|
|
122
|
+
try {
|
|
123
|
+
return await claude(messages, 'claude-3-5-sonnet-20241022');
|
|
124
|
+
} catch (err2) {
|
|
125
|
+
console.warn('Claude failed, using GPT-5:', err2);
|
|
126
|
+
return await gpt5(messages, 'gpt-4o');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Token Rotation (Ollama Only)
|
|
134
|
+
|
|
135
|
+
Ollama supports automatic API key rotation by passing an array:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { ollama } from '@backtest-kit/ollama';
|
|
139
|
+
|
|
140
|
+
const signal = await ollama(
|
|
141
|
+
messages,
|
|
142
|
+
'llama3.3:70b',
|
|
143
|
+
['key1', 'key2', 'key3'] // Rotates through keys
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// Other providers throw error:
|
|
147
|
+
// "Claude provider does not support token rotation"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Custom Logger
|
|
151
|
+
|
|
152
|
+
Enable logging for debugging:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { setLogger } from '@backtest-kit/ollama';
|
|
156
|
+
|
|
157
|
+
setLogger({
|
|
158
|
+
log: console.log,
|
|
159
|
+
debug: console.debug,
|
|
160
|
+
info: console.info,
|
|
161
|
+
warn: console.warn,
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## 💡 Why Use @backtest-kit/ollama?
|
|
166
|
+
|
|
167
|
+
Instead of manually integrating LLM SDKs:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
// ❌ Without ollama (manual work)
|
|
171
|
+
import OpenAI from 'openai';
|
|
172
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
173
|
+
|
|
174
|
+
const openai = new OpenAI({ apiKey: process.env.OPENAI_KEY });
|
|
175
|
+
const response = await openai.chat.completions.create({
|
|
176
|
+
model: 'gpt-4o',
|
|
177
|
+
messages,
|
|
178
|
+
response_format: { type: 'json_object' }
|
|
179
|
+
});
|
|
180
|
+
const signal = JSON.parse(response.choices[0].message.content);
|
|
181
|
+
// ... manual schema validation
|
|
182
|
+
// ... manual error handling
|
|
183
|
+
// ... no fallback
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
// ✅ With ollama (one line)
|
|
188
|
+
const signal = await gpt5(messages, 'gpt-4o');
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Benefits:**
|
|
192
|
+
|
|
193
|
+
- ⚡ Unified API across 10+ providers
|
|
194
|
+
- 🎯 Enforced JSON schema (no parsing errors)
|
|
195
|
+
- 🔄 Built-in token rotation (Ollama)
|
|
196
|
+
- 🔑 Context-based API keys
|
|
197
|
+
- 🛡️ Type-safe TypeScript interfaces
|
|
198
|
+
- 📊 Trading-specific output format
|
|
199
|
+
|
|
200
|
+
## 🤝 Contribute
|
|
201
|
+
|
|
202
|
+
Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
|
|
203
|
+
|
|
204
|
+
## 📜 License
|
|
205
|
+
|
|
206
|
+
MIT © [tripolskypetr](https://github.com/tripolskypetr)
|