oakscriptjs 0.9.5 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/array/index.d.ts +93 -50
- package/dist/cjs/array/index.d.ts.map +1 -1
- package/dist/cjs/array/index.js +239 -78
- package/dist/cjs/array/index.js.map +3 -3
- package/dist/cjs/callsite/index.d.ts +81 -2
- package/dist/cjs/callsite/index.d.ts.map +1 -1
- package/dist/cjs/callsite/index.js +62 -1
- package/dist/cjs/callsite/index.js.map +2 -2
- package/dist/cjs/color/index.d.ts +7 -3
- package/dist/cjs/color/index.d.ts.map +1 -1
- package/dist/cjs/color/index.js +6 -1
- package/dist/cjs/color/index.js.map +2 -2
- package/dist/cjs/drawing/bar-time.d.ts +24 -0
- package/dist/cjs/drawing/bar-time.d.ts.map +1 -0
- package/dist/cjs/drawing/bar-time.js +47 -0
- package/dist/cjs/drawing/bar-time.js.map +7 -0
- package/dist/cjs/drawing/output.d.ts +32 -0
- package/dist/cjs/drawing/output.d.ts.map +1 -0
- package/dist/cjs/drawing/output.js +184 -0
- package/dist/cjs/drawing/output.js.map +7 -0
- package/dist/cjs/drawing/registry.d.ts +5 -4
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +11 -2
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +6 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +9 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/label/index.d.ts.map +1 -1
- package/dist/cjs/label/index.js +2 -1
- package/dist/cjs/label/index.js.map +2 -2
- package/dist/cjs/linefill/index.d.ts.map +1 -1
- package/dist/cjs/linefill/index.js +1 -0
- package/dist/cjs/linefill/index.js.map +2 -2
- package/dist/cjs/math/index.d.ts +76 -21
- package/dist/cjs/math/index.d.ts.map +1 -1
- package/dist/cjs/math/index.js +155 -41
- package/dist/cjs/math/index.js.map +3 -3
- package/dist/cjs/matrix/index.d.ts.map +1 -1
- package/dist/cjs/matrix/index.js +42 -36
- package/dist/cjs/matrix/index.js.map +2 -2
- package/dist/cjs/polyline/index.d.ts +3 -5
- package/dist/cjs/polyline/index.d.ts.map +1 -1
- package/dist/cjs/polyline/index.js +0 -5
- package/dist/cjs/polyline/index.js.map +2 -2
- package/dist/cjs/runtime/series.d.ts +9 -0
- package/dist/cjs/runtime/series.d.ts.map +1 -1
- package/dist/cjs/runtime/series.js +16 -2
- package/dist/cjs/runtime/series.js.map +2 -2
- package/dist/cjs/runtime/types.d.ts +22 -0
- package/dist/cjs/runtime/types.d.ts.map +1 -1
- package/dist/cjs/runtime/types.js.map +1 -1
- package/dist/cjs/script/index.d.ts +97 -36
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +173 -26
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +15 -2
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/str/index.d.ts +50 -38
- package/dist/cjs/str/index.d.ts.map +1 -1
- package/dist/cjs/str/index.js +43 -15
- package/dist/cjs/str/index.js.map +2 -2
- package/dist/cjs/str/messageformat.d.ts +2 -1
- package/dist/cjs/str/messageformat.d.ts.map +1 -1
- package/dist/cjs/str/messageformat.js +4 -1
- package/dist/cjs/str/messageformat.js.map +2 -2
- package/dist/cjs/str/numberformat.d.ts +4 -0
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +26 -4
- package/dist/cjs/str/numberformat.js.map +3 -3
- package/dist/cjs/ta/index.d.ts +86 -43
- package/dist/cjs/ta/index.d.ts.map +1 -1
- package/dist/cjs/ta/index.js +173 -267
- package/dist/cjs/ta/index.js.map +3 -3
- package/dist/cjs/ta/running-sum.d.ts +54 -2
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +132 -52
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/ta-series.d.ts +4 -4
- package/dist/cjs/ta-series.d.ts.map +1 -1
- package/dist/cjs/ta-series.js +4 -3
- package/dist/cjs/ta-series.js.map +2 -2
- package/dist/cjs/table/index.d.ts +87 -0
- package/dist/cjs/table/index.d.ts.map +1 -0
- package/dist/cjs/table/index.js +150 -0
- package/dist/cjs/table/index.js.map +7 -0
- package/dist/cjs/text/index.d.ts +17 -2
- package/dist/cjs/text/index.d.ts.map +1 -1
- package/dist/cjs/text/index.js +15 -1
- package/dist/cjs/text/index.js.map +2 -2
- package/dist/cjs/time/datestring.d.ts +1 -0
- package/dist/cjs/time/datestring.d.ts.map +1 -1
- package/dist/cjs/time/datestring.js +7 -0
- package/dist/cjs/time/datestring.js.map +2 -2
- package/dist/cjs/time/index.d.ts.map +1 -1
- package/dist/cjs/time/index.js +2 -1
- package/dist/cjs/time/index.js.map +2 -2
- package/dist/cjs/time/session.d.ts +28 -9
- package/dist/cjs/time/session.d.ts.map +1 -1
- package/dist/cjs/time/session.js +74 -28
- package/dist/cjs/time/session.js.map +2 -2
- package/dist/cjs/time/timezone.d.ts +5 -0
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +15 -1
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/cjs/types/index.d.ts +30 -1
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +12 -0
- package/dist/cjs/types/index.js.map +2 -2
- package/dist/cjs/types/metadata.d.ts +258 -26
- package/dist/cjs/types/metadata.d.ts.map +1 -1
- package/dist/cjs/types/metadata.js.map +1 -1
- package/dist/esm/array/index.d.ts +93 -50
- package/dist/esm/array/index.d.ts.map +1 -1
- package/dist/esm/array/index.js +383 -133
- package/dist/esm/array/index.js.map +1 -1
- package/dist/esm/callsite/index.d.ts +81 -2
- package/dist/esm/callsite/index.d.ts.map +1 -1
- package/dist/esm/callsite/index.js +145 -3
- package/dist/esm/callsite/index.js.map +1 -1
- package/dist/esm/color/index.d.ts +7 -3
- package/dist/esm/color/index.d.ts.map +1 -1
- package/dist/esm/color/index.js +15 -3
- package/dist/esm/color/index.js.map +1 -1
- package/dist/esm/drawing/bar-time.d.ts +24 -0
- package/dist/esm/drawing/bar-time.d.ts.map +1 -0
- package/dist/esm/drawing/bar-time.js +44 -0
- package/dist/esm/drawing/bar-time.js.map +1 -0
- package/dist/esm/drawing/output.d.ts +32 -0
- package/dist/esm/drawing/output.d.ts.map +1 -0
- package/dist/esm/drawing/output.js +190 -0
- package/dist/esm/drawing/output.js.map +1 -0
- package/dist/esm/drawing/registry.d.ts +5 -4
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +12 -2
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +6 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +7 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/label/index.d.ts.map +1 -1
- package/dist/esm/label/index.js +2 -1
- package/dist/esm/label/index.js.map +1 -1
- package/dist/esm/linefill/index.d.ts.map +1 -1
- package/dist/esm/linefill/index.js +3 -0
- package/dist/esm/linefill/index.js.map +1 -1
- package/dist/esm/math/index.d.ts +76 -21
- package/dist/esm/math/index.d.ts.map +1 -1
- package/dist/esm/math/index.js +227 -61
- package/dist/esm/math/index.js.map +1 -1
- package/dist/esm/matrix/index.d.ts.map +1 -1
- package/dist/esm/matrix/index.js +57 -43
- package/dist/esm/matrix/index.js.map +1 -1
- package/dist/esm/polyline/index.d.ts +3 -5
- package/dist/esm/polyline/index.d.ts.map +1 -1
- package/dist/esm/polyline/index.js +3 -9
- package/dist/esm/polyline/index.js.map +1 -1
- package/dist/esm/runtime/series.d.ts +9 -0
- package/dist/esm/runtime/series.d.ts.map +1 -1
- package/dist/esm/runtime/series.js +18 -1
- package/dist/esm/runtime/series.js.map +1 -1
- package/dist/esm/runtime/types.d.ts +22 -0
- package/dist/esm/runtime/types.d.ts.map +1 -1
- package/dist/esm/script/index.d.ts +97 -36
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +211 -22
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +22 -2
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/str/index.d.ts +50 -38
- package/dist/esm/str/index.d.ts.map +1 -1
- package/dist/esm/str/index.js +103 -52
- package/dist/esm/str/index.js.map +1 -1
- package/dist/esm/str/messageformat.d.ts +2 -1
- package/dist/esm/str/messageformat.d.ts.map +1 -1
- package/dist/esm/str/messageformat.js +7 -2
- package/dist/esm/str/messageformat.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts +4 -0
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +38 -4
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/ta/index.d.ts +86 -43
- package/dist/esm/ta/index.d.ts.map +1 -1
- package/dist/esm/ta/index.js +296 -348
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +54 -2
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +166 -71
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/ta-series.d.ts +4 -4
- package/dist/esm/ta-series.d.ts.map +1 -1
- package/dist/esm/ta-series.js +5 -4
- package/dist/esm/ta-series.js.map +1 -1
- package/dist/esm/table/index.d.ts +87 -0
- package/dist/esm/table/index.d.ts.map +1 -0
- package/dist/esm/table/index.js +169 -0
- package/dist/esm/table/index.js.map +1 -0
- package/dist/esm/text/index.d.ts +17 -2
- package/dist/esm/text/index.d.ts.map +1 -1
- package/dist/esm/text/index.js +17 -2
- package/dist/esm/text/index.js.map +1 -1
- package/dist/esm/time/datestring.d.ts +1 -0
- package/dist/esm/time/datestring.d.ts.map +1 -1
- package/dist/esm/time/datestring.js +12 -0
- package/dist/esm/time/datestring.js.map +1 -1
- package/dist/esm/time/index.d.ts.map +1 -1
- package/dist/esm/time/index.js +3 -1
- package/dist/esm/time/index.js.map +1 -1
- package/dist/esm/time/session.d.ts +28 -9
- package/dist/esm/time/session.d.ts.map +1 -1
- package/dist/esm/time/session.js +113 -42
- package/dist/esm/time/session.js.map +1 -1
- package/dist/esm/time/timezone.d.ts +5 -0
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +21 -1
- package/dist/esm/time/timezone.js.map +1 -1
- package/dist/esm/types/index.d.ts +30 -1
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js +14 -0
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/metadata.d.ts +258 -26
- package/dist/esm/types/metadata.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/array/index.ts +364 -150
- package/src/callsite/index.ts +147 -3
- package/src/color/index.ts +15 -5
- package/src/drawing/bar-time.ts +42 -0
- package/src/drawing/output.ts +214 -0
- package/src/drawing/registry.ts +16 -6
- package/src/index.ts +9 -3
- package/src/label/index.ts +2 -1
- package/src/linefill/index.ts +2 -0
- package/src/math/index.ts +256 -74
- package/src/matrix/index.ts +52 -52
- package/src/polyline/index.ts +3 -12
- package/src/runtime/series.ts +22 -1
- package/src/runtime/types.ts +23 -0
- package/src/script/index.ts +307 -25
- package/src/session/calendar.ts +22 -2
- package/src/str/index.ts +95 -51
- package/src/str/messageformat.ts +7 -2
- package/src/str/numberformat.ts +39 -4
- package/src/ta/index.ts +306 -386
- package/src/ta/running-sum.ts +187 -74
- package/src/ta-series.ts +8 -7
- package/src/table/index.ts +221 -0
- package/src/text/index.ts +18 -2
- package/src/time/datestring.ts +10 -0
- package/src/time/index.ts +3 -1
- package/src/time/session.ts +97 -47
- package/src/time/timezone.ts +21 -1
- package/src/types/index.ts +32 -1
- package/src/types/metadata.ts +325 -26
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/array/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Array namespace\n * Mirrors PineScript's array.* functions\n */\n\nimport type { PineArray, int, float, bool, simple_int, color, Line, Box, Label, Linefill } from '../types/index.js';\n\n/**\n * Creates a new array with initial size and default value\n */\nexport function new_array<T>(size: simple_int = 0, initial_value?: T): PineArray<T> {\n const arr: PineArray<T> = [];\n for (let i = 0; i < size; i++) {\n arr.push(initial_value as T);\n }\n return arr;\n}\n\n/**\n * Returns the number of elements in an array\n */\nexport function size<T>(id: PineArray<T>): int {\n return id.length;\n}\n\n/**\n * Returns the element at the specified index\n */\nexport function get<T>(id: PineArray<T>, index: simple_int): T {\n return id[index]!;\n}\n\n/**\n * Sets the value of the element at the specified index\n */\nexport function set<T>(id: PineArray<T>, index: simple_int, value: T): void {\n id[index] = value;\n}\n\n/**\n * Appends a value to the end of the array\n */\nexport function push<T>(id: PineArray<T>, value: T): void {\n id.push(value);\n}\n\n/**\n * Removes and returns the last element of the array\n */\nexport function pop<T>(id: PineArray<T>): T {\n return id.pop() as T;\n}\n\n/**\n * Inserts a value at the beginning of the array\n */\nexport function unshift<T>(id: PineArray<T>, value: T): void {\n id.unshift(value);\n}\n\n/**\n * Removes and returns the first element of the array\n */\nexport function shift<T>(id: PineArray<T>): T {\n return id.shift() as T;\n}\n\n/**\n * Removes all elements from the array\n */\nexport function clear<T>(id: PineArray<T>): void {\n id.length = 0;\n}\n\n/**\n * Inserts a value at the specified index\n */\nexport function insert<T>(id: PineArray<T>, index: simple_int, value: T): void {\n id.splice(index, 0, value);\n}\n\n/**\n * Removes the element at the specified index\n */\nexport function remove<T>(id: PineArray<T>, index: simple_int): T {\n return id.splice(index, 1)[0]!;\n}\n\n/**\n * Returns true if the array contains the value\n */\nexport function includes<T>(id: PineArray<T>, value: T): bool {\n return id.includes(value);\n}\n\n/**\n * Returns the index of the first occurrence of the value\n */\nexport function indexof<T>(id: PineArray<T>, value: T): int {\n return id.indexOf(value);\n}\n\n/**\n * Returns the index of the last occurrence of the value\n */\nexport function lastindexof<T>(id: PineArray<T>, value: T): int {\n return id.lastIndexOf(value);\n}\n\n/**\n * Returns a shallow copy of the array\n */\nexport function copy<T>(id: PineArray<T>): PineArray<T> {\n return [...id] as PineArray<T>;\n}\n\n/**\n * Concatenates two arrays\n */\nexport function concat<T>(id1: PineArray<T>, id2: PineArray<T>): PineArray<T> {\n return [...id1, ...id2] as PineArray<T>;\n}\n\n/**\n * Joins all elements into a string\n */\nexport function join(id: PineArray<any>, separator: string = ','): string {\n return id.join(separator);\n}\n\n/**\n * Reverses the array in place\n */\nexport function reverse<T>(id: PineArray<T>): void {\n id.reverse();\n}\n\n/**\n * Returns a slice of the array\n */\nexport function slice<T>(id: PineArray<T>, index_from: simple_int, index_to?: simple_int): PineArray<T> {\n return id.slice(index_from, index_to) as PineArray<T>;\n}\n\n/**\n * Sorts array elements (numeric)\n */\nexport function sort<T>(id: PineArray<T>, order: 'asc' | 'desc' = 'asc'): void {\n id.sort((a, b) => {\n if (order === 'asc') {\n return (a as any) - (b as any);\n } else {\n return (b as any) - (a as any);\n }\n });\n}\n\n/**\n * Returns the sum of all elements (numeric arrays only)\n */\nexport function sum(id: PineArray<float>): float {\n return id.reduce((acc, val) => acc + val, 0);\n}\n\n/**\n * Average of the non-na elements (PineScript `array.avg(id)`); an array without values gives NaN.\n */\nexport function avg(id: PineArray<float>): float {\n const values = id.filter((v) => !Number.isNaN(v));\n return values.length ? values.reduce((a, b) => a + b, 0) / values.length : NaN;\n}\n\n/**\n * The `nth` smallest value (0 = the smallest) of the non-na elements (PineScript `array.min(id, nth)`).\n * An `nth` past the end gives the greatest value; an array without values gives NaN.\n */\nexport function min(id: PineArray<float>, nth: simple_int = 0): float {\n const values = id.filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);\n return values.length ? values[Math.min(nth, values.length - 1)]! : NaN;\n}\n\n/**\n * The `nth` greatest value (0 = the greatest) of the non-na elements (PineScript `array.max(id, nth)`).\n * An `nth` past the end gives the smallest value; an array without values gives NaN.\n */\nexport function max(id: PineArray<float>, nth: simple_int = 0): float {\n const values = id.filter((v) => !Number.isNaN(v)).sort((a, b) => b - a);\n return values.length ? values[Math.min(nth, values.length - 1)]! : NaN;\n}\n\n/**\n * Returns the median value (numeric arrays only)\n */\nexport function median(id: PineArray<float>): float {\n const sorted = [...id].sort((a, b) => a - b);\n const mid = Math.floor(sorted.length / 2);\n return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;\n}\n\n/**\n * Returns the mode (most frequent value)\n */\nexport function mode(id: PineArray<float>): float {\n const frequency: Map<float, int> = new Map();\n let maxFreq = 0;\n let mode = id[0]!;\n\n for (const val of id) {\n const freq = (frequency.get(val) || 0) + 1;\n frequency.set(val, freq);\n if (freq > maxFreq) {\n maxFreq = freq;\n mode = val;\n }\n }\n\n return mode;\n}\n\n/**\n * Standard deviation of the non-na elements (PineScript `array.stdev(id, biased)`).\n * `biased` true (default) divides by the count, false by the count - 1.\n */\nexport function stdev(id: PineArray<float>, biased: bool = true): float {\n return Math.sqrt(variance(id, biased));\n}\n\n/**\n * Variance of the non-na elements (PineScript `array.variance(id, biased)`).\n * `biased` true (default) divides by the count, false by the count - 1.\n */\nexport function variance(id: PineArray<float>, biased: bool = true): float {\n const values = id.filter((v) => !Number.isNaN(v));\n if (!values.length) return NaN;\n const mean = values.reduce((a, b) => a + b, 0) / values.length;\n const squares = values.reduce((a, b) => a + (b - mean) * (b - mean), 0);\n return squares / (biased ? values.length : values.length - 1);\n}\n\n/**\n * Fills the array with the specified value\n */\nexport function fill<T>(id: PineArray<T>, value: T, index_from: simple_int = 0, index_to?: simple_int): void {\n const end = index_to ?? id.length;\n for (let i = index_from; i < end; i++) {\n id[i] = value;\n }\n}\n\n/**\n * Creates an array from its arguments (PineScript `array.from(arg0, arg1, ...)`).\n * `na` values are kept.\n *\n * @example\n * ```typescript\n * array.from(0.236, 0.382, 0.5, 0.618); // [0.236, 0.382, 0.5, 0.618]\n * ```\n */\nexport function from<T>(...values: T[]): PineArray<T> {\n return [...values];\n}\n\n/**\n * Returns the first element of the array\n *\n * @param id - The array\n * @returns The first element, or undefined if array is empty\n *\n * @remarks\n * - Equivalent to `array.get(id, 0)`\n * - Returns undefined for empty arrays\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const firstElement = array.first(arr); // Returns: 1\n * ```\n */\nexport function first<T>(id: PineArray<T>): T {\n return id[0]!;\n}\n\n/**\n * Returns the last element of the array\n *\n * @param id - The array\n * @returns The last element, or undefined if array is empty\n *\n * @remarks\n * - Equivalent to `array.get(id, array.size(id) - 1)`\n * - Returns undefined for empty arrays\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const lastElement = array.last(arr); // Returns: 5\n * ```\n */\nexport function last<T>(id: PineArray<T>): T {\n return id[id.length - 1]!;\n}\n\n/**\n * True when at least one element of a bool array is true (PineScript `array.some(id)`).\n * An empty array gives false.\n */\nexport function some(id: PineArray<bool>): bool {\n return id.some((v) => v === true);\n}\n\n/**\n * True when every element of a bool array is true (PineScript `array.every(id)`).\n * An empty array gives false, as in PineScript.\n */\nexport function every(id: PineArray<bool>): bool {\n return id.length > 0 && id.every((v) => v === true);\n}\n\n/**\n * Create new boolean array\n *\n * Creates a new array object of bool type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: false)\n * @returns New boolean array\n *\n * @example\n * ```typescript\n * const arr = array.new_bool(5, true);\n * // Creates: [true, true, true, true, true]\n * ```\n */\nexport function new_bool(size: simple_int = 0, initial_value: bool = false): PineArray<bool> {\n return new_array<bool>(size, initial_value);\n}\n\n/**\n * Create new float array\n *\n * Creates a new array object of float type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: NaN)\n * @returns New float array\n *\n * @example\n * ```typescript\n * const arr = array.new_float(5, 0.0);\n * // Creates: [0.0, 0.0, 0.0, 0.0, 0.0]\n * ```\n */\nexport function new_float(size: simple_int = 0, initial_value: float = NaN): PineArray<float> {\n return new_array<float>(size, initial_value);\n}\n\n/**\n * Create new integer array\n *\n * Creates a new array object of int type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: NaN)\n * @returns New integer array\n *\n * @example\n * ```typescript\n * const arr = array.new_int(5, 0);\n * // Creates: [0, 0, 0, 0, 0]\n * ```\n */\nexport function new_int(size: simple_int = 0, initial_value: int = NaN): PineArray<int> {\n return new_array<int>(size, initial_value);\n}\n\n/**\n * Create new string array\n *\n * Creates a new array object of string type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New string array\n *\n * @example\n * ```typescript\n * const arr = array.new_string(5, \"text\");\n * // Creates: [\"text\", \"text\", \"text\", \"text\", \"text\"]\n * ```\n */\nexport function new_string(size: simple_int = 0, initial_value?: string): PineArray<string> {\n return new_array<string>(size, initial_value);\n}\n\n/**\n * Create new color array\n *\n * Creates a new array object of color type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New color array\n *\n * @example\n * ```typescript\n * const arr = array.new_color(5, \"#FF0000\");\n * // Creates: [\"#FF0000\", \"#FF0000\", \"#FF0000\", \"#FF0000\", \"#FF0000\"]\n * ```\n */\nexport function new_color(size: simple_int = 0, initial_value?: color): PineArray<color> {\n return new_array<color>(size, initial_value);\n}\n\n/**\n * Create new line array\n *\n * Creates a new array object of line type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New line array\n *\n * @example\n * ```typescript\n * import { array, line } from 'oakscriptjs';\n *\n * // Create array to store last 15 lines\n * const lines = array.new_line();\n * const trendLine = line.new(0, 100, 50, 150);\n * array.push(lines, trendLine);\n *\n * // Manage line count\n * if (array.size(lines) > 15) {\n * const oldLine = array.shift(lines);\n * }\n * ```\n *\n * @remarks\n * Useful for managing collections of trend lines, support/resistance levels, or channel lines.\n * In PineScript, this is typically used with line.delete() to manage chart object limits.\n */\nexport function new_line(size: simple_int = 0, initial_value?: Line): PineArray<Line> {\n return new_array<Line>(size, initial_value);\n}\n\n/**\n * Create new box array\n *\n * Creates a new array object of box type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New box array\n *\n * @example\n * ```typescript\n * import { array, box } from 'oakscriptjs';\n *\n * // Create array to store gap boxes\n * const gaps = array.new_box();\n * const gapBox = box.new(10, 120, 15, 110);\n * array.push(gaps, gapBox);\n *\n * // Track multiple gaps\n * for (let i = 0; i < array.size(gaps); i++) {\n * const gap = array.get(gaps, i);\n * const gapTop = box.get_top(gap);\n * const gapBottom = box.get_bottom(gap);\n * // Check if gap filled...\n * }\n * ```\n *\n * @remarks\n * Useful for tracking ranges, gaps, consolidation zones, or rectangle patterns.\n * Enables systematic analysis of multiple box objects.\n */\nexport function new_box(size: simple_int = 0, initial_value?: Box): PineArray<Box> {\n return new_array<Box>(size, initial_value);\n}\n\n/**\n * Create new label array\n *\n * Creates a new array object of label type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New label array\n *\n * @example\n * ```typescript\n * import { array, label } from 'oakscriptjs';\n *\n * // Create array to store pivot labels\n * const pivotLabels = array.new_label();\n *\n * // Add labels for pivot highs\n * const pivotLabel = label.new(50, 155.5, 'PH', 'bar_index', 'abovebar');\n * array.push(pivotLabels, pivotLabel);\n *\n * // Limit number of labels shown\n * const maxLabels = 50;\n * if (array.size(pivotLabels) > maxLabels) {\n * array.shift(pivotLabels);\n * }\n * ```\n *\n * @remarks\n * Useful for managing collections of annotations, pivot markers, or signal labels.\n * Helps limit the number of labels displayed by removing old ones.\n */\nexport function new_label(size: simple_int = 0, initial_value?: Label): PineArray<Label> {\n return new_array<Label>(size, initial_value);\n}\n\n/**\n * Create new linefill array\n *\n * Creates a new array object of linefill type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New linefill array\n *\n * @example\n * ```typescript\n * import { array, line, linefill } from 'oakscriptjs';\n *\n * // Create array to store channel fills\n * const channels = array.new_linefill();\n *\n * // Create channel\n * const upperLine = line.new(0, 120, 50, 130);\n * const lowerLine = line.new(0, 100, 50, 110);\n * const channelFill = linefill.new(upperLine, lowerLine, '#0000FF15');\n *\n * array.push(channels, channelFill);\n * ```\n *\n * @remarks\n * Useful for managing collections of channel fills, Bollinger Band fills, or regression channel fills.\n * Enables dynamic color changes across multiple channels.\n */\nexport function new_linefill(size: simple_int = 0, initial_value?: Linefill): PineArray<Linefill> {\n return new_array<Linefill>(size, initial_value);\n}\n\n/**\n * Absolute value of array elements\n *\n * Returns an array containing the absolute value of each element in the original array.\n *\n * @param id - Array of numeric values\n * @returns New array with absolute values\n *\n * @example\n * ```typescript\n * const arr = [-1, -2, 3, -4, 5];\n * const result = array.abs(arr);\n * // Returns: [1, 2, 3, 4, 5]\n * ```\n */\nexport function abs(id: PineArray<float>): PineArray<float> {\n return id.map(x => Math.abs(x)) as PineArray<float>;\n}\n\n/**\n * Range of array values\n *\n * Returns the difference between the maximum and minimum values in the array.\n *\n * @param id - Array of numeric values\n * @returns Difference between max and min (range)\n *\n * @example\n * ```typescript\n * const arr = [1, 5, 3, 9, 2];\n * const result = array.range(arr);\n * // Returns: 8 (9 - 1)\n * ```\n *\n * @remarks\n * Returns NaN if the array is empty.\n */\nexport function range(id: PineArray<float>): float {\n if (id.length === 0) {\n return NaN;\n }\n return max(id) - min(id);\n}\n\n/**\n * Binary Search\n *\n * Searches for a value in a sorted array using binary search algorithm.\n * Returns the index of the value if found, or -1 if not found.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of the value, or -1 if not found\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9]; // Must be sorted\n * const index = array.binary_search(arr, 5);\n * // Returns: 3\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - Uses standard binary search algorithm\n * - Time complexity: O(log n)\n */\nexport function binary_search(id: PineArray<float>, val: float): int {\n let left = 0;\n let right = id.length - 1;\n\n while (left <= right) {\n const mid = Math.floor((left + right) / 2);\n\n if (id[mid]! === val) {\n return mid;\n }\n\n if (id[mid]! < val) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return -1; // Not found\n}\n\n/**\n * Binary Search Leftmost\n *\n * Returns the index of the value if found. When not found, returns the index\n * of the next smallest element to the left of where the value would be.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of value or insertion point to the left\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9];\n * const index = array.binary_search_leftmost(arr, 3);\n * // Returns: 2 (index of 1, which is left of where 3 would be)\n * ```\n *\n * @example\n * ```typescript\n * const arr = [4, 5, 5, 5];\n * const index = array.binary_search_leftmost(arr, 5);\n * // Returns: 1 (index of first instance of 5)\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - For duplicate values, returns leftmost occurrence\n * - When value not found, returns position of next smaller element\n * - PineScript rules: a value below every element gives 0; `na` counts as greater than every number (an array\n * without na gives `size - 1`); the comparison is exact (no 1e-10 tolerance)\n */\nexport function binary_search_leftmost(id: PineArray<float>, val: float): int {\n if (id.length === 0) return -1;\n const at = searchBound(id, val, false);\n return at < id.length && compareSorted(id[at]!, val) === 0 ? at : Math.max(0, at - 1);\n}\n\n/**\n * Order of the PineScript binary searches: numbers in ascending order, na above every number (equal to na).\n */\nfunction compareSorted(a: float, b: float): number {\n const an = Number.isNaN(a);\n const bn = Number.isNaN(b);\n if (an || bn) return an === bn ? 0 : an ? 1 : -1;\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\n/** First index whose element is >= `val` (`after` false) or > `val` (`after` true); `id.length` when none. */\nfunction searchBound(id: PineArray<float>, val: float, after: boolean): int {\n let lo = 0;\n let hi = id.length;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n const c = compareSorted(id[mid]!, val);\n if (c < 0 || (after && c === 0)) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n}\n\n/**\n * Binary Search Rightmost\n *\n * Returns the index of the value if found. When not found, returns the index\n * of the element to the right of where the value would be.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of value or insertion point to the right\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9];\n * const index = array.binary_search_rightmost(arr, 3);\n * // Returns: 3 (index of 5, which is right of where 3 would be)\n * ```\n *\n * @example\n * ```typescript\n * const arr = [4, 5, 5, 5];\n * const index = array.binary_search_rightmost(arr, 5);\n * // Returns: 3 (index of last instance of 5)\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - For duplicate values, returns rightmost occurrence\n * - When value not found, returns position of next larger element\n * - PineScript rules: a value above every element gives `size` (the position after the last element); `na` counts\n * as greater than every number (an array without na gives `size`); the comparison is exact (no 1e-10 tolerance)\n */\nexport function binary_search_rightmost(id: PineArray<float>, val: float): int {\n if (id.length === 0) return -1;\n const at = searchBound(id, val, true);\n return at > 0 && compareSorted(id[at - 1]!, val) === 0 ? at - 1 : at;\n}\n\n/**\n * Covariance\n *\n * Returns the covariance between two arrays.\n *\n * @param id1 - First array\n * @param id2 - Second array\n * @param biased - If true, uses biased estimate (population). If false, uses unbiased estimate (sample). Default: true\n * @returns Covariance between the two arrays\n *\n * @example\n * ```typescript\n * const prices = [100, 102, 98, 105, 103];\n * const volume = [1000, 1200, 950, 1300, 1100];\n * const cov = array.covariance(prices, volume);\n * // Returns covariance between price and volume\n * ```\n *\n * @remarks\n * - Biased (true): divides by n (population covariance)\n * - Unbiased (false): divides by n-1 (sample covariance)\n * - Returns NaN if arrays are empty or have different lengths\n * - Formula: Cov(X,Y) = E[(X - \u03BCX)(Y - \u03BCY)]\n */\nexport function covariance(id1: PineArray<float>, id2: PineArray<float>, biased: bool = true): float {\n if (id1.length === 0 || id2.length === 0) {\n return NaN;\n }\n\n if (id1.length !== id2.length) {\n return NaN;\n }\n\n const n = id1.length;\n const mean1 = avg(id1);\n const mean2 = avg(id2);\n\n let sum = 0;\n for (let i = 0; i < n; i++) {\n sum += (id1[i]! - mean1) * (id2[i]! - mean2);\n }\n\n // Biased: divide by n, Unbiased: divide by n-1\n const divisor = biased ? n : n - 1;\n return sum / divisor;\n}\n\n/**\n * Percentile Linear Interpolation\n *\n * Returns the value for which the specified percentage of array values are\n * less than or equal to it, using linear interpolation.\n *\n * @param id - Array of numeric values\n * @param percentage - Percentage (0-100)\n * @returns Percentile value using linear interpolation\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const p50 = array.percentile_linear_interpolation(arr, 50);\n * // Returns: 3 (median)\n * ```\n *\n * @remarks\n * - Uses linear interpolation between adjacent values\n * - Result may not be a member of the array\n * - Returns NaN if array is empty\n *\n * PineScript rules:\n * - position in the sorted array: `percentage / 100 * size - 0.5`, clamped to the first / last value, linear\n * interpolation between the two neighbours\n * - `na` values are sorted after all numbers; with an `na` in the array, a position between two values gives `na`\n */\nexport function percentile_linear_interpolation(id: PineArray<float>, percentage: float): float {\n if (id.length === 0) {\n return NaN;\n }\n const s = sortedNaLast(id);\n const n = s.length;\n const pos = (percentage / 100) * n - 0.5;\n if (pos <= 0) return s[0]!;\n if (pos >= n - 1) return s[n - 1]!;\n const lo = Math.floor(pos);\n const f = pos - lo;\n if (f === 0) return s[lo]!;\n return Number.isNaN(s[n - 1]!) ? NaN : s[lo]! + f * (s[lo + 1]! - s[lo]!);\n}\n\n/** Array values sorted ascending, `na` values after the numbers (array.percentile_*). */\nfunction sortedNaLast(id: PineArray<float>): number[] {\n const numbers = id.filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);\n return [...numbers, ...Array<number>(id.length - numbers.length).fill(NaN)];\n}\n\n/**\n * Percentile Nearest Rank\n *\n * Returns the value for which the specified percentage of array values are\n * less than or equal to it, using the nearest-rank method.\n *\n * @param id - Array of numeric values\n * @param percentage - Percentage (0-100)\n * @returns Percentile value using nearest rank\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const p50 = array.percentile_nearest_rank(arr, 50);\n * // Returns: 3 (median, always a member of array)\n * ```\n *\n * @remarks\n * - Result is always a member of the array\n * - Uses nearest-rank method (no interpolation)\n * - Returns NaN if array is empty\n *\n * PineScript rules:\n * - the value at rank `ceil(percentage / 100 * size)` of the sorted array\n * - `na` values are sorted after all numbers (na when that rank holds an `na`)\n */\nexport function percentile_nearest_rank(id: PineArray<float>, percentage: float): float {\n if (id.length === 0) {\n return NaN;\n }\n const s = sortedNaLast(id);\n const rank = Math.min(s.length, Math.max(1, Math.ceil((percentage / 100) * s.length)));\n return s[rank - 1]!;\n}\n\n/**\n * Percent Rank\n *\n * Returns the percentile rank of the element at the specified index.\n * The percentile rank is the percentage of elements that are less than\n * or equal to the reference value.\n *\n * @param id - Array of numeric values\n * @param index - Index of element to rank\n * @returns Percentile rank (0-100)\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const rank = array.percentrank(arr, 2); // Value at index 2 is 3\n * // Returns: 40 (40% of values <= 3)\n * ```\n *\n * @remarks\n * - Returns percentage of elements <= the value at given index\n * - Range: 0 to 100\n * - Returns NaN if array is empty or index out of bounds\n */\nexport function percentrank(id: PineArray<float>, index: int): float {\n if (id.length === 0 || index < 0 || index >= id.length) {\n return NaN;\n }\n\n const value = id[index]!;\n\n if (isNaN(value)) {\n return NaN;\n }\n\n // Count elements less than or equal to the value\n let count = 0;\n for (let i = 0; i < id.length; i++) {\n if (!isNaN(id[i]!) && id[i]! <= value) {\n count++;\n }\n }\n\n // Calculate percentage\n return (count / id.length) * 100;\n}\n\n/**\n * Sort Indices\n *\n * Returns an array of indices which, when used to index the original array,\n * will access its elements in their sorted order. Does not modify the original array.\n *\n * @param id - Array to get sorted indices from\n * @param order - Sort order: 'asc' or 'desc' (default: 'asc')\n * @returns Array of indices in sorted order\n *\n * @example\n * ```typescript\n * const arr = [5, -2, 0, 9, 1];\n * const indices = array.sort_indices(arr); // [1, 2, 4, 0, 3]\n * // arr[1] = -2 (smallest)\n * // arr[2] = 0\n * // arr[4] = 1\n * // arr[0] = 5\n * // arr[3] = 9 (largest)\n * ```\n *\n * @remarks\n * - Original array is not modified\n * - Returns indices that would sort the array\n * - Useful for maintaining correspondence with other arrays\n */\nexport function sort_indices(id: PineArray<float>, order: 'asc' | 'desc' = 'asc'): PineArray<int> {\n // Create array of indices\n const indices: PineArray<int> = Array.from({ length: id.length }, (_, i) => i) as PineArray<int>;\n\n // Sort indices based on values in original array\n indices.sort((a, b) => {\n if (order === 'asc') {\n return id[a]! - id[b]!;\n } else {\n return id[b]! - id[a]!;\n }\n });\n\n return indices;\n}\n\n/**\n * Standardize\n *\n * Returns an array of standardized elements (z-score normalization).\n * Each element is transformed to (value - mean) / stddev.\n *\n * @param id - Array of numeric values\n * @returns Array of standardized values (z-scores)\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const standardized = array.standardize(arr);\n * // Mean = 3, StdDev \u2248 1.414\n * // Result \u2248 [-1.414, -0.707, 0, 0.707, 1.414]\n * ```\n *\n * @remarks\n * - Formula: z = (x - \u03BC) / \u03C3\n * - Result has mean of 0 and standard deviation of 1\n * - Returns array of NaN if standard deviation is 0\n * - Useful for comparing values on different scales\n */\nexport function standardize(id: PineArray<float>): PineArray<float> {\n if (id.length === 0) {\n return [] as PineArray<float>;\n }\n\n const mean = avg(id);\n const stdDev = stdev(id);\n\n // If stddev is 0, all values are the same - return array of 0s or NaN\n if (stdDev === 0) {\n return id.map(() => NaN) as PineArray<float>;\n }\n\n // Calculate z-score for each element\n return id.map(value => (value - mean) / stdDev) as PineArray<float>;\n}\n\n/**\n * New Type (User-Defined Type Array)\n *\n * Creates a new array of user-defined type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements\n * @returns New array of user-defined type\n *\n * @example\n * ```typescript\n * // NOTE: User-defined types (UDTs) are not fully supported yet\n * // This is a placeholder for future implementation\n * const arr = array.newtype<MyType>(5);\n * ```\n *\n * @remarks\n * - **\u26A0\uFE0F LIMITED SUPPORT**: User-defined types (UDTs) require a type system\n * that is not yet fully implemented in this library\n * - This function currently works like `new_array<T>()` for basic types\n * - Full UDT support will be added in a future version\n * - See PineScript documentation for UDT usage patterns\n */\nexport function newtype<T>(size: simple_int = 0, initial_value?: T): PineArray<T> {\n // For now, this is just an alias for new_array\n // Full UDT support requires implementing the type system\n return new_array<T>(size, initial_value);\n}\n\n/**\n * \u26A0\uFE0F RENDERING FUNCTIONS NOT IMPLEMENTED\n *\n * The following array constructor functions are intentionally excluded\n * because they create arrays for rendering objects, which are outside\n * the scope of this calculation-focused library:\n *\n * - `new_box()` - Creates arrays of box drawing objects\n * - `new_label()` - Creates arrays of label objects\n * - `new_line()` - Creates arrays of line drawing objects\n * - `new_linefill()` - Creates arrays of line fill objects\n * - `new_table()` - Creates arrays of table layout objects\n *\n * **Design Constraint**: This library focuses on calculation and indicator\n * functions only. Rendering, visualization, and UI functions require a\n * rendering engine and are not included.\n *\n * If you need these functions, consider:\n * 1. Using PineScript directly\n * 2. Implementing a rendering layer separately\n * 3. Using this library for calculations and another library for visualization\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;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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,S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S,QAAWF,QAAmB,GAAG,eAAiC;AAGhF,SAAO,UAAaA,OAAM,aAAa;AACzC;",
|
|
6
|
-
"names": ["size", "
|
|
4
|
+
"sourcesContent": ["/**\n * Array namespace\n * Mirrors PineScript's array.* functions\n */\n\nimport type { PineArray, int, float, bool, simple_int, color, Line, Box, Label, Linefill, Table } from '../types/index.js';\n\n/**\n * Creates a new array with initial size and default value\n */\nexport function new_array<T>(size: simple_int = 0, initial_value?: T): PineArray<T> {\n const arr: PineArray<T> = [];\n for (let i = 0; i < size; i++) {\n arr.push(initial_value as T);\n }\n return arr;\n}\n\n/**\n * Returns the number of elements in an array\n */\nexport function size<T>(id: PineArray<T>): int {\n return id.length;\n}\n\n/** True for a PineScript `na` element: NaN, null or undefined. */\nfunction isNa(v: unknown): boolean {\n return v === undefined || v === null || (typeof v === 'number' && Number.isNaN(v));\n}\n\n/** The `na` of the array's element type: NaN for a numeric (or empty) array, undefined otherwise. */\nfunction naOf<T>(id: PineArray<T>): T {\n return (id.length === 0 || typeof id[0] === 'number' ? NaN : undefined) as T;\n}\n\n/**\n * Position addressed by a PineScript index: truncated, a negative index counts from the end. An index outside\n * `-size .. size - 1 + extra` is a runtime error.\n */\nfunction position(name: string, id: PineArray<unknown>, index: number, extra = 0): number {\n const i = Math.trunc(index);\n const n = id.length;\n if (i < -n || i >= n + extra) {\n throw new Error(`array.${name}: index ${i} is out of bounds, array size is ${n}`);\n }\n return i < 0 ? i + n : i;\n}\n\n/**\n * Returns the element at the specified index.\n *\n * PineScript rules: a negative index counts from the end (`get(a, -1)` is the last element); an index outside\n * `-size .. size - 1` is a runtime error (throws); an `na` index gives `na`.\n */\nexport function get<T>(id: PineArray<T>, index: simple_int): T {\n if (Number.isNaN(index)) return naOf(id);\n return id[position('get', id, index)]!;\n}\n\n/**\n * Sets the value of the element at the specified index.\n *\n * PineScript rules: a negative index counts from the end; an index outside `-size .. size - 1` is a runtime error\n * (throws); an `na` index does nothing.\n */\nexport function set<T>(id: PineArray<T>, index: simple_int, value: T): void {\n if (Number.isNaN(index)) return;\n id[position('set', id, index)] = value;\n}\n\n/**\n * Appends a value to the end of the array\n */\nexport function push<T>(id: PineArray<T>, value: T): void {\n id.push(value);\n}\n\n/**\n * Removes and returns the last element of the array\n */\nexport function pop<T>(id: PineArray<T>): T {\n return id.pop() as T;\n}\n\n/**\n * Inserts a value at the beginning of the array\n */\nexport function unshift<T>(id: PineArray<T>, value: T): void {\n id.unshift(value);\n}\n\n/**\n * Removes and returns the first element of the array\n */\nexport function shift<T>(id: PineArray<T>): T {\n return id.shift() as T;\n}\n\n/**\n * Removes all elements from the array\n */\nexport function clear<T>(id: PineArray<T>): void {\n id.length = 0;\n}\n\n/**\n * Inserts a value at the specified index.\n *\n * PineScript rules: a negative index counts from the end and `size` appends, so `-size .. size` is valid; any other\n * index is a runtime error (throws); an `na` index appends.\n */\nexport function insert<T>(id: PineArray<T>, index: simple_int, value: T): void {\n if (Number.isNaN(index)) {\n id.push(value);\n return;\n }\n id.splice(position('insert', id, index, 1), 0, value);\n}\n\n/**\n * Removes the element at the specified index and returns it.\n *\n * PineScript rules: a negative index counts from the end; an index outside `-size .. size - 1` is a runtime error\n * (throws); an `na` index removes nothing and gives `na`.\n */\nexport function remove<T>(id: PineArray<T>, index: simple_int): T {\n if (Number.isNaN(index)) return naOf(id);\n return id.splice(position('remove', id, index), 1)[0]!;\n}\n\n/** PineScript `==` of two elements: numbers are equal when |a - b| <= 1e-10; `na` never equals anything. */\nfunction pineEqual(a: unknown, b: unknown): boolean {\n if (isNa(a) || isNa(b)) return false;\n if (a === b) return true;\n if (typeof a === 'number' && typeof b === 'number') {\n const d = a - b;\n return -1e-10 <= d && d <= 1e-10;\n }\n return false;\n}\n\n/**\n * Returns true if the array contains the value.\n *\n * PineScript rules: numbers are compared with a 1e-10 tolerance (|a - b| <= 1e-10); `na` is never found.\n */\nexport function includes<T>(id: PineArray<T>, value: T): bool {\n return indexof(id, value) >= 0;\n}\n\n/**\n * Returns the index of the first occurrence of the value, -1 when not found.\n *\n * PineScript rules: numbers are compared with a 1e-10 tolerance (|a - b| <= 1e-10); `na` is never found.\n */\nexport function indexof<T>(id: PineArray<T>, value: T): int {\n for (let i = 0; i < id.length; i++) {\n if (pineEqual(id[i], value)) return i;\n }\n return -1;\n}\n\n/**\n * Returns the index of the last occurrence of the value, -1 when not found.\n *\n * PineScript rules: numbers are compared with a 1e-10 tolerance (|a - b| <= 1e-10); `na` is never found.\n */\nexport function lastindexof<T>(id: PineArray<T>, value: T): int {\n for (let i = id.length - 1; i >= 0; i--) {\n if (pineEqual(id[i], value)) return i;\n }\n return -1;\n}\n\n/**\n * Returns a shallow copy of the array\n */\nexport function copy<T>(id: PineArray<T>): PineArray<T> {\n return [...id] as PineArray<T>;\n}\n\n/**\n * Appends the elements of `id2` to `id1` and returns `id1` (PineScript `array.concat` changes `id1`).\n */\nexport function concat<T>(id1: PineArray<T>, id2: PineArray<T>): PineArray<T> {\n for (const v of Array.from(id2)) id1.push(v);\n return id1;\n}\n\n/**\n * Joins all elements into a string\n */\nexport function join(id: PineArray<any>, separator: string = ','): string {\n return id.join(separator);\n}\n\n/**\n * Reverses the array in place\n */\nexport function reverse<T>(id: PineArray<T>): void {\n id.reverse();\n}\n\n/**\n * Returns the elements from `index_from` (included) to `index_to` (excluded, default: the size) as a live view of\n * the array (PineScript `array.slice`).\n *\n * PineScript rules:\n * - the slice shares the elements of the original array: a change of an element of the slice changes the original\n * array, and adding / removing elements of the slice adds / removes them in the original array, inside the slice\n * bounds (a push to the slice inserts at the end of the slice)\n * - `index_from` must address an element (`0 .. size - 1`) and `index_to` may be `size`; other bounds, or\n * `index_from > index_to`, are a runtime error (throws); an `na` bound means the start / the end of the array\n */\nexport function slice<T>(id: PineArray<T>, index_from: simple_int, index_to?: simple_int): PineArray<T> {\n const n = id.length;\n const start = Number.isNaN(index_from) ? 0 : Math.trunc(index_from);\n const stop = index_to === undefined || Number.isNaN(index_to) ? n : Math.trunc(index_to);\n if (start < 0 || start >= n) throw new Error(`array.slice: index ${start} is out of bounds, array size is ${n}`);\n if (stop < 0 || stop > n) throw new Error(`array.slice: index ${stop} is out of bounds, array size is ${n}`);\n if (start > stop) throw new Error(`array.slice: index_from ${start} is greater than index_to ${stop}`);\n return sliceView(id, start, stop - start);\n}\n\n/** Index of a property key that is an array index, -1 otherwise. */\nfunction indexKey(p: string | symbol): number {\n if (typeof p !== 'string') return -1;\n const i = Number(p);\n return Number.isInteger(i) && i >= 0 && String(i) === p ? i : -1;\n}\n\n/**\n * Live view of `size` elements of `parent` from `start`: an array (a Proxy) whose element reads and writes go to\n * `parent`, and whose size changes insert / remove elements in `parent` at the end of the view.\n */\nfunction sliceView<T>(parent: PineArray<T>, start: number, size: number): PineArray<T> {\n const resize = (target: number): void => {\n if (target > size) parent.splice(start + size, 0, ...Array<T>(target - size).fill(undefined as T));\n else if (target < size) parent.splice(start + target, size - target);\n size = target;\n };\n return new Proxy<T[]>([], {\n get(target, p, receiver) {\n const i = indexKey(p);\n if (i >= 0) return i < size ? parent[start + i] : undefined;\n if (p === 'length') return size;\n return Reflect.get(target, p, receiver);\n },\n set(target, p, value, receiver) {\n const i = indexKey(p);\n if (i >= 0) {\n if (i >= size) resize(i + 1);\n parent[start + i] = value;\n return true;\n }\n if (p === 'length') {\n resize(Number(value));\n return true;\n }\n return Reflect.set(target, p, value, receiver);\n },\n has(target, p) {\n const i = indexKey(p);\n return i >= 0 ? i < size : Reflect.has(target, p);\n },\n // Array methods delete the slots past the new end before they set the length, which does the removal.\n deleteProperty(target, p) {\n return indexKey(p) >= 0 ? true : Reflect.deleteProperty(target, p);\n },\n ownKeys(target) {\n return [...Array.from({ length: size }, (_, i) => String(i)), ...Reflect.ownKeys(target)];\n },\n getOwnPropertyDescriptor(target, p) {\n const i = indexKey(p);\n if (i >= 0) {\n return i < size ? { value: parent[start + i], writable: true, enumerable: true, configurable: true } : undefined;\n }\n if (p === 'length') return { value: size, writable: true, enumerable: false, configurable: false };\n return Reflect.getOwnPropertyDescriptor(target, p);\n },\n });\n}\n\n/** Sort order of array.sort / array.sort_indices (PineScript `order.ascending` / `order.descending`). */\nexport type SortOrder = 'asc' | 'desc' | 'ascending' | 'descending';\n\n/**\n * Positions of the elements in PineScript sorted order: ascending values, `na` after the numbers (before the\n * strings of a string array), stable; descending is the ascending result reversed.\n */\nfunction sortedPositions<T>(id: PineArray<T>, order: SortOrder): number[] {\n const values: number[] = [];\n const nas: number[] = [];\n for (let i = 0; i < id.length; i++) (isNa(id[i]) ? nas : values).push(i);\n values.sort((a, b) => {\n const x = id[a] as unknown as number;\n const y = id[b] as unknown as number;\n return x < y ? -1 : x > y ? 1 : 0;\n });\n const naFirst = values.length > 0 && typeof id[values[0]!] === 'string';\n const positions = naFirst ? [...nas, ...values] : [...values, ...nas];\n if (order === 'desc' || order === 'descending') positions.reverse();\n return positions;\n}\n\n/**\n * Sorts the array in place.\n *\n * PineScript rules: `na` goes after the numbers in ascending order and before them in descending order (the\n * ascending result reversed); in a string array `na` goes first in ascending order.\n */\nexport function sort<T>(id: PineArray<T>, order: SortOrder = 'asc'): void {\n const sorted = sortedPositions(id, order).map((p) => id[p]!);\n for (let i = 0; i < sorted.length; i++) id[i] = sorted[i]!;\n}\n\n/**\n * Sum of the non-na elements (PineScript `array.sum(id)`), added from left to right; an array without values gives NaN.\n * As PineScript, the running sum is set to 0 after a step when its absolute value is <= 1e-10.\n */\nexport function sum(id: PineArray<float>): float {\n let total = 0;\n let count = 0;\n for (const v of id) {\n if (Number.isNaN(v)) continue;\n count++;\n total += v;\n if (Math.abs(total) <= 1e-10) total = 0;\n }\n return count ? total : NaN;\n}\n\n/**\n * Average of the non-na elements (PineScript `array.avg(id)`); an array without values gives NaN.\n */\nexport function avg(id: PineArray<float>): float {\n const values = id.filter((v) => !Number.isNaN(v));\n return values.length ? values.reduce((a, b) => a + b, 0) / values.length : NaN;\n}\n\n/** The `nth` value of the non-na elements sorted with `compare` (array.min / array.max). */\nfunction nthValue(name: string, id: PineArray<float>, nth: number, compare: (a: number, b: number) => number): float {\n const rank = Number.isNaN(nth) ? 0 : Math.trunc(nth);\n const values = id.filter((v) => !Number.isNaN(v));\n if (!values.length) return NaN;\n if (rank < 0 || rank >= id.length) {\n throw new Error(`array.${name}: nth ${rank} is out of bounds, array size is ${id.length}`);\n }\n values.sort(compare);\n return values[Math.min(rank, values.length - 1)]!;\n}\n\n/**\n * The `nth` smallest value (0 = the smallest) of the non-na elements (PineScript `array.min(id, nth)`).\n *\n * PineScript rules: an `nth` past the non-na values but inside the array size gives the greatest value; an `nth`\n * outside `0 .. size - 1` is a runtime error (throws); an `na` nth counts as 0; an array without values gives NaN.\n */\nexport function min(id: PineArray<float>, nth: simple_int = 0): float {\n return nthValue('min', id, nth, (a, b) => a - b);\n}\n\n/**\n * The `nth` greatest value (0 = the greatest) of the non-na elements (PineScript `array.max(id, nth)`).\n *\n * PineScript rules: an `nth` past the non-na values but inside the array size gives the smallest value; an `nth`\n * outside `0 .. size - 1` is a runtime error (throws); an `na` nth counts as 0; an array without values gives NaN.\n */\nexport function max(id: PineArray<float>, nth: simple_int = 0): float {\n return nthValue('max', id, nth, (a, b) => b - a);\n}\n\n/**\n * Median of the non-na elements (PineScript `array.median(id)`): the middle value, or the mean of the two middle\n * values for an even count; an array without values gives NaN.\n */\nexport function median(id: PineArray<float>): float {\n const sorted = id.filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);\n if (!sorted.length) return NaN;\n const mid = Math.floor(sorted.length / 2);\n return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;\n}\n\n/**\n * Most frequent non-na element (PineScript `array.mode(id)`), compared exactly.\n *\n * PineScript rules: with several values of the same frequency, the smallest one; an array without values gives NaN.\n */\nexport function mode(id: PineArray<float>): float {\n const frequency = new Map<float, int>();\n for (const v of id) {\n if (!Number.isNaN(v)) frequency.set(v, (frequency.get(v) ?? 0) + 1);\n }\n let best = NaN;\n let bestCount = 0;\n for (const [v, count] of frequency) {\n if (count > bestCount || (count === bestCount && v < best)) {\n best = v;\n bestCount = count;\n }\n }\n return best;\n}\n\n/**\n * Standard deviation of the non-na elements (PineScript `array.stdev(id, biased)`): the square root of\n * `array.variance(id, biased)`.\n */\nexport function stdev(id: PineArray<float>, biased: bool = true): float {\n return Math.sqrt(variance(id, biased));\n}\n\n/** Sum of the values and sum of their squares, both added from the first to the last value. */\nfunction moments(values: number[]): [number, number] {\n let p = 0;\n let q = 0;\n for (const v of values) p += v;\n for (const v of values) q += v * v;\n return [p, q];\n}\n\n/**\n * Variance of the non-na elements (PineScript `array.variance(id, biased)`).\n *\n * PineScript rule (moment form): with `p` the sum of the values, `q` the sum of their squares, `n` the count and\n * `m = p / n`: biased (default) `max(0, q / n - m * m)`, unbiased `max(0, q / (n - 1) - (p / (n - 1)) * m)`.\n * One value gives 0 biased and NaN unbiased; an array without values gives NaN.\n */\nexport function variance(id: PineArray<float>, biased: bool = true): float {\n const values = id.filter((v) => !Number.isNaN(v));\n const n = values.length;\n if (!n) return NaN;\n if (n < 2) return biased ? 0 : NaN;\n const [p, q] = moments(values);\n const mean = p / n;\n return biased ? Math.max(0, q / n - mean * mean) : Math.max(0, q / (n - 1) - (p / (n - 1)) * mean);\n}\n\n/**\n * Fills the array with the specified value\n */\nexport function fill<T>(id: PineArray<T>, value: T, index_from: simple_int = 0, index_to?: simple_int): void {\n // PineScript: an na bound is the start / the end; a bound past the end is a runtime error\n const start = index_from === undefined || index_from === null || Number.isNaN(index_from) ? 0 : index_from;\n const end = index_to === undefined || index_to === null || Number.isNaN(index_to) ? id.length : index_to;\n for (const i of [start, end]) {\n if (i < 0 || i > id.length) throw new Error(`array.fill: index ${i} is out of bounds, array size is ${id.length}`);\n }\n for (let i = start; i < end; i++) {\n id[i] = value;\n }\n}\n\n/**\n * Creates an array from its arguments (PineScript `array.from(arg0, arg1, ...)`).\n * `na` values are kept.\n *\n * @example\n * ```typescript\n * array.from(0.236, 0.382, 0.5, 0.618); // [0.236, 0.382, 0.5, 0.618]\n * ```\n */\nexport function from<T>(...values: T[]): PineArray<T> {\n return [...values];\n}\n\n/**\n * Returns the first element of the array\n *\n * @param id - The array\n * @returns The first element, or undefined if array is empty\n *\n * @remarks\n * - Equivalent to `array.get(id, 0)`\n * - Returns undefined for empty arrays\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const firstElement = array.first(arr); // Returns: 1\n * ```\n */\nexport function first<T>(id: PineArray<T>): T {\n // PineScript: a runtime error on an empty array\n if (id.length === 0) throw new Error('Cannot call array.first() if array is empty');\n return id[0]!;\n}\n\n/**\n * Returns the last element of the array\n *\n * @param id - The array\n * @returns The last element, or undefined if array is empty\n *\n * @remarks\n * - Equivalent to `array.get(id, array.size(id) - 1)`\n * - Returns undefined for empty arrays\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const lastElement = array.last(arr); // Returns: 5\n * ```\n */\nexport function last<T>(id: PineArray<T>): T {\n // PineScript: a runtime error on an empty array (as array.first)\n if (id.length === 0) throw new Error('Cannot call array.last() if array is empty');\n return id[id.length - 1]!;\n}\n\n/**\n * True when at least one element of a bool array is true (PineScript `array.some(id)`).\n * An empty array gives false.\n */\nexport function some(id: PineArray<bool>): bool {\n return id.some((v) => v === true);\n}\n\n/**\n * True when every element of a bool array is true (PineScript `array.every(id)`).\n * An empty array gives false, as in PineScript.\n */\nexport function every(id: PineArray<bool>): bool {\n return id.length > 0 && id.every((v) => v === true);\n}\n\n/**\n * Create new boolean array\n *\n * Creates a new array object of bool type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: false)\n * @returns New boolean array\n *\n * @example\n * ```typescript\n * const arr = array.new_bool(5, true);\n * // Creates: [true, true, true, true, true]\n * ```\n */\nexport function new_bool(size: simple_int = 0, initial_value: bool = false): PineArray<bool> {\n return new_array<bool>(size, initial_value);\n}\n\n/**\n * Create new float array\n *\n * Creates a new array object of float type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: NaN)\n * @returns New float array\n *\n * @example\n * ```typescript\n * const arr = array.new_float(5, 0.0);\n * // Creates: [0.0, 0.0, 0.0, 0.0, 0.0]\n * ```\n */\nexport function new_float(size: simple_int = 0, initial_value: float = NaN): PineArray<float> {\n return new_array<float>(size, initial_value);\n}\n\n/**\n * Create new integer array\n *\n * Creates a new array object of int type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: NaN)\n * @returns New integer array\n *\n * @example\n * ```typescript\n * const arr = array.new_int(5, 0);\n * // Creates: [0, 0, 0, 0, 0]\n * ```\n */\nexport function new_int(size: simple_int = 0, initial_value: int = NaN): PineArray<int> {\n return new_array<int>(size, initial_value);\n}\n\n/**\n * Create new string array\n *\n * Creates a new array object of string type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New string array\n *\n * @example\n * ```typescript\n * const arr = array.new_string(5, \"text\");\n * // Creates: [\"text\", \"text\", \"text\", \"text\", \"text\"]\n * ```\n */\nexport function new_string(size: simple_int = 0, initial_value?: string): PineArray<string> {\n return new_array<string>(size, initial_value);\n}\n\n/**\n * Create new color array\n *\n * Creates a new array object of color type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New color array\n *\n * @example\n * ```typescript\n * const arr = array.new_color(5, \"#FF0000\");\n * // Creates: [\"#FF0000\", \"#FF0000\", \"#FF0000\", \"#FF0000\", \"#FF0000\"]\n * ```\n */\nexport function new_color(size: simple_int = 0, initial_value?: color): PineArray<color> {\n return new_array<color>(size, initial_value);\n}\n\n/**\n * Create new line array\n *\n * Creates a new array object of line type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New line array\n *\n * @example\n * ```typescript\n * import { array, line } from 'oakscriptjs';\n *\n * // Create array to store last 15 lines\n * const lines = array.new_line();\n * const trendLine = line.new(0, 100, 50, 150);\n * array.push(lines, trendLine);\n *\n * // Manage line count\n * if (array.size(lines) > 15) {\n * const oldLine = array.shift(lines);\n * }\n * ```\n *\n * @remarks\n * Useful for managing collections of trend lines, support/resistance levels, or channel lines.\n * In PineScript, this is typically used with line.delete() to manage chart object limits.\n */\nexport function new_line(size: simple_int = 0, initial_value?: Line): PineArray<Line> {\n return new_array<Line>(size, initial_value);\n}\n\n/**\n * Create new box array\n *\n * Creates a new array object of box type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New box array\n *\n * @example\n * ```typescript\n * import { array, box } from 'oakscriptjs';\n *\n * // Create array to store gap boxes\n * const gaps = array.new_box();\n * const gapBox = box.new(10, 120, 15, 110);\n * array.push(gaps, gapBox);\n *\n * // Track multiple gaps\n * for (let i = 0; i < array.size(gaps); i++) {\n * const gap = array.get(gaps, i);\n * const gapTop = box.get_top(gap);\n * const gapBottom = box.get_bottom(gap);\n * // Check if gap filled...\n * }\n * ```\n *\n * @remarks\n * Useful for tracking ranges, gaps, consolidation zones, or rectangle patterns.\n * Enables systematic analysis of multiple box objects.\n */\nexport function new_box(size: simple_int = 0, initial_value?: Box): PineArray<Box> {\n return new_array<Box>(size, initial_value);\n}\n\n/**\n * Create new label array\n *\n * Creates a new array object of label type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New label array\n *\n * @example\n * ```typescript\n * import { array, label } from 'oakscriptjs';\n *\n * // Create array to store pivot labels\n * const pivotLabels = array.new_label();\n *\n * // Add labels for pivot highs\n * const pivotLabel = label.new(50, 155.5, 'PH', 'bar_index', 'abovebar');\n * array.push(pivotLabels, pivotLabel);\n *\n * // Limit number of labels shown\n * const maxLabels = 50;\n * if (array.size(pivotLabels) > maxLabels) {\n * array.shift(pivotLabels);\n * }\n * ```\n *\n * @remarks\n * Useful for managing collections of annotations, pivot markers, or signal labels.\n * Helps limit the number of labels displayed by removing old ones.\n */\nexport function new_label(size: simple_int = 0, initial_value?: Label): PineArray<Label> {\n return new_array<Label>(size, initial_value);\n}\n\n/**\n * Create new linefill array\n *\n * Creates a new array object of linefill type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New linefill array\n *\n * @example\n * ```typescript\n * import { array, line, linefill } from 'oakscriptjs';\n *\n * // Create array to store channel fills\n * const channels = array.new_linefill();\n *\n * // Create channel\n * const upperLine = line.new(0, 120, 50, 130);\n * const lowerLine = line.new(0, 100, 50, 110);\n * const channelFill = linefill.new(upperLine, lowerLine, '#0000FF15');\n *\n * array.push(channels, channelFill);\n * ```\n *\n * @remarks\n * Useful for managing collections of channel fills, Bollinger Band fills, or regression channel fills.\n * Enables dynamic color changes across multiple channels.\n */\nexport function new_linefill(size: simple_int = 0, initial_value?: Linefill): PineArray<Linefill> {\n return new_array<Linefill>(size, initial_value);\n}\n\n/**\n * Creates a new array of tables (PineScript `array.new_table`).\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements (default: undefined)\n * @returns New table array\n */\nexport function new_table(size: simple_int = 0, initial_value?: Table): PineArray<Table> {\n return new_array<Table>(size, initial_value);\n}\n\n/**\n * Absolute value of array elements\n *\n * Returns an array containing the absolute value of each element in the original array.\n *\n * @param id - Array of numeric values\n * @returns New array with absolute values\n *\n * @example\n * ```typescript\n * const arr = [-1, -2, 3, -4, 5];\n * const result = array.abs(arr);\n * // Returns: [1, 2, 3, 4, 5]\n * ```\n */\nexport function abs(id: PineArray<float>): PineArray<float> {\n return id.map(x => Math.abs(x)) as PineArray<float>;\n}\n\n/**\n * Range of array values\n *\n * Returns the difference between the maximum and minimum values in the array.\n *\n * @param id - Array of numeric values\n * @returns Difference between max and min (range)\n *\n * @example\n * ```typescript\n * const arr = [1, 5, 3, 9, 2];\n * const result = array.range(arr);\n * // Returns: 8 (9 - 1)\n * ```\n *\n * @remarks\n * Returns NaN if the array is empty.\n */\nexport function range(id: PineArray<float>): float {\n if (id.length === 0) {\n return NaN;\n }\n return max(id) - min(id);\n}\n\n/**\n * Binary Search\n *\n * Searches for a value in a sorted array using binary search algorithm.\n * Returns the index of the value if found, or -1 if not found.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of the value, or -1 if not found\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9]; // Must be sorted\n * const index = array.binary_search(arr, 5);\n * // Returns: 3\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - Uses standard binary search algorithm\n * - Time complexity: O(log n)\n */\nexport function binary_search(id: PineArray<float>, val: float): int {\n let left = 0;\n let right = id.length - 1;\n\n while (left <= right) {\n const mid = Math.floor((left + right) / 2);\n\n if (id[mid]! === val) {\n return mid;\n }\n\n if (id[mid]! < val) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return -1; // Not found\n}\n\n/**\n * Binary Search Leftmost\n *\n * Returns the index of the value if found. When not found, returns the index\n * of the next smallest element to the left of where the value would be.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of value or insertion point to the left\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9];\n * const index = array.binary_search_leftmost(arr, 3);\n * // Returns: 2 (index of 1, which is left of where 3 would be)\n * ```\n *\n * @example\n * ```typescript\n * const arr = [4, 5, 5, 5];\n * const index = array.binary_search_leftmost(arr, 5);\n * // Returns: 1 (index of first instance of 5)\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - For duplicate values, returns leftmost occurrence\n * - When value not found, returns position of next smaller element\n * - PineScript rules: a value below every element gives 0; `na` counts as greater than every number (an array\n * without na gives `size - 1`); the comparison is exact (no 1e-10 tolerance)\n */\nexport function binary_search_leftmost(id: PineArray<float>, val: float): int {\n if (id.length === 0) return -1;\n const at = searchBound(id, val, false);\n return at < id.length && compareSorted(id[at]!, val) === 0 ? at : Math.max(0, at - 1);\n}\n\n/**\n * Order of the PineScript binary searches: numbers in ascending order, na above every number (equal to na).\n */\nfunction compareSorted(a: float, b: float): number {\n const an = Number.isNaN(a);\n const bn = Number.isNaN(b);\n if (an || bn) return an === bn ? 0 : an ? 1 : -1;\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\n/** First index whose element is >= `val` (`after` false) or > `val` (`after` true); `id.length` when none. */\nfunction searchBound(id: PineArray<float>, val: float, after: boolean): int {\n let lo = 0;\n let hi = id.length;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n const c = compareSorted(id[mid]!, val);\n if (c < 0 || (after && c === 0)) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n}\n\n/**\n * Binary Search Rightmost\n *\n * Returns the index of the value if found. When not found, returns the index\n * of the element to the right of where the value would be.\n *\n * @param id - Sorted array (ascending order)\n * @param val - Value to search for\n * @returns Index of value or insertion point to the right\n *\n * @example\n * ```typescript\n * const arr = [-2, 0, 1, 5, 9];\n * const index = array.binary_search_rightmost(arr, 3);\n * // Returns: 3 (index of 5, which is right of where 3 would be)\n * ```\n *\n * @example\n * ```typescript\n * const arr = [4, 5, 5, 5];\n * const index = array.binary_search_rightmost(arr, 5);\n * // Returns: 3 (index of last instance of 5)\n * ```\n *\n * @remarks\n * - Array must be sorted in ascending order\n * - For duplicate values, returns rightmost occurrence\n * - When value not found, returns position of next larger element\n * - PineScript rules: a value above every element gives `size` (the position after the last element); `na` counts\n * as greater than every number (an array without na gives `size`); the comparison is exact (no 1e-10 tolerance)\n */\nexport function binary_search_rightmost(id: PineArray<float>, val: float): int {\n if (id.length === 0) return -1;\n const at = searchBound(id, val, true);\n return at > 0 && compareSorted(id[at - 1]!, val) === 0 ? at - 1 : at;\n}\n\n/**\n * Covariance\n *\n * Returns the covariance between two arrays.\n *\n * @param id1 - First array\n * @param id2 - Second array\n * @param biased - If true, uses biased estimate (population). If false, uses unbiased estimate (sample). Default: true\n * @returns Covariance between the two arrays\n *\n * @example\n * ```typescript\n * const prices = [100, 102, 98, 105, 103];\n * const volume = [1000, 1200, 950, 1300, 1100];\n * const cov = array.covariance(prices, volume);\n * // Returns covariance between price and volume\n * ```\n *\n * @remarks\n * - Biased (true): divides by n (population covariance)\n * - Unbiased (false): divides by n-1 (sample covariance)\n * - Returns NaN if arrays are empty or have different lengths\n * - Formula: Cov(X,Y) = E[(X - \u03BCX)(Y - \u03BCY)]\n *\n * PineScript rules: a pair where either value is `na` is skipped and the count is the number of pairs kept; both\n * means first, then the sum of the products from the first to the last pair. One pair gives 0 biased, NaN unbiased.\n */\nexport function covariance(id1: PineArray<float>, id2: PineArray<float>, biased: bool = true): float {\n if (id1.length !== id2.length) {\n return NaN;\n }\n const xs: number[] = [];\n const ys: number[] = [];\n for (let i = 0; i < id1.length; i++) {\n if (Number.isNaN(id1[i]!) || Number.isNaN(id2[i]!)) continue;\n xs.push(id1[i]!);\n ys.push(id2[i]!);\n }\n const n = xs.length;\n if (!n || (!biased && n < 2)) return NaN;\n let mean1 = 0;\n let mean2 = 0;\n for (let i = 0; i < n; i++) {\n mean1 += xs[i]!;\n mean2 += ys[i]!;\n }\n mean1 /= n;\n mean2 /= n;\n let sum = 0;\n for (let i = 0; i < n; i++) {\n sum += (xs[i]! - mean1) * (ys[i]! - mean2);\n }\n return sum / (biased ? n : n - 1);\n}\n\n/**\n * Percentile Linear Interpolation\n *\n * Returns the value for which the specified percentage of array values are\n * less than or equal to it, using linear interpolation.\n *\n * @param id - Array of numeric values\n * @param percentage - Percentage (0-100)\n * @returns Percentile value using linear interpolation\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const p50 = array.percentile_linear_interpolation(arr, 50);\n * // Returns: 3 (median)\n * ```\n *\n * @remarks\n * - Uses linear interpolation between adjacent values\n * - Result may not be a member of the array\n * - Returns NaN if array is empty\n *\n * PineScript rules:\n * - 1-based position in the sorted array: `size * percentage / 100 + 0.5` (snapped to an integer within 1e-9),\n * clamped to the first / last value; between two values `lo * (1 - frac) + hi * frac`\n * - `na` values are sorted after all numbers; with an `na` in the array, a position between two values gives `na`\n * - an `na` percentage gives `na`; a percentage outside 0 .. 100 is a runtime error (throws)\n */\nexport function percentile_linear_interpolation(id: PineArray<float>, percentage: float): float {\n if (id.length === 0 || Number.isNaN(percentage)) {\n return NaN;\n }\n checkPercentage('percentile_linear_interpolation', percentage);\n const s = sortedNaLast(id);\n const n = s.length;\n let pos = (n * percentage) / 100 + 0.5;\n const nearest = Math.round(pos);\n if (Math.abs(pos - nearest) < 1e-9) pos = nearest;\n if (pos <= 1) return s[0]!;\n if (pos >= n) return s[n - 1]!;\n const lower = Math.floor(pos);\n const frac = pos - lower;\n if (frac === 0) return s[lower - 1]!;\n if (Number.isNaN(s[n - 1]!)) return NaN;\n return s[lower - 1]! * (1 - frac) + s[lower]! * frac;\n}\n\n/** Array values sorted ascending, `na` values after the numbers (array.percentile_*). */\nfunction sortedNaLast(id: PineArray<float>): number[] {\n const numbers = id.filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);\n return [...numbers, ...Array<number>(id.length - numbers.length).fill(NaN)];\n}\n\n/** A percentage outside 0 .. 100 is a PineScript runtime error. */\nfunction checkPercentage(name: string, percentage: number): void {\n if (!(percentage >= 0 && percentage <= 100)) {\n throw new Error(`array.${name}: percentage ${percentage} must be between 0 and 100`);\n }\n}\n\n/**\n * Percentile Nearest Rank\n *\n * Returns the value for which the specified percentage of array values are\n * less than or equal to it, using the nearest-rank method.\n *\n * @param id - Array of numeric values\n * @param percentage - Percentage (0-100)\n * @returns Percentile value using nearest rank\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const p50 = array.percentile_nearest_rank(arr, 50);\n * // Returns: 3 (median, always a member of array)\n * ```\n *\n * @remarks\n * - Result is always a member of the array\n * - Uses nearest-rank method (no interpolation)\n * - Returns NaN if array is empty\n *\n * PineScript rules:\n * - the value at rank `ceil(percentage * size / 100)` (clamped to 1 .. size) of the sorted array\n * - `na` values are sorted after all numbers (na when that rank holds an `na`)\n * - an `na` percentage counts as 0; a percentage outside 0 .. 100 is a runtime error (throws)\n */\nexport function percentile_nearest_rank(id: PineArray<float>, percentage: float): float {\n if (id.length === 0) {\n return NaN;\n }\n const pct = Number.isNaN(percentage) ? 0 : percentage;\n checkPercentage('percentile_nearest_rank', pct);\n const s = sortedNaLast(id);\n const rank = Math.min(s.length, Math.max(1, Math.ceil((pct * s.length) / 100)));\n return s[rank - 1]!;\n}\n\n/**\n * Percent Rank\n *\n * Returns the percentile rank of the element at the specified index.\n * The percentile rank is the percentage of elements that are less than\n * or equal to the reference value.\n *\n * @param id - Array of numeric values\n * @param index - Index of element to rank\n * @returns Percentile rank (0-100)\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const rank = array.percentrank(arr, 2); // Value at index 2 is 3\n * // Returns: 50 ((3 - 1) * 100 / (5 - 1))\n * ```\n *\n * @remarks\n * - Range: 0 to 100\n *\n * PineScript rules:\n * - `(count - 1) * 100 / (size - 1)`, with `count` the number of non-na elements <= the element (exact\n * comparison, no 1e-10 tolerance); `na` elements count in the size\n * - an `na` element, a one-element array or an empty array gives NaN\n * - an `na` index counts as 0; an index outside `0 .. size - 1` is a runtime error (throws)\n */\nexport function percentrank(id: PineArray<float>, index: int): float {\n const i = Number.isNaN(index) ? 0 : Math.trunc(index);\n if (id.length === 0) {\n return NaN;\n }\n if (i < 0 || i >= id.length) {\n throw new Error(`array.percentrank: index ${i} is out of bounds, array size is ${id.length}`);\n }\n if (id.length === 1) {\n return NaN;\n }\n const value = id[i]!;\n if (Number.isNaN(value)) {\n return NaN;\n }\n let count = 0;\n for (const v of id) {\n if (v <= value) count++;\n }\n return ((count - 1) * 100) / (id.length - 1);\n}\n\n/**\n * Sort Indices\n *\n * Returns an array of indices which, when used to index the original array,\n * will access its elements in their sorted order. Does not modify the original array.\n *\n * @param id - Array to get sorted indices from\n * @param order - Sort order: 'asc' or 'desc' (default: 'asc')\n * @returns Array of indices in sorted order\n *\n * @example\n * ```typescript\n * const arr = [5, -2, 0, 9, 1];\n * const indices = array.sort_indices(arr); // [1, 2, 4, 0, 3]\n * // arr[1] = -2 (smallest)\n * // arr[2] = 0\n * // arr[4] = 1\n * // arr[0] = 5\n * // arr[3] = 9 (largest)\n * ```\n *\n * @remarks\n * - Original array is not modified\n * - Returns indices that would sort the array\n * - Useful for maintaining correspondence with other arrays\n * - PineScript rule: the indices of `na` elements go where `array.sort` puts them (after the numbers in ascending\n * order, in their original order: `sort_indices([na, na, 5, 1])` is `[3, 2, 0, 1]`)\n */\nexport function sort_indices<T>(id: PineArray<T>, order: SortOrder = 'asc'): PineArray<int> {\n return sortedPositions(id, order);\n}\n\n/**\n * Standardize\n *\n * Returns an array of standardized elements (z-score normalization).\n * Each element is transformed to (value - mean) / stddev.\n *\n * @param id - Array of numeric values\n * @returns Array of standardized values (z-scores)\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const standardized = array.standardize(arr);\n * // Mean = 3, StdDev \u2248 1.414\n * // Result \u2248 [-1.414, -0.707, 0, 0.707, 1.414]\n * ```\n *\n * @remarks\n * - Formula: z = (x - \u03BC) / \u03C3\n * - Result has mean of 0 and standard deviation of 1\n * - Useful for comparing values on different scales\n *\n * PineScript rules:\n * - \u03BC and \u03C3 are the population mean and standard deviation of the non-na elements, as `array.variance` (moment\n * form); `na` elements stay `na`\n * - when \u03C3 is 0 (all values equal) every non-na element gives 1\n */\nexport function standardize(id: PineArray<float>): PineArray<float> {\n const values = id.filter((v) => !Number.isNaN(v));\n if (!values.length) {\n return id.map(() => NaN);\n }\n const [p, q] = moments(values);\n const mean = p / values.length;\n const stdDev = Math.sqrt(Math.max(0, q / values.length - mean * mean));\n if (stdDev === 0) {\n return id.map((v) => (Number.isNaN(v) ? NaN : 1));\n }\n return id.map((v) => (Number.isNaN(v) ? NaN : (v - mean) / stdDev));\n}\n\n/**\n * New Type (User-Defined Type Array)\n *\n * Creates a new array of user-defined type elements.\n *\n * @param size - Initial size of array (default: 0)\n * @param initial_value - Initial value for all elements\n * @returns New array of user-defined type\n *\n * @example\n * ```typescript\n * // NOTE: User-defined types (UDTs) are not fully supported yet\n * // This is a placeholder for future implementation\n * const arr = array.newtype<MyType>(5);\n * ```\n *\n * @remarks\n * - **\u26A0\uFE0F LIMITED SUPPORT**: User-defined types (UDTs) require a type system\n * that is not yet fully implemented in this library\n * - This function currently works like `new_array<T>()` for basic types\n * - Full UDT support will be added in a future version\n * - See PineScript documentation for UDT usage patterns\n */\nexport function newtype<T>(size: simple_int = 0, initial_value?: T): PineArray<T> {\n // For now, this is just an alias for new_array\n // Full UDT support requires implementing the type system\n return new_array<T>(size, initial_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;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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,UAAaA,QAAmB,GAAG,eAAiC;AAClF,QAAM,MAAoB,CAAC;AAC3B,WAAS,IAAI,GAAG,IAAIA,OAAM,KAAK;AAC7B,QAAI,KAAK,aAAkB;AAAA,EAC7B;AACA,SAAO;AACT;AAKO,SAAS,KAAQ,IAAuB;AAC7C,SAAO,GAAG;AACZ;AAGA,SAAS,KAAK,GAAqB;AACjC,SAAO,MAAM,UAAa,MAAM,QAAS,OAAO,MAAM,YAAY,OAAO,MAAM,CAAC;AAClF;AAGA,SAAS,KAAQ,IAAqB;AACpC,SAAQ,GAAG,WAAW,KAAK,OAAO,GAAG,CAAC,MAAM,WAAW,MAAM;AA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AG,CAAC;AAClB,MAAI,OAAO,MAAM,KAAK,GAAG;AACvB,WAAO;AAAA,EACT;AACA,MAAI,QAAQ;AACZ,aAAW,KAAK,IAAI;AAClB,QAAI,KAAK,MAAO;AAAA,EAClB;AACA,UAAS,QAAQ,KAAK,OAAQ,GAAG,SAAS;AAC5C;AA8BO,SAAS,aAAgB,IAAkB,QAAmB,OAAuB;AAC1F,SAAO,gBAAgB,IAAI,KAAK;AAClC;AA6BO,SAAS,YAAY,IAAwC;AAClE,QAAM,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,MAAM,CAAC,CAAC;AAChD,MAAI,CAAC,OAAO,QAAQ;AAClB,WAAO,GAAG,IAAI,MAAM,GAAG;AAAA,EACzB;AACA,QAAM,CAAC,GAAG,CAAC,IAAI,QAAQ,MAAM;AAC7B,QAAM,OAAO,IAAI,OAAO;AACxB,QAAM,SAAS,KAAK,KAAK,KAAK,IAAI,GAAG,IAAI,OAAO,SAAS,OAAO,IAAI,CAAC;AACrE,MAAI,WAAW,GAAG;AAChB,WAAO,GAAG,IAAI,CAAC,MAAO,OAAO,MAAM,CAAC,IAAI,MAAM,CAAE;AAAA,EAClD;AACA,SAAO,GAAG,IAAI,CAAC,MAAO,OAAO,MAAM,CAAC,IAAI,OAAO,IAAI,QAAQ,MAAO;AACpE;AAyBO,SAAS,QAAWD,QAAmB,GAAG,eAAiC;AAGhF,SAAO,UAAaA,OAAM,aAAa;AACzC;",
|
|
6
|
+
"names": ["size", "sum"]
|
|
7
7
|
}
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* call), which PineScript computes with a state kept between calls.
|
|
14
14
|
* - {@link lowestByBar} / {@link highestByBar} / {@link linreg}: `ta.lowest` / `ta.highest` / `ta.linreg` in a
|
|
15
15
|
* conditional block, whose history is kept by bar (not by call).
|
|
16
|
+
* - {@link sum} / {@link sma}: `math.sum` / `ta.sma` in a `for` / `while` loop, called several times on one bar.
|
|
16
17
|
*
|
|
17
18
|
* Since PineScript v6, `and` / `or` are lazy: the right operand runs only when the left operand does not decide
|
|
18
19
|
* the result. A ta.* call in the right operand of `a or b` runs on the bars where `a` is false, in `a and b` on the
|
|
@@ -67,8 +68,9 @@ export declare function crossunder(): (a: number, b: number) => boolean;
|
|
|
67
68
|
*/
|
|
68
69
|
export declare function barssince(): (condition: boolean | number) => number;
|
|
69
70
|
/**
|
|
70
|
-
* One `ta.cross(a, b)` call site
|
|
71
|
-
* a call
|
|
71
|
+
* One `ta.cross(a, b)` call site, rules of `ta.cross`: the site keeps the side of the last call where `a` was
|
|
72
|
+
* strictly above or strictly below `b` (none before such a call; equal values and na arguments do not change it),
|
|
73
|
+
* and is true when the arguments are strictly on the other side. False with an na argument.
|
|
72
74
|
*/
|
|
73
75
|
export declare function cross(): (a: number, b: number) => boolean;
|
|
74
76
|
/**
|
|
@@ -144,4 +146,81 @@ export declare function highestByBar(): (barIndex: number, value: number, length
|
|
|
144
146
|
* ```
|
|
145
147
|
*/
|
|
146
148
|
export declare function linreg(): (barIndex: number, value: number, length: number, offset?: number) => number;
|
|
149
|
+
/**
|
|
150
|
+
* One `math.sum(source, length)` call site inside a `for` / `while` loop, called with `(barIndex, value, length)` on
|
|
151
|
+
* each call.
|
|
152
|
+
*
|
|
153
|
+
* PineScript rules (measured on calls in loops, 1 to 3 calls per bar, and bars without a call):
|
|
154
|
+
* - the call site keeps one history value per bar: the value of its last call on that bar
|
|
155
|
+
* - every call on a bar starts from the state of the earlier bars, so it gives the sum of its value and the last
|
|
156
|
+
* `length - 1` kept values; the last call of the bar is the one kept for the later bars
|
|
157
|
+
* - the length can change from one call to the next (also on the same bar): each call first moves the kept sum to
|
|
158
|
+
* its own length as `math.sum` with a series length does (Kahan steps of the raw values)
|
|
159
|
+
* - a bar without a call is not in the history (as a call in an `if` block)
|
|
160
|
+
* - the sum is the same compensated running sum as `math.sum` (bit for bit)
|
|
161
|
+
*
|
|
162
|
+
* An na value is skipped as in `math.sum` (it returns the sum of the last `length` earlier values); when it is the
|
|
163
|
+
* last call of a bar, the bar is not in the history (not measured).
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* // Pine: for i = 0 to 2
|
|
168
|
+
* // s = math.sum(close * (1 + 0.01 * i), 7)
|
|
169
|
+
* const site = callsite.sum();
|
|
170
|
+
* for (let b = 0; b < closes.length; b++) {
|
|
171
|
+
* for (let i = 0; i <= 2; i++) {
|
|
172
|
+
* const s = site(b, closes[b]! * (1 + 0.01 * i), 7);
|
|
173
|
+
* }
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export declare function sum(): (barIndex: number, value: number, length: number) => number;
|
|
178
|
+
/**
|
|
179
|
+
* One `ta.sma(source, length)` call site inside a `for` / `while` loop, called with `(barIndex, value, length)` on
|
|
180
|
+
* each call: {@link sum} divided by the length (same rules).
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* // Pine: evaluate(ratio) => ta.sma(gann(ratio), n) // called once per ratio in a while loop
|
|
185
|
+
* const site = callsite.sma();
|
|
186
|
+
* for (let b = 0; b < closes.length; b++) {
|
|
187
|
+
* for (const ratio of ratios) {
|
|
188
|
+
* const avg = site(b, gann(b, ratio), n);
|
|
189
|
+
* }
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export declare function sma(): (barIndex: number, value: number, length: number) => number;
|
|
194
|
+
/**
|
|
195
|
+
* One `ta.stdev(source, length, biased)` call site inside a `for` / `while` loop, called with
|
|
196
|
+
* `(barIndex, value, length, biased?)` on each call: the running sums of the values and of their squares follow the
|
|
197
|
+
* rules of {@link sum}; the result is computed from them as `ta.stdev` (bit for bit).
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* // Pine: for len in lengths
|
|
202
|
+
* // [mid, up, lo] = ta.bb(close, len, 2)
|
|
203
|
+
* const avgSite = callsite.sma();
|
|
204
|
+
* const devSite = callsite.stdev();
|
|
205
|
+
* for (let b = 0; b < closes.length; b++) {
|
|
206
|
+
* for (const len of lengths) {
|
|
207
|
+
* const mid = avgSite(b, closes[b]!, len);
|
|
208
|
+
* const dev = devSite(b, closes[b]!, len);
|
|
209
|
+
* }
|
|
210
|
+
* }
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export declare function stdev(): (barIndex: number, value: number, length: number, biased?: boolean) => number;
|
|
214
|
+
/**
|
|
215
|
+
* One `math.random(min, max, seed)` call site: PineScript keeps one generator per call site, so two call sites
|
|
216
|
+
* with the same seed give the same sequence. Called with `(min?, max?)` once per bar.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* // Pine: a = math.random(0, 1, 42), b = math.random(0, 1, 42) // a == b on every bar
|
|
221
|
+
* const a = callsite.random(42);
|
|
222
|
+
* const b = callsite.random(42);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export declare function random(seed: number): (min?: number, max?: number) => number;
|
|
147
226
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/callsite/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/callsite/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAKhD;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,GAAG,OAAO,EACnD,MAAM,EAAE,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC,EACnC,EAAE,EAAE,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAC3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;GA2BG;AACH,wBAAgB,GAAG,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAGjF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAGjF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,MAAM,CAUrG;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,KAAK,MAAM,CAE3E"}
|
|
@@ -27,10 +27,16 @@ __export(callsite_exports, {
|
|
|
27
27
|
linreg: () => linreg,
|
|
28
28
|
lowest: () => lowest,
|
|
29
29
|
lowestByBar: () => lowestByBar,
|
|
30
|
+
random: () => random,
|
|
31
|
+
sma: () => sma,
|
|
32
|
+
stdev: () => stdev,
|
|
33
|
+
sum: () => sum,
|
|
30
34
|
whenCalled: () => whenCalled
|
|
31
35
|
});
|
|
32
36
|
module.exports = __toCommonJS(callsite_exports);
|
|
33
37
|
var import_ta = require("../ta/index.js");
|
|
38
|
+
var import_running_sum = require("../ta/running-sum.js");
|
|
39
|
+
var import_math = require("../math/index.js");
|
|
34
40
|
function whenCalled(called, fn, ...sources) {
|
|
35
41
|
const bars = [];
|
|
36
42
|
for (let i = 0; i < called.length; i++) if (called[i]) bars.push(i);
|
|
@@ -68,7 +74,14 @@ function barssince() {
|
|
|
68
74
|
};
|
|
69
75
|
}
|
|
70
76
|
function cross() {
|
|
71
|
-
|
|
77
|
+
let side = 0;
|
|
78
|
+
return (a, b) => {
|
|
79
|
+
if (Number.isNaN(a) || Number.isNaN(b)) return false;
|
|
80
|
+
const result = side < 0 && a > b || side > 0 && a < b;
|
|
81
|
+
if (a < b) side = -1;
|
|
82
|
+
else if (a > b) side = 1;
|
|
83
|
+
return result;
|
|
84
|
+
};
|
|
72
85
|
}
|
|
73
86
|
function extremeSite(isLowest) {
|
|
74
87
|
const better = isLowest ? (a, b) => a < b : (a, b) => a > b;
|
|
@@ -160,4 +173,52 @@ function linreg() {
|
|
|
160
173
|
return (0, import_ta.linreg)(window, len, offset)[len - 1];
|
|
161
174
|
};
|
|
162
175
|
}
|
|
176
|
+
function loopSumSite(name, squares) {
|
|
177
|
+
const running = new import_running_sum.RunningSum();
|
|
178
|
+
const runningSq = new import_running_sum.RunningSum();
|
|
179
|
+
let lastBar = NaN;
|
|
180
|
+
let pending = false;
|
|
181
|
+
return (barIndex, value, length) => {
|
|
182
|
+
const len = Math.floor(length);
|
|
183
|
+
if (!(len >= 1)) throw new Error(`callsite.${name}: length must be at least 1 (got ${length})`);
|
|
184
|
+
if (barIndex !== lastBar) {
|
|
185
|
+
if (pending) {
|
|
186
|
+
running.commit();
|
|
187
|
+
if (squares) runningSq.commit();
|
|
188
|
+
}
|
|
189
|
+
pending = false;
|
|
190
|
+
lastBar = barIndex;
|
|
191
|
+
}
|
|
192
|
+
if ((0, import_running_sum.isNa)(value)) {
|
|
193
|
+
pending = false;
|
|
194
|
+
return [running.stepNa(len), squares ? runningSq.stepNa(len) : NaN];
|
|
195
|
+
}
|
|
196
|
+
const x = value;
|
|
197
|
+
const out = [running.step(x, len), squares ? runningSq.step(x * x, len) : NaN];
|
|
198
|
+
pending = true;
|
|
199
|
+
return out;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function sum() {
|
|
203
|
+
const site = loopSumSite("sum", false);
|
|
204
|
+
return (barIndex, value, length) => site(barIndex, value, length)[0];
|
|
205
|
+
}
|
|
206
|
+
function sma() {
|
|
207
|
+
const site = loopSumSite("sma", false);
|
|
208
|
+
return (barIndex, value, length) => site(barIndex, value, length)[0] / Math.floor(length);
|
|
209
|
+
}
|
|
210
|
+
function stdev() {
|
|
211
|
+
const site = loopSumSite("stdev", true);
|
|
212
|
+
return (barIndex, value, length, biased = true) => {
|
|
213
|
+
const len = Math.floor(length);
|
|
214
|
+
const [s, sq] = site(barIndex, value, length);
|
|
215
|
+
const mean = s / len;
|
|
216
|
+
const variance = biased ? sq / len - mean * mean : sq / (len - 1) - mean * s / (len - 1);
|
|
217
|
+
if (!biased && len <= 1) return NaN;
|
|
218
|
+
return Number.isNaN(variance) ? NaN : variance > 0 ? Math.sqrt(variance) : 0;
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function random(seed) {
|
|
222
|
+
return (0, import_math.seededRandom)(seed);
|
|
223
|
+
}
|
|
163
224
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/callsite/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * @fileoverview Per-call history of ta.* functions called in a conditional block.\n *\n * In PineScript, every ta.* call keeps its own history of the values it receives. A call inside\n * an `if` block receives values only on the bars where the block runs, so its history skips the\n * other bars: `ta.crossover(a, b)` is false on the first bar the block runs, and `ta.sma(x, 3)`\n * averages the last 3 values it received, not the last 3 bars.\n *\n * - {@link whenCalled}: vectorized, when the bars of the calls are known in advance.\n * - {@link crossover} / {@link crossunder} / {@link cross} / {@link barssince}: one stateful call site for bar\n * loops, called only on the bars where the Pine call runs.\n * - {@link lowest} / {@link highest}: `ta.lowest` / `ta.highest` with a series length (a different length on each\n * call), which PineScript computes with a state kept between calls.\n * - {@link lowestByBar} / {@link highestByBar} / {@link linreg}: `ta.lowest` / `ta.highest` / `ta.linreg` in a\n * conditional block, whose history is kept by bar (not by call).\n *\n * Since PineScript v6, `and` / `or` are lazy: the right operand runs only when the left operand does not decide\n * the result. A ta.* call in the right operand of `a or b` runs on the bars where `a` is false, in `a and b` on the\n * bars where `a` is true, so it needs a call site too:\n *\n * ```typescript\n * // Pine: longCond = longUp or (src > sig and ta.barssince(longUp) <= lookback)\n * const since = callsite.whenCalled(longUp.map((u, i) => !u && src[i] > sig[i]), (c) => taCore.barssince(c.map(Boolean)), longUp);\n * // ta.barssince(longUp) only runs where longUp is false: it never sees true and stays na\n * ```\n *\n * ```typescript\n * // Pine: if inRange\n * // buy = ta.crossover(cumRsi, 60)\n * const buy = callsite.whenCalled(inRange, (a) => taCore.crossover(a, a.map(() => 60)), cumRsi);\n *\n * // Same call in a bar loop\n * const buyCross = callsite.crossover();\n * for (let i = 0; i < n; i++) {\n * if (inRange[i]) {\n * const buy = buyCross(cumRsi[i], 60);\n * }\n * }\n * ```\n *\n * @module callsite\n */\n\nimport type { Source } from '../types/index.js';\nimport { linreg as linregSeries } from '../ta/index.js';\n\n/**\n * Runs `fn` on the bars where `called` is true only, like a ta.* call inside an `if` block.\n * `fn` receives the values of `sources` on those bars, in order; its results are placed back on\n * the same bars. The other bars get na (NaN).\n *\n * @param called - true (or non-zero) on the bars where the call runs\n * @param fn - the calculation, for example `(x) => taCore.sma(x, 3)`\n * @param sources - the arguments of the call, one value per chart bar\n */\nexport function whenCalled<T extends number | boolean>(\n called: ArrayLike<boolean | number>,\n fn: (...values: number[][]) => ArrayLike<T>,\n ...sources: Source[]\n): Array<T | number> {\n const bars: number[] = [];\n for (let i = 0; i < called.length; i++) if (called[i]) bars.push(i);\n const result = fn(...sources.map(s => bars.map(i => s[i] ?? NaN)));\n const out = new Array<T | number>(called.length).fill(NaN);\n bars.forEach((bar, k) => {\n out[bar] = result[k]!;\n });\n return out;\n}\n\n/**\n * A call site that keeps the arguments of the last call where both were not na (na before such a call), as\n * ta.crossover / ta.crossunder / ta.cross.\n */\nfunction twoSeriesSite(test: (a: number, b: number, prevA: number, prevB: number) => boolean) {\n let prevA = NaN;\n let prevB = NaN;\n return (a: number, b: number): boolean => {\n const result = test(a, b, prevA, prevB);\n if (!Number.isNaN(a) && !Number.isNaN(b)) {\n prevA = a;\n prevB = b;\n }\n return result;\n };\n}\n\n/**\n * One `ta.crossover(a, b)` call site: compared with the last call where both arguments were not na; false before\n * such a call.\n */\nexport function crossover(): (a: number, b: number) => boolean {\n return twoSeriesSite((a, b, pa, pb) => a > b && pa <= pb);\n}\n\n/**\n * One `ta.crossunder(a, b)` call site: compared with the last call where both arguments were not na; false before\n * such a call.\n */\nexport function crossunder(): (a: number, b: number) => boolean {\n return twoSeriesSite((a, b, pa, pb) => a < b && pa >= pb);\n}\n\n/**\n * One `ta.barssince(condition)` call site: the number of calls since the condition was true on a call (0 on that\n * call), na before the first true condition. An na condition counts as false.\n */\nexport function barssince(): (condition: boolean | number) => number {\n let count = NaN;\n return (condition) => {\n if (condition && !Number.isNaN(condition)) count = 0;\n else if (!Number.isNaN(count)) count++;\n return count;\n };\n}\n\n/**\n * One `ta.cross(a, b)` call site: compared with the last call where both arguments were not na; false before such\n * a call.\n */\nexport function cross(): (a: number, b: number) => boolean {\n return twoSeriesSite((a, b, pa, pb) => (a > b && pa <= pb) || (a < b && pa >= pb));\n}\n\n/** One `ta.lowest` / `ta.highest` call site with a series length; rules in {@link lowest}. */\nfunction extremeSite(isLowest: boolean): (value: number, length: number) => number {\n const better = isLowest ? (a: number, b: number) => a < b : (a: number, b: number) => a > b;\n const values: number[] = [];\n let extreme = NaN;\n let extremeAt = -1;\n let prevLength = NaN;\n const keep = (v: number, at: number) => {\n extreme = v;\n extremeAt = at;\n };\n const rescan = (i: number, length: number) => {\n extreme = NaN;\n for (let b = i; b > i - length && b >= 0; b--) {\n const v = values[b]!;\n if (!Number.isNaN(v) && (Number.isNaN(extreme) || !better(extreme, v))) keep(v, b);\n }\n };\n return (value, length) => {\n const i = values.length;\n values.push(value);\n if (Number.isNaN(value)) {\n extreme = NaN;\n prevLength = length;\n return NaN;\n }\n if (length > prevLength) {\n const from = Math.max(0, i - length + 1);\n const to = Math.max(0, i - prevLength + 1);\n if (values.slice(from, to).some(Number.isNaN)) keep(value, i);\n else {\n for (let b = from; b < to; b++) if (Number.isNaN(extreme) || better(values[b]!, extreme)) keep(values[b]!, b);\n if (Number.isNaN(extreme) || better(value, extreme)) keep(value, i);\n }\n } else if (length < prevLength || Number.isNaN(extreme) || i - extremeAt >= length) rescan(i, length);\n else if (better(value, extreme)) keep(value, i);\n prevLength = length;\n return i < length - 1 ? NaN : extreme;\n };\n}\n\n/**\n * One `ta.lowest(source, length)` call site with a series length (a different length on each call), called with\n * `(value, length)`.\n *\n * PineScript rules (measured with a length that changes on each bar):\n * - the extreme is kept with its bar; a new value that passes it replaces it; when the kept extreme is `length`\n * calls old, the last `length` values are scanned again (na skipped, the oldest wins a tie)\n * - a length smaller than on the previous call: the last `length` values are scanned again\n * - a length larger than on the previous call: the values that enter the window at its old end are compared with the\n * extreme; when one of them is na, the extreme restarts from the current value (the values in between are not read)\n * - an na value gives na and resets the extreme; na before call `length - 1`\n *\n * @example\n * ```typescript\n * // Pine: lowPrice = ta.lowest(close[1], lookback) // lookback changes per bar\n * const lowPrice = callsite.lowest();\n * const out = closes.map((_, i) => lowPrice(i > 0 ? closes[i - 1]! : NaN, lookback[i]!));\n * ```\n */\nexport function lowest(): (value: number, length: number) => number {\n return extremeSite(true);\n}\n\n/**\n * One `ta.highest(source, length)` call site with a series length (a different length on each call), called with\n * `(value, length)`. Same rules as {@link lowest}.\n */\nexport function highest(): (value: number, length: number) => number {\n return extremeSite(false);\n}\n\n/** One `ta.lowest` / `ta.highest` call site whose history is kept by bar; rules in {@link lowestByBar}. */\nfunction extremeByBarSite(isLowest: boolean): (barIndex: number, value: number, length: number) => number {\n const better = isLowest ? (a: number, b: number) => a < b : (a: number, b: number) => a > b;\n let ring: number[] = [];\n let extreme = NaN;\n let extremeAt = -1;\n return (barIndex, value, length) => {\n const len = Math.floor(length);\n if (ring.length !== len + 1) {\n ring = new Array<number>(len + 1).fill(0);\n extreme = NaN;\n }\n ring[barIndex % (len + 1)] = value;\n if (Number.isNaN(extreme) || better(value, extreme)) {\n extreme = value;\n extremeAt = barIndex;\n } else if (barIndex - extremeAt >= len) {\n extreme = NaN;\n for (let b = barIndex - len + 1; b <= barIndex; b++) {\n const v = ring[b % (len + 1)]!;\n if (!Number.isNaN(v) && (Number.isNaN(extreme) || better(v, extreme))) {\n extreme = v;\n extremeAt = b;\n }\n }\n }\n return barIndex < len - 1 ? NaN : extreme;\n };\n}\n\n/**\n * One `ta.lowest(source, length)` call site in a conditional block whose history is kept by bar (not by call), called\n * with `(barIndex, value, length)` on the bars where the Pine call runs.\n *\n * PineScript rules (measured on a call in a ternary branch, length 6):\n * - the values are kept in a ring of `length + 1` slots indexed by the bar index, filled with 0 at the start and\n * written only on the bars where the call runs: a bar before the first call counts as 0, and a bar without a call\n * holds the value of the call `length + 1` bars earlier (0 if there was none)\n * - on a call, the value replaces the kept extreme when there is none or when it is strictly lower; only otherwise,\n * when the kept extreme is `length` bars old, the last `length` bars are read again from the ring (the oldest bar\n * wins a tie)\n * - na before bar `length - 1` (by bar, not by call)\n *\n * {@link lowest} keeps the history by call instead; both rules were measured, on different scripts.\n *\n * @example\n * ```typescript\n * // Pine: lo = cond ? ta.lowest(low, 6) : na\n * const site = callsite.lowestByBar();\n * const lo = lows.map((v, i) => (cond[i] ? site(i, v, 6) : NaN));\n * ```\n */\nexport function lowestByBar(): (barIndex: number, value: number, length: number) => number {\n return extremeByBarSite(true);\n}\n\n/**\n * One `ta.highest(source, length)` call site in a conditional block whose history is kept by bar, called with\n * `(barIndex, value, length)`. Same rules as {@link lowestByBar} (a value replaces the kept extreme when it is\n * strictly higher).\n */\nexport function highestByBar(): (barIndex: number, value: number, length: number) => number {\n return extremeByBarSite(false);\n}\n\n/**\n * One `ta.linreg(source, length, offset)` call site in a conditional block, called with\n * `(barIndex, value, length, offset)` on the bars where the Pine call runs.\n *\n * PineScript rules (measured): `ta.linreg` keeps the values it receives in a ring of `length + 1` slots indexed by the\n * bar index, filled with 0 at the start and written only on the bars where it is called. The window is read from the\n * ring for the last `length` bars: a bar before the first call counts as 0, and a bar without a call holds the value of\n * the call `length + 1` bars earlier (0 if there was none). The result is na before bar `length - 1`. Called on every\n * bar, it equals `ta.linreg`.\n *\n * @example\n * ```typescript\n * // Pine: if bar_index >= 20\n * // a := ta.linreg(close, 10, 0)\n * const site = callsite.linreg();\n * const a = closes.map((c, i) => (i >= 20 ? site(i, c, 10, 0) : NaN));\n * ```\n */\nexport function linreg(): (barIndex: number, value: number, length: number, offset?: number) => number {\n let ring: number[] = [];\n return (barIndex, value, length, offset = 0) => {\n const len = Math.floor(length);\n if (ring.length !== len + 1) ring = new Array<number>(len + 1).fill(0);\n ring[barIndex % (len + 1)] = value;\n if (barIndex < len - 1) return NaN;\n const window = Array.from({ length: len }, (_, k) => ring[(barIndex - len + 1 + k) % (len + 1)]!);\n return linregSeries(window, len, offset)[len - 1]!;\n };\n}\n\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["/**\n * @fileoverview Per-call history of ta.* functions called in a conditional block.\n *\n * In PineScript, every ta.* call keeps its own history of the values it receives. A call inside\n * an `if` block receives values only on the bars where the block runs, so its history skips the\n * other bars: `ta.crossover(a, b)` is false on the first bar the block runs, and `ta.sma(x, 3)`\n * averages the last 3 values it received, not the last 3 bars.\n *\n * - {@link whenCalled}: vectorized, when the bars of the calls are known in advance.\n * - {@link crossover} / {@link crossunder} / {@link cross} / {@link barssince}: one stateful call site for bar\n * loops, called only on the bars where the Pine call runs.\n * - {@link lowest} / {@link highest}: `ta.lowest` / `ta.highest` with a series length (a different length on each\n * call), which PineScript computes with a state kept between calls.\n * - {@link lowestByBar} / {@link highestByBar} / {@link linreg}: `ta.lowest` / `ta.highest` / `ta.linreg` in a\n * conditional block, whose history is kept by bar (not by call).\n * - {@link sum} / {@link sma}: `math.sum` / `ta.sma` in a `for` / `while` loop, called several times on one bar.\n *\n * Since PineScript v6, `and` / `or` are lazy: the right operand runs only when the left operand does not decide\n * the result. A ta.* call in the right operand of `a or b` runs on the bars where `a` is false, in `a and b` on the\n * bars where `a` is true, so it needs a call site too:\n *\n * ```typescript\n * // Pine: longCond = longUp or (src > sig and ta.barssince(longUp) <= lookback)\n * const since = callsite.whenCalled(longUp.map((u, i) => !u && src[i] > sig[i]), (c) => taCore.barssince(c.map(Boolean)), longUp);\n * // ta.barssince(longUp) only runs where longUp is false: it never sees true and stays na\n * ```\n *\n * ```typescript\n * // Pine: if inRange\n * // buy = ta.crossover(cumRsi, 60)\n * const buy = callsite.whenCalled(inRange, (a) => taCore.crossover(a, a.map(() => 60)), cumRsi);\n *\n * // Same call in a bar loop\n * const buyCross = callsite.crossover();\n * for (let i = 0; i < n; i++) {\n * if (inRange[i]) {\n * const buy = buyCross(cumRsi[i], 60);\n * }\n * }\n * ```\n *\n * @module callsite\n */\n\nimport type { Source } from '../types/index.js';\nimport { linreg as linregSeries } from '../ta/index.js';\nimport { RunningSum, isNa } from '../ta/running-sum.js';\nimport { seededRandom } from '../math/index.js';\n\n/**\n * Runs `fn` on the bars where `called` is true only, like a ta.* call inside an `if` block.\n * `fn` receives the values of `sources` on those bars, in order; its results are placed back on\n * the same bars. The other bars get na (NaN).\n *\n * @param called - true (or non-zero) on the bars where the call runs\n * @param fn - the calculation, for example `(x) => taCore.sma(x, 3)`\n * @param sources - the arguments of the call, one value per chart bar\n */\nexport function whenCalled<T extends number | boolean>(\n called: ArrayLike<boolean | number>,\n fn: (...values: number[][]) => ArrayLike<T>,\n ...sources: Source[]\n): Array<T | number> {\n const bars: number[] = [];\n for (let i = 0; i < called.length; i++) if (called[i]) bars.push(i);\n const result = fn(...sources.map(s => bars.map(i => s[i] ?? NaN)));\n const out = new Array<T | number>(called.length).fill(NaN);\n bars.forEach((bar, k) => {\n out[bar] = result[k]!;\n });\n return out;\n}\n\n/**\n * A call site that keeps the arguments of the last call where both were not na (na before such a call), as\n * ta.crossover / ta.crossunder / ta.cross.\n */\nfunction twoSeriesSite(test: (a: number, b: number, prevA: number, prevB: number) => boolean) {\n let prevA = NaN;\n let prevB = NaN;\n return (a: number, b: number): boolean => {\n const result = test(a, b, prevA, prevB);\n if (!Number.isNaN(a) && !Number.isNaN(b)) {\n prevA = a;\n prevB = b;\n }\n return result;\n };\n}\n\n/**\n * One `ta.crossover(a, b)` call site: compared with the last call where both arguments were not na; false before\n * such a call.\n */\nexport function crossover(): (a: number, b: number) => boolean {\n return twoSeriesSite((a, b, pa, pb) => a > b && pa <= pb);\n}\n\n/**\n * One `ta.crossunder(a, b)` call site: compared with the last call where both arguments were not na; false before\n * such a call.\n */\nexport function crossunder(): (a: number, b: number) => boolean {\n return twoSeriesSite((a, b, pa, pb) => a < b && pa >= pb);\n}\n\n/**\n * One `ta.barssince(condition)` call site: the number of calls since the condition was true on a call (0 on that\n * call), na before the first true condition. An na condition counts as false.\n */\nexport function barssince(): (condition: boolean | number) => number {\n let count = NaN;\n return (condition) => {\n if (condition && !Number.isNaN(condition)) count = 0;\n else if (!Number.isNaN(count)) count++;\n return count;\n };\n}\n\n/**\n * One `ta.cross(a, b)` call site, rules of `ta.cross`: the site keeps the side of the last call where `a` was\n * strictly above or strictly below `b` (none before such a call; equal values and na arguments do not change it),\n * and is true when the arguments are strictly on the other side. False with an na argument.\n */\nexport function cross(): (a: number, b: number) => boolean {\n let side = 0; // -1: last strictly below, 1: last strictly above, 0: none yet\n return (a, b) => {\n if (Number.isNaN(a) || Number.isNaN(b)) return false;\n const result = (side < 0 && a > b) || (side > 0 && a < b);\n if (a < b) side = -1;\n else if (a > b) side = 1;\n return result;\n };\n}\n\n/** One `ta.lowest` / `ta.highest` call site with a series length; rules in {@link lowest}. */\nfunction extremeSite(isLowest: boolean): (value: number, length: number) => number {\n const better = isLowest ? (a: number, b: number) => a < b : (a: number, b: number) => a > b;\n const values: number[] = [];\n let extreme = NaN;\n let extremeAt = -1;\n let prevLength = NaN;\n const keep = (v: number, at: number) => {\n extreme = v;\n extremeAt = at;\n };\n const rescan = (i: number, length: number) => {\n extreme = NaN;\n for (let b = i; b > i - length && b >= 0; b--) {\n const v = values[b]!;\n if (!Number.isNaN(v) && (Number.isNaN(extreme) || !better(extreme, v))) keep(v, b);\n }\n };\n return (value, length) => {\n const i = values.length;\n values.push(value);\n if (Number.isNaN(value)) {\n extreme = NaN;\n prevLength = length;\n return NaN;\n }\n if (length > prevLength) {\n const from = Math.max(0, i - length + 1);\n const to = Math.max(0, i - prevLength + 1);\n if (values.slice(from, to).some(Number.isNaN)) keep(value, i);\n else {\n for (let b = from; b < to; b++) if (Number.isNaN(extreme) || better(values[b]!, extreme)) keep(values[b]!, b);\n if (Number.isNaN(extreme) || better(value, extreme)) keep(value, i);\n }\n } else if (length < prevLength || Number.isNaN(extreme) || i - extremeAt >= length) rescan(i, length);\n else if (better(value, extreme)) keep(value, i);\n prevLength = length;\n return i < length - 1 ? NaN : extreme;\n };\n}\n\n/**\n * One `ta.lowest(source, length)` call site with a series length (a different length on each call), called with\n * `(value, length)`.\n *\n * PineScript rules (measured with a length that changes on each bar):\n * - the extreme is kept with its bar; a new value that passes it replaces it; when the kept extreme is `length`\n * calls old, the last `length` values are scanned again (na skipped, the oldest wins a tie)\n * - a length smaller than on the previous call: the last `length` values are scanned again\n * - a length larger than on the previous call: the values that enter the window at its old end are compared with the\n * extreme; when one of them is na, the extreme restarts from the current value (the values in between are not read)\n * - an na value gives na and resets the extreme; na before call `length - 1`\n *\n * @example\n * ```typescript\n * // Pine: lowPrice = ta.lowest(close[1], lookback) // lookback changes per bar\n * const lowPrice = callsite.lowest();\n * const out = closes.map((_, i) => lowPrice(i > 0 ? closes[i - 1]! : NaN, lookback[i]!));\n * ```\n */\nexport function lowest(): (value: number, length: number) => number {\n return extremeSite(true);\n}\n\n/**\n * One `ta.highest(source, length)` call site with a series length (a different length on each call), called with\n * `(value, length)`. Same rules as {@link lowest}.\n */\nexport function highest(): (value: number, length: number) => number {\n return extremeSite(false);\n}\n\n/** One `ta.lowest` / `ta.highest` call site whose history is kept by bar; rules in {@link lowestByBar}. */\nfunction extremeByBarSite(isLowest: boolean): (barIndex: number, value: number, length: number) => number {\n const better = isLowest ? (a: number, b: number) => a < b : (a: number, b: number) => a > b;\n let ring: number[] = [];\n let extreme = NaN;\n let extremeAt = -1;\n return (barIndex, value, length) => {\n const len = Math.floor(length);\n if (ring.length !== len + 1) {\n ring = new Array<number>(len + 1).fill(0);\n extreme = NaN;\n }\n ring[barIndex % (len + 1)] = value;\n if (Number.isNaN(extreme) || better(value, extreme)) {\n extreme = value;\n extremeAt = barIndex;\n } else if (barIndex - extremeAt >= len) {\n extreme = NaN;\n for (let b = barIndex - len + 1; b <= barIndex; b++) {\n const v = ring[b % (len + 1)]!;\n if (!Number.isNaN(v) && (Number.isNaN(extreme) || better(v, extreme))) {\n extreme = v;\n extremeAt = b;\n }\n }\n }\n return barIndex < len - 1 ? NaN : extreme;\n };\n}\n\n/**\n * One `ta.lowest(source, length)` call site in a conditional block whose history is kept by bar (not by call), called\n * with `(barIndex, value, length)` on the bars where the Pine call runs.\n *\n * PineScript rules (measured on a call in a ternary branch, length 6):\n * - the values are kept in a ring of `length + 1` slots indexed by the bar index, filled with 0 at the start and\n * written only on the bars where the call runs: a bar before the first call counts as 0, and a bar without a call\n * holds the value of the call `length + 1` bars earlier (0 if there was none)\n * - on a call, the value replaces the kept extreme when there is none or when it is strictly lower; only otherwise,\n * when the kept extreme is `length` bars old, the last `length` bars are read again from the ring (the oldest bar\n * wins a tie)\n * - na before bar `length - 1` (by bar, not by call)\n *\n * {@link lowest} keeps the history by call instead; both rules were measured, on different scripts.\n *\n * @example\n * ```typescript\n * // Pine: lo = cond ? ta.lowest(low, 6) : na\n * const site = callsite.lowestByBar();\n * const lo = lows.map((v, i) => (cond[i] ? site(i, v, 6) : NaN));\n * ```\n */\nexport function lowestByBar(): (barIndex: number, value: number, length: number) => number {\n return extremeByBarSite(true);\n}\n\n/**\n * One `ta.highest(source, length)` call site in a conditional block whose history is kept by bar, called with\n * `(barIndex, value, length)`. Same rules as {@link lowestByBar} (a value replaces the kept extreme when it is\n * strictly higher).\n */\nexport function highestByBar(): (barIndex: number, value: number, length: number) => number {\n return extremeByBarSite(false);\n}\n\n/**\n * One `ta.linreg(source, length, offset)` call site in a conditional block, called with\n * `(barIndex, value, length, offset)` on the bars where the Pine call runs.\n *\n * PineScript rules (measured): `ta.linreg` keeps the values it receives in a ring of `length + 1` slots indexed by the\n * bar index, filled with 0 at the start and written only on the bars where it is called. The window is read from the\n * ring for the last `length` bars: a bar before the first call counts as 0, and a bar without a call holds the value of\n * the call `length + 1` bars earlier (0 if there was none). The result is na before bar `length - 1`. Called on every\n * bar, it equals `ta.linreg`.\n *\n * @example\n * ```typescript\n * // Pine: if bar_index >= 20\n * // a := ta.linreg(close, 10, 0)\n * const site = callsite.linreg();\n * const a = closes.map((c, i) => (i >= 20 ? site(i, c, 10, 0) : NaN));\n * ```\n */\nexport function linreg(): (barIndex: number, value: number, length: number, offset?: number) => number {\n let ring: number[] = [];\n return (barIndex, value, length, offset = 0) => {\n const len = Math.floor(length);\n if (ring.length !== len + 1) ring = new Array<number>(len + 1).fill(0);\n ring[barIndex % (len + 1)] = value;\n if (barIndex < len - 1) return NaN;\n const window = Array.from({ length: len }, (_, k) => ring[(barIndex - len + 1 + k) % (len + 1)]!);\n return linregSeries(window, len, offset)[len - 1]!;\n };\n}\n\n\n/**\n * One `math.sum` call site in a loop; rules in {@link sum}. With `squares`, it also keeps the running sum of the\n * squared values (for {@link stdev}). Returns `[sum, sumOfSquares]` (NaN when not kept).\n */\nfunction loopSumSite(\n name: string,\n squares: boolean\n): (barIndex: number, value: number, length: number) => [number, number] {\n const running = new RunningSum();\n const runningSq = new RunningSum();\n let lastBar = NaN;\n let pending = false;\n return (barIndex, value, length) => {\n const len = Math.floor(length);\n if (!(len >= 1)) throw new Error(`callsite.${name}: length must be at least 1 (got ${length})`);\n if (barIndex !== lastBar) {\n if (pending) {\n running.commit();\n if (squares) runningSq.commit();\n }\n pending = false;\n lastBar = barIndex;\n }\n if (isNa(value)) {\n pending = false;\n return [running.stepNa(len), squares ? runningSq.stepNa(len) : NaN];\n }\n const x = value;\n const out: [number, number] = [running.step(x, len), squares ? runningSq.step(x * x, len) : NaN];\n pending = true;\n return out;\n };\n}\n\n/**\n * One `math.sum(source, length)` call site inside a `for` / `while` loop, called with `(barIndex, value, length)` on\n * each call.\n *\n * PineScript rules (measured on calls in loops, 1 to 3 calls per bar, and bars without a call):\n * - the call site keeps one history value per bar: the value of its last call on that bar\n * - every call on a bar starts from the state of the earlier bars, so it gives the sum of its value and the last\n * `length - 1` kept values; the last call of the bar is the one kept for the later bars\n * - the length can change from one call to the next (also on the same bar): each call first moves the kept sum to\n * its own length as `math.sum` with a series length does (Kahan steps of the raw values)\n * - a bar without a call is not in the history (as a call in an `if` block)\n * - the sum is the same compensated running sum as `math.sum` (bit for bit)\n *\n * An na value is skipped as in `math.sum` (it returns the sum of the last `length` earlier values); when it is the\n * last call of a bar, the bar is not in the history (not measured).\n *\n * @example\n * ```typescript\n * // Pine: for i = 0 to 2\n * // s = math.sum(close * (1 + 0.01 * i), 7)\n * const site = callsite.sum();\n * for (let b = 0; b < closes.length; b++) {\n * for (let i = 0; i <= 2; i++) {\n * const s = site(b, closes[b]! * (1 + 0.01 * i), 7);\n * }\n * }\n * ```\n */\nexport function sum(): (barIndex: number, value: number, length: number) => number {\n const site = loopSumSite('sum', false);\n return (barIndex, value, length) => site(barIndex, value, length)[0];\n}\n\n/**\n * One `ta.sma(source, length)` call site inside a `for` / `while` loop, called with `(barIndex, value, length)` on\n * each call: {@link sum} divided by the length (same rules).\n *\n * @example\n * ```typescript\n * // Pine: evaluate(ratio) => ta.sma(gann(ratio), n) // called once per ratio in a while loop\n * const site = callsite.sma();\n * for (let b = 0; b < closes.length; b++) {\n * for (const ratio of ratios) {\n * const avg = site(b, gann(b, ratio), n);\n * }\n * }\n * ```\n */\nexport function sma(): (barIndex: number, value: number, length: number) => number {\n const site = loopSumSite('sma', false);\n return (barIndex, value, length) => site(barIndex, value, length)[0] / Math.floor(length);\n}\n\n/**\n * One `ta.stdev(source, length, biased)` call site inside a `for` / `while` loop, called with\n * `(barIndex, value, length, biased?)` on each call: the running sums of the values and of their squares follow the\n * rules of {@link sum}; the result is computed from them as `ta.stdev` (bit for bit).\n *\n * @example\n * ```typescript\n * // Pine: for len in lengths\n * // [mid, up, lo] = ta.bb(close, len, 2)\n * const avgSite = callsite.sma();\n * const devSite = callsite.stdev();\n * for (let b = 0; b < closes.length; b++) {\n * for (const len of lengths) {\n * const mid = avgSite(b, closes[b]!, len);\n * const dev = devSite(b, closes[b]!, len);\n * }\n * }\n * ```\n */\nexport function stdev(): (barIndex: number, value: number, length: number, biased?: boolean) => number {\n const site = loopSumSite('stdev', true);\n return (barIndex, value, length, biased = true) => {\n const len = Math.floor(length);\n const [s, sq] = site(barIndex, value, length);\n const mean = s / len;\n const variance = biased ? sq / len - mean * mean : sq / (len - 1) - (mean * s) / (len - 1);\n if (!biased && len <= 1) return NaN;\n return Number.isNaN(variance) ? NaN : variance > 0 ? Math.sqrt(variance) : 0;\n };\n}\n\n/**\n * One `math.random(min, max, seed)` call site: PineScript keeps one generator per call site, so two call sites\n * with the same seed give the same sequence. Called with `(min?, max?)` once per bar.\n *\n * @example\n * ```typescript\n * // Pine: a = math.random(0, 1, 42), b = math.random(0, 1, 42) // a == b on every bar\n * const a = callsite.random(42);\n * const b = callsite.random(42);\n * ```\n */\nexport function random(seed: number): (min?: number, max?: number) => number {\n return seededRandom(seed);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6CA,gBAAuC;AAC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I,GAAG,GAAG,IAAI,GAAG;AAC/F,cAAU;AACV,WAAO;AAAA,EACT;AACF;AA8BO,SAAS,MAAmE;AACjF,QAAM,OAAO,YAAY,OAAO,KAAK;AACrC,SAAO,CAAC,UAAU,OAAO,WAAW,KAAK,UAAU,OAAO,MAAM,EAAE,CAAC;AACrE;AAiBO,SAAS,MAAmE;AACjF,QAAM,OAAO,YAAY,OAAO,KAAK;AACrC,SAAO,CAAC,UAAU,OAAO,WAAW,KAAK,UAAU,OAAO,MAAM,EAAE,CAAC,IAAI,KAAK,MAAM,MAAM;AAC1F;AAqBO,SAAS,QAAuF;AACrG,QAAM,OAAO,YAAY,SAAS,IAAI;AACtC,SAAO,CAAC,UAAU,OAAO,QAAQ,SAAS,SAAS;AACjD,UAAM,MAAM,KAAK,MAAM,MAAM;AAC7B,UAAM,CAAC,GAAG,EAAE,IAAI,KAAK,UAAU,OAAO,MAAM;AAC5C,UAAM,OAAO,IAAI;AACjB,UAAM,WAAW,SAAS,KAAK,MAAM,OAAO,OAAO,MAAM,MAAM,KAAM,OAAO,KAAM,MAAM;AACxF,QAAI,CAAC,UAAU,OAAO,EAAG,QAAO;AAChC,WAAO,OAAO,MAAM,QAAQ,IAAI,MAAM,WAAW,IAAI,KAAK,KAAK,QAAQ,IAAI;AAAA,EAC7E;AACF;AAaO,SAAS,OAAO,MAAsD;AAC3E,aAAO,0BAAa,IAAI;AAC1B;",
|
|
6
6
|
"names": ["linregSeries"]
|
|
7
7
|
}
|
|
@@ -72,7 +72,10 @@ export declare function from_hex(hex: string, transp?: simple_float): string;
|
|
|
72
72
|
* - Replaces the transparency with the new value
|
|
73
73
|
* - Useful for creating semi-transparent versions of existing colors
|
|
74
74
|
* - PineScript clamps the transparency to 0..100 (`color.new(c, 140)` is fully transparent, `color.new(c, -5)` is
|
|
75
|
-
* opaque); an `na` transparency is fully transparent
|
|
75
|
+
* opaque); an `na` transparency is fully transparent (measured: the colour is not na, its RGB is kept)
|
|
76
|
+
* - an `na` colour (null / undefined) gives black with the new transparency: `color.new(na, 30)` is
|
|
77
|
+
* `rgba(0, 0, 0, 0.7)`, as PineScript does for an na colour variable (measured, #150). In PineScript, the constant
|
|
78
|
+
* `color.new(color(na), 30)` is computed at compile time and stays na; a JS function cannot tell the two apart.
|
|
76
79
|
*
|
|
77
80
|
* @example
|
|
78
81
|
* ```typescript
|
|
@@ -82,7 +85,7 @@ export declare function from_hex(hex: string, transp?: simple_float): string;
|
|
|
82
85
|
* color.new_color(red, 100) // Returns: "rgba(255, 0, 0, 0)" - Fully transparent
|
|
83
86
|
* ```
|
|
84
87
|
*/
|
|
85
|
-
export declare function new_color(baseColor: color, transp: simple_float): string;
|
|
88
|
+
export declare function new_color(baseColor: color | null | undefined, transp: simple_float): string;
|
|
86
89
|
/**
|
|
87
90
|
* Extracts the red component from a color.
|
|
88
91
|
*
|
|
@@ -136,6 +139,7 @@ export declare function b(clr: color): int;
|
|
|
136
139
|
* - Converts alpha channel (0-1) to transparency (0-100)
|
|
137
140
|
* - PineScript stores the alpha as a byte (0-255) and returns an integer transparency:
|
|
138
141
|
* `color.t(color.new(c, 33.3))` is 33
|
|
142
|
+
* - an `na` colour (null / undefined) is fully transparent: 100
|
|
139
143
|
*
|
|
140
144
|
* @example
|
|
141
145
|
* ```typescript
|
|
@@ -144,7 +148,7 @@ export declare function b(clr: color): int;
|
|
|
144
148
|
* color.t(color.rgb(255, 0, 0, 100)) // Returns: 100 (fully transparent)
|
|
145
149
|
* ```
|
|
146
150
|
*/
|
|
147
|
-
export declare function t(clr: color): float;
|
|
151
|
+
export declare function t(clr: color | null | undefined): float;
|
|
148
152
|
/**
|
|
149
153
|
* Aqua/Cyan color constant
|
|
150
154
|
* @constant {string} #00BCD4 - RGB(0, 188, 212)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/color/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,K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,KAAK,EACnB,SAAS,EAAE,KAAK,GACf,MAAM,CA8BR;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,SAAS,IAAI,GAAG,EAAE,CAAC"}
|
package/dist/cjs/color/index.js
CHANGED
|
@@ -67,6 +67,7 @@ function from_hex(hex, transp) {
|
|
|
67
67
|
return rgb(r2, g2, b2, transp);
|
|
68
68
|
}
|
|
69
69
|
function new_color(baseColor, transp) {
|
|
70
|
+
if (baseColor === null || baseColor === void 0) return rgb(0, 0, 0, transp);
|
|
70
71
|
const rgba = (0, import_parse.parseColor)(baseColor);
|
|
71
72
|
return rgb(rgba.r, rgba.g, rgba.b, transp);
|
|
72
73
|
}
|
|
@@ -80,6 +81,7 @@ function b(clr) {
|
|
|
80
81
|
return (0, import_parse.parseColor)(clr).b;
|
|
81
82
|
}
|
|
82
83
|
function t(clr) {
|
|
84
|
+
if (clr === null || clr === void 0) return 100;
|
|
83
85
|
return Math.round((1 - (0, import_parse.alphaByte)((0, import_parse.parseColor)(clr).a) / 255) * 100);
|
|
84
86
|
}
|
|
85
87
|
const aqua = "#00BCD4";
|
|
@@ -114,7 +116,10 @@ function from_gradient(value, bottom_value, top_value, bottom_color, top_color)
|
|
|
114
116
|
const c2 = rgba(top_color);
|
|
115
117
|
const a = c1.a * (1 - k) + c2.a * k;
|
|
116
118
|
if (a === 0) return transparent;
|
|
117
|
-
const
|
|
119
|
+
const scale = a / 255;
|
|
120
|
+
const w1 = c1.a / 255;
|
|
121
|
+
const w2 = c2.a / 255;
|
|
122
|
+
const mix = (x1, x2) => Math.floor((w1 * x1 * (1 - k) + w2 * x2 * k) / scale);
|
|
118
123
|
const [r2, g2, b2] = [mix(c1.r, c2.r), mix(c1.g, c2.g), mix(c1.b, c2.b)];
|
|
119
124
|
const alpha = Math.floor(a);
|
|
120
125
|
return alpha === 255 ? `rgb(${r2}, ${g2}, ${b2})` : `rgba(${r2}, ${g2}, ${b2}, ${alpha / 255})`;
|