oakscriptjs 0.23.0 → 0.25.0
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 +25 -1
- package/bin/oakscript.mjs +10 -3
- package/dist/cjs/box/index.d.ts +348 -289
- package/dist/cjs/box/index.d.ts.map +1 -1
- package/dist/cjs/box/index.js.map +2 -2
- package/dist/cjs/check/node.d.ts.map +1 -1
- package/dist/cjs/check/node.js +1 -0
- package/dist/cjs/check/node.js.map +2 -2
- package/dist/cjs/check/preview.d.ts +31 -0
- package/dist/cjs/check/preview.d.ts.map +1 -0
- package/dist/cjs/check/preview.js +303 -0
- package/dist/cjs/check/preview.js.map +7 -0
- package/dist/cjs/color/index.d.ts +11 -11
- package/dist/cjs/color/index.js.map +1 -1
- package/dist/cjs/color/parse.d.ts +2 -0
- package/dist/cjs/color/parse.d.ts.map +1 -1
- package/dist/cjs/color/parse.js +6 -0
- package/dist/cjs/color/parse.js.map +2 -2
- package/dist/cjs/compare/index.d.ts +76 -12
- package/dist/cjs/compare/index.d.ts.map +1 -1
- package/dist/cjs/compare/index.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +28 -5
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +14 -0
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/label/index.d.ts +265 -187
- package/dist/cjs/label/index.d.ts.map +1 -1
- package/dist/cjs/label/index.js +1 -1
- package/dist/cjs/label/index.js.map +2 -2
- package/dist/cjs/line/index.d.ts +247 -207
- package/dist/cjs/line/index.d.ts.map +1 -1
- package/dist/cjs/line/index.js.map +2 -2
- package/dist/cjs/linefill/index.d.ts +61 -89
- package/dist/cjs/linefill/index.d.ts.map +1 -1
- package/dist/cjs/linefill/index.js.map +2 -2
- package/dist/cjs/math/index.d.ts +49 -40
- package/dist/cjs/math/index.d.ts.map +1 -1
- package/dist/cjs/math/index.js.map +2 -2
- package/dist/cjs/polyline/index.d.ts +57 -78
- package/dist/cjs/polyline/index.d.ts.map +1 -1
- package/dist/cjs/polyline/index.js.map +2 -2
- package/dist/cjs/runtime/input-active.d.ts +2 -2
- package/dist/cjs/runtime/input-active.js.map +1 -1
- package/dist/cjs/runtime/series.d.ts +241 -161
- package/dist/cjs/runtime/series.d.ts.map +1 -1
- package/dist/cjs/runtime/series.js +223 -127
- package/dist/cjs/runtime/series.js.map +2 -2
- package/dist/cjs/script/api/alert.d.ts +37 -0
- package/dist/cjs/script/api/alert.d.ts.map +1 -0
- package/dist/cjs/script/api/alert.js +17 -0
- package/dist/cjs/script/api/alert.js.map +7 -0
- package/dist/cjs/script/api/calc.d.ts +232 -0
- package/dist/cjs/script/api/calc.d.ts.map +1 -0
- package/dist/cjs/script/api/calc.js +17 -0
- package/dist/cjs/script/api/calc.js.map +7 -0
- package/dist/cjs/script/api/color.d.ts +213 -0
- package/dist/cjs/script/api/color.d.ts.map +1 -0
- package/dist/cjs/script/api/color.js +17 -0
- package/dist/cjs/script/api/color.js.map +7 -0
- package/dist/cjs/script/api/context.d.ts +764 -0
- package/dist/cjs/script/api/context.d.ts.map +1 -0
- package/dist/cjs/script/api/context.js +17 -0
- package/dist/cjs/script/api/context.js.map +7 -0
- package/dist/cjs/script/api/log.d.ts +58 -0
- package/dist/cjs/script/api/log.d.ts.map +1 -0
- package/dist/cjs/script/api/log.js +17 -0
- package/dist/cjs/script/api/log.js.map +7 -0
- package/dist/cjs/script/api/strategy.d.ts +391 -0
- package/dist/cjs/script/api/strategy.d.ts.map +1 -0
- package/dist/cjs/script/api/strategy.js +17 -0
- package/dist/cjs/script/api/strategy.js.map +7 -0
- package/dist/cjs/script/api/ta.d.ts +1012 -0
- package/dist/cjs/script/api/ta.d.ts.map +1 -0
- package/dist/cjs/script/api/ta.js +17 -0
- package/dist/cjs/script/api/ta.js.map +7 -0
- package/dist/cjs/script/api/table.d.ts +479 -0
- package/dist/cjs/script/api/table.d.ts.map +1 -0
- package/dist/cjs/script/api/table.js +17 -0
- package/dist/cjs/script/api/table.js.map +7 -0
- package/dist/cjs/script/constants.d.ts +228 -83
- package/dist/cjs/script/constants.d.ts.map +1 -1
- package/dist/cjs/script/constants.js +87 -0
- package/dist/cjs/script/constants.js.map +2 -2
- package/dist/cjs/script/define.d.ts +94 -0
- package/dist/cjs/script/define.d.ts.map +1 -0
- package/dist/cjs/script/define.js +127 -0
- package/dist/cjs/script/define.js.map +7 -0
- package/dist/cjs/script/drawing-args.d.ts.map +1 -1
- package/dist/cjs/script/drawing-args.js +1 -1
- package/dist/cjs/script/drawing-args.js.map +2 -2
- package/dist/cjs/script/formula.d.ts +53 -0
- package/dist/cjs/script/formula.d.ts.map +1 -0
- package/dist/cjs/script/formula.js +402 -0
- package/dist/cjs/script/formula.js.map +7 -0
- package/dist/cjs/script/globals.d.ts +10 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/index.d.ts +1065 -1006
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +532 -61
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/script/json.d.ts +117 -0
- package/dist/cjs/script/json.d.ts.map +1 -0
- package/dist/cjs/script/json.js +321 -0
- package/dist/cjs/script/json.js.map +7 -0
- package/dist/cjs/security/other.d.ts +8 -3
- package/dist/cjs/security/other.d.ts.map +1 -1
- package/dist/cjs/security/other.js +23 -6
- package/dist/cjs/security/other.js.map +2 -2
- package/dist/cjs/str/index.d.ts +50 -75
- package/dist/cjs/str/index.d.ts.map +1 -1
- package/dist/cjs/str/index.js +2 -2
- package/dist/cjs/str/index.js.map +2 -2
- package/dist/cjs/strategy/index.d.ts +102 -1
- package/dist/cjs/strategy/index.d.ts.map +1 -1
- package/dist/cjs/strategy/index.js.map +2 -2
- package/dist/cjs/table/index.d.ts +330 -49
- package/dist/cjs/table/index.d.ts.map +1 -1
- package/dist/cjs/table/index.js +55 -27
- package/dist/cjs/table/index.js.map +2 -2
- package/dist/cjs/text/index.d.ts +12 -14
- package/dist/cjs/text/index.d.ts.map +1 -1
- package/dist/cjs/text/index.js.map +2 -2
- package/dist/cjs/types/index.d.ts +15 -8
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +12 -3
- package/dist/cjs/types/index.js.map +2 -2
- package/dist/esm/box/index.d.ts +348 -289
- package/dist/esm/box/index.d.ts.map +1 -1
- package/dist/esm/box/index.js +276 -276
- package/dist/esm/box/index.js.map +1 -1
- package/dist/esm/check/node.d.ts.map +1 -1
- package/dist/esm/check/node.js +1 -0
- package/dist/esm/check/node.js.map +1 -1
- package/dist/esm/check/preview.d.ts +31 -0
- package/dist/esm/check/preview.d.ts.map +1 -0
- package/dist/esm/check/preview.js +296 -0
- package/dist/esm/check/preview.js.map +1 -0
- package/dist/esm/color/index.d.ts +11 -11
- package/dist/esm/color/index.js +11 -11
- package/dist/esm/color/parse.d.ts +2 -0
- package/dist/esm/color/parse.d.ts.map +1 -1
- package/dist/esm/color/parse.js +7 -0
- package/dist/esm/color/parse.js.map +1 -1
- package/dist/esm/compare/index.d.ts +76 -12
- package/dist/esm/compare/index.d.ts.map +1 -1
- package/dist/esm/compare/index.js +76 -12
- package/dist/esm/compare/index.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +28 -5
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +39 -3
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/label/index.d.ts +265 -187
- package/dist/esm/label/index.d.ts.map +1 -1
- package/dist/esm/label/index.js +207 -177
- package/dist/esm/label/index.js.map +1 -1
- package/dist/esm/line/index.d.ts +247 -207
- package/dist/esm/line/index.d.ts.map +1 -1
- package/dist/esm/line/index.js +202 -194
- package/dist/esm/line/index.js.map +1 -1
- package/dist/esm/linefill/index.d.ts +61 -89
- package/dist/esm/linefill/index.d.ts.map +1 -1
- package/dist/esm/linefill/index.js +61 -89
- package/dist/esm/linefill/index.js.map +1 -1
- package/dist/esm/math/index.d.ts +49 -40
- package/dist/esm/math/index.d.ts.map +1 -1
- package/dist/esm/math/index.js +22 -9
- package/dist/esm/math/index.js.map +1 -1
- package/dist/esm/polyline/index.d.ts +57 -78
- package/dist/esm/polyline/index.d.ts.map +1 -1
- package/dist/esm/polyline/index.js +57 -78
- package/dist/esm/polyline/index.js.map +1 -1
- package/dist/esm/runtime/input-active.d.ts +2 -2
- package/dist/esm/runtime/input-active.js +2 -2
- package/dist/esm/runtime/series.d.ts +241 -161
- package/dist/esm/runtime/series.d.ts.map +1 -1
- package/dist/esm/runtime/series.js +241 -161
- package/dist/esm/runtime/series.js.map +1 -1
- package/dist/esm/script/api/alert.d.ts +37 -0
- package/dist/esm/script/api/alert.d.ts.map +1 -0
- package/dist/esm/script/api/alert.js +7 -0
- package/dist/esm/script/api/alert.js.map +1 -0
- package/dist/esm/script/api/calc.d.ts +232 -0
- package/dist/esm/script/api/calc.d.ts.map +1 -0
- package/dist/esm/script/api/calc.js +6 -0
- package/dist/esm/script/api/calc.js.map +1 -0
- package/dist/esm/script/api/color.d.ts +213 -0
- package/dist/esm/script/api/color.d.ts.map +1 -0
- package/dist/esm/script/api/color.js +6 -0
- package/dist/esm/script/api/color.js.map +1 -0
- package/dist/esm/script/api/context.d.ts +764 -0
- package/dist/esm/script/api/context.d.ts.map +1 -0
- package/dist/esm/script/api/context.js +7 -0
- package/dist/esm/script/api/context.js.map +1 -0
- package/dist/esm/script/api/log.d.ts +58 -0
- package/dist/esm/script/api/log.d.ts.map +1 -0
- package/dist/esm/script/api/log.js +6 -0
- package/dist/esm/script/api/log.js.map +1 -0
- package/dist/esm/script/api/strategy.d.ts +391 -0
- package/dist/esm/script/api/strategy.d.ts.map +1 -0
- package/dist/esm/script/api/strategy.js +7 -0
- package/dist/esm/script/api/strategy.js.map +1 -0
- package/dist/esm/script/api/ta.d.ts +1012 -0
- package/dist/esm/script/api/ta.d.ts.map +1 -0
- package/dist/esm/script/api/ta.js +6 -0
- package/dist/esm/script/api/ta.js.map +1 -0
- package/dist/esm/script/api/table.d.ts +479 -0
- package/dist/esm/script/api/table.d.ts.map +1 -0
- package/dist/esm/script/api/table.js +7 -0
- package/dist/esm/script/api/table.js.map +1 -0
- package/dist/esm/script/constants.d.ts +228 -83
- package/dist/esm/script/constants.d.ts.map +1 -1
- package/dist/esm/script/constants.js +110 -24
- package/dist/esm/script/constants.js.map +1 -1
- package/dist/esm/script/define.d.ts +94 -0
- package/dist/esm/script/define.d.ts.map +1 -0
- package/dist/esm/script/define.js +136 -0
- package/dist/esm/script/define.js.map +1 -0
- package/dist/esm/script/drawing-args.d.ts.map +1 -1
- package/dist/esm/script/drawing-args.js +2 -1
- package/dist/esm/script/drawing-args.js.map +1 -1
- package/dist/esm/script/formula.d.ts +53 -0
- package/dist/esm/script/formula.d.ts.map +1 -0
- package/dist/esm/script/formula.js +408 -0
- package/dist/esm/script/formula.js.map +1 -0
- package/dist/esm/script/globals.d.ts +10 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/index.d.ts +1065 -1006
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +1099 -153
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/script/json.d.ts +117 -0
- package/dist/esm/script/json.d.ts.map +1 -0
- package/dist/esm/script/json.js +375 -0
- package/dist/esm/script/json.js.map +1 -0
- package/dist/esm/security/other.d.ts +8 -3
- package/dist/esm/security/other.d.ts.map +1 -1
- package/dist/esm/security/other.js +45 -10
- package/dist/esm/security/other.js.map +1 -1
- package/dist/esm/str/index.d.ts +50 -75
- package/dist/esm/str/index.d.ts.map +1 -1
- package/dist/esm/str/index.js +51 -76
- package/dist/esm/str/index.js.map +1 -1
- package/dist/esm/strategy/index.d.ts +102 -1
- package/dist/esm/strategy/index.d.ts.map +1 -1
- package/dist/esm/strategy/index.js +1 -1
- package/dist/esm/strategy/index.js.map +1 -1
- package/dist/esm/table/index.d.ts +330 -49
- package/dist/esm/table/index.d.ts.map +1 -1
- package/dist/esm/table/index.js +372 -61
- package/dist/esm/table/index.js.map +1 -1
- package/dist/esm/text/index.d.ts +12 -14
- package/dist/esm/text/index.d.ts.map +1 -1
- package/dist/esm/text/index.js +12 -14
- package/dist/esm/text/index.js.map +1 -1
- package/dist/esm/types/index.d.ts +15 -8
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js +15 -8
- package/dist/esm/types/index.js.map +1 -1
- package/docs/first-indicator.md +152 -0
- package/docs/guide.md +1185 -0
- package/docs/inventory.md +772 -0
- package/docs/no-code.md +207 -0
- package/docs/recipes.md +202 -0
- package/package.json +2 -1
- package/skills/oakscript/SKILL.md +8 -0
- package/skills/oakscript/reference/errors.md +3 -2
- package/skills/oakscript/reference/timeframes-symbols.md +4 -0
- package/src/box/index.ts +348 -289
- package/src/check/node.ts +1 -0
- package/src/check/preview.ts +307 -0
- package/src/color/index.ts +11 -11
- package/src/color/parse.ts +7 -0
- package/src/compare/index.ts +76 -12
- package/src/drawing/registry.ts +44 -5
- package/src/index.ts +1 -1
- package/src/label/index.ts +267 -189
- package/src/line/index.ts +248 -208
- package/src/linefill/index.ts +61 -89
- package/src/math/index.ts +51 -42
- package/src/polyline/index.ts +57 -78
- package/src/runtime/input-active.ts +2 -2
- package/src/runtime/series.ts +243 -163
- package/src/script/api/alert.ts +43 -0
- package/src/script/api/calc.ts +250 -0
- package/src/script/api/color.ts +245 -0
- package/src/script/api/context.ts +944 -0
- package/src/script/api/log.ts +60 -0
- package/src/script/api/strategy.ts +410 -0
- package/src/script/api/ta.ts +1115 -0
- package/src/script/api/table.ts +538 -0
- package/src/script/constants.ts +229 -25
- package/src/script/define.ts +221 -0
- package/src/script/drawing-args.ts +2 -1
- package/src/script/formula.ts +438 -0
- package/src/script/globals.ts +10 -0
- package/src/script/index.ts +1520 -241
- package/src/script/json.ts +457 -0
- package/src/security/other.ts +43 -9
- package/src/str/index.ts +51 -76
- package/src/strategy/index.ts +102 -1
- package/src/table/index.ts +388 -62
- package/src/text/index.ts +12 -14
- package/src/types/index.ts +15 -8
|
@@ -153,32 +153,30 @@ class Series {
|
|
|
153
153
|
cached = null;
|
|
154
154
|
cachedVersion = -1;
|
|
155
155
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* @param data - Bar data (Bar[] or BarData)
|
|
159
|
-
* @param extractor - Function to extract/compute value for each bar
|
|
156
|
+
* True for the built-in price series (`open`, `high`, `low`, `close`, `volume`, `hl2`...): their history covers
|
|
157
|
+
* every bar, so `offset()` may reach further than 5000 bars back.
|
|
160
158
|
*/
|
|
159
|
+
fullHistory = false;
|
|
161
160
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
161
|
+
* Makes a Series that computes its value on each bar with a function. Scripts rarely need it: Series come from the
|
|
162
|
+
* builtins, `ta.*` and the Series methods.
|
|
163
|
+
* @param data - The bars the series is aligned to (an array of bars, or a {@link BarData}).
|
|
164
|
+
* @param extractor - Gives the value of bar `index`: `(bar, index, bars) => number`; NaN for na.
|
|
165
|
+
* @example
|
|
166
|
+
* ```js
|
|
167
|
+
* const range = new Series(close.bars, (bar) => bar.high - bar.low);
|
|
168
|
+
* ```
|
|
164
169
|
*/
|
|
165
|
-
fullHistory = false;
|
|
166
170
|
constructor(data, extractor) {
|
|
167
171
|
this.dataSource = data instanceof BarData ? data : new BarData(data);
|
|
168
172
|
this.extractor = extractor;
|
|
169
173
|
runHooks?.created(this);
|
|
170
174
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Get the underlying bar data
|
|
173
|
-
* @returns Bar array
|
|
174
|
-
*/
|
|
175
|
+
/** The bars this series is aligned to (`{ time, open, high, low, close, volume }`, oldest first). */
|
|
175
176
|
get bars() {
|
|
176
177
|
return this.dataSource.bars;
|
|
177
178
|
}
|
|
178
|
-
/**
|
|
179
|
-
* Get the underlying BarData source
|
|
180
|
-
* @returns BarData instance
|
|
181
|
-
*/
|
|
179
|
+
/** The {@link BarData} holding the bars this series is aligned to. */
|
|
182
180
|
get barData() {
|
|
183
181
|
return this.dataSource;
|
|
184
182
|
}
|
|
@@ -186,10 +184,15 @@ class Series {
|
|
|
186
184
|
// Static Factory Methods
|
|
187
185
|
// ============================================
|
|
188
186
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @param
|
|
192
|
-
* @
|
|
187
|
+
* Makes the Series of one field of the bars (the price series). In a script, use the globals `open`, `high`, `low`,
|
|
188
|
+
* `close`, `volume` instead.
|
|
189
|
+
* @param bars - The bars (an array of bars, or a {@link BarData}).
|
|
190
|
+
* @param field - `'open'`, `'high'`, `'low'`, `'close'` or `'volume'`.
|
|
191
|
+
* @returns A Series of that field; NaN where a bar does not have it.
|
|
192
|
+
* @example
|
|
193
|
+
* ```js
|
|
194
|
+
* const myClose = Series.fromBars(close.bars, "close");
|
|
195
|
+
* ```
|
|
193
196
|
*/
|
|
194
197
|
static fromBars(bars, field) {
|
|
195
198
|
const series = new Series(bars, (bar) => bar[field] ?? NaN);
|
|
@@ -197,21 +200,31 @@ class Series {
|
|
|
197
200
|
return series;
|
|
198
201
|
}
|
|
199
202
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* @param
|
|
203
|
-
* @
|
|
203
|
+
* Makes a Series with the same value on every bar. The Series methods also take plain numbers, so a script seldom
|
|
204
|
+
* needs it.
|
|
205
|
+
* @param bars - The bars to align to (an array of bars, or a {@link BarData}).
|
|
206
|
+
* @param value - The value of every bar (NaN for na).
|
|
207
|
+
* @returns A Series of `value`.
|
|
208
|
+
* @example
|
|
209
|
+
* ```js
|
|
210
|
+
* const level = Series.constant(close.bars, 50);
|
|
211
|
+
* ```
|
|
204
212
|
*/
|
|
205
213
|
static constant(bars, value) {
|
|
206
214
|
return new Series(bars, () => value);
|
|
207
215
|
}
|
|
208
216
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @param
|
|
212
|
-
* @
|
|
217
|
+
* Makes a Series from an array with one value per bar, oldest first. In a script, `seriesOf(values)` does the same
|
|
218
|
+
* on the chart bars.
|
|
219
|
+
* @param bars - The bars the values belong to (an array of bars, or a {@link BarData}).
|
|
220
|
+
* @param values - One number per bar, oldest first; a missing value reads as NaN.
|
|
221
|
+
* @returns A Series of the values.
|
|
213
222
|
* @throws When read on other bars with a different length than `values` (e.g. a series computed on the
|
|
214
|
-
* chart bars and read inside a `request.security` expression, which runs on the
|
|
223
|
+
* chart bars and read inside a `request.security` expression, which runs on the other timeframe's bars).
|
|
224
|
+
* @example
|
|
225
|
+
* ```js
|
|
226
|
+
* const doubled = Series.fromArray(close.bars, close.toArray().map((v) => v * 2));
|
|
227
|
+
* ```
|
|
215
228
|
*/
|
|
216
229
|
static fromArray(bars, values) {
|
|
217
230
|
const origin = bars instanceof BarData ? bars.bars : bars;
|
|
@@ -228,9 +241,13 @@ class Series {
|
|
|
228
241
|
// Arithmetic Operations
|
|
229
242
|
// ============================================
|
|
230
243
|
/**
|
|
231
|
-
*
|
|
232
|
-
* @param other - Series or number to add
|
|
233
|
-
* @returns
|
|
244
|
+
* Adds a Series or a number to this series, bar by bar (the `+` of two values). NaN on either side gives NaN.
|
|
245
|
+
* @param other - Series or number to add.
|
|
246
|
+
* @returns A Series of the sums.
|
|
247
|
+
* @example
|
|
248
|
+
* ```js
|
|
249
|
+
* const mid = high.add(low).div(2);
|
|
250
|
+
* ```
|
|
234
251
|
*/
|
|
235
252
|
add(other) {
|
|
236
253
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -240,9 +257,13 @@ class Series {
|
|
|
240
257
|
});
|
|
241
258
|
}
|
|
242
259
|
/**
|
|
243
|
-
*
|
|
244
|
-
* @param other - Series or number to subtract
|
|
245
|
-
* @returns
|
|
260
|
+
* Subtracts a Series or a number from this series, bar by bar (the `-` of two values). NaN on either side gives NaN.
|
|
261
|
+
* @param other - Series or number to subtract.
|
|
262
|
+
* @returns A Series of the differences.
|
|
263
|
+
* @example
|
|
264
|
+
* ```js
|
|
265
|
+
* const body = close.sub(open);
|
|
266
|
+
* ```
|
|
246
267
|
*/
|
|
247
268
|
sub(other) {
|
|
248
269
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -252,9 +273,13 @@ class Series {
|
|
|
252
273
|
});
|
|
253
274
|
}
|
|
254
275
|
/**
|
|
255
|
-
*
|
|
256
|
-
* @param other - Series or number to multiply
|
|
257
|
-
* @returns
|
|
276
|
+
* Multiplies this series by a Series or a number, bar by bar (the `*` of two values). NaN on either side gives NaN.
|
|
277
|
+
* @param other - Series or number to multiply by.
|
|
278
|
+
* @returns A Series of the products.
|
|
279
|
+
* @example
|
|
280
|
+
* ```js
|
|
281
|
+
* const upper = ta.sma(close, 20).mul(1.02);
|
|
282
|
+
* ```
|
|
258
283
|
*/
|
|
259
284
|
mul(other) {
|
|
260
285
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -264,9 +289,14 @@ class Series {
|
|
|
264
289
|
});
|
|
265
290
|
}
|
|
266
291
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @
|
|
292
|
+
* Raises this series to a power, bar by bar (`math.pow` on a series). NaN on either side gives NaN; a negative base
|
|
293
|
+
* with a fractional exponent gives NaN.
|
|
294
|
+
* @param exponent - Series or number exponent.
|
|
295
|
+
* @returns A Series of `this ** exponent`.
|
|
296
|
+
* @example
|
|
297
|
+
* ```js
|
|
298
|
+
* const squared = close.sub(ta.sma(close, 20)).pow(2);
|
|
299
|
+
* ```
|
|
270
300
|
*/
|
|
271
301
|
pow(exponent) {
|
|
272
302
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -276,9 +306,15 @@ class Series {
|
|
|
276
306
|
});
|
|
277
307
|
}
|
|
278
308
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* @
|
|
309
|
+
* Divides this series by a Series or a number, bar by bar (the `/` of two values). A division by 0 gives
|
|
310
|
+
* +/-Infinity (which `na()` treats as na), 0 / 0 and NaN on either side give NaN.
|
|
311
|
+
* @param other - Series or number to divide by.
|
|
312
|
+
* @returns A Series of the quotients.
|
|
313
|
+
* @example
|
|
314
|
+
* ```js
|
|
315
|
+
* const range = high.sub(low);
|
|
316
|
+
* const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
|
|
317
|
+
* ```
|
|
282
318
|
*/
|
|
283
319
|
div(other) {
|
|
284
320
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -288,9 +324,14 @@ class Series {
|
|
|
288
324
|
});
|
|
289
325
|
}
|
|
290
326
|
/**
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
* @
|
|
327
|
+
* Remainder of the division of this series by a Series or a number, bar by bar (the `%` of two values). The
|
|
328
|
+
* remainder has the sign of this series; a divisor of 0 and NaN on either side give NaN.
|
|
329
|
+
* @param other - Series or number to divide by.
|
|
330
|
+
* @returns A Series of the remainders.
|
|
331
|
+
* @example
|
|
332
|
+
* ```js
|
|
333
|
+
* const everyFifth = bar_index.mod(5).eq(0);
|
|
334
|
+
* ```
|
|
294
335
|
*/
|
|
295
336
|
mod(other) {
|
|
296
337
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -300,8 +341,12 @@ class Series {
|
|
|
300
341
|
});
|
|
301
342
|
}
|
|
302
343
|
/**
|
|
303
|
-
*
|
|
304
|
-
* @returns
|
|
344
|
+
* Changes the sign of every value (the unary `-`). NaN stays NaN.
|
|
345
|
+
* @returns A Series of the opposite values.
|
|
346
|
+
* @example
|
|
347
|
+
* ```js
|
|
348
|
+
* const drop = close.sub(close.offset(1)).neg();
|
|
349
|
+
* ```
|
|
305
350
|
*/
|
|
306
351
|
neg() {
|
|
307
352
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -312,9 +357,14 @@ class Series {
|
|
|
312
357
|
// Comparison Operations (PineScript operators: tolerance 1e-10, false with na; see src/compare)
|
|
313
358
|
// ============================================
|
|
314
359
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
* @
|
|
360
|
+
* Tests whether this series is above a Series or a number, bar by bar (`a > b`).
|
|
361
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
362
|
+
* @param other - Series or number to compare with.
|
|
363
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
364
|
+
* @example
|
|
365
|
+
* ```js
|
|
366
|
+
* const up = close.gt(open);
|
|
367
|
+
* ```
|
|
318
368
|
*/
|
|
319
369
|
gt(other) {
|
|
320
370
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -324,9 +374,14 @@ class Series {
|
|
|
324
374
|
});
|
|
325
375
|
}
|
|
326
376
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* @
|
|
377
|
+
* Tests whether this series is above or equal to a Series or a number, bar by bar (`a >= b`).
|
|
378
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
379
|
+
* @param other - Series or number to compare with.
|
|
380
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
381
|
+
* @example
|
|
382
|
+
* ```js
|
|
383
|
+
* const atOrAbove = close.gte(ta.sma(close, 50));
|
|
384
|
+
* ```
|
|
330
385
|
*/
|
|
331
386
|
gte(other) {
|
|
332
387
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -336,9 +391,14 @@ class Series {
|
|
|
336
391
|
});
|
|
337
392
|
}
|
|
338
393
|
/**
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
* @
|
|
394
|
+
* Tests whether this series is below a Series or a number, bar by bar (`a < b`).
|
|
395
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
396
|
+
* @param other - Series or number to compare with.
|
|
397
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
398
|
+
* @example
|
|
399
|
+
* ```js
|
|
400
|
+
* const oversold = ta.rsi(close, 14).lt(30);
|
|
401
|
+
* ```
|
|
342
402
|
*/
|
|
343
403
|
lt(other) {
|
|
344
404
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -348,9 +408,14 @@ class Series {
|
|
|
348
408
|
});
|
|
349
409
|
}
|
|
350
410
|
/**
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* @
|
|
411
|
+
* Tests whether this series is below or equal to a Series or a number, bar by bar (`a <= b`).
|
|
412
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
413
|
+
* @param other - Series or number to compare with.
|
|
414
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
415
|
+
* @example
|
|
416
|
+
* ```js
|
|
417
|
+
* const atOrBelow = low.lte(ta.lowest(low, 20).offset(1));
|
|
418
|
+
* ```
|
|
354
419
|
*/
|
|
355
420
|
lte(other) {
|
|
356
421
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -360,9 +425,14 @@ class Series {
|
|
|
360
425
|
});
|
|
361
426
|
}
|
|
362
427
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
* @
|
|
428
|
+
* Tests whether this series equals a Series or a number, bar by bar (`a == b`).
|
|
429
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
430
|
+
* @param other - Series or number to compare with.
|
|
431
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
432
|
+
* @example
|
|
433
|
+
* ```js
|
|
434
|
+
* const doji = close.eq(open);
|
|
435
|
+
* ```
|
|
366
436
|
*/
|
|
367
437
|
eq(other) {
|
|
368
438
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -372,9 +442,14 @@ class Series {
|
|
|
372
442
|
});
|
|
373
443
|
}
|
|
374
444
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* @
|
|
445
|
+
* Tests whether this series differs from a Series or a number, bar by bar (`a != b`).
|
|
446
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
447
|
+
* @param other - Series or number to compare with.
|
|
448
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
449
|
+
* @example
|
|
450
|
+
* ```js
|
|
451
|
+
* const moved = close.neq(close.offset(1));
|
|
452
|
+
* ```
|
|
378
453
|
*/
|
|
379
454
|
neq(other) {
|
|
380
455
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -387,9 +462,13 @@ class Series {
|
|
|
387
462
|
// Logical Operations (for boolean series)
|
|
388
463
|
// ============================================
|
|
389
464
|
/**
|
|
390
|
-
* Logical
|
|
391
|
-
* @param other - Series or number
|
|
392
|
-
* @returns
|
|
465
|
+
* Logical "and" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
|
|
466
|
+
* @param other - The other condition: a Series or a number.
|
|
467
|
+
* @returns A Series of 1 where both are true, 0 elsewhere.
|
|
468
|
+
* @example
|
|
469
|
+
* ```js
|
|
470
|
+
* const strongUp = close.gt(open).and(volume.gt(ta.sma(volume, 20)));
|
|
471
|
+
* ```
|
|
393
472
|
*/
|
|
394
473
|
and(other) {
|
|
395
474
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -399,9 +478,13 @@ class Series {
|
|
|
399
478
|
});
|
|
400
479
|
}
|
|
401
480
|
/**
|
|
402
|
-
* Logical
|
|
403
|
-
* @param other - Series or number
|
|
404
|
-
* @returns
|
|
481
|
+
* Logical "or" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
|
|
482
|
+
* @param other - The other condition: a Series or a number.
|
|
483
|
+
* @returns A Series of 1 where at least one is true, 0 elsewhere.
|
|
484
|
+
* @example
|
|
485
|
+
* ```js
|
|
486
|
+
* const extreme = ta.rsi(close, 14).gt(70).or(ta.rsi(close, 14).lt(30));
|
|
487
|
+
* ```
|
|
405
488
|
*/
|
|
406
489
|
or(other) {
|
|
407
490
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -411,8 +494,12 @@ class Series {
|
|
|
411
494
|
});
|
|
412
495
|
}
|
|
413
496
|
/**
|
|
414
|
-
* Logical
|
|
415
|
-
* @returns
|
|
497
|
+
* Logical "not" of a condition, bar by bar: true (not 0, not NaN) becomes 0, and 0 or NaN becomes 1.
|
|
498
|
+
* @returns A Series of 1 where this series is false, 0 where it is true.
|
|
499
|
+
* @example
|
|
500
|
+
* ```js
|
|
501
|
+
* const notUp = close.gt(open).not();
|
|
502
|
+
* ```
|
|
416
503
|
*/
|
|
417
504
|
not() {
|
|
418
505
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -424,21 +511,16 @@ class Series {
|
|
|
424
511
|
// Conditional Selection
|
|
425
512
|
// ============================================
|
|
426
513
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
* @param
|
|
431
|
-
* @
|
|
432
|
-
* @returns New Series with bar-by-bar selected values
|
|
433
|
-
*
|
|
514
|
+
* Picks one of two values on each bar, with this series as the condition (the `cond ? x : y` of a series). The
|
|
515
|
+
* condition is true when it is not 0 and not NaN; 0 and NaN pick `falseValue`.
|
|
516
|
+
* @param trueValue - Series or number used where the condition is true.
|
|
517
|
+
* @param falseValue - Series or number used where the condition is false.
|
|
518
|
+
* @returns A Series of the picked values.
|
|
434
519
|
* @example
|
|
435
|
-
* ```
|
|
436
|
-
*
|
|
437
|
-
* const
|
|
438
|
-
*
|
|
439
|
-
* // Example: RSI formula uses ternary for edge cases
|
|
440
|
-
* // rsi = down == 0 ? 100 : up == 0 ? 0 : formula
|
|
441
|
-
* const rsi = down.eq(0).iff(100, up.eq(0).iff(0, formula));
|
|
520
|
+
* ```js
|
|
521
|
+
* const range = high.sub(low);
|
|
522
|
+
* const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
|
|
523
|
+
* plot(ratio, "Body / range");
|
|
442
524
|
* ```
|
|
443
525
|
*/
|
|
444
526
|
iff(trueValue, falseValue) {
|
|
@@ -456,10 +538,17 @@ class Series {
|
|
|
456
538
|
// Offset/History
|
|
457
539
|
// ============================================
|
|
458
540
|
/**
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
* @
|
|
462
|
-
*
|
|
541
|
+
* Shifts the series back in time: on each bar, the value `offset` bars earlier (`close.offset(1)` is the previous
|
|
542
|
+
* close). The first `offset` bars, which have no earlier bar, get NaN.
|
|
543
|
+
* @param offset - Number of bars back: 0 (the series itself) or more, a whole number. A series computed by the
|
|
544
|
+
* script can look back at most 5000 bars; the price series (`close`, `high`...) any number.
|
|
545
|
+
* @returns A Series of the earlier values.
|
|
546
|
+
* @throws RangeError when `offset` is negative (it would read a future bar); an error past 5000 bars.
|
|
547
|
+
* @example
|
|
548
|
+
* ```js
|
|
549
|
+
* const change = close.sub(close.offset(1));
|
|
550
|
+
* const higherHigh = high.gt(high.offset(1));
|
|
551
|
+
* ```
|
|
463
552
|
*/
|
|
464
553
|
offset(offset) {
|
|
465
554
|
if (offset < 0) throw new RangeError(`Series.offset(${offset}): the offset cannot be negative (it would read a future bar).`);
|
|
@@ -476,14 +565,14 @@ class Series {
|
|
|
476
565
|
// Computation & Access
|
|
477
566
|
// ============================================
|
|
478
567
|
/**
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
*
|
|
568
|
+
* Gives the values of all the bars as a plain array, oldest bar first (NaN for na). The values are computed on the
|
|
569
|
+
* first read and kept until the bars change; the array is shared, so copy it before changing it.
|
|
570
|
+
* @returns One number per bar, oldest first.
|
|
571
|
+
* @example
|
|
572
|
+
* ```js
|
|
573
|
+
* const values = ta.sma(close, 20).toArray();
|
|
574
|
+
* const shifted = seriesOf(values.map((v) => v * 1.01));
|
|
575
|
+
* ```
|
|
487
576
|
*/
|
|
488
577
|
toArray() {
|
|
489
578
|
if (this.cached !== null && this.cachedVersion === this.dataSource.version) {
|
|
@@ -506,24 +595,13 @@ class Series {
|
|
|
506
595
|
return this.cached;
|
|
507
596
|
}
|
|
508
597
|
/**
|
|
509
|
-
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
* - Computes all values immediately
|
|
513
|
-
* - Creates a new Series that stores values directly
|
|
514
|
-
* - Breaks references to parent Series objects
|
|
515
|
-
* - Useful for complex expressions to free intermediate Series memory
|
|
516
|
-
*
|
|
598
|
+
* Computes the values now and returns a Series that stores them, without links to the series it was built from.
|
|
599
|
+
* Use it on a long chain of methods to compute it once and free the steps in between.
|
|
600
|
+
* @returns A Series of the same values, stored.
|
|
517
601
|
* @example
|
|
518
|
-
* ```
|
|
519
|
-
*
|
|
520
|
-
* const result = a.add(b).mul(c).div(d).sub(e);
|
|
521
|
-
*
|
|
522
|
-
* // With materialize: breaks chain, frees a, b, c memory
|
|
523
|
-
* const result = a.add(b).mul(c).materialize().div(d).sub(e);
|
|
602
|
+
* ```js
|
|
603
|
+
* const spread = high.sub(low).div(close).mul(100).materialize();
|
|
524
604
|
* ```
|
|
525
|
-
*
|
|
526
|
-
* @returns New Series with computed values and no closure dependencies
|
|
527
605
|
*/
|
|
528
606
|
materialize() {
|
|
529
607
|
const values = this.toArray();
|
|
@@ -552,33 +630,43 @@ class Series {
|
|
|
552
630
|
return this.toArray();
|
|
553
631
|
}
|
|
554
632
|
/**
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
633
|
+
* Reads the value of one bar by its position: 0 is the oldest bar, `bar_index` counts the same way. This is not
|
|
634
|
+
* the "bars back" of `offset()`: inside a bar loop, read the current bar with `c.get(series)` and earlier bars with
|
|
635
|
+
* `c.get(series, k)`.
|
|
636
|
+
* @param index - Bar position, 0 for the oldest bar.
|
|
637
|
+
* @returns The value of that bar; NaN when there is no such bar.
|
|
638
|
+
* @example
|
|
639
|
+
* ```js
|
|
640
|
+
* const firstClose = close.get(0);
|
|
641
|
+
* ```
|
|
558
642
|
*/
|
|
559
643
|
get(index) {
|
|
560
644
|
const values = this.toArray();
|
|
561
645
|
return values[index] ?? NaN;
|
|
562
646
|
}
|
|
563
647
|
/**
|
|
564
|
-
*
|
|
565
|
-
* @returns
|
|
648
|
+
* Reads the value of the newest bar.
|
|
649
|
+
* @returns The value of the last bar; NaN when there are no bars.
|
|
650
|
+
* @example
|
|
651
|
+
* ```js
|
|
652
|
+
* const lastRsi = ta.rsi(close, 14).last();
|
|
653
|
+
* ```
|
|
566
654
|
*/
|
|
567
655
|
last() {
|
|
568
656
|
const values = this.toArray();
|
|
569
657
|
return values[values.length - 1] ?? NaN;
|
|
570
658
|
}
|
|
571
659
|
/**
|
|
572
|
-
*
|
|
573
|
-
* @returns
|
|
660
|
+
* Gives the number of bars of the series (a method, not a property).
|
|
661
|
+
* @returns The number of bars.
|
|
662
|
+
* @example
|
|
663
|
+
* ```js
|
|
664
|
+
* const bars = close.length();
|
|
665
|
+
* ```
|
|
574
666
|
*/
|
|
575
667
|
length() {
|
|
576
668
|
return this.dataSource.length;
|
|
577
669
|
}
|
|
578
|
-
/**
|
|
579
|
-
* Convert series to time-value pairs for charting
|
|
580
|
-
* @returns Array of { time, value } objects
|
|
581
|
-
*/
|
|
582
670
|
/**
|
|
583
671
|
* A Series holds the values of all the bars, so JavaScript operators cannot use it: without this guard
|
|
584
672
|
* `close - open` is NaN, `close > open` false and `` `${rsi}` `` "[object Object]", with no error. The
|
|
@@ -594,6 +682,14 @@ class Series {
|
|
|
594
682
|
"JavaScript operators (+ - * / % ** < > <= >= ==) do not work on a Series: use its methods, a.add(b) a.sub(b) a.mul(b) a.div(b) a.mod(b) a.pow(b) a.neg(), a.gt(b) a.gte(b) a.lt(b) a.lte(b) a.eq(b) a.neq(b), a.and(b) a.or(b) a.not(), cond.iff(x, y), a.offset(1) for a[1]; or read numbers inside eachBar() with c.get(a)."
|
|
595
683
|
);
|
|
596
684
|
}
|
|
685
|
+
/**
|
|
686
|
+
* Gives the values as `{ time, value }` points for a chart line, oldest first, leaving out the NaN bars.
|
|
687
|
+
* @returns The points; `time` is the bar time, in the unit of the bars.
|
|
688
|
+
* @example
|
|
689
|
+
* ```js
|
|
690
|
+
* const points = ta.sma(close, 20).toTimeValuePairs();
|
|
691
|
+
* ```
|
|
692
|
+
*/
|
|
597
693
|
toTimeValuePairs() {
|
|
598
694
|
const values = this.toArray();
|
|
599
695
|
const bars = this.dataSource.bars;
|