oakscriptjs 0.9.4 → 0.9.6

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.
Files changed (177) hide show
  1. package/dist/cjs/array/index.d.ts +88 -27
  2. package/dist/cjs/array/index.d.ts.map +1 -1
  3. package/dist/cjs/array/index.js +257 -117
  4. package/dist/cjs/array/index.js.map +3 -3
  5. package/dist/cjs/callsite/index.d.ts +122 -4
  6. package/dist/cjs/callsite/index.d.ts.map +1 -1
  7. package/dist/cjs/callsite/index.js +102 -3
  8. package/dist/cjs/callsite/index.js.map +2 -2
  9. package/dist/cjs/color/index.d.ts +7 -3
  10. package/dist/cjs/color/index.d.ts.map +1 -1
  11. package/dist/cjs/color/index.js +6 -1
  12. package/dist/cjs/color/index.js.map +2 -2
  13. package/dist/cjs/index.d.ts +1 -1
  14. package/dist/cjs/index.js +1 -1
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/label/index.d.ts.map +1 -1
  17. package/dist/cjs/label/index.js +2 -1
  18. package/dist/cjs/label/index.js.map +2 -2
  19. package/dist/cjs/linefill/index.d.ts.map +1 -1
  20. package/dist/cjs/linefill/index.js +1 -0
  21. package/dist/cjs/linefill/index.js.map +2 -2
  22. package/dist/cjs/math/index.d.ts +79 -22
  23. package/dist/cjs/math/index.d.ts.map +1 -1
  24. package/dist/cjs/math/index.js +157 -43
  25. package/dist/cjs/math/index.js.map +3 -3
  26. package/dist/cjs/matrix/index.d.ts.map +1 -1
  27. package/dist/cjs/matrix/index.js +42 -36
  28. package/dist/cjs/matrix/index.js.map +2 -2
  29. package/dist/cjs/runtime/series.d.ts +9 -0
  30. package/dist/cjs/runtime/series.d.ts.map +1 -1
  31. package/dist/cjs/runtime/series.js +16 -2
  32. package/dist/cjs/runtime/series.js.map +2 -2
  33. package/dist/cjs/script/index.d.ts +15 -31
  34. package/dist/cjs/script/index.d.ts.map +1 -1
  35. package/dist/cjs/script/index.js +32 -0
  36. package/dist/cjs/script/index.js.map +2 -2
  37. package/dist/cjs/session/calendar.d.ts.map +1 -1
  38. package/dist/cjs/session/calendar.js +15 -2
  39. package/dist/cjs/session/calendar.js.map +2 -2
  40. package/dist/cjs/str/index.d.ts +50 -38
  41. package/dist/cjs/str/index.d.ts.map +1 -1
  42. package/dist/cjs/str/index.js +43 -15
  43. package/dist/cjs/str/index.js.map +2 -2
  44. package/dist/cjs/str/messageformat.d.ts +2 -1
  45. package/dist/cjs/str/messageformat.d.ts.map +1 -1
  46. package/dist/cjs/str/messageformat.js +4 -1
  47. package/dist/cjs/str/messageformat.js.map +2 -2
  48. package/dist/cjs/str/numberformat.d.ts +4 -0
  49. package/dist/cjs/str/numberformat.d.ts.map +1 -1
  50. package/dist/cjs/str/numberformat.js +26 -4
  51. package/dist/cjs/str/numberformat.js.map +3 -3
  52. package/dist/cjs/ta/index.d.ts +95 -46
  53. package/dist/cjs/ta/index.d.ts.map +1 -1
  54. package/dist/cjs/ta/index.js +193 -318
  55. package/dist/cjs/ta/index.js.map +3 -3
  56. package/dist/cjs/ta/running-sum.d.ts +54 -1
  57. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  58. package/dist/cjs/ta/running-sum.js +132 -47
  59. package/dist/cjs/ta/running-sum.js.map +2 -2
  60. package/dist/cjs/ta-series.d.ts +4 -4
  61. package/dist/cjs/ta-series.d.ts.map +1 -1
  62. package/dist/cjs/ta-series.js +4 -3
  63. package/dist/cjs/ta-series.js.map +2 -2
  64. package/dist/cjs/time/datestring.d.ts +1 -0
  65. package/dist/cjs/time/datestring.d.ts.map +1 -1
  66. package/dist/cjs/time/datestring.js +7 -0
  67. package/dist/cjs/time/datestring.js.map +2 -2
  68. package/dist/cjs/time/index.d.ts.map +1 -1
  69. package/dist/cjs/time/index.js +2 -1
  70. package/dist/cjs/time/index.js.map +2 -2
  71. package/dist/cjs/time/session.d.ts +28 -9
  72. package/dist/cjs/time/session.d.ts.map +1 -1
  73. package/dist/cjs/time/session.js +74 -28
  74. package/dist/cjs/time/session.js.map +2 -2
  75. package/dist/cjs/time/timezone.d.ts +5 -0
  76. package/dist/cjs/time/timezone.d.ts.map +1 -1
  77. package/dist/cjs/time/timezone.js +15 -1
  78. package/dist/cjs/time/timezone.js.map +2 -2
  79. package/dist/esm/array/index.d.ts +88 -27
  80. package/dist/esm/array/index.d.ts.map +1 -1
  81. package/dist/esm/array/index.js +408 -157
  82. package/dist/esm/array/index.js.map +1 -1
  83. package/dist/esm/callsite/index.d.ts +122 -4
  84. package/dist/esm/callsite/index.d.ts.map +1 -1
  85. package/dist/esm/callsite/index.js +228 -8
  86. package/dist/esm/callsite/index.js.map +1 -1
  87. package/dist/esm/color/index.d.ts +7 -3
  88. package/dist/esm/color/index.d.ts.map +1 -1
  89. package/dist/esm/color/index.js +15 -3
  90. package/dist/esm/color/index.js.map +1 -1
  91. package/dist/esm/index.d.ts +1 -1
  92. package/dist/esm/index.js +1 -1
  93. package/dist/esm/label/index.d.ts.map +1 -1
  94. package/dist/esm/label/index.js +2 -1
  95. package/dist/esm/label/index.js.map +1 -1
  96. package/dist/esm/linefill/index.d.ts.map +1 -1
  97. package/dist/esm/linefill/index.js +3 -0
  98. package/dist/esm/linefill/index.js.map +1 -1
  99. package/dist/esm/math/index.d.ts +79 -22
  100. package/dist/esm/math/index.d.ts.map +1 -1
  101. package/dist/esm/math/index.js +229 -63
  102. package/dist/esm/math/index.js.map +1 -1
  103. package/dist/esm/matrix/index.d.ts.map +1 -1
  104. package/dist/esm/matrix/index.js +57 -43
  105. package/dist/esm/matrix/index.js.map +1 -1
  106. package/dist/esm/runtime/series.d.ts +9 -0
  107. package/dist/esm/runtime/series.d.ts.map +1 -1
  108. package/dist/esm/runtime/series.js +18 -1
  109. package/dist/esm/runtime/series.js.map +1 -1
  110. package/dist/esm/script/index.d.ts +15 -31
  111. package/dist/esm/script/index.d.ts.map +1 -1
  112. package/dist/esm/script/index.js +44 -1
  113. package/dist/esm/script/index.js.map +1 -1
  114. package/dist/esm/session/calendar.d.ts.map +1 -1
  115. package/dist/esm/session/calendar.js +22 -2
  116. package/dist/esm/session/calendar.js.map +1 -1
  117. package/dist/esm/str/index.d.ts +50 -38
  118. package/dist/esm/str/index.d.ts.map +1 -1
  119. package/dist/esm/str/index.js +103 -52
  120. package/dist/esm/str/index.js.map +1 -1
  121. package/dist/esm/str/messageformat.d.ts +2 -1
  122. package/dist/esm/str/messageformat.d.ts.map +1 -1
  123. package/dist/esm/str/messageformat.js +7 -2
  124. package/dist/esm/str/messageformat.js.map +1 -1
  125. package/dist/esm/str/numberformat.d.ts +4 -0
  126. package/dist/esm/str/numberformat.d.ts.map +1 -1
  127. package/dist/esm/str/numberformat.js +38 -4
  128. package/dist/esm/str/numberformat.js.map +1 -1
  129. package/dist/esm/ta/index.d.ts +95 -46
  130. package/dist/esm/ta/index.d.ts.map +1 -1
  131. package/dist/esm/ta/index.js +341 -414
  132. package/dist/esm/ta/index.js.map +1 -1
  133. package/dist/esm/ta/running-sum.d.ts +54 -1
  134. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  135. package/dist/esm/ta/running-sum.js +166 -60
  136. package/dist/esm/ta/running-sum.js.map +1 -1
  137. package/dist/esm/ta-series.d.ts +4 -4
  138. package/dist/esm/ta-series.d.ts.map +1 -1
  139. package/dist/esm/ta-series.js +5 -4
  140. package/dist/esm/ta-series.js.map +1 -1
  141. package/dist/esm/time/datestring.d.ts +1 -0
  142. package/dist/esm/time/datestring.d.ts.map +1 -1
  143. package/dist/esm/time/datestring.js +12 -0
  144. package/dist/esm/time/datestring.js.map +1 -1
  145. package/dist/esm/time/index.d.ts.map +1 -1
  146. package/dist/esm/time/index.js +3 -1
  147. package/dist/esm/time/index.js.map +1 -1
  148. package/dist/esm/time/session.d.ts +28 -9
  149. package/dist/esm/time/session.d.ts.map +1 -1
  150. package/dist/esm/time/session.js +113 -42
  151. package/dist/esm/time/session.js.map +1 -1
  152. package/dist/esm/time/timezone.d.ts +5 -0
  153. package/dist/esm/time/timezone.d.ts.map +1 -1
  154. package/dist/esm/time/timezone.js +21 -1
  155. package/dist/esm/time/timezone.js.map +1 -1
  156. package/package.json +2 -2
  157. package/src/array/index.ts +382 -174
  158. package/src/callsite/index.ts +232 -8
  159. package/src/color/index.ts +15 -5
  160. package/src/index.ts +1 -1
  161. package/src/label/index.ts +2 -1
  162. package/src/linefill/index.ts +2 -0
  163. package/src/math/index.ts +261 -77
  164. package/src/matrix/index.ts +52 -52
  165. package/src/runtime/series.ts +22 -1
  166. package/src/script/index.ts +53 -1
  167. package/src/session/calendar.ts +22 -2
  168. package/src/str/index.ts +95 -51
  169. package/src/str/messageformat.ts +7 -2
  170. package/src/str/numberformat.ts +39 -4
  171. package/src/ta/index.ts +356 -458
  172. package/src/ta/running-sum.ts +187 -62
  173. package/src/ta-series.ts +8 -7
  174. package/src/time/datestring.ts +10 -0
  175. package/src/time/index.ts +3 -1
  176. package/src/time/session.ts +97 -47
  177. package/src/time/timezone.ts +21 -1
