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
|
@@ -1,39 +1,33 @@
|
|
|
1
1
|
/** A value PineScript treats as na in window functions (also +/-Infinity). */
|
|
2
2
|
export declare function isNa(v: number | null | undefined): boolean;
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Window sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without
|
|
5
5
|
* changing it; {@link commit} keeps the last step. So several candidate values can be tried on one bar and only the
|
|
6
6
|
* last one kept (a ta.* call in a loop).
|
|
7
7
|
*
|
|
8
|
-
* The length can change from one step to the next (a series length)
|
|
9
|
-
*
|
|
10
|
-
* values that leave the window, oldest first; a larger length adds back the values from the one before the old window
|
|
11
|
-
* down to the one `length` values back, newest first (one more than the window needs). Then the usual step runs with
|
|
12
|
-
* the new length: it removes the stored value of the one `length` values back and adds the new value.
|
|
8
|
+
* The length can change from one step to the next (a series length): the window is then the last `length` values,
|
|
9
|
+
* the values that leave or come back are removed or added exactly.
|
|
13
10
|
*/
|
|
14
11
|
export declare class RunningSum {
|
|
15
12
|
private readonly values;
|
|
16
|
-
|
|
13
|
+
/** Exact sum of the kept window (the last `keptLength` values). */
|
|
14
|
+
private partials;
|
|
17
15
|
private keptLength;
|
|
18
|
-
private s;
|
|
19
|
-
private c;
|
|
20
16
|
private stepLength;
|
|
21
17
|
private stepX;
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
-
private
|
|
25
|
-
/** Kahan steps that move the kept sum from the kept length to `len`; the result goes to stepS / stepC. */
|
|
26
|
-
private resize;
|
|
18
|
+
private stepPartials;
|
|
19
|
+
/** The partials of the last `len` values (kept state, nothing added). */
|
|
20
|
+
private resized;
|
|
27
21
|
/**
|
|
28
|
-
* The
|
|
22
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
29
23
|
* unchanged until {@link commit}.
|
|
30
24
|
*
|
|
31
25
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
32
26
|
*/
|
|
33
27
|
step(x: number, length: number): number;
|
|
34
28
|
/**
|
|
35
|
-
* The
|
|
36
|
-
*
|
|
29
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
30
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
37
31
|
*
|
|
38
32
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
39
33
|
*/
|
|
@@ -42,7 +36,7 @@ export declare class RunningSum {
|
|
|
42
36
|
commit(): void;
|
|
43
37
|
}
|
|
44
38
|
/**
|
|
45
|
-
* The sum of the last `length` non-na values on each bar, as PineScript
|
|
39
|
+
* The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
|
|
46
40
|
*
|
|
47
41
|
* @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
|
|
48
42
|
* @param length - Number of non-na values in the window
|
|
@@ -58,7 +52,7 @@ export declare function runningSum(source: ArrayLike<number | null | undefined>,
|
|
|
58
52
|
*/
|
|
59
53
|
export declare function runningSumSeries(source: ArrayLike<number | null | undefined>, lengths: ArrayLike<number>, name?: string): number[];
|
|
60
54
|
/**
|
|
61
|
-
* Window variance from the
|
|
55
|
+
* Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
|
|
62
56
|
* biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
|
|
63
57
|
*/
|
|
64
58
|
export declare function runningVariance(source: ArrayLike<number | null | undefined>, length: number, biased: boolean): number[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"running-sum.d.ts","sourceRoot":"","sources":["../../../src/ta/running-sum.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"running-sum.d.ts","sourceRoot":"","sources":["../../../src/ta/running-sum.ts"],"names":[],"mappings":"AAiBA,8EAA8E;AAC9E,wBAAgB,IAAI,CAAC,CAAC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OAAO,EACf,IAAI,SAAgB,GACnB,MAAM,EAAE,CAGV"}
|
|
@@ -27,107 +27,95 @@ __export(running_sum_exports, {
|
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(running_sum_exports);
|
|
29
29
|
var import_limits = require("./limits.js");
|
|
30
|
-
function compensationLost(x, c) {
|
|
31
|
-
if (c === 0 || x === 0) return false;
|
|
32
|
-
const size = Math.abs(c);
|
|
33
|
-
return Math.abs(x + size - x) < size;
|
|
34
|
-
}
|
|
35
30
|
function isNa(v) {
|
|
36
31
|
return v === void 0 || v === null || !Number.isFinite(v);
|
|
37
32
|
}
|
|
33
|
+
function addExact(p, x) {
|
|
34
|
+
let i = 0;
|
|
35
|
+
for (let j = 0; j < p.length; j++) {
|
|
36
|
+
let y = p[j];
|
|
37
|
+
if (Math.abs(x) < Math.abs(y)) [x, y] = [y, x];
|
|
38
|
+
const hi = x + y;
|
|
39
|
+
const lo = y - (hi - x);
|
|
40
|
+
if (lo !== 0) p[i++] = lo;
|
|
41
|
+
x = hi;
|
|
42
|
+
}
|
|
43
|
+
p.length = i;
|
|
44
|
+
p.push(x);
|
|
45
|
+
}
|
|
46
|
+
function roundExact(p) {
|
|
47
|
+
let n = p.length;
|
|
48
|
+
if (n === 0) return 0;
|
|
49
|
+
let hi = p[--n];
|
|
50
|
+
while (n > 0) {
|
|
51
|
+
const x = hi;
|
|
52
|
+
const y = p[--n];
|
|
53
|
+
hi = x + y;
|
|
54
|
+
const lo = y - (hi - x);
|
|
55
|
+
if (lo !== 0) {
|
|
56
|
+
if (n > 0 && (lo < 0 && p[n - 1] < 0 || lo > 0 && p[n - 1] > 0)) {
|
|
57
|
+
const y2 = lo * 2;
|
|
58
|
+
const x2 = hi + y2;
|
|
59
|
+
if (y2 === x2 - hi) hi = x2;
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return hi;
|
|
65
|
+
}
|
|
38
66
|
class RunningSum {
|
|
39
67
|
values = [];
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
stepLength = NaN;
|
|
68
|
+
/** Exact sum of the kept window (the last `keptLength` values). */
|
|
69
|
+
partials = [];
|
|
70
|
+
keptLength = 0;
|
|
71
|
+
stepLength = 0;
|
|
45
72
|
stepX = NaN;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
/** Kahan steps that move the kept sum from the kept length to `len`; the result goes to stepS / stepC. */
|
|
50
|
-
resize(len) {
|
|
73
|
+
stepPartials = [];
|
|
74
|
+
/** The partials of the last `len` values (kept state, nothing added). */
|
|
75
|
+
resized(len) {
|
|
51
76
|
const values = this.values;
|
|
52
77
|
const n = values.length;
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
c = t - s - y;
|
|
60
|
-
s = t;
|
|
61
|
-
};
|
|
62
|
-
if (len < kept) {
|
|
63
|
-
for (let k = Math.max(0, n - kept); k <= n - len - 1; k++) add(-values[k]);
|
|
64
|
-
} else if (len > kept) {
|
|
65
|
-
for (let k = n - kept - 1; k >= Math.max(0, n - len); k--) add(values[k]);
|
|
66
|
-
}
|
|
67
|
-
this.stepS = s;
|
|
68
|
-
this.stepC = c;
|
|
78
|
+
const p = this.partials.slice();
|
|
79
|
+
const kept = Math.min(this.keptLength, n);
|
|
80
|
+
const want = Math.min(len, n);
|
|
81
|
+
if (want < kept) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]);
|
|
82
|
+
else for (let k = n - kept - 1; k >= n - want; k--) addExact(p, values[k]);
|
|
83
|
+
return p;
|
|
69
84
|
}
|
|
70
85
|
/**
|
|
71
|
-
* The
|
|
86
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
72
87
|
* unchanged until {@link commit}.
|
|
73
88
|
*
|
|
74
89
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
75
90
|
*/
|
|
76
91
|
step(x, length) {
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const c0 = c;
|
|
83
|
-
const n = values.length;
|
|
84
|
-
if (n >= len) {
|
|
85
|
-
const y = -this.stored[n - len] - c;
|
|
86
|
-
const t = s + y;
|
|
87
|
-
c = t - s - y;
|
|
88
|
-
s = t;
|
|
89
|
-
}
|
|
90
|
-
if (compensationLost(x, c0)) {
|
|
91
|
-
let fresh = x;
|
|
92
|
-
for (let k = n - 1; k >= Math.max(0, n + 1 - len); k--) fresh += values[k];
|
|
93
|
-
s = fresh;
|
|
94
|
-
c = 0;
|
|
95
|
-
this.stepY = x;
|
|
96
|
-
} else {
|
|
97
|
-
const y = x - c;
|
|
98
|
-
const t = s + y;
|
|
99
|
-
c = t - s - y;
|
|
100
|
-
s = t;
|
|
101
|
-
this.stepY = y;
|
|
102
|
-
}
|
|
103
|
-
this.stepLength = len;
|
|
92
|
+
const n = this.values.length;
|
|
93
|
+
const p = this.resized(length - 1);
|
|
94
|
+
addExact(p, x);
|
|
95
|
+
this.stepPartials = p;
|
|
96
|
+
this.stepLength = Math.min(length, n + 1);
|
|
104
97
|
this.stepX = x;
|
|
105
|
-
|
|
106
|
-
this.stepC = c;
|
|
107
|
-
return n + 1 >= len ? s : NaN;
|
|
98
|
+
return n + 1 >= length ? roundExact(p) : NaN;
|
|
108
99
|
}
|
|
109
100
|
/**
|
|
110
|
-
* The
|
|
111
|
-
*
|
|
101
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
102
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
112
103
|
*
|
|
113
104
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
114
105
|
*/
|
|
115
106
|
stepNa(length) {
|
|
116
|
-
this.resize(length);
|
|
117
|
-
this.stepLength = length;
|
|
118
|
-
this.stepX = NaN;
|
|
119
107
|
const n = this.values.length;
|
|
120
|
-
|
|
108
|
+
const p = this.resized(length);
|
|
109
|
+
this.stepPartials = p;
|
|
110
|
+
this.stepLength = Math.min(length, n);
|
|
111
|
+
this.stepX = NaN;
|
|
112
|
+
return n > 0 && n >= length ? roundExact(p) : NaN;
|
|
121
113
|
}
|
|
122
114
|
/** Keeps the state of the last {@link step} or {@link stepNa}. */
|
|
123
115
|
commit() {
|
|
124
|
-
if (!Number.isNaN(this.stepX))
|
|
125
|
-
this.values.push(this.stepX);
|
|
126
|
-
this.stored.push(this.stepY);
|
|
127
|
-
}
|
|
116
|
+
if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);
|
|
128
117
|
this.keptLength = this.stepLength;
|
|
129
|
-
this.
|
|
130
|
-
this.c = this.stepC;
|
|
118
|
+
this.partials = this.stepPartials;
|
|
131
119
|
}
|
|
132
120
|
}
|
|
133
121
|
function sumWindows(source, lengthAt) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/ta/running-sum.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["/**\n * Window sums of PineScript `math.sum` / `ta.sma` / `ta.stdev` / `ta.variance` / `ta.mfi` (bit for bit).\n *\n * PineScript gives the correctly rounded sum of the window values (the exact sum of the doubles, rounded once), on\n * every bar: `ta.sma` is that sum divided by the length (measured on the reference 05/11/2026, #188: built-in and\n * expression sources, with or without a `ta.stdev` of the same source, constant and series lengths, na values; 0\n * differing bars of 16,550 on BTCUSD 1D and 34,665 on AAPL 1D). The running sum of earlier versions (Kahan steps,\n * #114) gave a small residue on a window of zeros; the reference no longer does.\n *\n * The exact sum is kept as non-overlapping partials (Shewchuk's algorithm, as Python `math.fsum`): adding a value and\n * removing the one that leaves the window (adding its negation) are exact, so a bar costs the values that enter and\n * leave, not the whole window.\n *\n * @internal\n */\nimport { checkSumLength } from './limits.js';\n\n/** A value PineScript treats as na in window functions (also +/-Infinity). */\nexport function isNa(v: number | null | undefined): boolean {\n return v === undefined || v === null || !Number.isFinite(v);\n}\n\n/** Adds `x` to the partials `p` (non-overlapping, increasing magnitude), exactly. */\nfunction addExact(p: number[], x: number): void {\n let i = 0;\n for (let j = 0; j < p.length; j++) {\n let y = p[j]!;\n if (Math.abs(x) < Math.abs(y)) [x, y] = [y, x];\n const hi = x + y;\n const lo = y - (hi - x);\n if (lo !== 0) p[i++] = lo;\n x = hi;\n }\n p.length = i;\n p.push(x);\n}\n\n/** The partials' exact sum rounded once to a double (round half to even), as Python `math.fsum`. */\nfunction roundExact(p: readonly number[]): number {\n let n = p.length;\n if (n === 0) return 0;\n let hi = p[--n]!;\n while (n > 0) {\n const x = hi;\n const y = p[--n]!;\n hi = x + y;\n const lo = y - (hi - x);\n if (lo !== 0) {\n // half-way case: the rest has the same sign, so round away from the tie\n if (n > 0 && ((lo < 0 && p[n - 1]! < 0) || (lo > 0 && p[n - 1]! > 0))) {\n const y2 = lo * 2;\n const x2 = hi + y2;\n if (y2 === x2 - hi) hi = x2;\n }\n break;\n }\n }\n return hi;\n}\n\n/**\n * Window sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without\n * changing it; {@link commit} keeps the last step. So several candidate values can be tried on one bar and only the\n * last one kept (a ta.* call in a loop).\n *\n * The length can change from one step to the next (a series length): the window is then the last `length` values,\n * the values that leave or come back are removed or added exactly.\n */\nexport class RunningSum {\n private readonly values: number[] = [];\n /** Exact sum of the kept window (the last `keptLength` values). */\n private partials: number[] = [];\n private keptLength = 0;\n private stepLength = 0;\n private stepX = NaN;\n private stepPartials: number[] = [];\n\n /** The partials of the last `len` values (kept state, nothing added). */\n private resized(len: number): number[] {\n const values = this.values;\n const n = values.length;\n const p = this.partials.slice();\n const kept = Math.min(this.keptLength, n);\n const want = Math.min(len, n);\n if (want < kept) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]!);\n else for (let k = n - kept - 1; k >= n - want; k--) addExact(p, values[k]!);\n return p;\n }\n\n /**\n * The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is\n * unchanged until {@link commit}.\n *\n * @returns The sum, or NaN while fewer than `length` values exist (with `x`)\n */\n step(x: number, length: number): number {\n const n = this.values.length;\n const p = this.resized(length - 1);\n addExact(p, x);\n this.stepPartials = p;\n this.stepLength = Math.min(length, n + 1);\n this.stepX = x;\n return n + 1 >= length ? roundExact(p) : NaN;\n }\n\n /**\n * The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is\n * unchanged until {@link commit}. With an unchanged length, it is the previous result.\n *\n * @returns The sum, or NaN while fewer than `length` values exist\n */\n stepNa(length: number): number {\n const n = this.values.length;\n const p = this.resized(length);\n this.stepPartials = p;\n this.stepLength = Math.min(length, n);\n this.stepX = NaN;\n return n > 0 && n >= length ? roundExact(p) : NaN;\n }\n\n /** Keeps the state of the last {@link step} or {@link stepNa}. */\n commit(): void {\n if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);\n this.keptLength = this.stepLength;\n this.partials = this.stepPartials;\n }\n}\n\n/** The running sums of `source` with the length `lengthAt(i)` on bar `i`. */\nfunction sumWindows(source: ArrayLike<number | null | undefined>, lengthAt: (i: number) => number): number[] {\n const out: number[] = new Array<number>(source.length);\n const sum = new RunningSum();\n for (let i = 0; i < source.length; i++) {\n const v = source[i];\n const len = lengthAt(i);\n out[i] = isNa(v) ? sum.stepNa(len) : sum.step(v as number, len);\n sum.commit();\n }\n return out;\n}\n\n/**\n * The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.\n *\n * @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)\n * @param length - Number of non-na values in the window\n * @returns The running sum per bar: NaN until `length` non-na values exist; a bar with an na value keeps the\n * previous result\n */\nexport function runningSum(source: ArrayLike<number | null | undefined>, length: number): number[] {\n const len = Math.floor(length);\n if (!(len >= 1)) return new Array<number>(source.length).fill(NaN);\n return sumWindows(source, () => len);\n}\n\n/** The values of a series length, floored, checked to be at least 1 on every bar. */\nfunction seriesLengths(lengths: ArrayLike<number>, count: number, name: string): number[] {\n if (lengths.length < count) {\n throw new Error(`${name}: the length series has ${lengths.length} values for ${count} bars`);\n }\n return Array.from({ length: count }, (_, i) => {\n const len = Math.floor(lengths[i]!);\n checkSumLength(name, len, i);\n if (!(len >= 1)) throw new Error(`${name}: length must be at least 1 (bar ${i}: ${lengths[i]})`);\n return len;\n });\n}\n\n/**\n * The sum of the last `lengths[i]` non-na values on each bar `i` (a series length), as PineScript computes it (see\n * {@link RunningSum}). A bar with an na value gives the sum of the last `lengths[i]` earlier values.\n *\n * @param name - Function name for the error messages\n */\nexport function runningSumSeries(\n source: ArrayLike<number | null | undefined>,\n lengths: ArrayLike<number>,\n name = 'math.sum'\n): number[] {\n const lens = seriesLengths(lengths, source.length, name);\n return sumWindows(source, (i) => lens[i]!);\n}\n\n/** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */\nfunction varianceFromSums(\n source: ArrayLike<number | null | undefined>,\n sumOf: (src: ArrayLike<number | null | undefined>) => number[],\n lengthAt: (i: number) => number,\n biased: boolean\n): number[] {\n const squares = Array.from({ length: source.length }, (_, i) => {\n const v = source[i];\n return isNa(v) ? NaN : (v as number) * (v as number);\n });\n const sums = sumOf(source);\n const sumSquares = sumOf(squares);\n return sums.map((sum, i) => {\n const len = lengthAt(i);\n const mean = sum / len;\n const sq = sumSquares[i]!;\n return biased ? sq / len - mean * mean : sq / (len - 1) - (mean * sum) / (len - 1);\n });\n}\n\n/**\n * Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):\n * biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.\n */\nexport function runningVariance(\n source: ArrayLike<number | null | undefined>,\n length: number,\n biased: boolean\n): number[] {\n const len = Math.floor(length);\n return varianceFromSums(source, (src) => runningSum(src, len), () => len, biased);\n}\n\n/** {@link runningVariance} with a series length (the length of bar `i` is `lengths[i]`). */\nexport function runningVarianceSeries(\n source: ArrayLike<number | null | undefined>,\n lengths: ArrayLike<number>,\n biased: boolean,\n name = 'ta.variance'\n): number[] {\n const lens = seriesLengths(lengths, source.length, name);\n return varianceFromSums(source, (src) => sumWindows(src, (i) => lens[i]!), (i) => lens[i]!, biased);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,oBAA+B;AAGxB,SAAS,KAAK,GAAuC;AAC1D,SAAO,MAAM,UAAa,MAAM,QAAQ,CAAC,OAAO,SAAS,CAAC;AAC5D;AAGA,SAAS,SAAS,GAAa,GAAiB;AAC9C,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,QAAI,IAAI,EAAE,CAAC;AACX,QAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,EAAG,EAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;AAC7C,UAAM,KAAK,IAAI;AACf,UAAM,KAAK,KAAK,KAAK;AACrB,QAAI,OAAO,EAAG,GAAE,GAAG,IAAI;AACvB,QAAI;AAAA,EACN;AACA,IAAE,SAAS;AACX,IAAE,KAAK,CAAC;AACV;AAGA,SAAS,WAAW,GAA8B;AAChD,MAAI,IAAI,EAAE;AACV,MAAI,MAAM,EAAG,QAAO;AACpB,MAAI,KAAK,EAAE,EAAE,CAAC;AACd,SAAO,IAAI,GAAG;AACZ,UAAM,IAAI;AACV,UAAM,IAAI,EAAE,EAAE,CAAC;AACf,SAAK,IAAI;AACT,UAAM,KAAK,KAAK,KAAK;AACrB,QAAI,OAAO,GAAG;AAEZ,UAAI,IAAI,MAAO,KAAK,KAAK,EAAE,IAAI,CAAC,IAAK,KAAO,KAAK,KAAK,EAAE,IAAI,CAAC,IAAK,IAAK;AACrE,cA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;GAGG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,aAAa,GAAG,IAAI,CAI3E;AACD,+CAA+C;AAC/C,eAAO,MAAM,WAAW,aAhBJ,KAAK,+BAAoB,IAgBC,CAAC;AAC/C,0CAA0C;AAC1C,eAAO,MAAM,eAAe,aAlBR,KAAK,+BAAoB,IAkBS,CAAC;AACvD,yCAAyC;AACzC,eAAO,MAAM,eAAe,aApBR,KAAK,gCAAoB,IAoBS,CAAC;AACvD,2CAA2C;AAC3C,eAAO,MAAM,gBAAgB,aAtBT,KAAK,+BAAoB,IAsBW,CAAC;AACzD,0CAA0C;AAC1C,eAAO,MAAM,gBAAgB,aAxBT,KAAK,gCAAoB,IAwBW,CAAC;AAEzD,qDAAqD;AACrD,wBAAgB,YAAY,CAAC,QAAQ,EAAE,KAAK,GAAG,IAAI,CAElD;AAED,gFAAgF;AAChF,wBAAgB,GAAG,IAAI,KAAK,EAAE,CAE7B;AAGD,OAAO,EAAE,SAAS,IAAI,GAAG,EAAE,CAAC;AAC5B,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,CAAC"}
|
package/dist/cjs/table/index.js
CHANGED
|
@@ -123,7 +123,10 @@ function merge_cells(table_id, start_column, start_row, end_column, end_row) {
|
|
|
123
123
|
if (end_column < start_column || end_row < start_row) {
|
|
124
124
|
throw new RangeError("table.merge_cells(): the end cell is before the start cell.");
|
|
125
125
|
}
|
|
126
|
-
|
|
126
|
+
const merges = table_id.merges ??= [];
|
|
127
|
+
if (!merges.some((m) => m.start_column === start_column && m.start_row === start_row && m.end_column === end_column && m.end_row === end_row)) {
|
|
128
|
+
merges.push({ start_column, start_row, end_column, end_row });
|
|
129
|
+
}
|
|
127
130
|
}
|
|
128
131
|
function clear(table_id, start_column, start_row, end_column = start_column, end_row = start_row) {
|
|
129
132
|
if ((0, import_registry.isDeleted)(table_id)) return;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/table/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Table namespace\n * Functions for creating and filling table drawing objects.\n *\n * @remarks\n * A table is drawn at a fixed position of the pane (`position.*`), not at a bar. Tables have no getters in\n * PineScript; the script API returns the live tables in `result.tables`.\n *\n * PineScript rules:\n * - `table.cell` defines a cell: the arguments it does not give get their default, a previous definition of the\n * cell is replaced\n * - `table.cell_set_*` changes one property of a cell (a cell that was not defined is created)\n * - a column or row outside the table is a runtime error\n * - a deleted table is `na`: its functions do nothing\n * - one live table per position and pane: `table.new` or `table.set_position` at the position of a live table with\n * the same `force_overlay` deletes that table (#176, #182); a table with `force_overlay = true` and one without\n * keep each their own position, also in an overlay script\n *\n * @version 6\n */\n\nimport { all as allDrawings, isDeleted, register, remove } from '../drawing/registry.js';\nimport type { Table, TableCell, color } from '../types/index.js';\n\ntype TablePosition = Table['position'];\ntype HAlign = 'left' | 'center' | 'right';\ntype VAlign = 'top' | 'center' | 'bottom';\ntype FontFamily = 'default' | 'monospace';\n\n/** Deletes the live tables other than `keep` at `position` with the same `force_overlay` (#176, #182). */\nfunction freePosition(position: TablePosition, force_overlay: boolean, keep?: Table): void {\n for (const old of allDrawings('table')) {\n if (old !== keep && old.position === position && (old.force_overlay ?? false) === force_overlay) remove('table', old);\n }\n}\n\n/**\n * Creates a new table (PineScript `table.new`). A live table at the same position with the same `force_overlay` is\n * deleted, as PineScript keeps one table per position and pane (#176, #182).\n *\n * @example\n * ```typescript\n * const t = table.new(position.top_right, 2, 3, '#131722', '#787B86', 1);\n * table.cell(t, 0, 0, 'RSI', 0, 0, '#FFFFFF');\n * ```\n */\nexport function new_table(\n position: TablePosition,\n columns: number,\n rows: number,\n bgcolor?: color,\n frame_color?: color,\n frame_width?: number,\n border_color?: color,\n border_width?: number,\n force_overlay?: boolean\n): Table {\n freePosition(position, force_overlay ?? false);\n return register('table', {\n position,\n columns,\n rows,\n bgcolor,\n frame_color,\n frame_width,\n border_color,\n border_width,\n force_overlay: force_overlay ?? false,\n cells: new Map(),\n merges: [],\n });\n}\n\nfunction checkCell(caller: string, id: Table, column: number, row: number): void {\n if (!(column >= 0 && column < id.columns && row >= 0 && row < id.rows)) {\n throw new RangeError(\n `${caller}: column ${column}, row ${row} is outside the table (${id.columns} columns, ${id.rows} rows).`\n );\n }\n}\n\nconst key = (column: number, row: number): string => `${column},${row}`;\n\n/** The cell at column / row, created when it was not defined. */\nfunction cellOf(caller: string, id: Table, column: number, row: number): TableCell {\n checkCell(caller, id, column, row);\n const cells = (id.cells ??= new Map());\n let cell = cells.get(key(column, row));\n if (!cell) cells.set(key(column, row), (cell = { text: '' }));\n return cell;\n}\n\n/**\n * Defines a cell (PineScript `table.cell`): its text and properties. Omitted properties get their default.\n */\nexport function cell(\n table_id: Table,\n column: number,\n row: number,\n text?: string,\n width?: number,\n height?: number,\n text_color?: color,\n text_halign?: HAlign,\n text_valign?: VAlign,\n text_size?: string | number,\n bgcolor?: color,\n tooltip?: string,\n text_font_family?: FontFamily,\n text_formatting?: number\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.cell()', table_id, column, row);\n (table_id.cells ??= new Map()).set(key(column, row), {\n text: text ?? '',\n width,\n height,\n text_color,\n text_halign,\n text_valign,\n text_size,\n bgcolor,\n tooltip,\n text_font_family,\n text_formatting,\n });\n}\n\nfunction setCell<K extends keyof TableCell>(name: string, field: K) {\n return (table_id: Table, column: number, row: number, value: TableCell[K]): void => {\n if (isDeleted(table_id)) return;\n cellOf(`table.${name}()`, table_id, column, row)[field] = value;\n };\n}\n\n/** Sets the text of a cell. */\nexport const cell_set_text = setCell('cell_set_text', 'text');\n/** Sets the width of a cell (% of the chart width; 0 fits the text). */\nexport const cell_set_width = setCell('cell_set_width', 'width');\n/** Sets the height of a cell (% of the chart height; 0 fits the text). */\nexport const cell_set_height = setCell('cell_set_height', 'height');\n/** Sets the text colour of a cell. */\nexport const cell_set_text_color = setCell('cell_set_text_color', 'text_color');\n/** Sets the horizontal text alignment of a cell. */\nexport const cell_set_text_halign = setCell('cell_set_text_halign', 'text_halign');\n/** Sets the vertical text alignment of a cell. */\nexport const cell_set_text_valign = setCell('cell_set_text_valign', 'text_valign');\n/** Sets the text size of a cell. */\nexport const cell_set_text_size = setCell('cell_set_text_size', 'text_size');\n/** Sets the background colour of a cell. */\nexport const cell_set_bgcolor = setCell('cell_set_bgcolor', 'bgcolor');\n/** Sets the tooltip of a cell. */\nexport const cell_set_tooltip = setCell('cell_set_tooltip', 'tooltip');\n/** Sets the font family of a cell. */\nexport const cell_set_text_font_family = setCell('cell_set_text_font_family', 'text_font_family');\n/** Sets the text formatting of a cell (`text.format_*` flags). */\nexport const cell_set_text_formatting = setCell('cell_set_text_formatting', 'text_formatting');\n\n/**\n * Merges the cells from start_column / start_row to end_column / end_row (PineScript `table.merge_cells`). The\n * merged cell shows the content of the start cell.\n */\nexport function merge_cells(\n table_id: Table,\n start_column: number,\n start_row: number,\n end_column: number,\n end_row: number\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.merge_cells()', table_id, start_column, start_row);\n checkCell('table.merge_cells()', table_id, end_column, end_row);\n if (end_column < start_column || end_row < start_row) {\n throw new RangeError('table.merge_cells(): the end cell is before the start cell.');\n }\n (table_id.merges ??= []).push({ start_column, start_row, end_column, end_row });\n}\n\n/**\n * Removes the cells from start_column / start_row to end_column / end_row (PineScript `table.clear`; default end:\n * the start cell), and the merges that start in that range.\n */\nexport function clear(\n table_id: Table,\n start_column: number,\n start_row: number,\n end_column: number = start_column,\n end_row: number = start_row\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.clear()', table_id, start_column, start_row);\n checkCell('table.clear()', table_id, end_column, end_row);\n const inside = (column: number, row: number): boolean =>\n column >= start_column && column <= end_column && row >= start_row && row <= end_row;\n for (let column = start_column; column <= end_column; column++) {\n for (let row = start_row; row <= end_row; row++) table_id.cells?.delete(key(column, row));\n }\n if (table_id.merges) table_id.merges = table_id.merges.filter((m) => !inside(m.start_column, m.start_row));\n}\n\nfunction setTable<K extends keyof Table>(field: K) {\n return (table_id: Table, value: Table[K]): void => {\n if (isDeleted(table_id)) return;\n table_id[field] = value;\n };\n}\n\n/**\n * Moves the table to another position (`position.*`). A live table at that position with the same `force_overlay` is\n * deleted (#182).\n */\nexport function set_position(table_id: Table, position: TablePosition): void {\n if (isDeleted(table_id)) return;\n freePosition(position, table_id.force_overlay ?? false, table_id);\n table_id.position = position;\n}\n/** Sets the background colour of the table. */\nexport const set_bgcolor = setTable('bgcolor');\n/** Sets the colour of the outer frame. */\nexport const set_frame_color = setTable('frame_color');\n/** Sets the width of the outer frame. */\nexport const set_frame_width = setTable('frame_width');\n/** Sets the colour of the cell borders. */\nexport const set_border_color = setTable('border_color');\n/** Sets the width of the cell borders. */\nexport const set_border_width = setTable('border_width');\n\n/** Deletes the table (PineScript `table.delete`). */\nexport function delete_table(table_id: Table): void {\n remove('table', table_id);\n}\n\n/** All live tables, in creation order (PineScript `table.all`). A new array. */\nexport function all(): Table[] {\n return allDrawings('table');\n}\n\n// Alias to match PineScript API\nexport { new_table as new };\nexport { delete_table as delete };\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBA,sBAAgE;AAShE,SAAS,aAAa,UAAyB,eAAwB,MAAoB;AACzF,aAAW,WAAO,gBAAAA,KAAY,OAAO,GAAG;AACtC,QAAI,QAAQ,QAAQ,IAAI,aAAa,aAAa,IAAI,iBAAiB,WAAW,cAAe,6BAAO,SAAS,GAAG;AAAA,EACtH;AACF;AAYO,SAAS,UACd,UACA,SACA,MACA,SACA,aACA,aACA,cACA,cACA,eACO;AACP,eAAa,UAAU,iBAAiB,KAAK;AAC7C,aAAO,0BAA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U;AAClE,SAAO,CAAC,UAAiB,QAAgB,KAAa,UAA8B;AAClF,YAAI,2BAAU,QAAQ,EAAG;AACzB,WAAO,SAAS,IAAI,MAAM,UAAU,QAAQ,GAAG,EAAE,KAAK,IAAI;AAAA,EAC5D;AACF;AAGO,MAAM,gBAAgB,QAAQ,iBAAiB,MAAM;AAErD,MAAM,iBAAiB,QAAQ,kBAAkB,OAAO;AAExD,MAAM,kBAAkB,QAAQ,mBAAmB,QAAQ;AAE3D,MAAM,sBAAsB,QAAQ,uBAAuB,YAAY;AAEvE,MAAM,uBAAuB,QAAQ,wBAAwB,aAAa;AAE1E,MAAM,uBAAuB,QAAQ,wBAAwB,aAAa;AAE1E,MAAM,qBAAqB,QAAQ,sBAAsB,WAAW;AAEpE,MAAM,mBAAmB,QAAQ,oBAAoB,SAAS;AAE9D,MAAM,mBAAmB,QAAQ,oBAAoB,SAAS;AAE9D,MAAM,4BAA4B,QAAQ,6BAA6B,kBAAkB;AAEzF,MAAM,2BAA2B,QAAQ,4BAA4B,iBAAiB;AAMtF,SAAS,YACd,UACA,cACA,WACA,YACA,SACM;AACN,UAAI,2BAAU,QAAQ,EAAG;AACzB,YAAU,uBAAuB,UAAU,cAAc,SAAS;AAClE,YAAU,uBAAuB,UAAU,YAAY,OAAO;AAC9D,MAAI,aAAa,gBAAgB,UAAU,WAAW;AACpD,UAAM,IAAI,WAAW,6DAA6D;AAAA,EACpF;
|
|
4
|
+
"sourcesContent": ["/**\n * Table namespace\n * Functions for creating and filling table drawing objects.\n *\n * @remarks\n * A table is drawn at a fixed position of the pane (`position.*`), not at a bar. Tables have no getters in\n * PineScript; the script API returns the live tables in `result.tables`.\n *\n * PineScript rules:\n * - `table.cell` defines a cell: the arguments it does not give get their default, a previous definition of the\n * cell is replaced\n * - `table.cell_set_*` changes one property of a cell (a cell that was not defined is created)\n * - a column or row outside the table is a runtime error\n * - a deleted table is `na`: its functions do nothing\n * - one live table per position and pane: `table.new` or `table.set_position` at the position of a live table with\n * the same `force_overlay` deletes that table (#176, #182); a table with `force_overlay = true` and one without\n * keep each their own position, also in an overlay script\n *\n * @version 6\n */\n\nimport { all as allDrawings, isDeleted, register, remove } from '../drawing/registry.js';\nimport type { Table, TableCell, color } from '../types/index.js';\n\ntype TablePosition = Table['position'];\ntype HAlign = 'left' | 'center' | 'right';\ntype VAlign = 'top' | 'center' | 'bottom';\ntype FontFamily = 'default' | 'monospace';\n\n/** Deletes the live tables other than `keep` at `position` with the same `force_overlay` (#176, #182). */\nfunction freePosition(position: TablePosition, force_overlay: boolean, keep?: Table): void {\n for (const old of allDrawings('table')) {\n if (old !== keep && old.position === position && (old.force_overlay ?? false) === force_overlay) remove('table', old);\n }\n}\n\n/**\n * Creates a new table (PineScript `table.new`). A live table at the same position with the same `force_overlay` is\n * deleted, as PineScript keeps one table per position and pane (#176, #182).\n *\n * @example\n * ```typescript\n * const t = table.new(position.top_right, 2, 3, '#131722', '#787B86', 1);\n * table.cell(t, 0, 0, 'RSI', 0, 0, '#FFFFFF');\n * ```\n */\nexport function new_table(\n position: TablePosition,\n columns: number,\n rows: number,\n bgcolor?: color,\n frame_color?: color,\n frame_width?: number,\n border_color?: color,\n border_width?: number,\n force_overlay?: boolean\n): Table {\n freePosition(position, force_overlay ?? false);\n return register('table', {\n position,\n columns,\n rows,\n bgcolor,\n frame_color,\n frame_width,\n border_color,\n border_width,\n force_overlay: force_overlay ?? false,\n cells: new Map(),\n merges: [],\n });\n}\n\nfunction checkCell(caller: string, id: Table, column: number, row: number): void {\n if (!(column >= 0 && column < id.columns && row >= 0 && row < id.rows)) {\n throw new RangeError(\n `${caller}: column ${column}, row ${row} is outside the table (${id.columns} columns, ${id.rows} rows).`\n );\n }\n}\n\nconst key = (column: number, row: number): string => `${column},${row}`;\n\n/** The cell at column / row, created when it was not defined. */\nfunction cellOf(caller: string, id: Table, column: number, row: number): TableCell {\n checkCell(caller, id, column, row);\n const cells = (id.cells ??= new Map());\n let cell = cells.get(key(column, row));\n if (!cell) cells.set(key(column, row), (cell = { text: '' }));\n return cell;\n}\n\n/**\n * Defines a cell (PineScript `table.cell`): its text and properties. Omitted properties get their default.\n */\nexport function cell(\n table_id: Table,\n column: number,\n row: number,\n text?: string,\n width?: number,\n height?: number,\n text_color?: color,\n text_halign?: HAlign,\n text_valign?: VAlign,\n text_size?: string | number,\n bgcolor?: color,\n tooltip?: string,\n text_font_family?: FontFamily,\n text_formatting?: number\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.cell()', table_id, column, row);\n (table_id.cells ??= new Map()).set(key(column, row), {\n text: text ?? '',\n width,\n height,\n text_color,\n text_halign,\n text_valign,\n text_size,\n bgcolor,\n tooltip,\n text_font_family,\n text_formatting,\n });\n}\n\nfunction setCell<K extends keyof TableCell>(name: string, field: K) {\n return (table_id: Table, column: number, row: number, value: TableCell[K]): void => {\n if (isDeleted(table_id)) return;\n cellOf(`table.${name}()`, table_id, column, row)[field] = value;\n };\n}\n\n/** Sets the text of a cell. */\nexport const cell_set_text = setCell('cell_set_text', 'text');\n/** Sets the width of a cell (% of the chart width; 0 fits the text). */\nexport const cell_set_width = setCell('cell_set_width', 'width');\n/** Sets the height of a cell (% of the chart height; 0 fits the text). */\nexport const cell_set_height = setCell('cell_set_height', 'height');\n/** Sets the text colour of a cell. */\nexport const cell_set_text_color = setCell('cell_set_text_color', 'text_color');\n/** Sets the horizontal text alignment of a cell. */\nexport const cell_set_text_halign = setCell('cell_set_text_halign', 'text_halign');\n/** Sets the vertical text alignment of a cell. */\nexport const cell_set_text_valign = setCell('cell_set_text_valign', 'text_valign');\n/** Sets the text size of a cell. */\nexport const cell_set_text_size = setCell('cell_set_text_size', 'text_size');\n/** Sets the background colour of a cell. */\nexport const cell_set_bgcolor = setCell('cell_set_bgcolor', 'bgcolor');\n/** Sets the tooltip of a cell. */\nexport const cell_set_tooltip = setCell('cell_set_tooltip', 'tooltip');\n/** Sets the font family of a cell. */\nexport const cell_set_text_font_family = setCell('cell_set_text_font_family', 'text_font_family');\n/** Sets the text formatting of a cell (`text.format_*` flags). */\nexport const cell_set_text_formatting = setCell('cell_set_text_formatting', 'text_formatting');\n\n/**\n * Merges the cells from start_column / start_row to end_column / end_row (PineScript `table.merge_cells`). The\n * merged cell shows the content of the start cell.\n */\nexport function merge_cells(\n table_id: Table,\n start_column: number,\n start_row: number,\n end_column: number,\n end_row: number\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.merge_cells()', table_id, start_column, start_row);\n checkCell('table.merge_cells()', table_id, end_column, end_row);\n if (end_column < start_column || end_row < start_row) {\n throw new RangeError('table.merge_cells(): the end cell is before the start cell.');\n }\n // One merge per range, as PineScript: merging the same cells again (a script that merges on every bar) keeps one.\n const merges = (table_id.merges ??= []);\n if (!merges.some((m) => m.start_column === start_column && m.start_row === start_row && m.end_column === end_column && m.end_row === end_row)) {\n merges.push({ start_column, start_row, end_column, end_row });\n }\n}\n\n/**\n * Removes the cells from start_column / start_row to end_column / end_row (PineScript `table.clear`; default end:\n * the start cell), and the merges that start in that range.\n */\nexport function clear(\n table_id: Table,\n start_column: number,\n start_row: number,\n end_column: number = start_column,\n end_row: number = start_row\n): void {\n if (isDeleted(table_id)) return;\n checkCell('table.clear()', table_id, start_column, start_row);\n checkCell('table.clear()', table_id, end_column, end_row);\n const inside = (column: number, row: number): boolean =>\n column >= start_column && column <= end_column && row >= start_row && row <= end_row;\n for (let column = start_column; column <= end_column; column++) {\n for (let row = start_row; row <= end_row; row++) table_id.cells?.delete(key(column, row));\n }\n if (table_id.merges) table_id.merges = table_id.merges.filter((m) => !inside(m.start_column, m.start_row));\n}\n\nfunction setTable<K extends keyof Table>(field: K) {\n return (table_id: Table, value: Table[K]): void => {\n if (isDeleted(table_id)) return;\n table_id[field] = value;\n };\n}\n\n/**\n * Moves the table to another position (`position.*`). A live table at that position with the same `force_overlay` is\n * deleted (#182).\n */\nexport function set_position(table_id: Table, position: TablePosition): void {\n if (isDeleted(table_id)) return;\n freePosition(position, table_id.force_overlay ?? false, table_id);\n table_id.position = position;\n}\n/** Sets the background colour of the table. */\nexport const set_bgcolor = setTable('bgcolor');\n/** Sets the colour of the outer frame. */\nexport const set_frame_color = setTable('frame_color');\n/** Sets the width of the outer frame. */\nexport const set_frame_width = setTable('frame_width');\n/** Sets the colour of the cell borders. */\nexport const set_border_color = setTable('border_color');\n/** Sets the width of the cell borders. */\nexport const set_border_width = setTable('border_width');\n\n/** Deletes the table (PineScript `table.delete`). */\nexport function delete_table(table_id: Table): void {\n remove('table', table_id);\n}\n\n/** All live tables, in creation order (PineScript `table.all`). A new array. */\nexport function all(): Table[] {\n return allDrawings('table');\n}\n\n// Alias to match PineScript API\nexport { new_table as new };\nexport { delete_table as delete };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBA,sBAAgE;AAShE,SAAS,aAAa,UAAyB,eAAwB,MAAoB;AACzF,aAAW,WAAO,gBAAAA,KAAY,OAAO,GAAG;AACtC,QAAI,QAAQ,QAAQ,IAAI,aAAa,aAAa,IAAI,iBAAiB,WAAW,cAAe,6BAAO,SAAS,GAAG;AAAA,EACtH;AACF;AAYO,SAAS,UACd,UACA,SACA,MACA,SACA,aACA,aACA,cACA,cACA,eACO;AACP,eAAa,UAAU,iBAAiB,KAAK;AAC7C,aAAO,0BAAS,SAAS;AAAA,IACvB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,eAAe,iBAAiB;AAAA,IAChC,OAAO,oBAAI,IAAI;AAAA,IACf,QAAQ,CAAC;AAAA,EACX,CAAC;AACH;AAEA,SAAS,UAAU,QAAgB,IAAW,QAAgB,KAAmB;AAC/E,MAAI,EAAE,UAAU,K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AC1F;AACA,MAAI,SAAS,OAAQ,UAAS,SAAS,SAAS,OAAO,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,cAAc,EAAE,SAAS,CAAC;AAC3G;AAEA,SAAS,SAAgC,OAAU;AACjD,SAAO,CAAC,UAAiB,UAA0B;AACjD,YAAI,2BAAU,QAAQ,EAAG;AACzB,aAAS,KAAK,IAAI;AAAA,EACpB;AACF;AAMO,SAAS,aAAa,UAAiB,UAA+B;AAC3E,UAAI,2BAAU,QAAQ,EAAG;AACzB,eAAa,UAAU,SAAS,iBAAiB,OAAO,QAAQ;AAChE,WAAS,WAAW;AACtB;AAEO,MAAM,cAAc,SAAS,SAAS;AAEtC,MAAM,kBAAkB,SAAS,aAAa;AAE9C,MAAM,kBAAkB,SAAS,aAAa;AAE9C,MAAM,mBAAmB,SAAS,cAAc;AAEhD,MAAM,mBAAmB,SAAS,cAAc;AAGhD,SAAS,aAAa,UAAuB;AAClD,8BAAO,SAAS,QAAQ;AAC1B;AAGO,SAAS,MAAe;AAC7B,aAAO,gBAAAD,KAAY,OAAO;AAC5B;",
|
|
6
6
|
"names": ["allDrawings", "cell"]
|
|
7
7
|
}
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -129,7 +129,7 @@ function getSource(data, source = "close") {
|
|
|
129
129
|
case "ohlc4":
|
|
130
130
|
return ohlc.open.map((o, i) => (o + ohlc.high[i] + ohlc.low[i] + ohlc.close[i]) / 4);
|
|
131
131
|
case "hlcc4":
|
|
132
|
-
return ohlc.high.map((h, i) => (h + ohlc.low[i] +
|
|
132
|
+
return ohlc.high.map((h, i) => (h + ohlc.low[i] + 2 * ohlc.close[i]) / 4);
|
|
133
133
|
case "volume":
|
|
134
134
|
return ohlc.volume ?? ohlc.close.map(() => NaN);
|
|
135
135
|
}
|
|
@@ -154,7 +154,7 @@ function getSource(data, source = "close") {
|
|
|
154
154
|
case "ohlc4":
|
|
155
155
|
return bars.map((b) => (b.open + b.high + b.low + b.close) / 4);
|
|
156
156
|
case "hlcc4":
|
|
157
|
-
return bars.map((b) => (b.high + b.low +
|
|
157
|
+
return bars.map((b) => (b.high + b.low + 2 * b.close) / 4);
|
|
158
158
|
case "volume":
|
|
159
159
|
return bars.map((b) => b.volume ?? NaN);
|
|
160
160
|
}
|
|
@@ -184,7 +184,7 @@ function getSourceSeries(bars, source = "close") {
|
|
|
184
184
|
case "ohlc4":
|
|
185
185
|
return open.add(high).add(low).add(close).div(4);
|
|
186
186
|
case "hlcc4":
|
|
187
|
-
return high.add(low).add(close
|
|
187
|
+
return high.add(low).add(close.mul(2)).div(4);
|
|
188
188
|
case "volume":
|
|
189
189
|
return import_series.Series.fromBars(bars, "volume");
|
|
190
190
|
default: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/utils/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Utility functions for OakScriptJS\n * These are internal helpers and not part of the PineScript API\n */\n\nimport type { series_float, int, float, Source, Bar, OHLC } from '../types/index.js';\nimport { sma, ema, wma, rma } from '../ta/index.js';\nimport { Series } from '../runtime/series.js';\nimport type { SourceType } from '../input.js';\nimport { isDeleted } from '../drawing/registry.js';\nimport { externalSource } from './source-resolver.js';\n\n/**\n * Validates that a series has sufficient data for a given length\n */\nexport function validateSeriesLength(source: Source, length: int, functionName: string): void {\n if (source.length < length) {\n console.warn(`${functionName}: Source length (${source.length}) is less than required length (${length})`);\n }\n}\n\n/**\n * Creates a series filled with NaN values\n */\nexport function createNaNSeries(length: int): series_float {\n return new Array(length).fill(NaN);\n}\n\n/**\n * Checks if a value is `na`: NaN, null, undefined, +/-Infinity, or a deleted drawing object\n * (as in PineScript, `na(l)` is true after `line.delete(l)`, and `na(1 / 0)` is true).\n */\nexport function isNA(value: any): boolean {\n return (\n value === null ||\n value === undefined ||\n Number.isNaN(value) ||\n value === Infinity ||\n value === -Infinity ||\n isDeleted(value)\n );\n}\n\n/**\n * Returns the value or a default if it's NA\n */\nexport function nz(value: any, defaultValue: any = 0): any {\n return isNA(value) ? defaultValue : value;\n}\n\n/**\n * PineScript `fixnan(source)`: replaces each `na` value (NaN, null or undefined) with the\n * previous non-`na` value. Values before the first non-`na` value stay `na`.\n * Works for numeric series and for color series (arrays of color strings with `undefined` for `na`).\n */\nexport function fixnan<T>(source: T[]): T[] {\n let last: T | undefined;\n let seen = false;\n return source.map((v) => {\n // PineScript fixnan keeps +/-Infinity (only NaN / null / undefined are replaced)\n if (!(v === null || v === undefined || Number.isNaN(v) || isDeleted(v))) {\n last = v;\n seen = true;\n return v;\n }\n return seen ? (last as T) : v;\n });\n}\n\n/**\n * PineScript history reference `x[offset]` at bar `i` of an array: the value `offset` bars back,\n * `na` (NaN) before the first bar. A negative offset (a later bar) throws, as in PineScript.\n */\nexport function at(source: ArrayLike<number>, i: int, offset: int = 0): float {\n if (offset < 0) {\n throw new RangeError(`at(): the offset must be >= 0 (got ${offset}); a negative offset reads a later bar.`);\n }\n const j = i - offset;\n return j >= 0 && j < source.length ? (source[j] ?? NaN) : NaN;\n}\n\n/**\n * PineScript division: `a / 0` is +/-Infinity (0 / 0 is NaN), as in JavaScript. PineScript keeps the infinite value\n * in comparisons (`1 / 0 > 0` is true) and treats it as `na` in `na()`, `nz()` and plots.\n */\nexport function div(a: float, b: float): float {\n return a / b;\n}\n\n/**\n * Ensures a value is within bounds\n */\nexport function clamp(value: float, min: float, max: float): float {\n return Math.max(min, Math.min(max, value));\n}\n\n/**\n * Linear interpolation\n */\nexport function lerp(start: float, end: float, t: float): float {\n return start + (end - start) * t;\n}\n\n/**\n * Normalize a value from one range to another\n */\nexport function normalize(value: float, min: float, max: float, newMin: float = 0, newMax: float = 1): float {\n return newMin + ((value - min) * (newMax - newMin)) / (max - min);\n}\n\n// ===== Chart Data Utilities =====\n// These functions help convert between bar arrays and OHLC data structures\n\n/**\n * Creates OHLC data from bar array\n * @param bars - Array of bar data\n * @returns OHLC data structure\n */\nexport function ohlcFromBars(bars: Bar[]): OHLC {\n return {\n open: bars.map(b => b.open),\n high: bars.map(b => b.high),\n low: bars.map(b => b.low),\n close: bars.map(b => b.close),\n };\n}\n\n/**\n * Gets close prices from OHLC\n * @param ohlc - OHLC data\n * @returns Close price series\n */\nexport function getClose(ohlc: OHLC): series_float {\n return ohlc.close;\n}\n\n/**\n * Gets high prices from OHLC\n * @param ohlc - OHLC data\n * @returns High price series\n */\nexport function getHigh(ohlc: OHLC): series_float {\n return ohlc.high;\n}\n\n/**\n * Gets low prices from OHLC\n * @param ohlc - OHLC data\n * @returns Low price series\n */\nexport function getLow(ohlc: OHLC): series_float {\n return ohlc.low;\n}\n\n/**\n * Gets open prices from OHLC\n * @param ohlc - OHLC data\n * @returns Open price series\n */\nexport function getOpen(ohlc: OHLC): series_float {\n return ohlc.open;\n}\n\n// ===== Source Extraction Utilities =====\n\n/**\n * Extract a specific source from OHLC data or bar array\n *\n * @param data - Bar array or OHLC data\n * @param source - Source type: 'close', 'open', 'high', 'low', 'hl2', 'hlc3', 'ohlc4', 'hlcc4', 'volume' (NaN for a\n * bar without volume)\n * @returns Series of the requested source\n *\n * @example\n * ```typescript\n * const bars = [{ open: 100, high: 105, low: 98, close: 102 }, ...];\n * const closes = getSource(bars, 'close'); // [102, ...]\n * const hl2 = getSource(bars, 'hl2'); // [(105+98)/2, ...]\n * ```\n */\nexport function getSource(\n data: Bar[] | OHLC,\n source: SourceType = 'close'\n): series_float {\n // Check if data is OHLC structure\n const isOHLC = 'open' in data && Array.isArray(data.open);\n\n if (isOHLC) {\n const ohlc = data as OHLC;\n switch (source) {\n case 'close': return ohlc.close;\n case 'open': return ohlc.open;\n case 'high': return ohlc.high;\n case 'low': return ohlc.low;\n case 'hl2': return ohlc.high.map((h, i) => (h + ohlc.low[i]!) / 2);\n case 'hlc3': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + ohlc.close[i]!) / 3);\n case 'ohlc4': return ohlc.open.map((o, i) => (o + ohlc.high[i]! + ohlc.low[i]! + ohlc.close[i]!) / 4);\n case 'hlcc4': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + ohlc.close[i]! + ohlc.close[i]!) / 4);\n case 'volume': return ohlc.volume ?? ohlc.close.map(() => NaN);\n }\n const ext = externalSource(source);\n if (!ext) return ohlc.close;\n return ohlc.close.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);\n } else {\n const bars = data as Bar[];\n switch (source) {\n case 'close': return bars.map(b => b.close);\n case 'open': return bars.map(b => b.open);\n case 'high': return bars.map(b => b.high);\n case 'low': return bars.map(b => b.low);\n case 'hl2': return bars.map(b => (b.high + b.low) / 2);\n case 'hlc3': return bars.map(b => (b.high + b.low + b.close) / 3);\n case 'ohlc4': return bars.map(b => (b.open + b.high + b.low + b.close) / 4);\n case 'hlcc4': return bars.map(b => (b.high + b.low + b.close + b.close) / 4);\n case 'volume': return bars.map(b => b.volume ?? NaN);\n }\n // Another source (study on study), else close.\n const ext = externalSource(source);\n if (!ext) return bars.map(b => b.close);\n return bars.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);\n }\n}\n\n/**\n * Creates a Series from bars based on the specified source type.\n * Unlike getSource() which returns number[], this returns a Series\n * with chainable methods like .add(), .sub(), .mul(), .div()\n *\n * @param bars - Array of bar data\n * @param source - Source type (default: 'close')\n * @returns Series object for the requested source\n *\n * @example\n * ```typescript\n * const bars = [{ time: 1, open: 100, high: 105, low: 98, close: 102, volume: 1000 }, ...];\n * const source = getSourceSeries(bars, 'hlc3');\n * const smaResult = ta.sma(source, 14);\n * ```\n */\nexport function getSourceSeries(\n bars: Bar[],\n source: SourceType = 'close'\n): Series {\n const open = new Series(bars, (bar) => bar.open);\n const high = new Series(bars, (bar) => bar.high);\n const low = new Series(bars, (bar) => bar.low);\n const close = new Series(bars, (bar) => bar.close);\n\n switch (source) {\n case 'open': return open;\n case 'high': return high;\n case 'low': return low;\n case 'close': return close;\n case 'hl2': return high.add(low).div(2);\n case 'hlc3': return high.add(low).add(close).div(3);\n case 'ohlc4': return open.add(high).add(low).add(close).div(4);\n case 'hlcc4': return high.add(low).add(close).add(close).div(4);\n case 'volume': return Series.fromBars(bars, 'volume');\n default: {\n // Another source (study on study), else close.\n const ext = externalSource(source);\n if (!ext) return close;\n const values = Array.from({ length: bars.length }, (_, i) => ext[i] ?? NaN);\n return new Series(bars, (_bar, i) => values[i] ?? NaN);\n }\n }\n}\n\n// ===== Series Manipulation Utilities =====\n\n/**\n * Shift (offset) a series forward or backward in time\n *\n * @param series - Series to shift\n * @param offset - Number of bars to shift (positive = forward/right, negative = backward/left)\n * @returns Shifted series with NaN padding\n *\n * @example\n * ```typescript\n * const values = [1, 2, 3, 4, 5];\n * shift(values, 2); // [NaN, NaN, 1, 2, 3] (shift forward)\n * shift(values, -2); // [3, 4, 5, NaN, NaN] (shift backward)\n * ```\n */\nexport function shift(series: series_float, offset: int): series_float {\n if (offset === 0) return series;\n\n if (offset > 0) {\n // Shift forward: add NaN at start, remove from end\n return Array(offset).fill(NaN).concat(series.slice(0, -offset));\n } else {\n // Shift backward: remove from start, add NaN at end\n return series.slice(-offset).concat(Array(-offset).fill(NaN));\n }\n}\n\n/**\n * Apply smoothing to a series using a specified method\n *\n * @param series - Series to smooth\n * @param method - Smoothing method: 'SMA', 'EMA', 'WMA', 'RMA'\n * @param length - Smoothing length\n * @returns Smoothed series\n *\n * @example\n * ```typescript\n * const values = [10, 11, 12, 13, 14];\n * const smoothed = smooth(values, 'EMA', 3);\n * ```\n *\n * @remarks\n * This is a convenience function that delegates to the appropriate ta function.\n * Import this from utils, not from ta namespace.\n */\nexport function smooth(\n series: series_float,\n method: 'SMA' | 'EMA' | 'WMA' | 'RMA',\n length: int\n): series_float {\n switch (method.toUpperCase()) {\n case 'SMA': return sma(series, length);\n case 'EMA': return ema(series, length);\n case 'WMA': return wma(series, length);\n case 'RMA': return rma(series, length);\n default:\n throw new Error(`Unknown smoothing method: ${method}`);\n }\n}\n\n/**\n * Format series values with timestamps for output\n *\n * @param values - Series of values to format\n * @param timestamps - Array of timestamps (optional)\n * @returns Array of objects with time and value properties\n *\n * @example\n * ```typescript\n * const values = [100, 101, NaN, 103];\n * const times = [1, 2, 3, 4];\n * const formatted = formatOutput(values, times);\n * // [{ time: 1, value: 100 }, { time: 2, value: 101 }, { time: 3, value: null }, { time: 4, value: 103 }]\n * ```\n */\nexport function formatOutput(\n values: series_float,\n timestamps?: number[]\n): Array<{ time: number; value: number | null }> {\n return values.map((value, index) => ({\n time: timestamps ? timestamps[index]! : index,\n value: isNaN(value) ? null : value\n }));\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,gBAAmC;AACnC,oBAAuB;AAEvB,sBAA0B;AAC1B,6BAA+B;AAKxB,SAAS,qBAAqB,QAAgB,QAAa,cAA4B;AAC5F,MAAI,OAAO,SAAS,QAAQ;AAC1B,YAAQ,KAAK,GAAG,YAAY,oBAAoB,OAAO,MAAM,mCAAmC,MAAM,GAAG;AAAA,EAC3G;AACF;AAKO,SAAS,gBAAgB,QAA2B;AACzD,SAAO,IAAI,MAAM,MAAM,EAAE,KAAK,GAAG;AACnC;AAMO,SAAS,KAAK,OAAqB;AACxC,SACE,UAAU,QACV,UAAU,UACV,OAAO,MAAM,KAAK,KAClB,UAAU,YACV,UAAU,iBACV,2BAAU,KAAK;AAEnB;AAKO,SAAS,GAAG,OAAY,eAAoB,GAAQ;AACzD,SAAO,KAAK,KAAK,IAAI,eAAe;AACtC;AAOO,SAAS,OAAU,QAAkB;AAC1C,MAAI;AACJ,MAAI,OAAO;AACX,SAAO,OAAO,IAAI,CAAC,MAAM;AAEvB,QAAI,EAAE,MAAM,QAAQ,MAAM,UAAa,OAAO,MAAM,CAAC,SAAK,2BAAU,CAAC,IAAI;AACvE,aAA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AAO;AACb,YAAQ,QAAQ;AAAA,MACd,KAAK;AAAS,eAAO,KAAK;AAAA,MAC1B,KAAK;AAAQ,eAAO,KAAK;AAAA,MACzB,KAAK;AAAQ,eAAO,KAAK;AAAA,MACzB,KAAK;AAAO,eAAO,KAAK;AAAA,MACxB,KAAK;AAAO,eAAO,KAAK,KAAK,IAAI,CAAC,GAAG,OAAO,IAAI,KAAK,IAAI,CAAC,KAAM,CAAC;AAAA,MACjE,KAAK;AAAQ,eAAO,KAAK,KAAK,IAAI,CAAC,GAAG,OAAO,IAAI,KAAK,IAAI,CAAC,IAAK,KAAK,MAAM,CAAC,KAAM,CAAC;AAAA,MACnF,KAAK;AAAS,eAAO,KAAK,KAAK,IAAI,CAAC,GAAG,OAAO,IAAI,KAAK,KAAK,CAAC,IAAK,KAAK,IAAI,CAAC,IAAK,KAAK,MAAM,CAAC,KAAM,CAAC;AAAA,MACpG,KAAK;AAAS,eAAO,KAAK,KAAK,IAAI,CAAC,GAAG,OAAO,IAAI,KAAK,IAAI,CAAC,IAAK,
|
|
4
|
+
"sourcesContent": ["/**\n * Utility functions for OakScriptJS\n * These are internal helpers and not part of the PineScript API\n */\n\nimport type { series_float, int, float, Source, Bar, OHLC } from '../types/index.js';\nimport { sma, ema, wma, rma } from '../ta/index.js';\nimport { Series } from '../runtime/series.js';\nimport type { SourceType } from '../input.js';\nimport { isDeleted } from '../drawing/registry.js';\nimport { externalSource } from './source-resolver.js';\n\n/**\n * Validates that a series has sufficient data for a given length\n */\nexport function validateSeriesLength(source: Source, length: int, functionName: string): void {\n if (source.length < length) {\n console.warn(`${functionName}: Source length (${source.length}) is less than required length (${length})`);\n }\n}\n\n/**\n * Creates a series filled with NaN values\n */\nexport function createNaNSeries(length: int): series_float {\n return new Array(length).fill(NaN);\n}\n\n/**\n * Checks if a value is `na`: NaN, null, undefined, +/-Infinity, or a deleted drawing object\n * (as in PineScript, `na(l)` is true after `line.delete(l)`, and `na(1 / 0)` is true).\n */\nexport function isNA(value: any): boolean {\n return (\n value === null ||\n value === undefined ||\n Number.isNaN(value) ||\n value === Infinity ||\n value === -Infinity ||\n isDeleted(value)\n );\n}\n\n/**\n * Returns the value or a default if it's NA\n */\nexport function nz(value: any, defaultValue: any = 0): any {\n return isNA(value) ? defaultValue : value;\n}\n\n/**\n * PineScript `fixnan(source)`: replaces each `na` value (NaN, null or undefined) with the\n * previous non-`na` value. Values before the first non-`na` value stay `na`.\n * Works for numeric series and for color series (arrays of color strings with `undefined` for `na`).\n */\nexport function fixnan<T>(source: T[]): T[] {\n let last: T | undefined;\n let seen = false;\n return source.map((v) => {\n // PineScript fixnan keeps +/-Infinity (only NaN / null / undefined are replaced)\n if (!(v === null || v === undefined || Number.isNaN(v) || isDeleted(v))) {\n last = v;\n seen = true;\n return v;\n }\n return seen ? (last as T) : v;\n });\n}\n\n/**\n * PineScript history reference `x[offset]` at bar `i` of an array: the value `offset` bars back,\n * `na` (NaN) before the first bar. A negative offset (a later bar) throws, as in PineScript.\n */\nexport function at(source: ArrayLike<number>, i: int, offset: int = 0): float {\n if (offset < 0) {\n throw new RangeError(`at(): the offset must be >= 0 (got ${offset}); a negative offset reads a later bar.`);\n }\n const j = i - offset;\n return j >= 0 && j < source.length ? (source[j] ?? NaN) : NaN;\n}\n\n/**\n * PineScript division: `a / 0` is +/-Infinity (0 / 0 is NaN), as in JavaScript. PineScript keeps the infinite value\n * in comparisons (`1 / 0 > 0` is true) and treats it as `na` in `na()`, `nz()` and plots.\n */\nexport function div(a: float, b: float): float {\n return a / b;\n}\n\n/**\n * Ensures a value is within bounds\n */\nexport function clamp(value: float, min: float, max: float): float {\n return Math.max(min, Math.min(max, value));\n}\n\n/**\n * Linear interpolation\n */\nexport function lerp(start: float, end: float, t: float): float {\n return start + (end - start) * t;\n}\n\n/**\n * Normalize a value from one range to another\n */\nexport function normalize(value: float, min: float, max: float, newMin: float = 0, newMax: float = 1): float {\n return newMin + ((value - min) * (newMax - newMin)) / (max - min);\n}\n\n// ===== Chart Data Utilities =====\n// These functions help convert between bar arrays and OHLC data structures\n\n/**\n * Creates OHLC data from bar array\n * @param bars - Array of bar data\n * @returns OHLC data structure\n */\nexport function ohlcFromBars(bars: Bar[]): OHLC {\n return {\n open: bars.map(b => b.open),\n high: bars.map(b => b.high),\n low: bars.map(b => b.low),\n close: bars.map(b => b.close),\n };\n}\n\n/**\n * Gets close prices from OHLC\n * @param ohlc - OHLC data\n * @returns Close price series\n */\nexport function getClose(ohlc: OHLC): series_float {\n return ohlc.close;\n}\n\n/**\n * Gets high prices from OHLC\n * @param ohlc - OHLC data\n * @returns High price series\n */\nexport function getHigh(ohlc: OHLC): series_float {\n return ohlc.high;\n}\n\n/**\n * Gets low prices from OHLC\n * @param ohlc - OHLC data\n * @returns Low price series\n */\nexport function getLow(ohlc: OHLC): series_float {\n return ohlc.low;\n}\n\n/**\n * Gets open prices from OHLC\n * @param ohlc - OHLC data\n * @returns Open price series\n */\nexport function getOpen(ohlc: OHLC): series_float {\n return ohlc.open;\n}\n\n// ===== Source Extraction Utilities =====\n\n/**\n * Extract a specific source from OHLC data or bar array\n *\n * @param data - Bar array or OHLC data\n * @param source - Source type: 'close', 'open', 'high', 'low', 'hl2', 'hlc3', 'ohlc4', 'hlcc4', 'volume' (NaN for a\n * bar without volume)\n * @returns Series of the requested source\n *\n * @example\n * ```typescript\n * const bars = [{ open: 100, high: 105, low: 98, close: 102 }, ...];\n * const closes = getSource(bars, 'close'); // [102, ...]\n * const hl2 = getSource(bars, 'hl2'); // [(105+98)/2, ...]\n * ```\n */\nexport function getSource(\n data: Bar[] | OHLC,\n source: SourceType = 'close'\n): series_float {\n // Check if data is OHLC structure\n const isOHLC = 'open' in data && Array.isArray(data.open);\n\n if (isOHLC) {\n const ohlc = data as OHLC;\n switch (source) {\n case 'close': return ohlc.close;\n case 'open': return ohlc.open;\n case 'high': return ohlc.high;\n case 'low': return ohlc.low;\n case 'hl2': return ohlc.high.map((h, i) => (h + ohlc.low[i]!) / 2);\n case 'hlc3': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + ohlc.close[i]!) / 3);\n case 'ohlc4': return ohlc.open.map((o, i) => (o + ohlc.high[i]! + ohlc.low[i]! + ohlc.close[i]!) / 4);\n case 'hlcc4': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + 2 * ohlc.close[i]!) / 4);\n case 'volume': return ohlc.volume ?? ohlc.close.map(() => NaN);\n }\n const ext = externalSource(source);\n if (!ext) return ohlc.close;\n return ohlc.close.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);\n } else {\n const bars = data as Bar[];\n switch (source) {\n case 'close': return bars.map(b => b.close);\n case 'open': return bars.map(b => b.open);\n case 'high': return bars.map(b => b.high);\n case 'low': return bars.map(b => b.low);\n case 'hl2': return bars.map(b => (b.high + b.low) / 2);\n case 'hlc3': return bars.map(b => (b.high + b.low + b.close) / 3);\n case 'ohlc4': return bars.map(b => (b.open + b.high + b.low + b.close) / 4);\n case 'hlcc4': return bars.map(b => (b.high + b.low + 2 * b.close) / 4);\n case 'volume': return bars.map(b => b.volume ?? NaN);\n }\n // Another source (study on study), else close.\n const ext = externalSource(source);\n if (!ext) return bars.map(b => b.close);\n return bars.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);\n }\n}\n\n/**\n * Creates a Series from bars based on the specified source type.\n * Unlike getSource() which returns number[], this returns a Series\n * with chainable methods like .add(), .sub(), .mul(), .div()\n *\n * @param bars - Array of bar data\n * @param source - Source type (default: 'close')\n * @returns Series object for the requested source\n *\n * @example\n * ```typescript\n * const bars = [{ time: 1, open: 100, high: 105, low: 98, close: 102, volume: 1000 }, ...];\n * const source = getSourceSeries(bars, 'hlc3');\n * const smaResult = ta.sma(source, 14);\n * ```\n */\nexport function getSourceSeries(\n bars: Bar[],\n source: SourceType = 'close'\n): Series {\n const open = new Series(bars, (bar) => bar.open);\n const high = new Series(bars, (bar) => bar.high);\n const low = new Series(bars, (bar) => bar.low);\n const close = new Series(bars, (bar) => bar.close);\n\n switch (source) {\n case 'open': return open;\n case 'high': return high;\n case 'low': return low;\n case 'close': return close;\n case 'hl2': return high.add(low).div(2);\n case 'hlc3': return high.add(low).add(close).div(3);\n case 'ohlc4': return open.add(high).add(low).add(close).div(4);\n case 'hlcc4': return high.add(low).add(close.mul(2)).div(4);\n case 'volume': return Series.fromBars(bars, 'volume');\n default: {\n // Another source (study on study), else close.\n const ext = externalSource(source);\n if (!ext) return close;\n const values = Array.from({ length: bars.length }, (_, i) => ext[i] ?? NaN);\n return new Series(bars, (_bar, i) => values[i] ?? NaN);\n }\n }\n}\n\n// ===== Series Manipulation Utilities =====\n\n/**\n * Shift (offset) a series forward or backward in time\n *\n * @param series - Series to shift\n * @param offset - Number of bars to shift (positive = forward/right, negative = backward/left)\n * @returns Shifted series with NaN padding\n *\n * @example\n * ```typescript\n * const values = [1, 2, 3, 4, 5];\n * shift(values, 2); // [NaN, NaN, 1, 2, 3] (shift forward)\n * shift(values, -2); // [3, 4, 5, NaN, NaN] (shift backward)\n * ```\n */\nexport function shift(series: series_float, offset: int): series_float {\n if (offset === 0) return series;\n\n if (offset > 0) {\n // Shift forward: add NaN at start, remove from end\n return Array(offset).fill(NaN).concat(series.slice(0, -offset));\n } else {\n // Shift backward: remove from start, add NaN at end\n return series.slice(-offset).concat(Array(-offset).fill(NaN));\n }\n}\n\n/**\n * Apply smoothing to a series using a specified method\n *\n * @param series - Series to smooth\n * @param method - Smoothing method: 'SMA', 'EMA', 'WMA', 'RMA'\n * @param length - Smoothing length\n * @returns Smoothed series\n *\n * @example\n * ```typescript\n * const values = [10, 11, 12, 13, 14];\n * const smoothed = smooth(values, 'EMA', 3);\n * ```\n *\n * @remarks\n * This is a convenience function that delegates to the appropriate ta function.\n * Import this from utils, not from ta namespace.\n */\nexport function smooth(\n series: series_float,\n method: 'SMA' | 'EMA' | 'WMA' | 'RMA',\n length: int\n): series_float {\n switch (method.toUpperCase()) {\n case 'SMA': return sma(series, length);\n case 'EMA': return ema(series, length);\n case 'WMA': return wma(series, length);\n case 'RMA': return rma(series, length);\n default:\n throw new Error(`Unknown smoothing method: ${method}`);\n }\n}\n\n/**\n * Format series values with timestamps for output\n *\n * @param values - Series of values to format\n * @param timestamps - Array of timestamps (optional)\n * @returns Array of objects with time and value properties\n *\n * @example\n * ```typescript\n * const values = [100, 101, NaN, 103];\n * const times = [1, 2, 3, 4];\n * const formatted = formatOutput(values, times);\n * // [{ time: 1, value: 100 }, { time: 2, value: 101 }, { time: 3, value: null }, { time: 4, value: 103 }]\n * ```\n */\nexport function formatOutput(\n values: series_float,\n timestamps?: number[]\n): Array<{ time: number; value: number | null }> {\n return values.map((value, index) => ({\n time: timestamps ? timestamps[index]! : index,\n value: isNaN(value) ? null : value\n }));\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,gBAAmC;AACnC,oBAAuB;AAEvB,sBAA0B;AAC1B,6BAA+B;AAKxB,SAAS,qBAAqB,QAAgB,QAAa,cAA4B;AAC5F,MAAI,OAAO,SAAS,QAAQ;AAC1B,YAAQ,KAAK,GAAG,YAAY,oBAAoB,OAAO,MAAM,mCAAmC,MAAM,GAAG;AAAA,EAC3G;AACF;AAKO,SAAS,gBAAgB,QAA2B;AACzD,SAAO,IAAI,MAAM,MAAM,EAAE,KAAK,GAAG;AACnC;AAMO,SAAS,KAAK,OAAqB;AACxC,SACE,UAAU,QACV,UAAU,UACV,OAAO,MAAM,KAAK,KAClB,UAAU,YACV,UAAU,iBACV,2BAAU,KAAK;AAEnB;AAKO,SAAS,GAAG,OAAY,eAAoB,GAAQ;AACzD,SAAO,KAAK,KAAK,IAAI,eAAe;AACtC;AAOO,SAAS,OAAU,QAAkB;AAC1C,MAAI;AACJ,MAAI,OAAO;AACX,SAAO,OAAO,IAAI,CA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AAAA,EACzD;AACF;AAiBO,SAAS,aACd,QACA,YAC+C;AAC/C,SAAO,OAAO,IAAI,CAAC,OAAO,WAAW;AAAA,IACnC,MAAM,aAAa,WAAW,KAAK,IAAK;AAAA,IACxC,OAAO,MAAM,KAAK,IAAI,OAAO;AAAA,EAC/B,EAAE;AACJ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* - {@link whenCalled}: vectorized, when the bars of the calls are known in advance.
|
|
10
10
|
* - {@link crossover} / {@link crossunder} / {@link cross} / {@link barssince}: one stateful call site for bar
|
|
11
11
|
* loops, called only on the bars where the Pine call runs.
|
|
12
|
+
* - {@link crossoverByBar} / {@link crossunderByBar}: `ta.crossover` / `ta.crossunder` called several times on one
|
|
13
|
+
* bar (a `for` loop over zones): every call compares with the last call of the previous bar.
|
|
12
14
|
* - {@link lowestByBar} / {@link highestByBar} / {@link lowestbars} / {@link highestbars} / {@link pivothigh} /
|
|
13
15
|
* {@link pivotlow}: `ta.lowest` / `ta.highest` / `ta.lowestbars` / `ta.highestbars` / `ta.pivothigh` /
|
|
14
16
|
* `ta.pivotlow` in a conditional block or a loop, also with a length that changes on each call. They keep the
|
|
@@ -63,6 +65,23 @@ export declare function crossover(): (a: number, b: number) => boolean;
|
|
|
63
65
|
* such a call.
|
|
64
66
|
*/
|
|
65
67
|
export declare function crossunder(): (a: number, b: number) => boolean;
|
|
68
|
+
/**
|
|
69
|
+
* One `ta.crossover(a, b)` call site in a loop, called several times on one bar: each call compares with the last
|
|
70
|
+
* call of the previous bar (where both arguments were not na); false before such a call.
|
|
71
|
+
*
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // Pine: for z in zones
|
|
74
|
+
* // if ta.crossover(close, z.mid)
|
|
75
|
+
* const reclaim = callsite.crossoverByBar();
|
|
76
|
+
* eachBar((c) => { for (const z of zones) if (reclaim(c.i, c.close, z.mid)) { ... } });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function crossoverByBar(): (barIndex: number, a: number, b: number) => boolean;
|
|
80
|
+
/**
|
|
81
|
+
* One `ta.crossunder(a, b)` call site in a loop, called several times on one bar: each call compares with the last
|
|
82
|
+
* call of the previous bar (where both arguments were not na); false before such a call.
|
|
83
|
+
*/
|
|
84
|
+
export declare function crossunderByBar(): (barIndex: number, a: number, b: number) => boolean;
|
|
66
85
|
/**
|
|
67
86
|
* One `ta.barssince(condition)` call site: the number of calls since the condition was true on a call (0 on that
|
|
68
87
|
* call), na before the first true condition. An na condition counts as false.
|
|
@@ -196,9 +215,9 @@ export declare function linreg(): (barIndex: number, value: number, length: numb
|
|
|
196
215
|
* - every call on a bar starts from the state of the earlier bars, so it gives the sum of its value and the last
|
|
197
216
|
* `length - 1` kept values; the last call of the bar is the one kept for the later bars
|
|
198
217
|
* - the length can change from one call to the next (also on the same bar): each call first moves the kept sum to
|
|
199
|
-
* its own length as `math.sum` with a series length
|
|
218
|
+
* its own length, as `math.sum` with a series length
|
|
200
219
|
* - a bar without a call is not in the history (as a call in an `if` block)
|
|
201
|
-
* - the sum is the
|
|
220
|
+
* - the sum is the correctly rounded window sum, as `math.sum` (bit for bit, #188)
|
|
202
221
|
*
|
|
203
222
|
* An na value is skipped as in `math.sum` (it returns the sum of the last `length` earlier values); when it is the
|
|
204
223
|
* last call of a bar, the bar is not in the history (not measured).
|
|
@@ -234,7 +253,7 @@ export declare function sum(): (barIndex: number, value: number, length: number)
|
|
|
234
253
|
export declare function sma(): (barIndex: number, value: number, length: number) => number;
|
|
235
254
|
/**
|
|
236
255
|
* One `ta.stdev(source, length, biased)` call site inside a `for` / `while` loop, called with
|
|
237
|
-
* `(barIndex, value, length, biased?)` on each call: the
|
|
256
|
+
* `(barIndex, value, length, biased?)` on each call: the window sums of the values and of their squares follow the
|
|
238
257
|
* rules of {@link sum}; the result is computed from them as `ta.stdev` (bit for bit).
|
|
239
258
|
*
|
|
240
259
|
* @example
|