@backtest-kit/signals 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 +302 -0
- package/build/index.cjs +3004 -0
- package/build/index.mjs +2991 -0
- package/package.json +81 -0
- package/types.d.ts +294 -0
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# 📊 @backtest-kit/signals
|
|
2
|
+
|
|
3
|
+
> Technical analysis and trading signal generation library for AI-powered trading systems. Computes 50+ indicators across 4 timeframes and generates markdown reports for LLM consumption.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
[](https://npmjs.org/package/@backtest-kit/signals)
|
|
8
|
+
[]()
|
|
9
|
+
|
|
10
|
+
Transform raw market data into actionable trading insights with multi-timeframe technical analysis, order book depth, and AI-ready markdown reports.
|
|
11
|
+
|
|
12
|
+
📚 **[Backtest Kit Docs](https://backtest-kit.github.io/documents/example_02_first_backtest.html)** | 🌟 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
|
|
13
|
+
|
|
14
|
+
## ✨ Features
|
|
15
|
+
|
|
16
|
+
- 📈 **Multi-Timeframe Analysis**: 1m, 15m, 30m, 1h with synchronized indicator computation
|
|
17
|
+
- 🎯 **50+ Technical Indicators**: RSI, MACD, Bollinger Bands, Stochastic, ADX, ATR, CCI, Fibonacci, Support/Resistance
|
|
18
|
+
- 📊 **Order Book Analysis**: Bid/ask depth, spread, liquidity imbalance, top 20 levels
|
|
19
|
+
- 🤖 **AI-Ready Output**: Markdown reports formatted for LLM context injection
|
|
20
|
+
- ⚡ **Performance Optimized**: Intelligent caching with configurable TTL per timeframe
|
|
21
|
+
- 🧮 **Custom Algorithms**: Fibonacci retracements, support/resistance detection, volume analysis
|
|
22
|
+
- 📦 **Zero Config**: Works out-of-the-box with backtest-kit
|
|
23
|
+
|
|
24
|
+
## 📋 What It Does
|
|
25
|
+
|
|
26
|
+
`@backtest-kit/signals` analyzes market data and generates comprehensive technical reports across multiple timeframes:
|
|
27
|
+
|
|
28
|
+
| Timeframe | Candles | Indicators | Use Case |
|
|
29
|
+
|-----------|---------|------------|----------|
|
|
30
|
+
| **MicroTerm** (1m) | 60 | RSI(9,14), MACD(8,21,5), Stochastic, ADX(9), Bollinger(8,2), ATR, CCI, Volume, Squeeze | Scalping, ultra-short entries |
|
|
31
|
+
| **ShortTerm** (15m) | 144 | RSI(9), MACD(8,21,5), Stochastic(5,3,3), ADX(14), Bollinger(10,2), Fibonacci | Day trading |
|
|
32
|
+
| **SwingTerm** (30m) | 96 | RSI(14), MACD(12,26,9), Stochastic(14,3,3), Bollinger(20,2), Support/Resistance | Swing trading |
|
|
33
|
+
| **LongTerm** (1h) | 100 | RSI(14), MACD(12,26,9), ADX(14), Bollinger(20,2), SMA(50), DEMA, WMA, Volume Trend | Trend analysis |
|
|
34
|
+
|
|
35
|
+
**Plus:** Real-time order book analysis with bid/ask depth and imbalance metrics.
|
|
36
|
+
|
|
37
|
+
## 🚀 Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @backtest-kit/signals backtest-kit
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 📖 Usage
|
|
44
|
+
|
|
45
|
+
### Quick Start - All-in-One Report
|
|
46
|
+
|
|
47
|
+
The easiest way to inject technical analysis into your LLM strategy:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { commitHistorySetup } from '@backtest-kit/signals';
|
|
51
|
+
import { getCandles } from 'backtest-kit';
|
|
52
|
+
|
|
53
|
+
// In your strategy's getSignal function:
|
|
54
|
+
const messages = [];
|
|
55
|
+
|
|
56
|
+
// Add all technical analysis + order book + candle history
|
|
57
|
+
await commitHistorySetup('BTCUSDT', messages);
|
|
58
|
+
|
|
59
|
+
// Now messages contains:
|
|
60
|
+
// - Order book analysis (bids/asks, spread, imbalance)
|
|
61
|
+
// - Candle history (1m, 15m, 30m, 1h)
|
|
62
|
+
// - Technical indicators for all 4 timeframes
|
|
63
|
+
// - System info (symbol, price, timestamp)
|
|
64
|
+
|
|
65
|
+
// Send to LLM
|
|
66
|
+
const signal = await llm(messages);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Granular Control - Individual Reports
|
|
70
|
+
|
|
71
|
+
For fine-grained control over what data to include:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import {
|
|
75
|
+
commitBookDataReport,
|
|
76
|
+
commitOneMinuteHistory,
|
|
77
|
+
commitFifteenMinuteHistory,
|
|
78
|
+
commitThirtyMinuteHistory,
|
|
79
|
+
commitHourHistory,
|
|
80
|
+
commitMicroTermMath,
|
|
81
|
+
commitShortTermMath,
|
|
82
|
+
commitSwingTermMath,
|
|
83
|
+
commitLongTermMath,
|
|
84
|
+
} from '@backtest-kit/signals';
|
|
85
|
+
|
|
86
|
+
const messages = [];
|
|
87
|
+
|
|
88
|
+
// Order book analysis
|
|
89
|
+
await commitBookDataReport('BTCUSDT', messages);
|
|
90
|
+
|
|
91
|
+
// Candle histories
|
|
92
|
+
await commitOneMinuteHistory('BTCUSDT', messages); // Last 15 candles
|
|
93
|
+
await commitFifteenMinuteHistory('BTCUSDT', messages); // Last 8 candles
|
|
94
|
+
await commitThirtyMinuteHistory('BTCUSDT', messages); // Last 6 candles
|
|
95
|
+
await commitHourHistory('BTCUSDT', messages); // Last 6 candles
|
|
96
|
+
|
|
97
|
+
// Technical indicators
|
|
98
|
+
await commitMicroTermMath('BTCUSDT', messages); // 1m indicators
|
|
99
|
+
await commitShortTermMath('BTCUSDT', messages); // 15m indicators
|
|
100
|
+
await commitSwingTermMath('BTCUSDT', messages); // 30m indicators
|
|
101
|
+
await commitLongTermMath('BTCUSDT', messages); // 1h indicators
|
|
102
|
+
|
|
103
|
+
// Send to LLM
|
|
104
|
+
const signal = await llm(messages);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Complete LLM Strategy Example
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { v4 as uuid } from 'uuid';
|
|
111
|
+
import { addStrategy, dumpSignal } from 'backtest-kit';
|
|
112
|
+
import { commitHistorySetup } from '@backtest-kit/signals';
|
|
113
|
+
import { json } from './utils/json.mjs'; // Your LLM wrapper
|
|
114
|
+
|
|
115
|
+
addStrategy({
|
|
116
|
+
strategyName: 'llm-strategy',
|
|
117
|
+
interval: '5m',
|
|
118
|
+
riskName: 'demo',
|
|
119
|
+
getSignal: async (symbol) => {
|
|
120
|
+
const messages = [
|
|
121
|
+
{
|
|
122
|
+
role: 'system',
|
|
123
|
+
content: 'You are a trading bot. Analyze technical indicators and generate signals.'
|
|
124
|
+
}
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
// Inject all technical analysis
|
|
128
|
+
await commitHistorySetup(symbol, messages);
|
|
129
|
+
|
|
130
|
+
// Add trading instructions
|
|
131
|
+
messages.push({
|
|
132
|
+
role: 'user',
|
|
133
|
+
content: [
|
|
134
|
+
'Based on the technical analysis above, generate a trading signal.',
|
|
135
|
+
'Use position: "wait" if signals are unclear or contradictory.',
|
|
136
|
+
'Return JSON: { position: "long"|"short"|"wait", priceTakeProfit: number, priceStopLoss: number }'
|
|
137
|
+
].join('\n')
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Generate signal via LLM
|
|
141
|
+
const resultId = uuid();
|
|
142
|
+
const signal = await json(messages);
|
|
143
|
+
|
|
144
|
+
// Save conversation for debugging
|
|
145
|
+
await dumpSignal(resultId, messages, signal);
|
|
146
|
+
|
|
147
|
+
return { ...signal, id: resultId };
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Custom Logger
|
|
153
|
+
|
|
154
|
+
By default, signals uses a no-op logger. To enable logging:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { setLogger } from '@backtest-kit/signals';
|
|
158
|
+
|
|
159
|
+
setLogger({
|
|
160
|
+
log: console.log,
|
|
161
|
+
debug: console.debug,
|
|
162
|
+
info: console.info,
|
|
163
|
+
warn: console.warn,
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## 📊 Generated Report Structure
|
|
168
|
+
|
|
169
|
+
### Order Book Report
|
|
170
|
+
|
|
171
|
+
```markdown
|
|
172
|
+
## Order Book Analysis
|
|
173
|
+
|
|
174
|
+
**Symbol:** BTCUSDT
|
|
175
|
+
**Best Bid:** 50000.00 | **Best Ask:** 50001.00
|
|
176
|
+
**Mid Price:** 50000.50 | **Spread:** 1.00
|
|
177
|
+
**Depth Imbalance:** +5.2% (buy pressure)
|
|
178
|
+
|
|
179
|
+
### Top 20 Levels (Bids)
|
|
180
|
+
| Price | Volume | % Total |
|
|
181
|
+
|-------|--------|---------|
|
|
182
|
+
| 50000.00 | 1.234 | 15.5% |
|
|
183
|
+
...
|
|
184
|
+
|
|
185
|
+
### Top 20 Levels (Asks)
|
|
186
|
+
| Price | Volume | % Total |
|
|
187
|
+
|-------|--------|---------|
|
|
188
|
+
| 50001.00 | 0.987 | 12.3% |
|
|
189
|
+
...
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Candle History Report
|
|
193
|
+
|
|
194
|
+
```markdown
|
|
195
|
+
## 1-Minute Candle History (Last 15)
|
|
196
|
+
|
|
197
|
+
| Timestamp | Open | High | Low | Close | Volume | Volatility | Body Size |
|
|
198
|
+
|-----------|------|------|-----|-------|--------|------------|-----------|
|
|
199
|
+
| 2025-01-13 10:00 | 50000 | 50050 | 49990 | 50020 | 123.45 | 0.12% | 0.04% |
|
|
200
|
+
...
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Technical Indicators Report
|
|
204
|
+
|
|
205
|
+
```markdown
|
|
206
|
+
## MicroTerm Analysis (1-Minute Timeframe)
|
|
207
|
+
|
|
208
|
+
| Time | Price | RSI(9) | RSI(14) | MACD | Signal | Histogram | Stoch %K | Stoch %D | ADX | +DI | -DI | BB Upper | BB Middle | BB Lower | ATR(5) | ATR(9) | CCI(9) | Volume | Vol Trend | Momentum | ROC | Support | Resistance | Squeeze | Pressure |
|
|
209
|
+
|------|-------|--------|---------|------|--------|-----------|----------|----------|-----|-----|-----|----------|-----------|----------|--------|--------|--------|--------|-----------|----------|-----|---------|------------|---------|----------|
|
|
210
|
+
| 10:00 | 50020 | 55.2 | 52.8 | 12.5 | 8.3 | 4.2 | 45.6 | 42.1 | 28.5 | 22.3 | 18.7 | 50100 | 50000 | 49900 | 15.2 | 18.9 | 45.7 | 123.45 | increasing | 0.8% | 1.2% | 49950 | 50100 | 0.85 | 15.2 |
|
|
211
|
+
...
|
|
212
|
+
|
|
213
|
+
**Data Sources:**
|
|
214
|
+
- RSI periods: 9, 14
|
|
215
|
+
- MACD: Fast=8, Slow=21, Signal=5
|
|
216
|
+
- Stochastic: K=3, D=3, Smooth=3 (primary), K=5, D=3, Smooth=3 (secondary)
|
|
217
|
+
...
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 🔄 Caching Strategy
|
|
221
|
+
|
|
222
|
+
Reports are cached to avoid redundant calculations:
|
|
223
|
+
|
|
224
|
+
| Timeframe | Cache Duration |
|
|
225
|
+
|-----------|----------------|
|
|
226
|
+
| 1-minute data | 1 minute |
|
|
227
|
+
| 15-minute data | 5 minutes |
|
|
228
|
+
| 30-minute data | 15 minutes |
|
|
229
|
+
| 1-hour data | 30 minutes |
|
|
230
|
+
| Order book | 5 minutes |
|
|
231
|
+
|
|
232
|
+
Cache is automatically cleared on errors.
|
|
233
|
+
|
|
234
|
+
## 🧮 Key Algorithms
|
|
235
|
+
|
|
236
|
+
### Support/Resistance Detection
|
|
237
|
+
- **MicroTerm/SwingTerm**: Looks back N candles for significant highs/lows (±0.3% threshold)
|
|
238
|
+
- **LongTerm**: 4-candle pivot point method
|
|
239
|
+
|
|
240
|
+
### Fibonacci Retracement
|
|
241
|
+
- Calculates levels: 0%, 23.6%, 38.2%, 50%, 61.8%, 78.6%, 100%
|
|
242
|
+
- Extensions: 127.2%, 161.8%, 261.8%
|
|
243
|
+
- Finds nearest level to current price (1.5% tolerance)
|
|
244
|
+
|
|
245
|
+
### Volume Analysis
|
|
246
|
+
- **MicroTerm**: SMA(5) volume with increasing/decreasing/stable trend (±20% threshold)
|
|
247
|
+
- **LongTerm**: 6-candle average comparison (±10% threshold)
|
|
248
|
+
|
|
249
|
+
### Order Book Imbalance
|
|
250
|
+
|
|
251
|
+
Imbalance = (bid_volume - ask_volume) / (bid_volume + ask_volume)
|
|
252
|
+
|
|
253
|
+
Positive = buy pressure, Negative = sell pressure
|
|
254
|
+
|
|
255
|
+
## 🎯 Use Cases
|
|
256
|
+
|
|
257
|
+
### 1. LLM-Powered Trading Strategies
|
|
258
|
+
Inject technical analysis into your LLM's context for intelligent signal generation.
|
|
259
|
+
|
|
260
|
+
### 2. Multi-Timeframe Confirmation
|
|
261
|
+
Combine indicators from different timeframes to filter false signals.
|
|
262
|
+
|
|
263
|
+
### 3. Market Context for AI Agents
|
|
264
|
+
Provide comprehensive market state to AI agents making trading decisions.
|
|
265
|
+
|
|
266
|
+
### 4. Debugging & Analysis
|
|
267
|
+
Save generated reports for post-analysis and strategy improvement.
|
|
268
|
+
|
|
269
|
+
## 💡 Why Use @backtest-kit/signals?
|
|
270
|
+
|
|
271
|
+
Instead of manually calculating indicators and formatting data for your LLM:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
// ❌ Without signals (manual work)
|
|
275
|
+
const candles = await getCandles('BTCUSDT', '1m', 60);
|
|
276
|
+
const rsi = calculateRSI(candles, 14);
|
|
277
|
+
const macd = calculateMACD(candles, 12, 26, 9);
|
|
278
|
+
const bb = calculateBollingerBands(candles, 20, 2);
|
|
279
|
+
// ... 40+ more indicators
|
|
280
|
+
const report = formatToMarkdown(rsi, macd, bb, ...);
|
|
281
|
+
messages.push({ role: 'user', content: report });
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
// ✅ With signals (one line)
|
|
286
|
+
await commitHistorySetup('BTCUSDT', messages);
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
**Benefits:**
|
|
290
|
+
- ⚡ Pre-computed, cached, optimized
|
|
291
|
+
- 📊 50+ indicators across 4 timeframes
|
|
292
|
+
- 🎨 Formatted markdown tables ready for LLM
|
|
293
|
+
- 🔄 Synchronized with backtest timeline
|
|
294
|
+
- 🛡️ Error handling and validation built-in
|
|
295
|
+
|
|
296
|
+
## 🤝 Contribute
|
|
297
|
+
|
|
298
|
+
Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
|
|
299
|
+
|
|
300
|
+
## 📜 License
|
|
301
|
+
|
|
302
|
+
MIT © [tripolskypetr](https://github.com/tripolskypetr)
|