@@ -8,9 +8,10 @@
8
8
  *
9
9
  * Known difference: `log10`, `exp`, `log`, `pow`, `sin`, `cos`, `tan` use JavaScript's `Math`. PineScript uses
10
10
  * other algorithms, whose results differ by 1 or 2 units in the last place on part of the inputs (`log10` about 4 %,
11
- * `exp` about 9 %, `sin` / `cos` / `tan` about 3 %, `log` 0.4 %, `pow` 0.05 % of random inputs). A constant argument
12
- * (`math.log10(2)`) can differ by up to 3 units in the last place. This is visible only when a script amplifies
13
- * these tiny differences (e.g. `ma > ma[1]` on values of 1e195); `sqrt`, `atan`, `asin`, `acos` are equal.
11
+ * `exp` about 9 %, `sin` / `cos` / `tan` about 3 %, `log` 0.4 %, `pow` 0.05 % of random inputs). This is visible
12
+ * only when a script amplifies these tiny differences (e.g. `ma > ma[1]` on values of 1e195); `sqrt`, `atan`,
13
+ * `asin`, `acos` are equal. A constant argument (`math.log10(2)`) is computed before the script runs, with other
14
+ * rules: see `constant`.
14
15
  *
15
16
  * @version 6
16
17
  */
@@ -90,20 +91,55 @@ export declare function floor(value: float): int;
90
91
  * @remarks
