@luxalgo/vela-pinets 0.2.9 → 0.2.11

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 CHANGED
@@ -1,55 +1,180 @@
1
- # Vela-pinets
1
+ <!-- markdownlint-disable no-inline-html first-line-h1 -->
2
2
 
3
- The PineTS scripting engine for the [Vela](https://github.com/LuxAlgo/Vela) charting
4
- library — Pine Script indicators **and strategies** executed in-process (`PineEngine`) or
5
- off the main thread (`PineWorkerEngine`), plugged into Vela's public `ScriptingEngine`
6
- port. A `strategy()` script also emits its broker-emulator order fills as
7
- `IndicatorModel.trades` (one marker per fill, at the fill bar and price), which Vela
8
- paints as on-chart trade markers.
3
+ <div align="center">
4
+
5
+ <img src=".github/banner.png" alt="Vela PineTS Pine Script indicators and strategies for Vela" width="100%">
6
+
7
+ <p><strong>Pine Script indicators and strategies for Vela.</strong><br>
8
+ In-process · Web Worker · Vela's public ScriptingEngine port</p>
9
+
10
+ [![npm version][npm-version-img]][npm-link]
11
+ [![Downloads][npm-downloads-img]][npm-link]
12
+ [![License][license-img]][license-link]
13
+
14
+ <p>
15
+ <a href="https://github.com/LuxAlgo/Vela">Vela</a> ·
16
+ <a href="#quick-start">Quick start</a> ·
17
+ <a href="CHANGELOG.md">Changelog</a> ·
18
+ <a href="#license">License</a>
19
+ </p>
20
+
21
+ </div>
22
+
23
+ <!-- markdownlint-enable no-inline-html -->
24
+
25
+ Vela PineTS is the Pine Script addon for [Vela](https://github.com/LuxAlgo/Vela). It
26
+ runs `indicator()` and `strategy()` scripts through Vela's public `ScriptingEngine`
27
+ port — in-process (`PineEngine`) or off the main thread (`PineWorkerEngine`). A
28
+ `strategy()` script also emits its broker-emulator order fills as
29
+ `IndicatorModel.trades`, which Vela paints as on-chart trade markers.
30
+
31
+ Vela itself ships no scripting engine and stays Apache-2.0. This package is
32
+ **AGPL-3.0** because the [PineTS](https://github.com/LuxAlgo/PineTS) runtime it
33
+ executes is.
34
+
35
+ ## What's in the box
36
+
37
+ - **`PineEngine`**: the in-process engine. The simplest setup — transpile and execute
38
+ on the chart's thread.
39
+ - **`PineWorkerEngine`**: the same Pine semantics in a Web Worker. The worker source
40
+ is inlined at build time and spawned from a Blob URL, so heavy scripts never block
41
+ the chart.
42
+ - **Declaration props**: both engines publish the mutable `indicator()` /
43
+ `strategy()` arguments (`initial_capital`, `precision`, …) as a props schema. Vela
44
+ shows them on the settings dialog's **Properties** tab; hosts override them via
45
+ `addIndicator({ props })` / `handle.setProps()`.
46
+ - **Browser builds**: `vela-pinets.global.js` / `.global.min.js` expose
47
+ `window.VelaPinets` for script-tag usage. Load `vela.global.js` first.
48
+
49
+ ## Installing
50
+
51
+ ```bash
52
+ npm install @luxalgo/vela-pinets @luxalgo/vela pinets
53
+ ```
54
+
55
+ `@luxalgo/vela` and `pinets` are peers of the whole package: the published entry
56
+ imports both unconditionally, so each must resolve whichever engine you pick. Vela
57
+ is a peer rather than a dependency for the same reason the browser build maps it
58
+ onto `window.Vela` — a second copy would duplicate the SDK registries, not just the
59
+ bytes. The worker avoids a second *pinets* by inlining its own at build time.
60
+
61
+ ## Quick start
62
+
63
+ Register the engine, then add a script. The language is `pine`, so calls that omit
64
+ `language` still resolve:
9
65
 
10
66
  ```ts
11
67
  import { Vela } from '@luxalgo/vela';
12
68
  import { PineEngine } from '@luxalgo/vela-pinets';
13
69
 
14
- const chart = new Vela('#chart', { symbol: 'BTCUSDT', timeframe: '60' });
70
+ const chart = new Vela('#chart', { symbol: 'BTCUSDT', timeframe: '60', live: true });
15
71
  chart.registerEngine('pine', new PineEngine());
16
72
  chart.addIndicator(`//@version=5
17
73
  indicator("EMA 20", overlay=true)
18
- plot(ta.ema(close, 20), color=color.orange)`);
74
+ plot(ta.ema(close, 20), color=color.orange, linewidth=2)`);
19
75
  ```
20
76
 
21
- - **`PineEngine`** in-process: the simplest setup. `@luxalgo/vela` and `pinets` are both
22
- peers of the whole package (`npm i @luxalgo/vela-pinets @luxalgo/vela pinets`) — the
23
- published entry imports both unconditionally, so each must resolve whichever engine you
24
- pick. Vela is a peer rather than a dependency for the same reason the browser build maps
25
- it onto `window.Vela`: a second copy would duplicate the SDK registries, not just the
26
- bytes. The worker avoids a second *pinets* by inlining its own at build time.
27
- - **`PineWorkerEngine`** — the same Pine semantics in a Web Worker (source inlined at
28
- build time, spawned from a Blob URL): heavy scripts never block the chart.
29
- - **Declaration props** — both engines publish the mutable `indicator()` / `strategy()`
30
- declaration arguments (`initial_capital`, `precision`, …) as a props schema: Vela shows
31
- them on the settings dialog's **Properties** tab, and hosts override them via
32
- `addIndicator({ props })` / `handle.setProps()`. The optional `defaultProps` engine
33
- option (`new PineEngine({ defaultProps: { initial_capital: 50000 } })`) sets host-level
34
- defaults for scripts that don't declare the prop themselves, and `props` gates which
35
- scripts publish the schema — `'all' | 'strategy' | 'none'`, or a whitelist of prop
36
- keys published in the list's order (`'strategy'` gives strategies a Properties tab
37
- while plain indicators keep an inputs-only dialog).
38
- - **Browser builds** — `vela-pinets.global.js` / `.global.min.js` expose
39
- `window.VelaPinets` for script-tag usage; load `vela.global.js` first.
77
+ Prefer the chart to stay responsive under heavy scripts? Same port, off the main
78
+ thread:
40
79
 
41
- ## Development
80
+ ```ts
81
+ import { PineWorkerEngine } from '@luxalgo/vela-pinets';
42
82
 
83
+ chart.registerEngine('pine', new PineWorkerEngine());
43
84
  ```
85
+
86
+ The workspace takes an engine factory and an **indicator manifest** — inline JSON,
87
+ a URL returning it, or an async loader:
88
+
89
+ ```ts
90
+ import { VelaWorkspace } from '@luxalgo/vela/workspace';
91
+ import { PineWorkerEngine } from '@luxalgo/vela-pinets';
92
+
93
+ new VelaWorkspace('#chart', {
94
+ symbol: 'BTCUSDT',
95
+ timeframe: '60',
96
+ live: true,
97
+ engines: { pine: () => new PineWorkerEngine() },
98
+ indicators: '/indicators.json', // or an inline [{ name, script | url, language?, enabled? }]
99
+ });
100
+ ```
101
+
102
+ Host tooling can execute-and-inject safely with `chart.runIndicator(source)`
103
+ (structured errors, no dead legend rows) and read a running script's state,
104
+ including its **return value**, via `handle.context()` (read-only snapshots,
105
+ worker-safe). See Vela's [scripting engines](https://github.com/LuxAlgo/Vela/blob/dev/docs/user/scripting-engines.md)
106
+ guide.
107
+
108
+ ### Browser bundle
109
+
110
+ The package also ships self-contained browser builds for script-tag usage:
111
+ `dist/vela-pinets.global.js` (readable, development) and
112
+ `dist/vela-pinets.global.min.js` (minified, production). Either file attaches the
113
+ engines to `window.VelaPinets`. Load Vela's `vela.global.js` first so
114
+ `@luxalgo/vela` resolves to the page's `window.Vela`.
115
+
116
+ ## Strategies
117
+
118
+ A `strategy()` script runs through the same engine as an indicator. PineTS's
119
+ broker emulator already computes the full ledger; this package emits **one marker
120
+ per order fill** as `IndicatorModel.trades`, at the fill bar and price. Ledger
121
+ slices of the same fill merge: a reversal paints a single entry carrying the
122
+ summed quantity, and an exit that closes several lots FIFO paints once. Order ids
123
+ label the markers; a `comment=` replaces the id. Vela paints them on the price
124
+ pane.
125
+
126
+ ## Declaration props
127
+
128
+ Both engines expose the mutable `indicator()` / `strategy()` declaration arguments
129
+ through Vela's props channel. `prepare` publishes a schema whose defaults are the
130
+ effective values (source-declared ← engine default ← Pine spec), and prop
131
+ overrides — add-time, live, or edited on the **Properties** tab — replay the
132
+ script.
133
+
134
+ ```ts
135
+ const engine = new PineEngine({
136
+ defaultProps: { initial_capital: 50_000 },
137
+ props: 'strategy', // 'all' | 'strategy' | 'none' | a whitelist of prop keys
138
+ });
139
+
140
+ chart.addIndicator({
141
+ script: `//@version=5
142
+ strategy("Demo", overlay=true, initial_capital=10000)
143
+ if ta.crossover(close, ta.sma(close, 20))
144
+ strategy.entry("Long", strategy.long)`,
145
+ props: { commission_value: 0.05 },
146
+ });
147
+ ```
148
+
149
+ `defaultProps` sets host-level defaults for scripts that don't declare the prop
150
+ themselves. `props` gates which scripts publish the schema — `'strategy'` gives
151
+ strategies a Properties tab while plain indicators keep an inputs-only dialog.
152
+
153
+ ## Development
154
+
155
+ ```bash
44
156
  npm install
45
- npm run playground # http://localhost:5192 — Vela widget + this engine, HMR
46
- npm run typecheck && npm run lint && npm run test && npm run build
157
+ npm run playground # vite playground on http://localhost:5192
158
+ npm test # vitest
159
+ npm run build # tsup → dist/
47
160
  ```
48
161
 
162
+ The playground is the Vela widget plus this package's `PineWorkerEngine` (HMR,
163
+ engine sources from `src/`). An EMA is on from the first paint; the **Code**
164
+ topbar entry runs arbitrary Pine through `chart.runIndicator`.
165
+
49
166
  Vela is consumed as `file:../Vela` (built dist): clone this repo next to
50
167
  [Vela](https://github.com/LuxAlgo/Vela) and build Vela first.
51
168
 
52
169
  ## License
53
170
 
54
- [AGPL-3.0](LICENSE) this package depends on `pinets`, which is AGPL-3.0 licensed.
55
- The Vela charting library itself is Apache-2.0.
171
+ Vela PineTS is licensed under the **GNU Affero General Public License v3.0**
172
+ (see [LICENSE](LICENSE)) because it depends on `pinets`, which is AGPL-3.0.
173
+ The Vela charting library itself is Apache-2.0 and carries no Pine code.
174
+
175
+ [npm-version-img]: https://img.shields.io/npm/v/%40luxalgo%2Fvela-pinets.svg
176
+ [npm-downloads-img]: https://img.shields.io/npm/dm/%40luxalgo%2Fvela-pinets.svg
177
+ [npm-link]: https://www.npmjs.com/package/@luxalgo/vela-pinets
178
+
179
+ [license-img]: https://img.shields.io/badge/license-AGPL--3.0-blue.svg
180
+ [license-link]: LICENSE