oakscriptjs 0.14.1 → 0.15.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/dist/cjs/callsite/index.d.ts +22 -3
- package/dist/cjs/callsite/index.d.ts.map +1 -1
- package/dist/cjs/callsite/index.js +32 -0
- package/dist/cjs/callsite/index.js.map +2 -2
- package/dist/cjs/color/index.d.ts.map +1 -1
- package/dist/cjs/color/index.js +1 -1
- package/dist/cjs/color/index.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +2 -2
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +17 -8
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/math/index.d.ts +2 -6
- package/dist/cjs/math/index.d.ts.map +1 -1
- package/dist/cjs/math/index.js.map +2 -2
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +1 -1
- package/dist/cjs/script/index.js.map +2 -2
- package/dist/cjs/security/other.d.ts +8 -1
- package/dist/cjs/security/other.d.ts.map +1 -1
- package/dist/cjs/security/other.js +8 -1
- package/dist/cjs/security/other.js.map +2 -2
- package/dist/cjs/ta/index.d.ts +9 -11
- package/dist/cjs/ta/index.d.ts.map +1 -1
- package/dist/cjs/ta/index.js.map +2 -2
- package/dist/cjs/ta/running-sum.d.ts +13 -19
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +62 -74
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/table/index.d.ts.map +1 -1
- package/dist/cjs/table/index.js +4 -1
- package/dist/cjs/table/index.js.map +2 -2
- package/dist/cjs/utils/index.js +3 -3
- package/dist/cjs/utils/index.js.map +2 -2
- package/dist/esm/callsite/index.d.ts +22 -3
- package/dist/esm/callsite/index.d.ts.map +1 -1
- package/dist/esm/callsite/index.js +55 -3
- package/dist/esm/callsite/index.js.map +1 -1
- package/dist/esm/color/index.d.ts.map +1 -1
- package/dist/esm/color/index.js +3 -2
- package/dist/esm/color/index.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +2 -2
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +28 -12
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/index.d.ts +2 -6
- package/dist/esm/math/index.d.ts.map +1 -1
- package/dist/esm/math/index.js +1 -1
- package/dist/esm/math/index.js.map +1 -1
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +1 -1
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/security/other.d.ts +8 -1
- package/dist/esm/security/other.d.ts.map +1 -1
- package/dist/esm/security/other.js +13 -1
- package/dist/esm/security/other.js.map +1 -1
- package/dist/esm/ta/index.d.ts +9 -11
- package/dist/esm/ta/index.d.ts.map +1 -1
- package/dist/esm/ta/index.js +13 -15
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +13 -19
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +88 -103
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/table/index.d.ts.map +1 -1
- package/dist/esm/table/index.js +5 -1
- package/dist/esm/table/index.js.map +1 -1
- package/dist/esm/utils/index.js +3 -3
- package/dist/esm/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/pine-differences.md +3 -1
- package/src/callsite/index.ts +58 -3
- package/src/color/index.ts +3 -2
- package/src/drawing/registry.ts +29 -11
- package/src/index.ts +2 -1
- package/src/math/index.ts +3 -7
- package/src/script/index.ts +1 -1
- package/src/security/other.ts +14 -2
- package/src/ta/index.ts +13 -15
- package/src/ta/running-sum.ts +82 -99
- package/src/table/index.ts +5 -1
- package/src/utils/index.ts +3 -3
package/src/drawing/registry.ts
CHANGED
|
@@ -92,7 +92,8 @@ let held = new WeakMap<object, number>();
|
|
|
92
92
|
let releasedAt = new WeakMap<object, number>();
|
|
93
93
|
/** Bars during which a released linefill is still skipped by the maximum count. */
|
|
94
94
|
const LINEFILL_RELEASE_BARS = 5000;
|
|
95
|
-
/** Bar on which a `var` variable got an object: the maximum count
|
|
95
|
+
/** Bar on which a `var` variable got an object: on that bar the maximum count deletes it only when no other object can
|
|
96
|
+
* go; from the next bar on, never. */
|
|
96
97
|
let heldAt = new WeakMap<object, number>();
|
|
97
98
|
/** Kinds with a maximum count of their own. */
|
|
98
99
|
type CountedKind = 'line' | 'label' | 'box' | 'polyline';
|
|
@@ -131,6 +132,16 @@ function heldBefore(obj: object): boolean {
|
|
|
131
132
|
return bar === undefined || currentBar === null || bar < currentBar;
|
|
132
133
|
}
|
|
133
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Held by a `var` variable since the current bar, or released by its last variable on the current bar: the maximum
|
|
137
|
+
* count deletes such an object only when the objects it may delete are not enough (#193; 12 `var` lines made on the
|
|
138
|
+
* first bar with a maximum of 5 lose the first 6, #158).
|
|
139
|
+
*/
|
|
140
|
+
function heldOnThisBar(obj: object): boolean {
|
|
141
|
+
if (currentBar === null) return false;
|
|
142
|
+
return held.has(obj) ? heldAt.get(obj) === currentBar : releasedAt.get(obj) === currentBar;
|
|
143
|
+
}
|
|
144
|
+
|
|
134
145
|
/** Linefill rule: held, or released less than 5,000 bars ago. */
|
|
135
146
|
function fillProtected(obj: object): boolean {
|
|
136
147
|
if (held.has(obj)) return true;
|
|
@@ -201,8 +212,8 @@ let lastBar: LastBar | null = null;
|
|
|
201
212
|
* Tells the drawing functions which bar is being calculated (`bar_index`), so that a bar index more than 10,000
|
|
202
213
|
* bars back throws as in PineScript; `null` turns the check off. `eachBar` of the script API calls it.
|
|
203
214
|
*
|
|
204
|
-
* The bar also dates the objects held by a `var` variable (
|
|
205
|
-
* the last bar of the data, which has rules of its own for the maximum count. `lastIndex` is the bar index of the
|
|
215
|
+
* The bar also dates the objects held by a `var` variable (on that bar deleted only when no other object can go,
|
|
216
|
+
* never after). `last` says that it is the last bar of the data, which has rules of its own for the maximum count. `lastIndex` is the bar index of the
|
|
206
217
|
* last bar of the data (PineScript `last_bar_index`): with it, a bar index more than 500 bars after it throws, also
|
|
207
218
|
* on the bars before the last one (RE10020).
|
|
208
219
|
*/
|
|
@@ -287,15 +298,22 @@ function cutOldest<K extends DrawingKind>(
|
|
|
287
298
|
if (kind === 'table') return []; // tables have no maximum count
|
|
288
299
|
const max = limits[kind === 'linefill' ? 'line' : (kind as keyof DrawingLimits)] + allowance(kind);
|
|
289
300
|
if (list.length <= max + SLACK) return [];
|
|
290
|
-
const
|
|
291
|
-
const cut: Array<DrawingTypes[K]> = [];
|
|
301
|
+
const gone = new Set<object>();
|
|
292
302
|
let excess = list.length - max;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
303
|
+
const candidate = (old: DrawingTypes[K]): boolean =>
|
|
304
|
+
old !== obj && (!only || only(old)) && !(kind === 'linefill' ? fillProtected(old) : heldBefore(old));
|
|
305
|
+
// the oldest first; the objects a `var` variable got (or released) on this bar only when the others are not enough
|
|
306
|
+
for (const weak of kind === 'linefill' ? [false] : [false, true]) {
|
|
307
|
+
for (const old of list) {
|
|
308
|
+
if (excess <= 0) break;
|
|
309
|
+
if (!gone.has(old) && candidate(old) && (kind === 'linefill' || heldOnThisBar(old) === weak)) {
|
|
310
|
+
excess--;
|
|
311
|
+
gone.add(old);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
298
314
|
}
|
|
315
|
+
const kept = list.filter((old) => !gone.has(old));
|
|
316
|
+
const cut = list.filter((old) => gone.has(old));
|
|
299
317
|
// linefills: still above the maximum + 5, the oldest go, the held ones too
|
|
300
318
|
if (kind === 'linefill' && kept.length > max + SLACK) cut.push(...kept.splice(0, kept.length - max));
|
|
301
319
|
list.length = 0;
|
|
@@ -334,7 +352,7 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
|
|
|
334
352
|
const list = live[kind];
|
|
335
353
|
list.push(obj);
|
|
336
354
|
const isOld = (old: object): boolean => bar.old.has(old);
|
|
337
|
-
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old))) {
|
|
355
|
+
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old) && !heldOnThisBar(old))) {
|
|
338
356
|
// the objects of the bars before only; a line takes its linefills with it
|
|
339
357
|
const cut = cutOldest(kind, list, obj, isOld);
|
|
340
358
|
if (kind === 'polyline') {
|
package/src/index.ts
CHANGED
|
@@ -85,6 +85,7 @@ export {
|
|
|
85
85
|
securityOther,
|
|
86
86
|
securityOtherValues,
|
|
87
87
|
securityLowerTf,
|
|
88
|
+
MAX_INTRABARS,
|
|
88
89
|
type OtherSymbolChart,
|
|
89
90
|
type OtherSymbolOptions,
|
|
90
91
|
type SymbolCalendar,
|
|
@@ -224,7 +225,7 @@ export type { LightweightSeriesDefinitions } from './runtime/adapters/Lightweigh
|
|
|
224
225
|
export { SimpleInputAdapter } from './runtime/adapters/SimpleInputAdapter.js';
|
|
225
226
|
|
|
226
227
|
// Version
|
|
227
|
-
export const VERSION = '0.
|
|
228
|
+
export const VERSION = '0.15.1';
|
|
228
229
|
|
|
229
230
|
/**
|
|
230
231
|
* Library information
|
package/src/math/index.ts
CHANGED
|
@@ -467,13 +467,9 @@ function avgOf(nums: number[]): number {
|
|
|
467
467
|
* - Calculates the sum of the last `length` non-`na` values at each point; an `na` value (also +/-Infinity) is
|
|
468
468
|
* skipped and the bar keeps the previous result (as in PineScript)
|
|
469
469
|
* - Returns NaN until `length` non-`na` values are available
|
|
470
|
-
* -
|
|
471
|
-
* bits (bit for bit with PineScript)
|
|
470
|
+
* - The correctly rounded sum of the window values (the exact sum, rounded once), as PineScript (#188)
|
|
472
471
|
* - When passed a Series, returns a Series; when passed an array, returns an array
|
|
473
|
-
* - With a series length,
|
|
474
|
-
* steps of the raw values: a smaller length removes the values that leave the window (oldest first); a larger
|
|
475
|
-
* length adds back the values from the one before the old window down to the one `length` values back (newest
|
|
476
|
-
* first), then the usual step removes the stored value of the one `length` values back. A bar with an `na` value
|
|
472
|
+
* - With a series length, the window is the last `length` values of each bar. A bar with an `na` value
|
|
477
473
|
* gives the sum of the last `length` earlier values
|
|
478
474
|
*
|
|
479
475
|
* @example
|
|
@@ -492,7 +488,7 @@ export function sum(
|
|
|
492
488
|
const sourceArray = source instanceof Series ? source.toArray() : source;
|
|
493
489
|
if (typeof length === 'number') checkSumLength('math.sum', length, 0);
|
|
494
490
|
// PineScript: the sum of the last `length` non-na values (also +/-Infinity skipped); an na bar keeps the previous
|
|
495
|
-
// result.
|
|
491
|
+
// result. The correctly rounded window sum, bit for bit as PineScript (#188).
|
|
496
492
|
const result: series_float =
|
|
497
493
|
typeof length === 'number'
|
|
498
494
|
? runningSum(sourceArray, length)
|
package/src/script/index.ts
CHANGED
|
@@ -839,7 +839,7 @@ export const volume = Series.fromBars(ctxBars, 'volume');
|
|
|
839
839
|
export const hl2 = new Series(ctxBars, (b) => (b.high + b.low) / 2);
|
|
840
840
|
export const hlc3 = new Series(ctxBars, (b) => (b.high + b.low + b.close) / 3);
|
|
841
841
|
export const ohlc4 = new Series(ctxBars, (b) => (b.open + b.high + b.low + b.close) / 4);
|
|
842
|
-
export const hlcc4 = new Series(ctxBars, (b) => (b.high + b.low +
|
|
842
|
+
export const hlcc4 = new Series(ctxBars, (b) => (b.high + b.low + 2 * b.close) / 4);
|
|
843
843
|
/** PineScript `bar_index`: the index of each bar (0 on the first). In a bar loop, `c.i` is the same number. */
|
|
844
844
|
export const bar_index = new Series(ctxBars, (_b, i) => i);
|
|
845
845
|
bar_index.fullHistory = true;
|
package/src/security/other.ts
CHANGED
|
@@ -238,13 +238,16 @@ export function securityOther(
|
|
|
238
238
|
* `otherBars` (a lower timeframe, the chart symbol or another one) that it contains: the bars whose close time is
|
|
239
239
|
* after the open of the chart bar and at or before its close (a 13:00 hourly bar of a 24x7 symbol is in a 13:30 stock
|
|
240
240
|
* day, which closes at 20:00). An empty range for a chart bar without such bars.
|
|
241
|
+
*
|
|
242
|
+
* As PineScript, at most the last {@link MAX_INTRABARS} (100,000) lower-timeframe bars are given out
|
|
243
|
+
* (`options.maxIntrabars` changes it): the older chart bars get an empty range (#192).
|
|
241
244
|
*/
|
|
242
245
|
export function securityLowerTf(
|
|
243
246
|
chartBars: ReadonlyArray<{ time: number }>,
|
|
244
247
|
otherBars: ReadonlyArray<{ time: number }>,
|
|
245
248
|
tf: string,
|
|
246
249
|
chart: OtherSymbolChart,
|
|
247
|
-
options: Pick<OtherSymbolOptions, 'other'> = {}
|
|
250
|
+
options: Pick<OtherSymbolOptions, 'other'> & { maxIntrabars?: number } = {}
|
|
248
251
|
): Array<[number, number]> {
|
|
249
252
|
const unit = chart.timeUnit === 'ms' ? 1 : 1000;
|
|
250
253
|
const ct = chartBars.map((b) => b.time * unit);
|
|
@@ -253,9 +256,18 @@ export function securityLowerTf(
|
|
|
253
256
|
const oc = closeTimes(ot, otherTf, otherZone);
|
|
254
257
|
const from = latestAtOrBefore(oc, ct);
|
|
255
258
|
const to = latestAtOrBefore(oc, closeTimes(ct, chartTf, chartZone));
|
|
256
|
-
|
|
259
|
+
const ranges = ct.map((_, i): [number, number] => [from[i]! + 1, Math.max(from[i]! + 1, to[i]! + 1)]);
|
|
260
|
+
// the last `max` intrabars of the chart bars: the ones before are not given
|
|
261
|
+
const max = options.maxIntrabars ?? MAX_INTRABARS;
|
|
262
|
+
const end = ranges.reduce((m, [, t]) => Math.max(m, t), 0);
|
|
263
|
+
const first = end - max;
|
|
264
|
+
if (first <= 0) return ranges;
|
|
265
|
+
return ranges.map(([f, t]) => (t <= first ? [f, f] : [Math.max(f, first), t]));
|
|
257
266
|
}
|
|
258
267
|
|
|
268
|
+
/** Lower-timeframe bars `request.security_lower_tf` gives out at most, as PineScript (the last ones). */
|
|
269
|
+
export const MAX_INTRABARS = 100_000;
|
|
270
|
+
|
|
259
271
|
/**
|
|
260
272
|
* {@link securityOther} with the values of an expression: `values` has one value per bar of the other symbol; the
|
|
261
273
|
* result has one value per chart bar, NaN for na.
|
package/src/ta/index.ts
CHANGED
|
@@ -24,10 +24,9 @@ import { checkPositiveLength, checkSumLength, checkWindowLength } from './limits
|
|
|
24
24
|
* - `na` values (also +/-Infinity) in the source series are ignored: the mean of the last `length` non-`na`
|
|
25
25
|
* values, and a bar with an `na` source keeps the previous result (as in PineScript)
|
|
26
26
|
* - Returns NaN until `length` non-`na` values are available
|
|
27
|
-
* - The
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* (see {@link math.sum}); a bar with an `na` source gives the mean of the last `length` earlier values
|
|
27
|
+
* - The correctly rounded window sum divided by the length, bit for bit with PineScript (#188)
|
|
28
|
+
* - With a series length, the window is the last `length` values of each bar (see {@link math.sum}); a bar with an
|
|
29
|
+
* `na` source gives the mean of the last `length` earlier values
|
|
31
30
|
*
|
|
32
31
|
* @example
|
|
33
32
|
* ```typescript
|
|
@@ -86,7 +85,7 @@ export function ema(source: Source, length: simple_int): series_float {
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
const emaInitialized = firstValidIndex >= 0;
|
|
89
|
-
// The seed is ta.sma on that bar: the
|
|
88
|
+
// The seed is ta.sma on that bar: the window sum of math.sum (PineScript bits), not a plain loop sum
|
|
90
89
|
let emaValue = emaInitialized ? runningSum(source.slice(0, firstValidIndex + 1), len)[firstValidIndex]! / len : NaN;
|
|
91
90
|
|
|
92
91
|
for (let i = 0; i < source.length; i++) {
|
|
@@ -282,7 +281,7 @@ export function bb(
|
|
|
282
281
|
* As in PineScript: `na` values (also +/-Infinity) are skipped, the window holds the last
|
|
283
282
|
* `length` non-na values, and a bar whose own value is `na` keeps the previous result.
|
|
284
283
|
* The result is the square root of {@link variance} (0 when the variance is not positive), computed from
|
|
285
|
-
*
|
|
284
|
+
* the correctly rounded window sums of the values and of their squares, bit for bit as PineScript (#188).
|
|
286
285
|
*/
|
|
287
286
|
export function stdev(
|
|
288
287
|
source: Source,
|
|
@@ -769,7 +768,7 @@ export function supertrend(
|
|
|
769
768
|
* - Moving average used in RSI calculation
|
|
770
769
|
* - Alpha = 1 / length (different from EMA which uses alpha = 2 / (length + 1))
|
|
771
770
|
* - First value is initialized with SMA, then uses exponential smoothing. As in PineScript, the seed is the
|
|
772
|
-
* `ta.sma` value (
|
|
771
|
+
* `ta.sma` value (the correctly rounded window sum divided by the length)
|
|
773
772
|
* - Formula: `RMA = (source + (length - 1) * RMA[1]) / length`, evaluated in this order as in PineScript
|
|
774
773
|
* (equal to `alpha * source + (1 - alpha) * RMA[1]`, but bit for bit)
|
|
775
774
|
* - `na` values (also +/-Infinity) in the source series are ignored
|
|
@@ -803,7 +802,7 @@ export function rma(source: Source, length: simple_int): series_float {
|
|
|
803
802
|
}
|
|
804
803
|
|
|
805
804
|
const rmaInitialized = firstValidIndex >= 0;
|
|
806
|
-
// The seed is ta.sma on that bar: the
|
|
805
|
+
// The seed is ta.sma on that bar: the window sum of math.sum (PineScript bits), not a plain loop sum
|
|
807
806
|
let rmaValue = rmaInitialized ? runningSum(source.slice(0, firstValidIndex + 1), len)[firstValidIndex]! / len : NaN;
|
|
808
807
|
|
|
809
808
|
for (let i = 0; i < source.length; i++) {
|
|
@@ -1170,8 +1169,7 @@ export function mom(source: Source, length: simple_int): series_float {
|
|
|
1170
1169
|
* - Measures average absolute distance from the mean
|
|
1171
1170
|
* - Less sensitive to outliers than standard deviation
|
|
1172
1171
|
* - PineScript rule (bit for bit): `sum(abs(source[i] - mean)) / length` for `i = 0 .. length - 1`, added newest
|
|
1173
|
-
* first, with `mean = ta.sma(source, length)`
|
|
1174
|
-
* non-zero deviation)
|
|
1172
|
+
* first, with `mean = ta.sma(source, length)`
|
|
1175
1173
|
* - The loop reads the last `length` bars: an `na` among them gives `na`
|
|
1176
1174
|
*
|
|
1177
1175
|
* @example
|
|
@@ -1208,7 +1206,7 @@ export function dev(source: Source, length: simple_int): series_float {
|
|
|
1208
1206
|
* - `na` values in the source series are ignored
|
|
1209
1207
|
* - The function calculates on the `length` quantity of non-`na` values
|
|
1210
1208
|
* - Relationship: `stdev = sqrt(variance)`
|
|
1211
|
-
* - Computed as PineScript, bit for bit, from
|
|
1209
|
+
* - Computed as PineScript, bit for bit, from the correctly rounded window sums of the values and of their squares:
|
|
1212
1210
|
* biased `sumSq / length - mean * mean`, unbiased `sumSq / (length - 1) - mean * sum / (length - 1)`
|
|
1213
1211
|
*
|
|
1214
1212
|
* @example
|
|
@@ -1467,7 +1465,7 @@ export function linreg(source: Source, length: simple_int, offset: simple_int =
|
|
|
1467
1465
|
* - 0 = no correlation
|
|
1468
1466
|
* - Measures linear relationship between two series
|
|
1469
1467
|
* - As PineScript: `cov = sma(x * y) - sma(x) * sma(y)`, `r = cov / sqrt(variance(x) * variance(y))` with the
|
|
1470
|
-
*
|
|
1468
|
+
* the correctly rounded window sums of `ta.sma` / `ta.variance` (each skips its own `na` values); `|cov| <= 1e-10` gives 0, a
|
|
1471
1469
|
* zero variance gives `na`
|
|
1472
1470
|
*
|
|
1473
1471
|
* @example
|
|
@@ -1478,7 +1476,7 @@ export function linreg(source: Source, length: simple_int, offset: simple_int =
|
|
|
1478
1476
|
*/
|
|
1479
1477
|
export function correlation(source1: Source, source2: Source, length: simple_int): series_float {
|
|
1480
1478
|
checkPositiveLength('correlation', length, 0);
|
|
1481
|
-
// PineScript: from
|
|
1479
|
+
// PineScript: from the correctly rounded window sums, cov = sma(x * y) - sma(x) * sma(y), r = cov / sqrt(var(x) * var(y))
|
|
1482
1480
|
// (biased variances); each running sum skips its own na values
|
|
1483
1481
|
const product = Array.from({ length: source1.length }, (_, i) => {
|
|
1484
1482
|
const v1 = source1[i];
|
|
@@ -1689,7 +1687,7 @@ export function mfi(source: Source, length: simple_int, volume?: Source): series
|
|
|
1689
1687
|
upperFlow.push(le(change, 0) ? 0 : flow);
|
|
1690
1688
|
lowerFlow.push(ge(change, 0) ? 0 : flow);
|
|
1691
1689
|
}
|
|
1692
|
-
// math.sum: the
|
|
1690
|
+
// math.sum: the correctly rounded sum of the last `length` non-na values (as PineScript, bit for bit)
|
|
1693
1691
|
const upper = runningSum(upperFlow, length);
|
|
1694
1692
|
const lower = runningSum(lowerFlow, length);
|
|
1695
1693
|
|
|
@@ -2612,7 +2610,7 @@ export function min(source: Source): series_float {
|
|
|
2612
2610
|
* @remarks
|
|
2613
2611
|
* PineScript rule (bit for bit): `-num / math.sum(source, length)`, with `num` the sum of `source[i] * (i + 1)`
|
|
2614
2612
|
* for `i = 0 .. length - 1`, added newest first.
|
|
2615
|
-
* - The denominator is the
|
|
2613
|
+
* - The denominator is the correctly rounded window sum of `math.sum` (the last `length` non-`na` values)
|
|
2616
2614
|
* - The numerator reads the last `length` bars, so an `na` among them gives `na`
|
|
2617
2615
|
* - A zero denominator gives `na` (PineScript division by zero)
|
|
2618
2616
|
*
|
package/src/ta/running-sum.ts
CHANGED
|
@@ -1,145 +1,128 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* `ta.variance` (bit for bit).
|
|
2
|
+
* Window sums of PineScript `math.sum` / `ta.sma` / `ta.stdev` / `ta.variance` / `ta.mfi` (bit for bit).
|
|
4
3
|
*
|
|
5
|
-
* PineScript
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* +infinity by the size of the compensation, in floating point (#136, #142).
|
|
4
|
+
* PineScript gives the correctly rounded sum of the window values (the exact sum of the doubles, rounded once), on
|
|
5
|
+
* every bar: `ta.sma` is that sum divided by the length (measured on the reference 05/11/2026, #188: built-in and
|
|
6
|
+
* expression sources, with or without a `ta.stdev` of the same source, constant and series lengths, na values; 0
|
|
7
|
+
* differing bars of 16,550 on BTCUSD 1D and 34,665 on AAPL 1D). The running sum of earlier versions (Kahan steps,
|
|
8
|
+
* #114) gave a small residue on a window of zeros; the reference no longer does.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* The exact sum is kept as non-overlapping partials (Shewchuk's algorithm, as Python `math.fsum`): adding a value and
|
|
11
|
+
* removing the one that leaves the window (adding its negation) are exact, so a bar costs the values that enter and
|
|
12
|
+
* leave, not the whole window.
|
|
14
13
|
*
|
|
15
14
|
* @internal
|
|
16
15
|
*/
|
|
17
16
|
import { checkSumLength } from './limits.js';
|
|
18
17
|
|
|
19
|
-
/**
|
|
20
|
-
* Whether the compensation `c` is (partly) lost when it is applied to `x`: `x + |c|` in floating point moves `x` by
|
|
21
|
-
* less than `|c|`. Moving toward +infinity uses the grid above `x` (the coarser binade just below a power of two, the
|
|
22
|
-
* finer binade for a negative power of two), as PineScript (measured bit for bit, #142).
|
|
23
|
-
*/
|
|
24
|
-
function compensationLost(x: number, c: number): boolean {
|
|
25
|
-
if (c === 0 || x === 0) return false;
|
|
26
|
-
const size = Math.abs(c);
|
|
27
|
-
return Math.abs(x + size - x) < size;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
18
|
/** A value PineScript treats as na in window functions (also +/-Infinity). */
|
|
31
19
|
export function isNa(v: number | null | undefined): boolean {
|
|
32
20
|
return v === undefined || v === null || !Number.isFinite(v);
|
|
33
21
|
}
|
|
34
22
|
|
|
23
|
+
/** Adds `x` to the partials `p` (non-overlapping, increasing magnitude), exactly. */
|
|
24
|
+
function addExact(p: number[], x: number): void {
|
|
25
|
+
let i = 0;
|
|
26
|
+
for (let j = 0; j < p.length; j++) {
|
|
27
|
+
let y = p[j]!;
|
|
28
|
+
if (Math.abs(x) < Math.abs(y)) [x, y] = [y, x];
|
|
29
|
+
const hi = x + y;
|
|
30
|
+
const lo = y - (hi - x);
|
|
31
|
+
if (lo !== 0) p[i++] = lo;
|
|
32
|
+
x = hi;
|
|
33
|
+
}
|
|
34
|
+
p.length = i;
|
|
35
|
+
p.push(x);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The partials' exact sum rounded once to a double (round half to even), as Python `math.fsum`. */
|
|
39
|
+
function roundExact(p: readonly number[]): number {
|
|
40
|
+
let n = p.length;
|
|
41
|
+
if (n === 0) return 0;
|
|
42
|
+
let hi = p[--n]!;
|
|
43
|
+
while (n > 0) {
|
|
44
|
+
const x = hi;
|
|
45
|
+
const y = p[--n]!;
|
|
46
|
+
hi = x + y;
|
|
47
|
+
const lo = y - (hi - x);
|
|
48
|
+
if (lo !== 0) {
|
|
49
|
+
// half-way case: the rest has the same sign, so round away from the tie
|
|
50
|
+
if (n > 0 && ((lo < 0 && p[n - 1]! < 0) || (lo > 0 && p[n - 1]! > 0))) {
|
|
51
|
+
const y2 = lo * 2;
|
|
52
|
+
const x2 = hi + y2;
|
|
53
|
+
if (y2 === x2 - hi) hi = x2;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return hi;
|
|
59
|
+
}
|
|
60
|
+
|
|
35
61
|
/**
|
|
36
|
-
*
|
|
62
|
+
* Window sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without
|
|
37
63
|
* changing it; {@link commit} keeps the last step. So several candidate values can be tried on one bar and only the
|
|
38
64
|
* last one kept (a ta.* call in a loop).
|
|
39
65
|
*
|
|
40
|
-
* The length can change from one step to the next (a series length)
|
|
41
|
-
*
|
|
42
|
-
* values that leave the window, oldest first; a larger length adds back the values from the one before the old window
|
|
43
|
-
* down to the one `length` values back, newest first (one more than the window needs). Then the usual step runs with
|
|
44
|
-
* the new length: it removes the stored value of the one `length` values back and adds the new value.
|
|
66
|
+
* The length can change from one step to the next (a series length): the window is then the last `length` values,
|
|
67
|
+
* the values that leave or come back are removed or added exactly.
|
|
45
68
|
*/
|
|
46
69
|
export class RunningSum {
|
|
47
70
|
private readonly values: number[] = [];
|
|
48
|
-
|
|
49
|
-
private
|
|
50
|
-
private
|
|
51
|
-
private
|
|
52
|
-
private stepLength = NaN;
|
|
71
|
+
/** Exact sum of the kept window (the last `keptLength` values). */
|
|
72
|
+
private partials: number[] = [];
|
|
73
|
+
private keptLength = 0;
|
|
74
|
+
private stepLength = 0;
|
|
53
75
|
private stepX = NaN;
|
|
54
|
-
private
|
|
55
|
-
private stepS = 0;
|
|
56
|
-
private stepC = 0;
|
|
76
|
+
private stepPartials: number[] = [];
|
|
57
77
|
|
|
58
|
-
/**
|
|
59
|
-
private
|
|
78
|
+
/** The partials of the last `len` values (kept state, nothing added). */
|
|
79
|
+
private resized(len: number): number[] {
|
|
60
80
|
const values = this.values;
|
|
61
81
|
const n = values.length;
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
c = (t - s) - y;
|
|
69
|
-
s = t;
|
|
70
|
-
};
|
|
71
|
-
if (len < kept) {
|
|
72
|
-
for (let k = Math.max(0, n - kept); k <= n - len - 1; k++) add(-values[k]!);
|
|
73
|
-
} else if (len > kept) {
|
|
74
|
-
for (let k = n - kept - 1; k >= Math.max(0, n - len); k--) add(values[k]!);
|
|
75
|
-
}
|
|
76
|
-
this.stepS = s;
|
|
77
|
-
this.stepC = c;
|
|
82
|
+
const p = this.partials.slice();
|
|
83
|
+
const kept = Math.min(this.keptLength, n);
|
|
84
|
+
const want = Math.min(len, n);
|
|
85
|
+
if (want < kept) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]!);
|
|
86
|
+
else for (let k = n - kept - 1; k >= n - want; k--) addExact(p, values[k]!);
|
|
87
|
+
return p;
|
|
78
88
|
}
|
|
79
89
|
|
|
80
90
|
/**
|
|
81
|
-
* The
|
|
91
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
82
92
|
* unchanged until {@link commit}.
|
|
83
93
|
*
|
|
84
94
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
85
95
|
*/
|
|
86
96
|
step(x: number, length: number): number {
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const c0 = c;
|
|
93
|
-
const n = values.length;
|
|
94
|
-
if (n >= len) {
|
|
95
|
-
const y = -this.stored[n - len]! - c;
|
|
96
|
-
const t = s + y;
|
|
97
|
-
c = (t - s) - y;
|
|
98
|
-
s = t;
|
|
99
|
-
}
|
|
100
|
-
if (compensationLost(x, c0)) {
|
|
101
|
-
let fresh = x;
|
|
102
|
-
for (let k = n - 1; k >= Math.max(0, n + 1 - len); k--) fresh += values[k]!;
|
|
103
|
-
s = fresh;
|
|
104
|
-
c = 0;
|
|
105
|
-
this.stepY = x;
|
|
106
|
-
} else {
|
|
107
|
-
const y = x - c;
|
|
108
|
-
const t = s + y;
|
|
109
|
-
c = (t - s) - y;
|
|
110
|
-
s = t;
|
|
111
|
-
this.stepY = y;
|
|
112
|
-
}
|
|
113
|
-
this.stepLength = len;
|
|
97
|
+
const n = this.values.length;
|
|
98
|
+
const p = this.resized(length - 1);
|
|
99
|
+
addExact(p, x);
|
|
100
|
+
this.stepPartials = p;
|
|
101
|
+
this.stepLength = Math.min(length, n + 1);
|
|
114
102
|
this.stepX = x;
|
|
115
|
-
|
|
116
|
-
this.stepC = c;
|
|
117
|
-
return n + 1 >= len ? s : NaN;
|
|
103
|
+
return n + 1 >= length ? roundExact(p) : NaN;
|
|
118
104
|
}
|
|
119
105
|
|
|
120
106
|
/**
|
|
121
|
-
* The
|
|
122
|
-
*
|
|
107
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
108
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
123
109
|
*
|
|
124
110
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
125
111
|
*/
|
|
126
112
|
stepNa(length: number): number {
|
|
127
|
-
this.resize(length);
|
|
128
|
-
this.stepLength = length;
|
|
129
|
-
this.stepX = NaN;
|
|
130
113
|
const n = this.values.length;
|
|
131
|
-
|
|
114
|
+
const p = this.resized(length);
|
|
115
|
+
this.stepPartials = p;
|
|
116
|
+
this.stepLength = Math.min(length, n);
|
|
117
|
+
this.stepX = NaN;
|
|
118
|
+
return n > 0 && n >= length ? roundExact(p) : NaN;
|
|
132
119
|
}
|
|
133
120
|
|
|
134
121
|
/** Keeps the state of the last {@link step} or {@link stepNa}. */
|
|
135
122
|
commit(): void {
|
|
136
|
-
if (!Number.isNaN(this.stepX))
|
|
137
|
-
this.values.push(this.stepX);
|
|
138
|
-
this.stored.push(this.stepY);
|
|
139
|
-
}
|
|
123
|
+
if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);
|
|
140
124
|
this.keptLength = this.stepLength;
|
|
141
|
-
this.
|
|
142
|
-
this.c = this.stepC;
|
|
125
|
+
this.partials = this.stepPartials;
|
|
143
126
|
}
|
|
144
127
|
}
|
|
145
128
|
|
|
@@ -157,7 +140,7 @@ function sumWindows(source: ArrayLike<number | null | undefined>, lengthAt: (i:
|
|
|
157
140
|
}
|
|
158
141
|
|
|
159
142
|
/**
|
|
160
|
-
* The sum of the last `length` non-na values on each bar, as PineScript
|
|
143
|
+
* The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
|
|
161
144
|
*
|
|
162
145
|
* @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
|
|
163
146
|
* @param length - Number of non-na values in the window
|
|
@@ -198,7 +181,7 @@ export function runningSumSeries(
|
|
|
198
181
|
return sumWindows(source, (i) => lens[i]!);
|
|
199
182
|
}
|
|
200
183
|
|
|
201
|
-
/** Variance from the
|
|
184
|
+
/** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */
|
|
202
185
|
function varianceFromSums(
|
|
203
186
|
source: ArrayLike<number | null | undefined>,
|
|
204
187
|
sumOf: (src: ArrayLike<number | null | undefined>) => number[],
|
|
@@ -220,7 +203,7 @@ function varianceFromSums(
|
|
|
220
203
|
}
|
|
221
204
|
|
|
222
205
|
/**
|
|
223
|
-
* Window variance from the
|
|
206
|
+
* Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
|
|
224
207
|
* biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
|
|
225
208
|
*/
|
|
226
209
|
export function runningVariance(
|
package/src/table/index.ts
CHANGED
|
@@ -173,7 +173,11 @@ export function merge_cells(
|
|
|
173
173
|
if (end_column < start_column || end_row < start_row) {
|
|
174
174
|
throw new RangeError('table.merge_cells(): the end cell is before the start cell.');
|
|
175
175
|
}
|
|
176
|
-
|
|
176
|
+
// One merge per range, as PineScript: merging the same cells again (a script that merges on every bar) keeps one.
|
|
177
|
+
const merges = (table_id.merges ??= []);
|
|
178
|
+
if (!merges.some((m) => m.start_column === start_column && m.start_row === start_row && m.end_column === end_column && m.end_row === end_row)) {
|
|
179
|
+
merges.push({ start_column, start_row, end_column, end_row });
|
|
180
|
+
}
|
|
177
181
|
}
|
|
178
182
|
|
|
179
183
|
/**
|
package/src/utils/index.ts
CHANGED
|
@@ -195,7 +195,7 @@ export function getSource(
|
|
|
195
195
|
case 'hl2': return ohlc.high.map((h, i) => (h + ohlc.low[i]!) / 2);
|
|
196
196
|
case 'hlc3': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + ohlc.close[i]!) / 3);
|
|
197
197
|
case 'ohlc4': return ohlc.open.map((o, i) => (o + ohlc.high[i]! + ohlc.low[i]! + ohlc.close[i]!) / 4);
|
|
198
|
-
case 'hlcc4': return ohlc.high.map((h, i) => (h + ohlc.low[i]! +
|
|
198
|
+
case 'hlcc4': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + 2 * ohlc.close[i]!) / 4);
|
|
199
199
|
case 'volume': return ohlc.volume ?? ohlc.close.map(() => NaN);
|
|
200
200
|
}
|
|
201
201
|
const ext = externalSource(source);
|
|
@@ -211,7 +211,7 @@ export function getSource(
|
|
|
211
211
|
case 'hl2': return bars.map(b => (b.high + b.low) / 2);
|
|
212
212
|
case 'hlc3': return bars.map(b => (b.high + b.low + b.close) / 3);
|
|
213
213
|
case 'ohlc4': return bars.map(b => (b.open + b.high + b.low + b.close) / 4);
|
|
214
|
-
case 'hlcc4': return bars.map(b => (b.high + b.low +
|
|
214
|
+
case 'hlcc4': return bars.map(b => (b.high + b.low + 2 * b.close) / 4);
|
|
215
215
|
case 'volume': return bars.map(b => b.volume ?? NaN);
|
|
216
216
|
}
|
|
217
217
|
// Another source (study on study), else close.
|
|
@@ -254,7 +254,7 @@ export function getSourceSeries(
|
|
|
254
254
|
case 'hl2': return high.add(low).div(2);
|
|
255
255
|
case 'hlc3': return high.add(low).add(close).div(3);
|
|
256
256
|
case 'ohlc4': return open.add(high).add(low).add(close).div(4);
|
|
257
|
-
case 'hlcc4': return high.add(low).add(close
|
|
257
|
+
case 'hlcc4': return high.add(low).add(close.mul(2)).div(4);
|
|
258
258
|
case 'volume': return Series.fromBars(bars, 'volume');
|
|
259
259
|
default: {
|
|
260
260
|
// Another source (study on study), else close.
|