91
92
  * - Rounds to nearest integer by default
92
93
  * - With precision, rounds to that many decimal places
93
- * - Uses "round half up" strategy (0.5 rounds to 1)
94
94
  * - If value is a Series, returns a Series; if value is a number, returns a number
95
95
  *
96
+ * PineScript rules:
97
+ * - ties go away from zero (2.5 -> 3, -2.5 -> -3, -0.5 -> -1)
98
+ * - without a precision, or with a precision <= 0, the result is the integer rounding (1234.5678 @ -2 -> 1235)
99
+ * - a positive precision rounds only the fraction: `int + round(frac * scale) / scale` with
100
+ * `scale = 1 / 10^-precision` (precision above 16 acts as 16); the tie is decided on the exact value of
101
+ * `frac * scale` with a 1e-10 tolerance (1.005 @ 2 -> 1.01, 0.285 @ 2 -> 0.29, -1.45 @ 1 -> -1.5)
102
+ * - an infinite value gives na
103
+ *
96
104
  * @example
97
105
  * ```typescript
98
106
  * math.round(4.2) // Returns: 4
99
107
  * math.round(4.5) // Returns: 5
100
- * math.round(4.8) // Returns: 5
108
+ * math.round(-2.5) // Returns: -3
101
109
  * math.round(4.567, 2) // Returns: 4.57
102
- * math.round(4.567, 1) // Returns: 4.6
110
+ * math.round(1.005, 2) // Returns: 1.01
103
111
  * ```
104
112
  */
105
113
  export declare function round(value: Series, precision?: int): Series;
106
114
  export declare function round(value: float, precision?: int): float;
