@backtest-kit/pinets 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 +218 -0
- package/build/index.cjs +588 -0
- package/build/index.mjs +578 -0
- package/package.json +88 -0
- package/types.d.ts +159 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# π @backtest-kit/pinets
|
|
2
|
+
|
|
3
|
+
> Run TradingView Pine Script strategies in Node.js self hosted enviroment. Execute your existing Pine Script indicators and generate trading signals - pure technical analysis with 1:1 syntax compatibility.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
+
[](https://npmjs.org/package/@backtest-kit/pinets)
|
|
9
|
+
[]()
|
|
10
|
+
|
|
11
|
+
Port your TradingView strategies to backtest-kit with zero rewrite. Powered by [PineTS](https://github.com/QuantForgeOrg/PineTS) - an open-source Pine Script transpiler and runtime.
|
|
12
|
+
|
|
13
|
+
π **[Backtest Kit Docs](https://backtest-kit.github.io/documents/example_02_first_backtest.html)** | π **[GitHub](https://github.com/tripolskypetr/backtest-kit)** | π **[PineTS Docs](https://quantforgeorg.github.io/PineTS/)**
|
|
14
|
+
|
|
15
|
+
## β¨ Features
|
|
16
|
+
|
|
17
|
+
- π **Pine Script v5/v6**: Native TradingView syntax with 1:1 compatibility
|
|
18
|
+
- π― **60+ Indicators**: SMA, EMA, RSI, MACD, Bollinger Bands, ATR, Stochastic, and more
|
|
19
|
+
- π **Backtest Integration**: Seamless `getCandles` integration with temporal context
|
|
20
|
+
- π **File or Code**: Load `.pine` files or pass code strings directly
|
|
21
|
+
- πΊοΈ **Plot Extraction**: Flexible mapping from Pine `plot()` outputs to structured data
|
|
22
|
+
- β‘ **Cached Execution**: Memoized file reads for repeated strategy runs
|
|
23
|
+
- π‘οΈ **Type Safe**: Full TypeScript support with generics for extracted data
|
|
24
|
+
|
|
25
|
+
## π What It Does
|
|
26
|
+
|
|
27
|
+
`@backtest-kit/pinets` executes TradingView Pine Script and extracts trading signals for backtest-kit:
|
|
28
|
+
|
|
29
|
+
| Function | Description |
|
|
30
|
+
|----------|-------------|
|
|
31
|
+
| **`getSignal()`** | Run Pine Script and get structured `ISignalDto` (position, TP/SL, estimated time) |
|
|
32
|
+
| **`run()`** | Run Pine Script with custom plot mapping for advanced extraction |
|
|
33
|
+
| **`File.fromPath()`** | Load Pine Script from `.pine` file |
|
|
34
|
+
| **`Code.fromString()`** | Use inline Pine Script code |
|
|
35
|
+
|
|
36
|
+
## π Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install @backtest-kit/pinets pinets backtest-kit
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## π Usage
|
|
43
|
+
|
|
44
|
+
### Quick Start - Pine Script Strategy
|
|
45
|
+
|
|
46
|
+
Create a Pine Script file (`strategy.pine`):
|
|
47
|
+
|
|
48
|
+
```pine
|
|
49
|
+
//@version=5
|
|
50
|
+
indicator("Signal Strategy")
|
|
51
|
+
|
|
52
|
+
// Indicators
|
|
53
|
+
rsi = ta.rsi(close, 14)
|
|
54
|
+
atr = ta.atr(14)
|
|
55
|
+
ema_fast = ta.ema(close, 9)
|
|
56
|
+
ema_slow = ta.ema(close, 21)
|
|
57
|
+
|
|
58
|
+
// Conditions
|
|
59
|
+
long_cond = ta.crossover(ema_fast, ema_slow) and rsi < 70
|
|
60
|
+
short_cond = ta.crossunder(ema_fast, ema_slow) and rsi > 30
|
|
61
|
+
|
|
62
|
+
// Levels
|
|
63
|
+
sl_long = close - atr * 2
|
|
64
|
+
tp_long = close + atr * 3
|
|
65
|
+
sl_short = close + atr * 2
|
|
66
|
+
tp_short = close - atr * 3
|
|
67
|
+
|
|
68
|
+
// Plots for extraction
|
|
69
|
+
plot(close, "Close")
|
|
70
|
+
plot(long_cond ? 1 : short_cond ? -1 : 0, "Signal")
|
|
71
|
+
plot(long_cond ? sl_long : sl_short, "StopLoss")
|
|
72
|
+
plot(long_cond ? tp_long : tp_short, "TakeProfit")
|
|
73
|
+
plot(240, "EstimatedTime") // 4 hours in minutes
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Use it in your strategy:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { File, getSignal } from '@backtest-kit/pinets';
|
|
80
|
+
import { addStrategy } from 'backtest-kit';
|
|
81
|
+
|
|
82
|
+
addStrategy({
|
|
83
|
+
strategyName: 'pine-ema-cross',
|
|
84
|
+
interval: '5m',
|
|
85
|
+
riskName: 'demo',
|
|
86
|
+
getSignal: async (symbol) => {
|
|
87
|
+
const source = File.fromPath('strategy.pine');
|
|
88
|
+
|
|
89
|
+
return await getSignal(source, {
|
|
90
|
+
symbol,
|
|
91
|
+
timeframe: '5m',
|
|
92
|
+
limit: 100,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Inline Code Strategy
|
|
99
|
+
|
|
100
|
+
No file needed - pass Pine Script as a string:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { Code, getSignal } from '@backtest-kit/pinets';
|
|
104
|
+
|
|
105
|
+
const pineScript = `
|
|
106
|
+
//@version=5
|
|
107
|
+
indicator("RSI Strategy")
|
|
108
|
+
|
|
109
|
+
rsi = ta.rsi(close, 14)
|
|
110
|
+
atr = ta.atr(14)
|
|
111
|
+
|
|
112
|
+
long_cond = rsi < 30
|
|
113
|
+
short_cond = rsi > 70
|
|
114
|
+
|
|
115
|
+
plot(close, "Close")
|
|
116
|
+
plot(long_cond ? 1 : short_cond ? -1 : 0, "Signal")
|
|
117
|
+
plot(close - atr * 2, "StopLoss")
|
|
118
|
+
plot(close + atr * 3, "TakeProfit")
|
|
119
|
+
`;
|
|
120
|
+
|
|
121
|
+
const source = Code.fromString(pineScript);
|
|
122
|
+
const signal = await getSignal(source, {
|
|
123
|
+
symbol: 'BTCUSDT',
|
|
124
|
+
timeframe: '15m',
|
|
125
|
+
limit: 100,
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Custom Plot Extraction
|
|
130
|
+
|
|
131
|
+
For advanced use cases, extract any Pine `plot()` with custom mapping:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { File, run } from '@backtest-kit/pinets';
|
|
135
|
+
|
|
136
|
+
const source = File.fromPath('indicators.pine');
|
|
137
|
+
|
|
138
|
+
const data = await run(source, {
|
|
139
|
+
symbol: 'ETHUSDT',
|
|
140
|
+
timeframe: '1h',
|
|
141
|
+
limit: 200,
|
|
142
|
+
mapping: {
|
|
143
|
+
// Simple: plot name -> number
|
|
144
|
+
rsi: 'RSI',
|
|
145
|
+
macd: 'MACD',
|
|
146
|
+
|
|
147
|
+
// Advanced: with transform and lookback
|
|
148
|
+
prevRsi: {
|
|
149
|
+
plot: 'RSI',
|
|
150
|
+
barsBack: 1, // Previous bar value
|
|
151
|
+
},
|
|
152
|
+
trendStrength: {
|
|
153
|
+
plot: 'ADX',
|
|
154
|
+
transform: (v) => v > 25 ? 'strong' : 'weak',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// data = { rsi: 55.2, macd: 12.5, prevRsi: 52.1, trendStrength: 'strong' }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## π Pine Script Conventions
|
|
163
|
+
|
|
164
|
+
For `getSignal()` to work, your Pine Script must include these plots:
|
|
165
|
+
|
|
166
|
+
| Plot Name | Value | Description |
|
|
167
|
+
|-----------|-------|-------------|
|
|
168
|
+
| `"Signal"` | `1` / `-1` / `0` | Long / Short / No signal |
|
|
169
|
+
| `"Close"` | `close` | Entry price |
|
|
170
|
+
| `"StopLoss"` | price | Stop loss level |
|
|
171
|
+
| `"TakeProfit"` | price | Take profit level |
|
|
172
|
+
| `"EstimatedTime"` | minutes | Hold duration (optional, default: 240) |
|
|
173
|
+
|
|
174
|
+
Using custom plots is also possible with `run`, it allows to reconfigure the mapper
|
|
175
|
+
|
|
176
|
+
## π‘ Why Use @backtest-kit/pinets?
|
|
177
|
+
|
|
178
|
+
Instead of rewriting your TradingView strategies:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// β Without pinets (manual rewrite)
|
|
182
|
+
import { getCandles } from 'backtest-kit';
|
|
183
|
+
import { RSI, EMA, ATR } from 'technicalindicators';
|
|
184
|
+
|
|
185
|
+
const candles = await getCandles('BTCUSDT', '5m', 100);
|
|
186
|
+
const closes = candles.map(c => c.close);
|
|
187
|
+
const rsi = RSI.calculate({ values: closes, period: 14 });
|
|
188
|
+
const emaFast = EMA.calculate({ values: closes, period: 9 });
|
|
189
|
+
const emaSlow = EMA.calculate({ values: closes, period: 21 });
|
|
190
|
+
// ... rewrite all your Pine Script logic in JS
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
// β
With pinets (copy-paste from TradingView)
|
|
195
|
+
import { File, getSignal } from '@backtest-kit/pinets';
|
|
196
|
+
|
|
197
|
+
const signal = await getSignal(File.fromPath('strategy.pine'), {
|
|
198
|
+
symbol: 'BTCUSDT',
|
|
199
|
+
timeframe: '5m',
|
|
200
|
+
limit: 100,
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**Benefits:**
|
|
205
|
+
|
|
206
|
+
- π Use existing TradingView Pine Script as-is
|
|
207
|
+
- π― 60+ built-in indicators (no manual calculation)
|
|
208
|
+
- β‘ Same code for backtest and live trading
|
|
209
|
+
- π Full time-series semantics with lookback support
|
|
210
|
+
- π‘οΈ Type-safe extraction with TypeScript generics
|
|
211
|
+
|
|
212
|
+
## π€ Contribute
|
|
213
|
+
|
|
214
|
+
Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
|
|
215
|
+
|
|
216
|
+
## π License
|
|
217
|
+
|
|
218
|
+
MIT Β© [tripolskypetr](https://github.com/tripolskypetr)
|