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
package/src/runtime/series.ts
CHANGED
|
@@ -13,12 +13,9 @@ import type { Bar } from '../types/index.js';
|
|
|
13
13
|
export type SeriesExtractor = (bar: Bar, index: number, data: Bar[]) => number;
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* whenever the data is mutated. Series objects reference this to detect when
|
|
20
|
-
* their cached values need to be recomputed.
|
|
21
|
-
*
|
|
16
|
+
* Host-side: an array of bars with a version number that grows on every change, so the Series made on it know when
|
|
17
|
+
* their cached values are stale. Scripts do not use it: their Series are made on the chart's bars.
|
|
18
|
+
*
|
|
22
19
|
* @example
|
|
23
20
|
* ```typescript
|
|
24
21
|
* const barData = new BarData(bars);
|
|
@@ -145,35 +142,6 @@ export class BarData {
|
|
|
145
142
|
}
|
|
146
143
|
}
|
|
147
144
|
|
|
148
|
-
/**
|
|
149
|
-
* Series - represents a time-series of values with lazy evaluation
|
|
150
|
-
*
|
|
151
|
-
* Series are lazy - they don't compute values until needed.
|
|
152
|
-
* Operations on Series return new Series with composed extractors.
|
|
153
|
-
*
|
|
154
|
-
* Now supports automatic cache invalidation via BarData versioning
|
|
155
|
-
* and memory-efficient closure chain breaking via materialize().
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* // Basic usage (backward compatible)
|
|
160
|
-
* const close = Series.fromBars(bars, 'close');
|
|
161
|
-
* const open = Series.fromBars(bars, 'open');
|
|
162
|
-
* const range = close.sub(open); // Creates new Series, doesn't compute yet
|
|
163
|
-
* const values = range.toArray(); // Now computes for all bars
|
|
164
|
-
*
|
|
165
|
-
* // With automatic cache invalidation
|
|
166
|
-
* const barData = new BarData(bars);
|
|
167
|
-
* const close = Series.fromBars(barData, 'close');
|
|
168
|
-
* const values1 = close.toArray(); // Computes and caches
|
|
169
|
-
* barData.push(newBar); // Version increments
|
|
170
|
-
* const values2 = close.toArray(); // Detects stale cache, recomputes
|
|
171
|
-
*
|
|
172
|
-
* // Breaking closure chains for memory efficiency
|
|
173
|
-
* const complex = a.add(b).mul(c).div(d).sub(e);
|
|
174
|
-
* const materialized = complex.materialize(); // Breaks closure chain
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
145
|
/** Largest history reference of a script series (PineScript max_bars_back limit). */
|
|
178
146
|
export const MAX_BARS_BACK = 5000;
|
|
179
147
|
|
|
@@ -204,6 +172,22 @@ export function setSeriesRunHooks(hooks: SeriesRunHooks | null): void {
|
|
|
204
172
|
runHooks = hooks;
|
|
205
173
|
}
|
|
206
174
|
|
|
175
|
+
/**
|
|
176
|
+
* A series: one number per bar, oldest bar first, NaN where the value is missing (na). `close`, `open`, `high`,
|
|
177
|
+
* `low`, `volume`, the `ta.*` results and `input.source()` are Series.
|
|
178
|
+
*
|
|
179
|
+
* JavaScript operators do not work on a Series (`close - open` throws): use its methods, which work on all the bars
|
|
180
|
+
* at once and return a new Series. The other operand can be a Series or a plain number. Comparisons and logic give
|
|
181
|
+
* a Series of 1 (true) and 0 (false). Inside `eachBar()`, read the number of the current bar with `c.get(series)`.
|
|
182
|
+
* Values are computed when first read, then kept.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```js
|
|
186
|
+
* const body = close.sub(open);
|
|
187
|
+
* const up = close.gt(open);
|
|
188
|
+
* plot(up.iff(body, body.neg()), "Body size");
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
207
191
|
export class Series {
|
|
208
192
|
private extractor: SeriesExtractor;
|
|
209
193
|
private dataSource: BarData;
|
|
@@ -211,17 +195,21 @@ export class Series {
|
|
|
211
195
|
private cachedVersion: number = -1;
|
|
212
196
|
|
|
213
197
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* @param data - Bar data (Bar[] or BarData)
|
|
217
|
-
* @param extractor - Function to extract/compute value for each bar
|
|
218
|
-
*/
|
|
219
|
-
/**
|
|
220
|
-
* True for PineScript's built-in price variables (open, high, low, close, volume, hl2...): their history covers
|
|
221
|
-
* every bar, so references further than 5000 bars back are allowed (measured: close[bar_index] runs).
|
|
198
|
+
* True for the built-in price series (`open`, `high`, `low`, `close`, `volume`, `hl2`...): their history covers
|
|
199
|
+
* every bar, so `offset()` may reach further than 5000 bars back.
|
|
222
200
|
*/
|
|
223
201
|
fullHistory = false;
|
|
224
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Makes a Series that computes its value on each bar with a function. Scripts rarely need it: Series come from the
|
|
205
|
+
* builtins, `ta.*` and the Series methods.
|
|
206
|
+
* @param data - The bars the series is aligned to (an array of bars, or a {@link BarData}).
|
|
207
|
+
* @param extractor - Gives the value of bar `index`: `(bar, index, bars) => number`; NaN for na.
|
|
208
|
+
* @example
|
|
209
|
+
* ```js
|
|
210
|
+
* const range = new Series(close.bars, (bar) => bar.high - bar.low);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
225
213
|
constructor(data: Bar[] | BarData, extractor: SeriesExtractor) {
|
|
226
214
|
// Support both Bar[] and BarData for backward compatibility
|
|
227
215
|
this.dataSource = data instanceof BarData ? data : new BarData(data);
|
|
@@ -229,18 +217,12 @@ export class Series {
|
|
|
229
217
|
runHooks?.created(this);
|
|
230
218
|
}
|
|
231
219
|
|
|
232
|
-
/**
|
|
233
|
-
* Get the underlying bar data
|
|
234
|
-
* @returns Bar array
|
|
235
|
-
*/
|
|
220
|
+
/** The bars this series is aligned to (`{ time, open, high, low, close, volume }`, oldest first). */
|
|
236
221
|
get bars(): Bar[] {
|
|
237
222
|
return this.dataSource.bars;
|
|
238
223
|
}
|
|
239
224
|
|
|
240
|
-
/**
|
|
241
|
-
* Get the underlying BarData source
|
|
242
|
-
* @returns BarData instance
|
|
243
|
-
*/
|
|
225
|
+
/** The {@link BarData} holding the bars this series is aligned to. */
|
|
244
226
|
get barData(): BarData {
|
|
245
227
|
return this.dataSource;
|
|
246
228
|
}
|
|
@@ -250,10 +232,15 @@ export class Series {
|
|
|
250
232
|
// ============================================
|
|
251
233
|
|
|
252
234
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
* @param
|
|
256
|
-
* @
|
|
235
|
+
* Makes the Series of one field of the bars (the price series). In a script, use the globals `open`, `high`, `low`,
|
|
236
|
+
* `close`, `volume` instead.
|
|
237
|
+
* @param bars - The bars (an array of bars, or a {@link BarData}).
|
|
238
|
+
* @param field - `'open'`, `'high'`, `'low'`, `'close'` or `'volume'`.
|
|
239
|
+
* @returns A Series of that field; NaN where a bar does not have it.
|
|
240
|
+
* @example
|
|
241
|
+
* ```js
|
|
242
|
+
* const myClose = Series.fromBars(close.bars, "close");
|
|
243
|
+
* ```
|
|
257
244
|
*/
|
|
258
245
|
static fromBars(bars: Bar[] | BarData, field: 'open' | 'high' | 'low' | 'close' | 'volume'): Series {
|
|
259
246
|
const series = new Series(bars, (bar) => bar[field] ?? NaN);
|
|
@@ -262,22 +249,32 @@ export class Series {
|
|
|
262
249
|
}
|
|
263
250
|
|
|
264
251
|
/**
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
* @param
|
|
268
|
-
* @
|
|
252
|
+
* Makes a Series with the same value on every bar. The Series methods also take plain numbers, so a script seldom
|
|
253
|
+
* needs it.
|
|
254
|
+
* @param bars - The bars to align to (an array of bars, or a {@link BarData}).
|
|
255
|
+
* @param value - The value of every bar (NaN for na).
|
|
256
|
+
* @returns A Series of `value`.
|
|
257
|
+
* @example
|
|
258
|
+
* ```js
|
|
259
|
+
* const level = Series.constant(close.bars, 50);
|
|
260
|
+
* ```
|
|
269
261
|
*/
|
|
270
262
|
static constant(bars: Bar[] | BarData, value: number): Series {
|
|
271
263
|
return new Series(bars, () => value);
|
|
272
264
|
}
|
|
273
265
|
|
|
274
266
|
/**
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* @param
|
|
278
|
-
* @
|
|
267
|
+
* Makes a Series from an array with one value per bar, oldest first. In a script, `seriesOf(values)` does the same
|
|
268
|
+
* on the chart bars.
|
|
269
|
+
* @param bars - The bars the values belong to (an array of bars, or a {@link BarData}).
|
|
270
|
+
* @param values - One number per bar, oldest first; a missing value reads as NaN.
|
|
271
|
+
* @returns A Series of the values.
|
|
279
272
|
* @throws When read on other bars with a different length than `values` (e.g. a series computed on the
|
|
280
|
-
* chart bars and read inside a `request.security` expression, which runs on the
|
|
273
|
+
* chart bars and read inside a `request.security` expression, which runs on the other timeframe's bars).
|
|
274
|
+
* @example
|
|
275
|
+
* ```js
|
|
276
|
+
* const doubled = Series.fromArray(close.bars, close.toArray().map((v) => v * 2));
|
|
277
|
+
* ```
|
|
281
278
|
*/
|
|
282
279
|
static fromArray(bars: Bar[] | BarData, values: number[]): Series {
|
|
283
280
|
const origin = bars instanceof BarData ? bars.bars : bars;
|
|
@@ -297,9 +294,13 @@ export class Series {
|
|
|
297
294
|
// ============================================
|
|
298
295
|
|
|
299
296
|
/**
|
|
300
|
-
*
|
|
301
|
-
* @param other - Series or number to add
|
|
302
|
-
* @returns
|
|
297
|
+
* Adds a Series or a number to this series, bar by bar (the `+` of two values). NaN on either side gives NaN.
|
|
298
|
+
* @param other - Series or number to add.
|
|
299
|
+
* @returns A Series of the sums.
|
|
300
|
+
* @example
|
|
301
|
+
* ```js
|
|
302
|
+
* const mid = high.add(low).div(2);
|
|
303
|
+
* ```
|
|
303
304
|
*/
|
|
304
305
|
add(other: Series | number): Series {
|
|
305
306
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -310,9 +311,13 @@ export class Series {
|
|
|
310
311
|
}
|
|
311
312
|
|
|
312
313
|
/**
|
|
313
|
-
*
|
|
314
|
-
* @param other - Series or number to subtract
|
|
315
|
-
* @returns
|
|
314
|
+
* Subtracts a Series or a number from this series, bar by bar (the `-` of two values). NaN on either side gives NaN.
|
|
315
|
+
* @param other - Series or number to subtract.
|
|
316
|
+
* @returns A Series of the differences.
|
|
317
|
+
* @example
|
|
318
|
+
* ```js
|
|
319
|
+
* const body = close.sub(open);
|
|
320
|
+
* ```
|
|
316
321
|
*/
|
|
317
322
|
sub(other: Series | number): Series {
|
|
318
323
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -323,9 +328,13 @@ export class Series {
|
|
|
323
328
|
}
|
|
324
329
|
|
|
325
330
|
/**
|
|
326
|
-
*
|
|
327
|
-
* @param other - Series or number to multiply
|
|
328
|
-
* @returns
|
|
331
|
+
* Multiplies this series by a Series or a number, bar by bar (the `*` of two values). NaN on either side gives NaN.
|
|
332
|
+
* @param other - Series or number to multiply by.
|
|
333
|
+
* @returns A Series of the products.
|
|
334
|
+
* @example
|
|
335
|
+
* ```js
|
|
336
|
+
* const upper = ta.sma(close, 20).mul(1.02);
|
|
337
|
+
* ```
|
|
329
338
|
*/
|
|
330
339
|
mul(other: Series | number): Series {
|
|
331
340
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -336,9 +345,14 @@ export class Series {
|
|
|
336
345
|
}
|
|
337
346
|
|
|
338
347
|
/**
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
* @
|
|
348
|
+
* Raises this series to a power, bar by bar (`math.pow` on a series). NaN on either side gives NaN; a negative base
|
|
349
|
+
* with a fractional exponent gives NaN.
|
|
350
|
+
* @param exponent - Series or number exponent.
|
|
351
|
+
* @returns A Series of `this ** exponent`.
|
|
352
|
+
* @example
|
|
353
|
+
* ```js
|
|
354
|
+
* const squared = close.sub(ta.sma(close, 20)).pow(2);
|
|
355
|
+
* ```
|
|
342
356
|
*/
|
|
343
357
|
pow(exponent: Series | number): Series {
|
|
344
358
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -349,9 +363,15 @@ export class Series {
|
|
|
349
363
|
}
|
|
350
364
|
|
|
351
365
|
/**
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
* @
|
|
366
|
+
* Divides this series by a Series or a number, bar by bar (the `/` of two values). A division by 0 gives
|
|
367
|
+
* +/-Infinity (which `na()` treats as na), 0 / 0 and NaN on either side give NaN.
|
|
368
|
+
* @param other - Series or number to divide by.
|
|
369
|
+
* @returns A Series of the quotients.
|
|
370
|
+
* @example
|
|
371
|
+
* ```js
|
|
372
|
+
* const range = high.sub(low);
|
|
373
|
+
* const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
|
|
374
|
+
* ```
|
|
355
375
|
*/
|
|
356
376
|
div(other: Series | number): Series {
|
|
357
377
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -362,9 +382,14 @@ export class Series {
|
|
|
362
382
|
}
|
|
363
383
|
|
|
364
384
|
/**
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
* @
|
|
385
|
+
* Remainder of the division of this series by a Series or a number, bar by bar (the `%` of two values). The
|
|
386
|
+
* remainder has the sign of this series; a divisor of 0 and NaN on either side give NaN.
|
|
387
|
+
* @param other - Series or number to divide by.
|
|
388
|
+
* @returns A Series of the remainders.
|
|
389
|
+
* @example
|
|
390
|
+
* ```js
|
|
391
|
+
* const everyFifth = bar_index.mod(5).eq(0);
|
|
392
|
+
* ```
|
|
368
393
|
*/
|
|
369
394
|
mod(other: Series | number): Series {
|
|
370
395
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -375,8 +400,12 @@ export class Series {
|
|
|
375
400
|
}
|
|
376
401
|
|
|
377
402
|
/**
|
|
378
|
-
*
|
|
379
|
-
* @returns
|
|
403
|
+
* Changes the sign of every value (the unary `-`). NaN stays NaN.
|
|
404
|
+
* @returns A Series of the opposite values.
|
|
405
|
+
* @example
|
|
406
|
+
* ```js
|
|
407
|
+
* const drop = close.sub(close.offset(1)).neg();
|
|
408
|
+
* ```
|
|
380
409
|
*/
|
|
381
410
|
neg(): Series {
|
|
382
411
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -389,9 +418,14 @@ export class Series {
|
|
|
389
418
|
// ============================================
|
|
390
419
|
|
|
391
420
|
/**
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
* @
|
|
421
|
+
* Tests whether this series is above a Series or a number, bar by bar (`a > b`).
|
|
422
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
423
|
+
* @param other - Series or number to compare with.
|
|
424
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
425
|
+
* @example
|
|
426
|
+
* ```js
|
|
427
|
+
* const up = close.gt(open);
|
|
428
|
+
* ```
|
|
395
429
|
*/
|
|
396
430
|
gt(other: Series | number): Series {
|
|
397
431
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -402,9 +436,14 @@ export class Series {
|
|
|
402
436
|
}
|
|
403
437
|
|
|
404
438
|
/**
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* @
|
|
439
|
+
* Tests whether this series is above or equal to a Series or a number, bar by bar (`a >= b`).
|
|
440
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
441
|
+
* @param other - Series or number to compare with.
|
|
442
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
443
|
+
* @example
|
|
444
|
+
* ```js
|
|
445
|
+
* const atOrAbove = close.gte(ta.sma(close, 50));
|
|
446
|
+
* ```
|
|
408
447
|
*/
|
|
409
448
|
gte(other: Series | number): Series {
|
|
410
449
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -415,9 +454,14 @@ export class Series {
|
|
|
415
454
|
}
|
|
416
455
|
|
|
417
456
|
/**
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* @
|
|
457
|
+
* Tests whether this series is below a Series or a number, bar by bar (`a < b`).
|
|
458
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
459
|
+
* @param other - Series or number to compare with.
|
|
460
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
461
|
+
* @example
|
|
462
|
+
* ```js
|
|
463
|
+
* const oversold = ta.rsi(close, 14).lt(30);
|
|
464
|
+
* ```
|
|
421
465
|
*/
|
|
422
466
|
lt(other: Series | number): Series {
|
|
423
467
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -428,9 +472,14 @@ export class Series {
|
|
|
428
472
|
}
|
|
429
473
|
|
|
430
474
|
/**
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
* @
|
|
475
|
+
* Tests whether this series is below or equal to a Series or a number, bar by bar (`a <= b`).
|
|
476
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
477
|
+
* @param other - Series or number to compare with.
|
|
478
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
479
|
+
* @example
|
|
480
|
+
* ```js
|
|
481
|
+
* const atOrBelow = low.lte(ta.lowest(low, 20).offset(1));
|
|
482
|
+
* ```
|
|
434
483
|
*/
|
|
435
484
|
lte(other: Series | number): Series {
|
|
436
485
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -441,9 +490,14 @@ export class Series {
|
|
|
441
490
|
}
|
|
442
491
|
|
|
443
492
|
/**
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
* @
|
|
493
|
+
* Tests whether this series equals a Series or a number, bar by bar (`a == b`).
|
|
494
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
495
|
+
* @param other - Series or number to compare with.
|
|
496
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
497
|
+
* @example
|
|
498
|
+
* ```js
|
|
499
|
+
* const doji = close.eq(open);
|
|
500
|
+
* ```
|
|
447
501
|
*/
|
|
448
502
|
eq(other: Series | number): Series {
|
|
449
503
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -454,9 +508,14 @@ export class Series {
|
|
|
454
508
|
}
|
|
455
509
|
|
|
456
510
|
/**
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
* @
|
|
511
|
+
* Tests whether this series differs from a Series or a number, bar by bar (`a != b`).
|
|
512
|
+
* Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
|
|
513
|
+
* @param other - Series or number to compare with.
|
|
514
|
+
* @returns A Series of 1 where it is true, 0 where it is false.
|
|
515
|
+
* @example
|
|
516
|
+
* ```js
|
|
517
|
+
* const moved = close.neq(close.offset(1));
|
|
518
|
+
* ```
|
|
460
519
|
*/
|
|
461
520
|
neq(other: Series | number): Series {
|
|
462
521
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -471,9 +530,13 @@ export class Series {
|
|
|
471
530
|
// ============================================
|
|
472
531
|
|
|
473
532
|
/**
|
|
474
|
-
* Logical
|
|
475
|
-
* @param other - Series or number
|
|
476
|
-
* @returns
|
|
533
|
+
* Logical "and" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
|
|
534
|
+
* @param other - The other condition: a Series or a number.
|
|
535
|
+
* @returns A Series of 1 where both are true, 0 elsewhere.
|
|
536
|
+
* @example
|
|
537
|
+
* ```js
|
|
538
|
+
* const strongUp = close.gt(open).and(volume.gt(ta.sma(volume, 20)));
|
|
539
|
+
* ```
|
|
477
540
|
*/
|
|
478
541
|
and(other: Series | number): Series {
|
|
479
542
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -484,9 +547,13 @@ export class Series {
|
|
|
484
547
|
}
|
|
485
548
|
|
|
486
549
|
/**
|
|
487
|
-
* Logical
|
|
488
|
-
* @param other - Series or number
|
|
489
|
-
* @returns
|
|
550
|
+
* Logical "or" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
|
|
551
|
+
* @param other - The other condition: a Series or a number.
|
|
552
|
+
* @returns A Series of 1 where at least one is true, 0 elsewhere.
|
|
553
|
+
* @example
|
|
554
|
+
* ```js
|
|
555
|
+
* const extreme = ta.rsi(close, 14).gt(70).or(ta.rsi(close, 14).lt(30));
|
|
556
|
+
* ```
|
|
490
557
|
*/
|
|
491
558
|
or(other: Series | number): Series {
|
|
492
559
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -497,8 +564,12 @@ export class Series {
|
|
|
497
564
|
}
|
|
498
565
|
|
|
499
566
|
/**
|
|
500
|
-
* Logical
|
|
501
|
-
* @returns
|
|
567
|
+
* Logical "not" of a condition, bar by bar: true (not 0, not NaN) becomes 0, and 0 or NaN becomes 1.
|
|
568
|
+
* @returns A Series of 1 where this series is false, 0 where it is true.
|
|
569
|
+
* @example
|
|
570
|
+
* ```js
|
|
571
|
+
* const notUp = close.gt(open).not();
|
|
572
|
+
* ```
|
|
502
573
|
*/
|
|
503
574
|
not(): Series {
|
|
504
575
|
return new Series(this.dataSource, (bar, i, data) => {
|
|
@@ -512,21 +583,16 @@ export class Series {
|
|
|
512
583
|
// ============================================
|
|
513
584
|
|
|
514
585
|
/**
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
* @param
|
|
519
|
-
* @
|
|
520
|
-
* @returns New Series with bar-by-bar selected values
|
|
521
|
-
*
|
|
586
|
+
* Picks one of two values on each bar, with this series as the condition (the `cond ? x : y` of a series). The
|
|
587
|
+
* condition is true when it is not 0 and not NaN; 0 and NaN pick `falseValue`.
|
|
588
|
+
* @param trueValue - Series or number used where the condition is true.
|
|
589
|
+
* @param falseValue - Series or number used where the condition is false.
|
|
590
|
+
* @returns A Series of the picked values.
|
|
522
591
|
* @example
|
|
523
|
-
* ```
|
|
524
|
-
*
|
|
525
|
-
* const
|
|
526
|
-
*
|
|
527
|
-
* // Example: RSI formula uses ternary for edge cases
|
|
528
|
-
* // rsi = down == 0 ? 100 : up == 0 ? 0 : formula
|
|
529
|
-
* const rsi = down.eq(0).iff(100, up.eq(0).iff(0, formula));
|
|
592
|
+
* ```js
|
|
593
|
+
* const range = high.sub(low);
|
|
594
|
+
* const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
|
|
595
|
+
* plot(ratio, "Body / range");
|
|
530
596
|
* ```
|
|
531
597
|
*/
|
|
532
598
|
iff(trueValue: Series | number, falseValue: Series | number): Series {
|
|
@@ -548,10 +614,17 @@ export class Series {
|
|
|
548
614
|
// ============================================
|
|
549
615
|
|
|
550
616
|
/**
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
* @
|
|
554
|
-
*
|
|
617
|
+
* Shifts the series back in time: on each bar, the value `offset` bars earlier (`close.offset(1)` is the previous
|
|
618
|
+
* close). The first `offset` bars, which have no earlier bar, get NaN.
|
|
619
|
+
* @param offset - Number of bars back: 0 (the series itself) or more, a whole number. A series computed by the
|
|
620
|
+
* script can look back at most 5000 bars; the price series (`close`, `high`...) any number.
|
|
621
|
+
* @returns A Series of the earlier values.
|
|
622
|
+
* @throws RangeError when `offset` is negative (it would read a future bar); an error past 5000 bars.
|
|
623
|
+
* @example
|
|
624
|
+
* ```js
|
|
625
|
+
* const change = close.sub(close.offset(1));
|
|
626
|
+
* const higherHigh = high.gt(high.offset(1));
|
|
627
|
+
* ```
|
|
555
628
|
*/
|
|
556
629
|
offset(offset: number): Series {
|
|
557
630
|
if (offset < 0) throw new RangeError(`Series.offset(${offset}): the offset cannot be negative (it would read a future bar).`);
|
|
@@ -571,14 +644,14 @@ export class Series {
|
|
|
571
644
|
// ============================================
|
|
572
645
|
|
|
573
646
|
/**
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
647
|
+
* Gives the values of all the bars as a plain array, oldest bar first (NaN for na). The values are computed on the
|
|
648
|
+
* first read and kept until the bars change; the array is shared, so copy it before changing it.
|
|
649
|
+
* @returns One number per bar, oldest first.
|
|
650
|
+
* @example
|
|
651
|
+
* ```js
|
|
652
|
+
* const values = ta.sma(close, 20).toArray();
|
|
653
|
+
* const shifted = seriesOf(values.map((v) => v * 1.01));
|
|
654
|
+
* ```
|
|
582
655
|
*/
|
|
583
656
|
toArray(): number[] {
|
|
584
657
|
// Check if cache is valid by comparing versions
|
|
@@ -606,24 +679,13 @@ export class Series {
|
|
|
606
679
|
}
|
|
607
680
|
|
|
608
681
|
/**
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
* - Computes all values immediately
|
|
613
|
-
* - Creates a new Series that stores values directly
|
|
614
|
-
* - Breaks references to parent Series objects
|
|
615
|
-
* - Useful for complex expressions to free intermediate Series memory
|
|
616
|
-
*
|
|
682
|
+
* Computes the values now and returns a Series that stores them, without links to the series it was built from.
|
|
683
|
+
* Use it on a long chain of methods to compute it once and free the steps in between.
|
|
684
|
+
* @returns A Series of the same values, stored.
|
|
617
685
|
* @example
|
|
618
|
-
* ```
|
|
619
|
-
*
|
|
620
|
-
* const result = a.add(b).mul(c).div(d).sub(e);
|
|
621
|
-
*
|
|
622
|
-
* // With materialize: breaks chain, frees a, b, c memory
|
|
623
|
-
* const result = a.add(b).mul(c).materialize().div(d).sub(e);
|
|
686
|
+
* ```js
|
|
687
|
+
* const spread = high.sub(low).div(close).mul(100).materialize();
|
|
624
688
|
* ```
|
|
625
|
-
*
|
|
626
|
-
* @returns New Series with computed values and no closure dependencies
|
|
627
689
|
*/
|
|
628
690
|
materialize(): Series {
|
|
629
691
|
const values = this.toArray();
|
|
@@ -656,9 +718,15 @@ export class Series {
|
|
|
656
718
|
}
|
|
657
719
|
|
|
658
720
|
/**
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
*
|
|
721
|
+
* Reads the value of one bar by its position: 0 is the oldest bar, `bar_index` counts the same way. This is not
|
|
722
|
+
* the "bars back" of `offset()`: inside a bar loop, read the current bar with `c.get(series)` and earlier bars with
|
|
723
|
+
* `c.get(series, k)`.
|
|
724
|
+
* @param index - Bar position, 0 for the oldest bar.
|
|
725
|
+
* @returns The value of that bar; NaN when there is no such bar.
|
|
726
|
+
* @example
|
|
727
|
+
* ```js
|
|
728
|
+
* const firstClose = close.get(0);
|
|
729
|
+
* ```
|
|
662
730
|
*/
|
|
663
731
|
get(index: number): number {
|
|
664
732
|
const values = this.toArray();
|
|
@@ -666,8 +734,12 @@ export class Series {
|
|
|
666
734
|
}
|
|
667
735
|
|
|
668
736
|
/**
|
|
669
|
-
*
|
|
670
|
-
* @returns
|
|
737
|
+
* Reads the value of the newest bar.
|
|
738
|
+
* @returns The value of the last bar; NaN when there are no bars.
|
|
739
|
+
* @example
|
|
740
|
+
* ```js
|
|
741
|
+
* const lastRsi = ta.rsi(close, 14).last();
|
|
742
|
+
* ```
|
|
671
743
|
*/
|
|
672
744
|
last(): number {
|
|
673
745
|
const values = this.toArray();
|
|
@@ -675,17 +747,17 @@ export class Series {
|
|
|
675
747
|
}
|
|
676
748
|
|
|
677
749
|
/**
|
|
678
|
-
*
|
|
679
|
-
* @returns
|
|
750
|
+
* Gives the number of bars of the series (a method, not a property).
|
|
751
|
+
* @returns The number of bars.
|
|
752
|
+
* @example
|
|
753
|
+
* ```js
|
|
754
|
+
* const bars = close.length();
|
|
755
|
+
* ```
|
|
680
756
|
*/
|
|
681
757
|
length(): number {
|
|
682
758
|
return this.dataSource.length;
|
|
683
759
|
}
|
|
684
760
|
|
|
685
|
-
/**
|
|
686
|
-
* Convert series to time-value pairs for charting
|
|
687
|
-
* @returns Array of { time, value } objects
|
|
688
|
-
*/
|
|
689
761
|
/**
|
|
690
762
|
* A Series holds the values of all the bars, so JavaScript operators cannot use it: without this guard
|
|
691
763
|
* `close - open` is NaN, `close > open` false and `` `${rsi}` `` "[object Object]", with no error. The
|
|
@@ -705,6 +777,14 @@ export class Series {
|
|
|
705
777
|
);
|
|
706
778
|
}
|
|
707
779
|
|
|
780
|
+
/**
|
|
781
|
+
* Gives the values as `{ time, value }` points for a chart line, oldest first, leaving out the NaN bars.
|
|
782
|
+
* @returns The points; `time` is the bar time, in the unit of the bars.
|
|
783
|
+
* @example
|
|
784
|
+
* ```js
|
|
785
|
+
* const points = ta.sma(close, 20).toTimeValuePairs();
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
708
788
|
toTimeValuePairs(): Array<{ time: any; value: number }> {
|
|
709
789
|
const values = this.toArray();
|
|
710
790
|
const bars = this.dataSource.bars;
|