115
+ /**
116
+ * Returns the value PineScript gives to a constant float expression (not a PineScript function).
117
+ *
118
+ * @param value - The result of the constant expression, computed in JavaScript (e.g. `1 / 255`)
119
+ * @returns The value of the same expression in a PineScript script
120
+ *
121
+ * @remarks
122
+ * PineScript computes a constant expression (only literals and constants: `1 / 255`, `0.1 * 3`,
123
+ * `math.sqrt(0.1)`) before the script runs, and the value is then rounded. JavaScript cannot tell a constant from a
124
+ * series value, so wrap the constant expressions of a script with this function.
125
+ * - |value| >= 0.001: the exact value of the double is rounded to 16 decimals, ties to even
126
+ * (`1 / 255` -> 0.003921568627451, `1 / 7` -> 0.1428571428571428)
127
+ * - |value| < 0.001, na and infinities: unchanged
128
+ * - the whole expression is computed first, then rounded once (`(1 / 7) * 7` is 1)
129
+ * - math functions of constants use JavaScript's `Math` there: `math.constant(math.sqrt(0.1))` is the PineScript
130
+ * value; `math.log10` of a constant is `math.log(x) / math.log(10)` there:
131
+ * `math.constant(math.log(2) / math.log(10))` -> 0.3010299956639811
132
+ * - the same expression with a series operand (`close / 255`) is not rounded: do not wrap it
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * // Pine: var const float i_255 = 1 / 255
137
+ * const i_255 = math.constant(1 / 255) // 0.003921568627451 (JavaScript: 0.00392156862745098)
138
+ * math.constant(1 / 7) // 0.1428571428571428 (JavaScript: 0.14285714285714285)
139
+ * math.constant(1 / 3) // 0.3333333333333333 (unchanged)
140
+ * ```
141
+ */
142
+ export declare function constant(value: float): float;
107
143
  /**
108
144
  * Returns the maximum value from the arguments.
109
145
  *
@@ -156,6 +192,8 @@ export declare function min(...values: (float | Series)[]): float | Series;
156
192
  * - Calculates sum of all values divided by count
157
193
  * - Returns NaN if no arguments provided (division by zero)
158
194
  * - If any argument is a Series, returns a Series; otherwise returns a number
195
+ * - PineScript rule: with 1 or 2 values the sum is plain; with 3 values or more it is a Kahan compensated sum whose
196
+ * pending correction is subtracted before the division (math.avg(0.1, 0.2, 0.3) = 0.19999999999999998)
159
197
  *
160
198
  * @example
161
199
  * ```typescript
@@ -170,7 +208,7 @@ export declare function avg(...values: (float | Series)[]): float | Series;
170
208
  * Returns the sum of values over a sliding window.
171
209
  *
172
210
  * @param source - Series of values or array
173
- * @param length - Window length for summation
211
+ * @param length - Window length for summation, or one length per bar (a series length: array or Series)
174
212
  * @returns Series or array with rolling sum values
175
213
  *
176
214
  * @remarks
@@ -180,6 +218,11 @@ export declare function avg(...values: (float | Series)[]): float | Series;
180
218
  * - A compensated running sum, as in PineScript, so the result can differ from the exact window sum in the last
181
219
  * bits (bit for bit with PineScript)
182
220
  * - When passed a Series, returns a Series; when passed an array, returns an array
221
+ * - With a series length, PineScript moves the running sum to the new length when the length changes, with Kahan
222
+ * steps of the raw values: a smaller length removes the values that leave the window (oldest first); a larger
223
+ * length adds back the values from the one before the old window down to the one `length` values back (newest
224
+ * first), then the usual step removes the stored value of the one `length` values back. A bar with an `na` value
225
+ * gives the sum of the last `length` earlier values
183
226
  *
184
227
  * @example
185
228
  * ```typescript
@@ -187,8 +230,8 @@ export declare function avg(...values: (float | Series)[]): float | Series;
187
230
  * // Explanation: [NaN, NaN, 1+2+3, 2+3+4, 3+4+5]
188
231
  * ```
189
232
  */
190
- export declare function sum(source: Series, length: simple_int): Series;
191
- export declare function sum(source: series_float, length: simple_int): series_float;
233
+ export declare function sum(source: Series, length: simple_int | ArrayLike<number> | Series): Series;
234
+ export declare function sum(source: series_float, length: simple_int | ArrayLike<number> | Series): series_float;
192
235
  /**
193
236
  * Returns the square root of a number.
194
237
  *
@@ -485,39 +528,47 @@ export declare function toradians(degrees: float): float;
485
528
  */
486
529
  export declare function todegrees(radians: Series): Series;
487
530
  export declare function todegrees(radians: float): float;
531
+ /**
532
+ * PineScript's seeded generator: Java's linear congruential generator (`java.util.Random`), `nextDouble()`.
533
+ * Each generator draws one number per call.
534
+ */
535
+ export declare function seededRandom(seed: int): (min?: float, max?: float) => float;
536
+ /** Starts every seeded sequence of `math.random` again (called at the start of a script run). */
537
+ export declare function resetRandom(): void;
488
538
  /**
489
539
  * Returns a pseudo-random number.
490
540
  *
491
- * @param min - Optional minimum value (inclusive)
492
- * @param max - Optional maximum value (exclusive)
493
- * @param seed - Optional seed for deterministic random
541
+ * @param min - Optional minimum value (inclusive), default 0
542
+ * @param max - Optional maximum value (exclusive), default 1
543
+ * @param seed - Optional seed: a repeatable sequence, as PineScript (Java's `java.util.Random`)
494
544
  * @returns A random number in the specified range, or [0, 1) if no range specified
495
545
  *
496
546
  * @remarks
497
- * - Without arguments: returns value in [0, 1)
498
- * - With min and max: returns value in [min, max)
499
- * - **WARNING**: The seed parameter is accepted for API compatibility but NOT currently implemented
500
- * - This implementation always generates non-deterministic random values
501
- * - PineScript's version supports deterministic seeding for repeatable sequences
547
+ * - Without a seed: `Math.random()`
548
+ * - With a seed: PineScript's sequence, one number per call (measured: the call on each bar draws the next number
549
+ * of `new java.util.Random(seed).nextDouble()` from the first bar of the history). PineScript keeps one generator
550
+ * per call site; here there is one generator per seed value (started again by `executeScript`): for two call sites
551
+ * with the same seed use `callsite.random(seed)`
502
552
  *
503
553
  * @example
504
554
  * ```typescript
505
555
  * math.random() // Returns: value between 0 and 1 (e.g., 0.234...)
506
556
  * math.random(0, 10) // Returns: value between 0 and 10 (e.g., 7.45...)
507
- * math.random(5, 15) // Returns: value between 5 and 15 (e.g., 11.82...)
557
+ * math.random(0, 1, 42) // Returns: 0.7275636800328681 on the first call, as PineScript
508
558
  * ```
509
559
  */
510
- export declare function random(min?: float, max?: float, _seed?: int): float;
560
+ export declare function random(min?: float, max?: float, seed?: int): float;
511
561
  /**
512
562
  * Returns the sign of a number.
513
563
  *
514
564
  * @param value - The number to check or Series
515
- * @returns 1 for positive, -1 for negative, 0 for zero
565
+ * @returns 1 for positive, -1 for negative, 0 for zero, na for na
516
566
  *
517
567
  * @remarks
518
568
  * - Returns 1 if value > 0
519
569
  * - Returns -1 if value < 0
520
570
  * - Returns 0 if value = 0
571
+ * - Returns na if value is na (as PineScript)
521
572
  * - Useful for determining direction or polarity
522
573
  * - If value is a Series, returns a Series; if value is a number, returns a number
523
574
  *
@@ -527,6 +578,7 @@ export declare function random(min?: float, max?: float, _seed?: int): float;
527
578
  * math.sign(-3) // Returns: -1
528
579
  * math.sign(0) // Returns: 0
529
580
  * math.sign(0.001) // Returns: 1
581
+ * math.sign(NaN) // Returns: NaN
530
582
  * ```
531
583
  */
532
584
  export declare function sign(value: Series): Series;
@@ -543,9 +595,14 @@ export declare function sign(value: float): int;
543
595
  * - **JavaScript signature**: Requires explicit `mintick` OR use `createContext()` with syminfo
544
596
  * - Rounds to the nearest value divisible by mintick
545
597
  * - Returns NaN for NaN input
546
- * - Ties round up (0.5 -> 1)
547
598
  * - If number is a Series, returns a Series; if number is a float, returns a float
548
599
  *
600
+ * PineScript rules:
601
+ * - mintick is `minmove / pricescale` (here rebuilt from `mintick`: the smallest power of 10 that makes it an
602
+ * integer); the position on the tick grid is `|number| * pricescale / minmove`
603
+ * - ties go away from zero, decided on the exact value of that product with a 1e-10 tolerance
604
+ * (-1.075 -> -1.08, 1.005 -> 1.01, 2.675 -> 2.68 at mintick 0.01)
605
+ *
549
606
  * @example
550
607
  * ```typescript
551
608
  * // Direct call with explicit mintick
@@ -562,5 +619,5 @@ export declare function round_to_mintick(number: float, mintick?: float): float;
562
619
  export declare const pi: number;
563
620
  export declare const e: number;
564
621
  export declare const phi = 1.618033988749895;
565
- export declare const rphi = 0.618033988749895;
622
+ export declare const rphi = 0.6180339887498948;
566
623
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/math/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAG9C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAC3C,wBAAgB,GAAG,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC;AAmBzC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAC5C,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,GAAG,CAAC;AAmBxC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAC7C,wBAAgB,KAAK,CAAC,KAAK,EA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AK,GAAG,GAAG,CAAC;AAmBxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;AAC1E,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC;AA2ExE,eAAO,MAAM,EAAE,QAAU,CAAC;AAC1B,eAAO,MAAM,CAAC,QAAS,CAAC;AACxB,eAAO,MAAM,GAAG,oBAAoB,CAAC;AACrC,eAAO,MAAM,IAAI,qBAAqB,CAAC"}
@@ -24,6 +24,7 @@ __export(math_exports, {
24
24
  atan: () => atan,
25
25
  avg: () => avg,
26
26
  ceil: () => ceil,
27
+ constant: () => constant,
27
28
  cos: () => cos,
28
29
  e: () => e,
29
30
  exp: () => exp,
@@ -36,9 +37,11 @@ __export(math_exports, {
36
37
  pi: () => pi,
37
38
  pow: () => pow,
38
39
  random: () => random,
40
+ resetRandom: () => resetRandom,
39
41
  round: () => round,
40
42
  round_to_mintick: () => round_to_mintick,
41
43
  rphi: () => rphi,
44
+ seededRandom: () => seededRandom,
42
45
  sign: () => sign,
43
46
  sin: () => sin,
44
47
  sqrt: () => sqrt,
@@ -96,27 +99,75 @@ function round(value, precision) {
96
99
  if (value instanceof import_series.Series) {
97
100
  const bars = value.bars;
98
101
  const valueArray = value.toArray();
99
- const length = bars.length;
100
102
  const result = [];
101
- if (precision === void 0) {
102
- for (let i = 0; i < length; i++) {
103
- const v = valueArray[i] ?? NaN;
104
- result.push(Math.round(v));
105
- }
106
- } else {
107
- const multiplier2 = Math.pow(10, precision);
108
- for (let i = 0; i < length; i++) {
109
- const v = valueArray[i] ?? NaN;
110
- result.push(Math.round(v * multiplier2) / multiplier2);
111
- }
103
+ for (let i = 0; i < bars.length; i++) {
104
+ result.push(roundValue(valueArray[i] ?? NaN, precision));
112
105
  }
113
106
  return import_series.Series.fromArray(bars, result);
114
107
  }
115
- if (precision === void 0) {
116
- return Math.round(value);
117
- }
118
- const multiplier = Math.pow(10, precision);
119
- return Math.round(value * multiplier) / multiplier;
108
+ return roundValue(value, precision);
109
+ }
110
+ const TIE_SCALE = 10n ** 10n;
111
+ const TIE_HALF = 5n * 10n ** 9n - 1n;
112
+ const INT_LIMIT = 2 ** 52;
113
+ function exactRatio(x) {
114
+ const view = new DataView(new ArrayBuffer(8));
115
+ view.setFloat64(0, x);
116
+ const hi = view.getUint32(0);
117
+ const lo = view.getUint32(4);
118
+ const expField = hi >>> 20 & 2047;
119
+ let mantissa = BigInt(hi & 1048575) << 32n | BigInt(lo);
120
+ let exp2 = -1074;
121
+ if (expField !== 0) {
122
+ mantissa |= 1n << 52n;
123
+ exp2 = expField - 1075;
124
+ }
125
+ return exp2 >= 0 ? [mantissa << BigInt(exp2), 1n] : [mantissa, 1n << BigInt(-exp2)];
126
+ }
127
+ function reachesTie(num, den, units) {
128
+ return (num - units * den) * TIE_SCALE >= TIE_HALF * den;
129
+ }
130
+ function tenToMinus(p) {
131
+ return Number.isInteger(p) ? Number(`1e-${p}`) : Math.pow(10, -p);
132
+ }
133
+ function roundValue(value, precision) {
134
+ if (!Number.isFinite(value)) return NaN;
135
+ const negative = value < 0;
136
+ const magnitude = negative ? -value : value;
137
+ if (precision === void 0 || Number.isNaN(precision) || precision <= 0) {
138
+ if (magnitude >= INT_LIMIT) return value;
139
+ let units2 = Math.floor(magnitude);
140
+ if (magnitude - units2 >= 0.5) units2++;
141
+ return negative ? 0 - units2 : units2;
142
+ }
143
+ const scale = 1 / tenToMinus(Math.min(precision, 16));
144
+ const fraction = magnitude % 1;
145
+ const scaled = fraction * scale;
146
+ let units = Math.floor(scaled);
147
+ const offset = scaled - units - 0.5;
148
+ const slack = 1e-9 + scaled * 18e-16;
149
+ if (-slack < offset && offset < slack) {
150
+ const [fn, fd] = exactRatio(fraction);
151
+ const [sn, sd] = exactRatio(scale);
152
+ if (reachesTie(fn * sn, fd * sd, BigInt(units))) units++;
153
+ } else if (offset > 0) {
154
+ units++;
155
+ }
156
+ const result = magnitude - fraction + units / scale;
157
+ return negative ? 0 - result : result;
158
+ }
159
+ const CONSTANT_SCALE = 10n ** 16n;
160
+ function constant(value) {
161
+ const magnitude = Math.abs(value);
162
+ if (!(magnitude >= 1e-3 && magnitude < 0.5)) return value;
163
+ const [num, den] = exactRatio(magnitude);
164
+ const scaled = num * CONSTANT_SCALE;
165
+ let units = scaled / den;
166
+ const twiceRest = (scaled - units * den) * 2n;
167
+ if (twiceRest > den || twiceRest === den && (units & 1n) === 1n) units++;
168
+ const digits = units.toString().padStart(17, "0");
169
+ const result = Number(`${digits.slice(0, -16)}.${digits.slice(-16)}`);
170
+ return value < 0 ? -result : result;
120
171
  }
121
172
  function max(...values) {
122
173
  const hasSeries = values.some((v) => v instanceof import_series.Series);
@@ -161,7 +212,7 @@ function min(...values) {
161
212
  function avg(...values) {
162
213
  const hasSeries = values.some((v) => v instanceof import_series.Series);
163
214
  if (!hasSeries) {
164
- return values.reduce((a, b) => a + b, 0) / values.length;
215
+ return avgOf(values);
165
216
  }
166
217
  const firstSeries = values.find((v) => v instanceof import_series.Series);
167
218
  const bars = firstSeries.bars;
@@ -174,14 +225,29 @@ function avg(...values) {
174
225
  const nums = values.map(
175
226
  (v, idx) => valueArrays[idx] ? valueArrays[idx][i] ?? NaN : v
176
227
  );
177
- const sum2 = nums.reduce((a, b) => a + b, 0);
178
- result.push(sum2 / nums.length);
228
+ result.push(avgOf(nums));
179
229
  }
180
230
  return import_series.Series.fromArray(bars, result);
181
231
  }
232
+ function avgOf(nums) {
233
+ if (nums.length <= 2) {
234
+ let sum3 = 0;
235
+ for (const n of nums) sum3 = sum3 + n;
236
+ return sum3 / nums.length;
237
+ }
238
+ let sum2 = 0;
239
+ let c = 0;
240
+ for (const n of nums) {
241
+ const y = n - c;
242
+ const t = sum2 + y;
243
+ c = t - sum2 - y;
244
+ sum2 = t;
245
+ }
246
+ return (sum2 - c) / nums.length;
247
+ }
182
248
  function sum(source, length) {
183
249
  const sourceArray = source instanceof import_series.Series ? source.toArray() : source;
184
- const result = (0, import_running_sum.runningSum)(sourceArray, length);
250
+ const result = typeof length === "number" ? (0, import_running_sum.runningSum)(sourceArray, length) : (0, import_running_sum.runningSumSeries)(sourceArray, length instanceof import_series.Series ? length.toArray() : length, "math.sum");
185
251
  if (source instanceof import_series.Series) {
186
252
  return import_series.Series.fromArray(source.bars, result);
187
253
  }
@@ -373,7 +439,33 @@ function todegrees(radians) {
373
439
  }
374
440
  return radians * (180 / Math.PI);
375
441
  }
376
- function random(min2, max2, _seed) {
442
+ function seededRandom(seed) {
443
+ const mask = (1n << 48n) - 1n;
444
+ let state = (BigInt(Math.trunc(seed)) ^ 0x5deece66dn) & mask;
445
+ const next = (bits) => {
446
+ state = 0x5deece66dn * state + 11n & mask;
447
+ return state >> 48n - bits;
448
+ };
449
+ return (min2 = 0, max2 = 1) => {
450
+ const hi = next(26n);
451
+ const lo = next(27n);
452
+ const unit = Number((hi << 27n) + lo) / 2 ** 53;
453
+ return min2 + unit * (max2 - min2);
454
+ };
455
+ }
456
+ const seeded = /* @__PURE__ */ new Map();
457
+ function resetRandom() {
458
+ seeded.clear();
459
+ }
460
+ function random(min2, max2, seed) {
461
+ if (seed !== void 0 && seed !== null && !Number.isNaN(seed)) {
462
+ let gen = seeded.get(seed);
463
+ if (!gen) {
464
+ gen = seededRandom(seed);
465
+ seeded.set(seed, gen);
466
+ }
467
+ return gen(min2 ?? 0, max2 ?? 1);
468
+ }
377
469
  const rand = Math.random();
378
470
  if (min2 !== void 0 && max2 !== void 0) {
379
471
  return min2 + rand * (max2 - min2);
@@ -388,36 +480,24 @@ function sign(value) {
388
480
  const result = [];
389
481
  for (let i = 0; i < length; i++) {
390
482
  const v = valueArray[i] ?? NaN;
391
- result.push(v > 0 ? 1 : v < 0 ? -1 : 0);
483
+ result.push(v > 0 ? 1 : v < 0 ? -1 : v === 0 ? 0 : NaN);
392
484
  }
393
485
  return import_series.Series.fromArray(bars, result);
394
486
  }
395
- return value > 0 ? 1 : value < 0 ? -1 : 0;
487
+ return value > 0 ? 1 : value < 0 ? -1 : value === 0 ? 0 : NaN;
396
488
  }
397
489
  function round_to_mintick(number, mintick) {
398
490
  if (number instanceof import_series.Series) {
399
491
  const bars = number.bars;
400
492
  const valueArray = number.toArray();
401
- const length = bars.length;
402
493
  if (mintick === void 0) {
403
494
  throw new Error(
404
495
  "math.round_to_mintick() requires mintick value. Either pass it explicitly or use createContext({ syminfo: { mintick } }) for implicit data."
405
496
  );
406
497
  }
407
498
  const result = [];
408
- if (mintick === 0) {
409
- for (let i = 0; i < length; i++) {
410
- result.push(valueArray[i] ?? NaN);
411
- }
412
- } else {
413
- for (let i = 0; i < length; i++) {
414
- const v = valueArray[i];
415
- if (v === void 0 || isNaN(v)) {
416
- result.push(NaN);
417
- } else {
418
- result.push(Math.round(v / mintick) * mintick);
419
- }
420
- }
499
+ for (let i = 0; i < bars.length; i++) {
500
+ result.push(roundToTick(valueArray[i] ?? NaN, mintick));
421
501
  }
422
502
  return import_series.Series.fromArray(bars, result);
423
503
  }
@@ -429,13 +509,47 @@ function round_to_mintick(number, mintick) {
429
509
  "math.round_to_mintick() requires mintick value. Either pass it explicitly or use createContext({ syminfo: { mintick } }) for implicit data."
430
510
  );
431
511
  }
432
- if (mintick === 0) {
433
- return number;
434
- }
435
- return Math.round(number / mintick) * mintick;
512
+ return roundToTick(number, mintick);
513
+ }
514
+ function tickPair(mintick) {
515
+ for (let k = 0; k <= 15; k++) {
516
+ const pricescale = Number(`1e${k}`);
517
+ const scaled = mintick * pricescale;
518
+ const minmove = Math.round(scaled);
519
+ if (minmove >= 1 && Math.abs(scaled - minmove) <= 1e-9 * minmove) return [minmove, pricescale];
520
+ }
521
+ return null;
522
+ }
523
+ function roundToTick(number, mintick) {
524
+ if (!Number.isFinite(number)) return NaN;
525
+ if (mintick === 0) return number;
526
+ const pair = tickPair(mintick);
527
+ const negative = number < 0;
528
+ const magnitude = negative ? -number : number;
529
+ if (pair === null) {
530
+ const scaled2 = magnitude / mintick;
531
+ let units2 = Math.trunc(scaled2);
532
+ if (scaled2 - units2 >= 0.5) units2++;
533
+ return (negative ? 0 - units2 : units2) * mintick;
534
+ }
535
+ const [minmove, pricescale] = pair;
536
+ const scaled = magnitude * pricescale / minmove;
537
+ let units = Math.trunc(scaled);
538
+ const offset = scaled - units - 0.5;
539
+ const slack = 1e-6 + scaled * 18e-16;
540
+ if (-slack < offset && offset < slack) {
541
+ const [n, d] = exactRatio(magnitude);
542
+ const num = n * BigInt(pricescale);
543
+ const den = d * BigInt(minmove);
544
+ const whole = num / den;
545
+ units = Number(whole) + (reachesTie(num, den, whole) ? 1 : 0);
546
+ } else if (offset > 0) {
547
+ units++;
548
+ }
549
+ return (negative ? 0 - units : units) * minmove / pricescale;
436
550
  }
437
551
  const pi = Math.PI;
438
552
  const e = Math.E;
439
553
  const phi = 1.618033988749895;
440
- const rphi = 0.618033988749895;
554
+ const rphi = 0.6180339887498948;
441
555
  //# sourceMappingURL=index.js.map