oakscriptjs 0.24.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) hide show
  1. package/README.md +25 -1
  2. package/bin/oakscript.mjs +10 -3
  3. package/dist/cjs/box/index.d.ts +348 -289
  4. package/dist/cjs/box/index.d.ts.map +1 -1
  5. package/dist/cjs/box/index.js.map +2 -2
  6. package/dist/cjs/check/node.d.ts.map +1 -1
  7. package/dist/cjs/check/node.js +1 -0
  8. package/dist/cjs/check/node.js.map +2 -2
  9. package/dist/cjs/check/preview.d.ts +31 -0
  10. package/dist/cjs/check/preview.d.ts.map +1 -0
  11. package/dist/cjs/check/preview.js +303 -0
  12. package/dist/cjs/check/preview.js.map +7 -0
  13. package/dist/cjs/color/index.d.ts +11 -11
  14. package/dist/cjs/color/index.js.map +1 -1
  15. package/dist/cjs/color/parse.d.ts +2 -0
  16. package/dist/cjs/color/parse.d.ts.map +1 -1
  17. package/dist/cjs/color/parse.js +6 -0
  18. package/dist/cjs/color/parse.js.map +2 -2
  19. package/dist/cjs/compare/index.d.ts +76 -12
  20. package/dist/cjs/compare/index.d.ts.map +1 -1
  21. package/dist/cjs/compare/index.js.map +2 -2
  22. package/dist/cjs/drawing/registry.d.ts +28 -5
  23. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  24. package/dist/cjs/drawing/registry.js +14 -0
  25. package/dist/cjs/drawing/registry.js.map +2 -2
  26. package/dist/cjs/index.d.ts +1 -1
  27. package/dist/cjs/index.js +1 -1
  28. package/dist/cjs/index.js.map +1 -1
  29. package/dist/cjs/label/index.d.ts +265 -187
  30. package/dist/cjs/label/index.d.ts.map +1 -1
  31. package/dist/cjs/label/index.js +1 -1
  32. package/dist/cjs/label/index.js.map +2 -2
  33. package/dist/cjs/line/index.d.ts +247 -207
  34. package/dist/cjs/line/index.d.ts.map +1 -1
  35. package/dist/cjs/line/index.js.map +2 -2
  36. package/dist/cjs/linefill/index.d.ts +61 -89
  37. package/dist/cjs/linefill/index.d.ts.map +1 -1
  38. package/dist/cjs/linefill/index.js.map +2 -2
  39. package/dist/cjs/math/index.d.ts +49 -40
  40. package/dist/cjs/math/index.d.ts.map +1 -1
  41. package/dist/cjs/math/index.js.map +2 -2
  42. package/dist/cjs/polyline/index.d.ts +57 -78
  43. package/dist/cjs/polyline/index.d.ts.map +1 -1
  44. package/dist/cjs/polyline/index.js.map +2 -2
  45. package/dist/cjs/runtime/input-active.d.ts +2 -2
  46. package/dist/cjs/runtime/input-active.js.map +1 -1
  47. package/dist/cjs/runtime/series.d.ts +241 -161
  48. package/dist/cjs/runtime/series.d.ts.map +1 -1
  49. package/dist/cjs/runtime/series.js +223 -127
  50. package/dist/cjs/runtime/series.js.map +2 -2
  51. package/dist/cjs/script/api/alert.d.ts +37 -0
  52. package/dist/cjs/script/api/alert.d.ts.map +1 -0
  53. package/dist/cjs/script/api/alert.js +17 -0
  54. package/dist/cjs/script/api/alert.js.map +7 -0
  55. package/dist/cjs/script/api/calc.d.ts +232 -0
  56. package/dist/cjs/script/api/calc.d.ts.map +1 -0
  57. package/dist/cjs/script/api/calc.js +17 -0
  58. package/dist/cjs/script/api/calc.js.map +7 -0
  59. package/dist/cjs/script/api/color.d.ts +213 -0
  60. package/dist/cjs/script/api/color.d.ts.map +1 -0
  61. package/dist/cjs/script/api/color.js +17 -0
  62. package/dist/cjs/script/api/color.js.map +7 -0
  63. package/dist/cjs/script/api/context.d.ts +764 -0
  64. package/dist/cjs/script/api/context.d.ts.map +1 -0
  65. package/dist/cjs/script/api/context.js +17 -0
  66. package/dist/cjs/script/api/context.js.map +7 -0
  67. package/dist/cjs/script/api/log.d.ts +58 -0
  68. package/dist/cjs/script/api/log.d.ts.map +1 -0
  69. package/dist/cjs/script/api/log.js +17 -0
  70. package/dist/cjs/script/api/log.js.map +7 -0
  71. package/dist/cjs/script/api/strategy.d.ts +391 -0
  72. package/dist/cjs/script/api/strategy.d.ts.map +1 -0
  73. package/dist/cjs/script/api/strategy.js +17 -0
  74. package/dist/cjs/script/api/strategy.js.map +7 -0
  75. package/dist/cjs/script/api/ta.d.ts +1012 -0
  76. package/dist/cjs/script/api/ta.d.ts.map +1 -0
  77. package/dist/cjs/script/api/ta.js +17 -0
  78. package/dist/cjs/script/api/ta.js.map +7 -0
  79. package/dist/cjs/script/api/table.d.ts +479 -0
  80. package/dist/cjs/script/api/table.d.ts.map +1 -0
  81. package/dist/cjs/script/api/table.js +17 -0
  82. package/dist/cjs/script/api/table.js.map +7 -0
  83. package/dist/cjs/script/constants.d.ts +228 -83
  84. package/dist/cjs/script/constants.d.ts.map +1 -1
  85. package/dist/cjs/script/constants.js +87 -0
  86. package/dist/cjs/script/constants.js.map +2 -2
  87. package/dist/cjs/script/define.d.ts +94 -0
  88. package/dist/cjs/script/define.d.ts.map +1 -0
  89. package/dist/cjs/script/define.js +127 -0
  90. package/dist/cjs/script/define.js.map +7 -0
  91. package/dist/cjs/script/drawing-args.d.ts.map +1 -1
  92. package/dist/cjs/script/drawing-args.js +1 -1
  93. package/dist/cjs/script/drawing-args.js.map +2 -2
  94. package/dist/cjs/script/formula.d.ts +53 -0
  95. package/dist/cjs/script/formula.d.ts.map +1 -0
  96. package/dist/cjs/script/formula.js +402 -0
  97. package/dist/cjs/script/formula.js.map +7 -0
  98. package/dist/cjs/script/globals.d.ts +10 -0
  99. package/dist/cjs/script/globals.d.ts.map +1 -1
  100. package/dist/cjs/script/globals.js.map +1 -1
  101. package/dist/cjs/script/index.d.ts +1067 -1006
  102. package/dist/cjs/script/index.d.ts.map +1 -1
  103. package/dist/cjs/script/index.js +530 -60
  104. package/dist/cjs/script/index.js.map +3 -3
  105. package/dist/cjs/script/json.d.ts +117 -0
  106. package/dist/cjs/script/json.d.ts.map +1 -0
  107. package/dist/cjs/script/json.js +321 -0
  108. package/dist/cjs/script/json.js.map +7 -0
  109. package/dist/cjs/str/index.d.ts +50 -75
  110. package/dist/cjs/str/index.d.ts.map +1 -1
  111. package/dist/cjs/str/index.js +2 -2
  112. package/dist/cjs/str/index.js.map +2 -2
  113. package/dist/cjs/strategy/index.d.ts +102 -1
  114. package/dist/cjs/strategy/index.d.ts.map +1 -1
  115. package/dist/cjs/strategy/index.js.map +2 -2
  116. package/dist/cjs/table/index.d.ts +330 -49
  117. package/dist/cjs/table/index.d.ts.map +1 -1
  118. package/dist/cjs/table/index.js +55 -27
  119. package/dist/cjs/table/index.js.map +2 -2
  120. package/dist/cjs/text/index.d.ts +12 -14
  121. package/dist/cjs/text/index.d.ts.map +1 -1
  122. package/dist/cjs/text/index.js.map +2 -2
  123. package/dist/cjs/types/index.d.ts +15 -8
  124. package/dist/cjs/types/index.d.ts.map +1 -1
  125. package/dist/cjs/types/index.js +12 -3
  126. package/dist/cjs/types/index.js.map +2 -2
  127. package/dist/esm/box/index.d.ts +348 -289
  128. package/dist/esm/box/index.d.ts.map +1 -1
  129. package/dist/esm/box/index.js +276 -276
  130. package/dist/esm/box/index.js.map +1 -1
  131. package/dist/esm/check/node.d.ts.map +1 -1
  132. package/dist/esm/check/node.js +1 -0
  133. package/dist/esm/check/node.js.map +1 -1
  134. package/dist/esm/check/preview.d.ts +31 -0
  135. package/dist/esm/check/preview.d.ts.map +1 -0
  136. package/dist/esm/check/preview.js +296 -0
  137. package/dist/esm/check/preview.js.map +1 -0
  138. package/dist/esm/color/index.d.ts +11 -11
  139. package/dist/esm/color/index.js +11 -11
  140. package/dist/esm/color/parse.d.ts +2 -0
  141. package/dist/esm/color/parse.d.ts.map +1 -1
  142. package/dist/esm/color/parse.js +7 -0
  143. package/dist/esm/color/parse.js.map +1 -1
  144. package/dist/esm/compare/index.d.ts +76 -12
  145. package/dist/esm/compare/index.d.ts.map +1 -1
  146. package/dist/esm/compare/index.js +76 -12
  147. package/dist/esm/compare/index.js.map +1 -1
  148. package/dist/esm/drawing/registry.d.ts +28 -5
  149. package/dist/esm/drawing/registry.d.ts.map +1 -1
  150. package/dist/esm/drawing/registry.js +39 -3
  151. package/dist/esm/drawing/registry.js.map +1 -1
  152. package/dist/esm/index.d.ts +1 -1
  153. package/dist/esm/index.js +1 -1
  154. package/dist/esm/label/index.d.ts +265 -187
  155. package/dist/esm/label/index.d.ts.map +1 -1
  156. package/dist/esm/label/index.js +207 -177
  157. package/dist/esm/label/index.js.map +1 -1
  158. package/dist/esm/line/index.d.ts +247 -207
  159. package/dist/esm/line/index.d.ts.map +1 -1
  160. package/dist/esm/line/index.js +202 -194
  161. package/dist/esm/line/index.js.map +1 -1
  162. package/dist/esm/linefill/index.d.ts +61 -89
  163. package/dist/esm/linefill/index.d.ts.map +1 -1
  164. package/dist/esm/linefill/index.js +61 -89
  165. package/dist/esm/linefill/index.js.map +1 -1
  166. package/dist/esm/math/index.d.ts +49 -40
  167. package/dist/esm/math/index.d.ts.map +1 -1
  168. package/dist/esm/math/index.js +22 -9
  169. package/dist/esm/math/index.js.map +1 -1
  170. package/dist/esm/polyline/index.d.ts +57 -78
  171. package/dist/esm/polyline/index.d.ts.map +1 -1
  172. package/dist/esm/polyline/index.js +57 -78
  173. package/dist/esm/polyline/index.js.map +1 -1
  174. package/dist/esm/runtime/input-active.d.ts +2 -2
  175. package/dist/esm/runtime/input-active.js +2 -2
  176. package/dist/esm/runtime/series.d.ts +241 -161
  177. package/dist/esm/runtime/series.d.ts.map +1 -1
  178. package/dist/esm/runtime/series.js +241 -161
  179. package/dist/esm/runtime/series.js.map +1 -1
  180. package/dist/esm/script/api/alert.d.ts +37 -0
  181. package/dist/esm/script/api/alert.d.ts.map +1 -0
  182. package/dist/esm/script/api/alert.js +7 -0
  183. package/dist/esm/script/api/alert.js.map +1 -0
  184. package/dist/esm/script/api/calc.d.ts +232 -0
  185. package/dist/esm/script/api/calc.d.ts.map +1 -0
  186. package/dist/esm/script/api/calc.js +6 -0
  187. package/dist/esm/script/api/calc.js.map +1 -0
  188. package/dist/esm/script/api/color.d.ts +213 -0
  189. package/dist/esm/script/api/color.d.ts.map +1 -0
  190. package/dist/esm/script/api/color.js +6 -0
  191. package/dist/esm/script/api/color.js.map +1 -0
  192. package/dist/esm/script/api/context.d.ts +764 -0
  193. package/dist/esm/script/api/context.d.ts.map +1 -0
  194. package/dist/esm/script/api/context.js +7 -0
  195. package/dist/esm/script/api/context.js.map +1 -0
  196. package/dist/esm/script/api/log.d.ts +58 -0
  197. package/dist/esm/script/api/log.d.ts.map +1 -0
  198. package/dist/esm/script/api/log.js +6 -0
  199. package/dist/esm/script/api/log.js.map +1 -0
  200. package/dist/esm/script/api/strategy.d.ts +391 -0
  201. package/dist/esm/script/api/strategy.d.ts.map +1 -0
  202. package/dist/esm/script/api/strategy.js +7 -0
  203. package/dist/esm/script/api/strategy.js.map +1 -0
  204. package/dist/esm/script/api/ta.d.ts +1012 -0
  205. package/dist/esm/script/api/ta.d.ts.map +1 -0
  206. package/dist/esm/script/api/ta.js +6 -0
  207. package/dist/esm/script/api/ta.js.map +1 -0
  208. package/dist/esm/script/api/table.d.ts +479 -0
  209. package/dist/esm/script/api/table.d.ts.map +1 -0
  210. package/dist/esm/script/api/table.js +7 -0
  211. package/dist/esm/script/api/table.js.map +1 -0
  212. package/dist/esm/script/constants.d.ts +228 -83
  213. package/dist/esm/script/constants.d.ts.map +1 -1
  214. package/dist/esm/script/constants.js +110 -24
  215. package/dist/esm/script/constants.js.map +1 -1
  216. package/dist/esm/script/define.d.ts +94 -0
  217. package/dist/esm/script/define.d.ts.map +1 -0
  218. package/dist/esm/script/define.js +136 -0
  219. package/dist/esm/script/define.js.map +1 -0
  220. package/dist/esm/script/drawing-args.d.ts.map +1 -1
  221. package/dist/esm/script/drawing-args.js +2 -1
  222. package/dist/esm/script/drawing-args.js.map +1 -1
  223. package/dist/esm/script/formula.d.ts +53 -0
  224. package/dist/esm/script/formula.d.ts.map +1 -0
  225. package/dist/esm/script/formula.js +408 -0
  226. package/dist/esm/script/formula.js.map +1 -0
  227. package/dist/esm/script/globals.d.ts +10 -0
  228. package/dist/esm/script/globals.d.ts.map +1 -1
  229. package/dist/esm/script/globals.js.map +1 -1
  230. package/dist/esm/script/index.d.ts +1067 -1006
  231. package/dist/esm/script/index.d.ts.map +1 -1
  232. package/dist/esm/script/index.js +1096 -151
  233. package/dist/esm/script/index.js.map +1 -1
  234. package/dist/esm/script/json.d.ts +117 -0
  235. package/dist/esm/script/json.d.ts.map +1 -0
  236. package/dist/esm/script/json.js +375 -0
  237. package/dist/esm/script/json.js.map +1 -0
  238. package/dist/esm/str/index.d.ts +50 -75
  239. package/dist/esm/str/index.d.ts.map +1 -1
  240. package/dist/esm/str/index.js +51 -76
  241. package/dist/esm/str/index.js.map +1 -1
  242. package/dist/esm/strategy/index.d.ts +102 -1
  243. package/dist/esm/strategy/index.d.ts.map +1 -1
  244. package/dist/esm/strategy/index.js +1 -1
  245. package/dist/esm/strategy/index.js.map +1 -1
  246. package/dist/esm/table/index.d.ts +330 -49
  247. package/dist/esm/table/index.d.ts.map +1 -1
  248. package/dist/esm/table/index.js +372 -61
  249. package/dist/esm/table/index.js.map +1 -1
  250. package/dist/esm/text/index.d.ts +12 -14
  251. package/dist/esm/text/index.d.ts.map +1 -1
  252. package/dist/esm/text/index.js +12 -14
  253. package/dist/esm/text/index.js.map +1 -1
  254. package/dist/esm/types/index.d.ts +15 -8
  255. package/dist/esm/types/index.d.ts.map +1 -1
  256. package/dist/esm/types/index.js +15 -8
  257. package/dist/esm/types/index.js.map +1 -1
  258. package/docs/first-indicator.md +152 -0
  259. package/docs/guide.md +1185 -0
  260. package/docs/inventory.md +772 -0
  261. package/docs/no-code.md +207 -0
  262. package/docs/recipes.md +202 -0
  263. package/package.json +2 -1
  264. package/skills/oakscript/SKILL.md +8 -0
  265. package/skills/oakscript/reference/errors.md +3 -2
  266. package/src/box/index.ts +348 -289
  267. package/src/check/node.ts +1 -0
  268. package/src/check/preview.ts +307 -0
  269. package/src/color/index.ts +11 -11
  270. package/src/color/parse.ts +7 -0
  271. package/src/compare/index.ts +76 -12
  272. package/src/drawing/registry.ts +44 -5
  273. package/src/index.ts +1 -1
  274. package/src/label/index.ts +267 -189
  275. package/src/line/index.ts +248 -208
  276. package/src/linefill/index.ts +61 -89
  277. package/src/math/index.ts +51 -42
  278. package/src/polyline/index.ts +57 -78
  279. package/src/runtime/input-active.ts +2 -2
  280. package/src/runtime/series.ts +243 -163
  281. package/src/script/api/alert.ts +43 -0
  282. package/src/script/api/calc.ts +250 -0
  283. package/src/script/api/color.ts +245 -0
  284. package/src/script/api/context.ts +944 -0
  285. package/src/script/api/log.ts +60 -0
  286. package/src/script/api/strategy.ts +410 -0
  287. package/src/script/api/ta.ts +1115 -0
  288. package/src/script/api/table.ts +538 -0
  289. package/src/script/constants.ts +229 -25
  290. package/src/script/define.ts +221 -0
  291. package/src/script/drawing-args.ts +2 -1
  292. package/src/script/formula.ts +438 -0
  293. package/src/script/globals.ts +10 -0
  294. package/src/script/index.ts +1519 -239
  295. package/src/script/json.ts +457 -0
  296. package/src/str/index.ts +51 -76
  297. package/src/strategy/index.ts +102 -1
  298. package/src/table/index.ts +388 -62
  299. package/src/text/index.ts +12 -14
  300. package/src/types/index.ts +15 -8
@@ -5,11 +5,8 @@
5
5
  */
6
6
  import * as compare from '../compare/index.js';
7
7
  /**
8
- * BarData - Versioned wrapper around Bar[] array for cache invalidation
9
- *
10
- * This class wraps a Bar[] array and tracks a version number that increments
11
- * whenever the data is mutated. Series objects reference this to detect when
12
- * their cached values need to be recomputed.
8
+ * Host-side: an array of bars with a version number that grows on every change, so the Series made on it know when
9
+ * their cached values are stale. Scripts do not use it: their Series are made on the chart's bars.
13
10
  *
14
11
  * @example
15
12
  * ```typescript
@@ -124,35 +121,6 @@ export class BarData {
124
121
  return new BarData(bars);
125
122
  }
126
123
  }
127
- /**
128
- * Series - represents a time-series of values with lazy evaluation
129
- *
130
- * Series are lazy - they don't compute values until needed.
131
- * Operations on Series return new Series with composed extractors.
132
- *
133
- * Now supports automatic cache invalidation via BarData versioning
134
- * and memory-efficient closure chain breaking via materialize().
135
- *
136
- * @example
137
- * ```typescript
138
- * // Basic usage (backward compatible)
139
- * const close = Series.fromBars(bars, 'close');
140
- * const open = Series.fromBars(bars, 'open');
141
- * const range = close.sub(open); // Creates new Series, doesn't compute yet
142
- * const values = range.toArray(); // Now computes for all bars
143
- *
144
- * // With automatic cache invalidation
145
- * const barData = new BarData(bars);
146
- * const close = Series.fromBars(barData, 'close');
147
- * const values1 = close.toArray(); // Computes and caches
148
- * barData.push(newBar); // Version increments
149
- * const values2 = close.toArray(); // Detects stale cache, recomputes
150
- *
151
- * // Breaking closure chains for memory efficiency
152
- * const complex = a.add(b).mul(c).div(d).sub(e);
153
- * const materialized = complex.materialize(); // Breaks closure chain
154
- * ```
155
- */
156
124
  /** Largest history reference of a script series (PineScript max_bars_back limit). */
157
125
  export const MAX_BARS_BACK = 5000;
158
126
  /** PineScript's runtime error RE10008 text. */
@@ -165,39 +133,53 @@ let runHooks = null;
165
133
  export function setSeriesRunHooks(hooks) {
166
134
  runHooks = hooks;
167
135
  }
136
+ /**
137
+ * A series: one number per bar, oldest bar first, NaN where the value is missing (na). `close`, `open`, `high`,
138
+ * `low`, `volume`, the `ta.*` results and `input.source()` are Series.
139
+ *
140
+ * JavaScript operators do not work on a Series (`close - open` throws): use its methods, which work on all the bars
141
+ * at once and return a new Series. The other operand can be a Series or a plain number. Comparisons and logic give
142
+ * a Series of 1 (true) and 0 (false). Inside `eachBar()`, read the number of the current bar with `c.get(series)`.
143
+ * Values are computed when first read, then kept.
144
+ *
145
+ * @example
146
+ * ```js
147
+ * const body = close.sub(open);
148
+ * const up = close.gt(open);
149
+ * plot(up.iff(body, body.neg()), "Body size");
150
+ * ```
151
+ */
168
152
  export class Series {
169
153
  extractor;
170
154
  dataSource;
171
155
  cached = null;
172
156
  cachedVersion = -1;
173
157
  /**
174
- * Create a new Series
175
- *
176
- * @param data - Bar data (Bar[] or BarData)
177
- * @param extractor - Function to extract/compute value for each bar
158
+ * True for the built-in price series (`open`, `high`, `low`, `close`, `volume`, `hl2`...): their history covers
159
+ * every bar, so `offset()` may reach further than 5000 bars back.
178
160
  */
161
+ fullHistory = false;
179
162
  /**
180
- * True for PineScript's built-in price variables (open, high, low, close, volume, hl2...): their history covers
181
- * every bar, so references further than 5000 bars back are allowed (measured: close[bar_index] runs).
163
+ * Makes a Series that computes its value on each bar with a function. Scripts rarely need it: Series come from the
164
+ * builtins, `ta.*` and the Series methods.
165
+ * @param data - The bars the series is aligned to (an array of bars, or a {@link BarData}).
166
+ * @param extractor - Gives the value of bar `index`: `(bar, index, bars) => number`; NaN for na.
167
+ * @example
168
+ * ```js
169
+ * const range = new Series(close.bars, (bar) => bar.high - bar.low);
170
+ * ```
182
171
  */
183
- fullHistory = false;
184
172
  constructor(data, extractor) {
185
173
  // Support both Bar[] and BarData for backward compatibility
186
174
  this.dataSource = data instanceof BarData ? data : new BarData(data);
187
175
  this.extractor = extractor;
188
176
  runHooks?.created(this);
189
177
  }
190
- /**
191
- * Get the underlying bar data
192
- * @returns Bar array
193
- */
178
+ /** The bars this series is aligned to (`{ time, open, high, low, close, volume }`, oldest first). */
194
179
  get bars() {
195
180
  return this.dataSource.bars;
196
181
  }
197
- /**
198
- * Get the underlying BarData source
199
- * @returns BarData instance
200
- */
182
+ /** The {@link BarData} holding the bars this series is aligned to. */
201
183
  get barData() {
202
184
  return this.dataSource;
203
185
  }
@@ -205,10 +187,15 @@ export class Series {
205
187
  // Static Factory Methods
206
188
  // ============================================
207
189
  /**
208
- * Create Series from bar data by extracting a specific field
209
- * @param bars - Bar data (Bar[] or BarData)
210
- * @param field - Field to extract ('open', 'high', 'low', 'close', 'volume')
211
- * @returns Series with extracted field values
190
+ * Makes the Series of one field of the bars (the price series). In a script, use the globals `open`, `high`, `low`,
191
+ * `close`, `volume` instead.
192
+ * @param bars - The bars (an array of bars, or a {@link BarData}).
193
+ * @param field - `'open'`, `'high'`, `'low'`, `'close'` or `'volume'`.
194
+ * @returns A Series of that field; NaN where a bar does not have it.
195
+ * @example
196
+ * ```js
197
+ * const myClose = Series.fromBars(close.bars, "close");
198
+ * ```
212
199
  */
213
200
  static fromBars(bars, field) {
214
201
  const series = new Series(bars, (bar) => bar[field] ?? NaN);
@@ -216,21 +203,31 @@ export class Series {
216
203
  return series;
217
204
  }
218
205
  /**
219
- * Create a constant series (same value for all bars)
220
- * @param bars - Bar data (Bar[] or BarData)
221
- * @param value - Constant value
222
- * @returns Series with constant value
206
+ * Makes a Series with the same value on every bar. The Series methods also take plain numbers, so a script seldom
207
+ * needs it.
208
+ * @param bars - The bars to align to (an array of bars, or a {@link BarData}).
209
+ * @param value - The value of every bar (NaN for na).
210
+ * @returns A Series of `value`.
211
+ * @example
212
+ * ```js
213
+ * const level = Series.constant(close.bars, 50);
214
+ * ```
223
215
  */
224
216
  static constant(bars, value) {
225
217
  return new Series(bars, () => value);
226
218
  }
227
219
  /**
228
- * Create series from array of values
229
- * @param bars - Bar data (Bar[] or BarData) for time alignment
230
- * @param values - Array of values
231
- * @returns Series
220
+ * Makes a Series from an array with one value per bar, oldest first. In a script, `seriesOf(values)` does the same
221
+ * on the chart bars.
222
+ * @param bars - The bars the values belong to (an array of bars, or a {@link BarData}).
223
+ * @param values - One number per bar, oldest first; a missing value reads as NaN.
224
+ * @returns A Series of the values.
232
225
  * @throws When read on other bars with a different length than `values` (e.g. a series computed on the
233
- * chart bars and read inside a `request.security` expression, which runs on the higher-timeframe bars)
226
+ * chart bars and read inside a `request.security` expression, which runs on the other timeframe's bars).
227
+ * @example
228
+ * ```js
229
+ * const doubled = Series.fromArray(close.bars, close.toArray().map((v) => v * 2));
230
+ * ```
234
231
  */
235
232
  static fromArray(bars, values) {
236
233
  const origin = bars instanceof BarData ? bars.bars : bars;
@@ -246,9 +243,13 @@ export class Series {
246
243
  // Arithmetic Operations
247
244
  // ============================================
248
245
  /**
249
- * Add two series or a series and a number
250
- * @param other - Series or number to add
251
- * @returns New Series representing the sum
246
+ * Adds a Series or a number to this series, bar by bar (the `+` of two values). NaN on either side gives NaN.
247
+ * @param other - Series or number to add.
248
+ * @returns A Series of the sums.
249
+ * @example
250
+ * ```js
251
+ * const mid = high.add(low).div(2);
252
+ * ```
252
253
  */
253
254
  add(other) {
254
255
  return new Series(this.dataSource, (bar, i, data) => {
@@ -258,9 +259,13 @@ export class Series {
258
259
  });
259
260
  }
260
261
  /**
261
- * Subtract two series or subtract a number from a series
262
- * @param other - Series or number to subtract
263
- * @returns New Series representing the difference
262
+ * Subtracts a Series or a number from this series, bar by bar (the `-` of two values). NaN on either side gives NaN.
263
+ * @param other - Series or number to subtract.
264
+ * @returns A Series of the differences.
265
+ * @example
266
+ * ```js
267
+ * const body = close.sub(open);
268
+ * ```
264
269
  */
265
270
  sub(other) {
266
271
  return new Series(this.dataSource, (bar, i, data) => {
@@ -270,9 +275,13 @@ export class Series {
270
275
  });
271
276
  }
272
277
  /**
273
- * Multiply two series or a series by a number
274
- * @param other - Series or number to multiply
275
- * @returns New Series representing the product
278
+ * Multiplies this series by a Series or a number, bar by bar (the `*` of two values). NaN on either side gives NaN.
279
+ * @param other - Series or number to multiply by.
280
+ * @returns A Series of the products.
281
+ * @example
282
+ * ```js
283
+ * const upper = ta.sma(close, 20).mul(1.02);
284
+ * ```
276
285
  */
277
286
  mul(other) {
278
287
  return new Series(this.dataSource, (bar, i, data) => {
@@ -282,9 +291,14 @@ export class Series {
282
291
  });
283
292
  }
284
293
  /**
285
- * Raise the series to a power (PineScript math.pow on a series)
286
- * @param exponent - Series or number exponent
287
- * @returns New Series representing base ** exponent
294
+ * Raises this series to a power, bar by bar (`math.pow` on a series). NaN on either side gives NaN; a negative base
295
+ * with a fractional exponent gives NaN.
296
+ * @param exponent - Series or number exponent.
297
+ * @returns A Series of `this ** exponent`.
298
+ * @example
299
+ * ```js
300
+ * const squared = close.sub(ta.sma(close, 20)).pow(2);
301
+ * ```
288
302
  */
289
303
  pow(exponent) {
290
304
  return new Series(this.dataSource, (bar, i, data) => {
@@ -294,9 +308,15 @@ export class Series {
294
308
  });
295
309
  }
296
310
  /**
297
- * Divide two series or a series by a number
298
- * @param other - Series or number to divide by
299
- * @returns New Series representing the quotient
311
+ * Divides this series by a Series or a number, bar by bar (the `/` of two values). A division by 0 gives
312
+ * +/-Infinity (which `na()` treats as na), 0 / 0 and NaN on either side give NaN.
313
+ * @param other - Series or number to divide by.
314
+ * @returns A Series of the quotients.
315
+ * @example
316
+ * ```js
317
+ * const range = high.sub(low);
318
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
319
+ * ```
300
320
  */
301
321
  div(other) {
302
322
  return new Series(this.dataSource, (bar, i, data) => {
@@ -306,9 +326,14 @@ export class Series {
306
326
  });
307
327
  }
308
328
  /**
309
- * Modulo operation
310
- * @param other - Series or number for modulo
311
- * @returns New Series representing the remainder
329
+ * Remainder of the division of this series by a Series or a number, bar by bar (the `%` of two values). The
330
+ * remainder has the sign of this series; a divisor of 0 and NaN on either side give NaN.
331
+ * @param other - Series or number to divide by.
332
+ * @returns A Series of the remainders.
333
+ * @example
334
+ * ```js
335
+ * const everyFifth = bar_index.mod(5).eq(0);
336
+ * ```
312
337
  */
313
338
  mod(other) {
314
339
  return new Series(this.dataSource, (bar, i, data) => {
@@ -318,8 +343,12 @@ export class Series {
318
343
  });
319
344
  }
320
345
  /**
321
- * Negation
322
- * @returns New Series with negated values
346
+ * Changes the sign of every value (the unary `-`). NaN stays NaN.
347
+ * @returns A Series of the opposite values.
348
+ * @example
349
+ * ```js
350
+ * const drop = close.sub(close.offset(1)).neg();
351
+ * ```
323
352
  */
324
353
  neg() {
325
354
  return new Series(this.dataSource, (bar, i, data) => {
@@ -330,9 +359,14 @@ export class Series {
330
359
  // Comparison Operations (PineScript operators: tolerance 1e-10, false with na; see src/compare)
331
360
  // ============================================
332
361
  /**
333
- * Greater than comparison
334
- * @param other - Series or number to compare
335
- * @returns New Series with 1 where true, 0 where false
362
+ * Tests whether this series is above a Series or a number, bar by bar (`a > b`).
363
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
364
+ * @param other - Series or number to compare with.
365
+ * @returns A Series of 1 where it is true, 0 where it is false.
366
+ * @example
367
+ * ```js
368
+ * const up = close.gt(open);
369
+ * ```
336
370
  */
337
371
  gt(other) {
338
372
  return new Series(this.dataSource, (bar, i, data) => {
@@ -342,9 +376,14 @@ export class Series {
342
376
  });
343
377
  }
344
378
  /**
345
- * Greater than or equal comparison
346
- * @param other - Series or number to compare
347
- * @returns New Series with 1 where true, 0 where false
379
+ * Tests whether this series is above or equal to a Series or a number, bar by bar (`a >= b`).
380
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
381
+ * @param other - Series or number to compare with.
382
+ * @returns A Series of 1 where it is true, 0 where it is false.
383
+ * @example
384
+ * ```js
385
+ * const atOrAbove = close.gte(ta.sma(close, 50));
386
+ * ```
348
387
  */
349
388
  gte(other) {
350
389
  return new Series(this.dataSource, (bar, i, data) => {
@@ -354,9 +393,14 @@ export class Series {
354
393
  });
355
394
  }
356
395
  /**
357
- * Less than comparison
358
- * @param other - Series or number to compare
359
- * @returns New Series with 1 where true, 0 where false
396
+ * Tests whether this series is below a Series or a number, bar by bar (`a < b`).
397
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
398
+ * @param other - Series or number to compare with.
399
+ * @returns A Series of 1 where it is true, 0 where it is false.
400
+ * @example
401
+ * ```js
402
+ * const oversold = ta.rsi(close, 14).lt(30);
403
+ * ```
360
404
  */
361
405
  lt(other) {
362
406
  return new Series(this.dataSource, (bar, i, data) => {
@@ -366,9 +410,14 @@ export class Series {
366
410
  });
367
411
  }
368
412
  /**
369
- * Less than or equal comparison
370
- * @param other - Series or number to compare
371
- * @returns New Series with 1 where true, 0 where false
413
+ * Tests whether this series is below or equal to a Series or a number, bar by bar (`a <= b`).
414
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
415
+ * @param other - Series or number to compare with.
416
+ * @returns A Series of 1 where it is true, 0 where it is false.
417
+ * @example
418
+ * ```js
419
+ * const atOrBelow = low.lte(ta.lowest(low, 20).offset(1));
420
+ * ```
372
421
  */
373
422
  lte(other) {
374
423
  return new Series(this.dataSource, (bar, i, data) => {
@@ -378,9 +427,14 @@ export class Series {
378
427
  });
379
428
  }
380
429
  /**
381
- * Equality comparison
382
- * @param other - Series or number to compare
383
- * @returns New Series with 1 where true, 0 where false
430
+ * Tests whether this series equals a Series or a number, bar by bar (`a == b`).
431
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
432
+ * @param other - Series or number to compare with.
433
+ * @returns A Series of 1 where it is true, 0 where it is false.
434
+ * @example
435
+ * ```js
436
+ * const doji = close.eq(open);
437
+ * ```
384
438
  */
385
439
  eq(other) {
386
440
  return new Series(this.dataSource, (bar, i, data) => {
@@ -390,9 +444,14 @@ export class Series {
390
444
  });
391
445
  }
392
446
  /**
393
- * Not equal comparison
394
- * @param other - Series or number to compare
395
- * @returns New Series with 1 where true, 0 where false
447
+ * Tests whether this series differs from a Series or a number, bar by bar (`a != b`).
448
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
449
+ * @param other - Series or number to compare with.
450
+ * @returns A Series of 1 where it is true, 0 where it is false.
451
+ * @example
452
+ * ```js
453
+ * const moved = close.neq(close.offset(1));
454
+ * ```
396
455
  */
397
456
  neq(other) {
398
457
  return new Series(this.dataSource, (bar, i, data) => {
@@ -405,9 +464,13 @@ export class Series {
405
464
  // Logical Operations (for boolean series)
406
465
  // ============================================
407
466
  /**
408
- * Logical AND
409
- * @param other - Series or number (truthy/falsy)
410
- * @returns New Series with 1 where both true, 0 otherwise
467
+ * Logical "and" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
468
+ * @param other - The other condition: a Series or a number.
469
+ * @returns A Series of 1 where both are true, 0 elsewhere.
470
+ * @example
471
+ * ```js
472
+ * const strongUp = close.gt(open).and(volume.gt(ta.sma(volume, 20)));
473
+ * ```
411
474
  */
412
475
  and(other) {
413
476
  return new Series(this.dataSource, (bar, i, data) => {
@@ -417,9 +480,13 @@ export class Series {
417
480
  });
418
481
  }
419
482
  /**
420
- * Logical OR
421
- * @param other - Series or number (truthy/falsy)
422
- * @returns New Series with 1 where either true, 0 otherwise
483
+ * Logical "or" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
484
+ * @param other - The other condition: a Series or a number.
485
+ * @returns A Series of 1 where at least one is true, 0 elsewhere.
486
+ * @example
487
+ * ```js
488
+ * const extreme = ta.rsi(close, 14).gt(70).or(ta.rsi(close, 14).lt(30));
489
+ * ```
423
490
  */
424
491
  or(other) {
425
492
  return new Series(this.dataSource, (bar, i, data) => {
@@ -429,8 +496,12 @@ export class Series {
429
496
  });
430
497
  }
431
498
  /**
432
- * Logical NOT
433
- * @returns New Series with inverted boolean values
499
+ * Logical "not" of a condition, bar by bar: true (not 0, not NaN) becomes 0, and 0 or NaN becomes 1.
500
+ * @returns A Series of 1 where this series is false, 0 where it is true.
501
+ * @example
502
+ * ```js
503
+ * const notUp = close.gt(open).not();
504
+ * ```
434
505
  */
435
506
  not() {
436
507
  return new Series(this.dataSource, (bar, i, data) => {
@@ -442,21 +513,16 @@ export class Series {
442
513
  // Conditional Selection
443
514
  // ============================================
444
515
  /**
445
- * Bar-by-bar conditional selection (ternary operator for Series)
446
- * Uses this Series as the condition (truthy = nonzero, falsy = zero/NaN)
447
- *
448
- * @param trueValue - Value or Series to use when condition is truthy
449
- * @param falseValue - Value or Series to use when condition is falsy
450
- * @returns New Series with bar-by-bar selected values
451
- *
516
+ * Picks one of two values on each bar, with this series as the condition (the `cond ? x : y` of a series). The
517
+ * condition is true when it is not 0 and not NaN; 0 and NaN pick `falseValue`.
518
+ * @param trueValue - Series or number used where the condition is true.
519
+ * @param falseValue - Series or number used where the condition is false.
520
+ * @returns A Series of the picked values.
452
521
  * @example
453
- * ```typescript
454
- * // PineScript: result = condition ? trueVal : falseVal
455
- * const result = condition.iff(trueVal, falseVal);
456
- *
457
- * // Example: RSI formula uses ternary for edge cases
458
- * // rsi = down == 0 ? 100 : up == 0 ? 0 : formula
459
- * const rsi = down.eq(0).iff(100, up.eq(0).iff(0, formula));
522
+ * ```js
523
+ * const range = high.sub(low);
524
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
525
+ * plot(ratio, "Body / range");
460
526
  * ```
461
527
  */
462
528
  iff(trueValue, falseValue) {
@@ -476,10 +542,17 @@ export class Series {
476
542
  // Offset/History
477
543
  // ============================================
478
544
  /**
479
- * Access previous bars (like close[1] in PineScript)
480
- * @param offset - Number of bars back (0 or more)
481
- * @returns New Series with offset values
482
- * @throws RangeError when `offset` is negative
545
+ * Shifts the series back in time: on each bar, the value `offset` bars earlier (`close.offset(1)` is the previous
546
+ * close). The first `offset` bars, which have no earlier bar, get NaN.
547
+ * @param offset - Number of bars back: 0 (the series itself) or more, a whole number. A series computed by the
548
+ * script can look back at most 5000 bars; the price series (`close`, `high`...) any number.
549
+ * @returns A Series of the earlier values.
550
+ * @throws RangeError when `offset` is negative (it would read a future bar); an error past 5000 bars.
551
+ * @example
552
+ * ```js
553
+ * const change = close.sub(close.offset(1));
554
+ * const higherHigh = high.gt(high.offset(1));
555
+ * ```
483
556
  */
484
557
  offset(offset) {
485
558
  if (offset < 0)
@@ -499,14 +572,14 @@ export class Series {
499
572
  // Computation & Access
500
573
  // ============================================
501
574
  /**
502
- * Compute all values for the series
503
- *
504
- * Now includes automatic cache invalidation:
505
- * - Checks if dataSource version has changed since cache was computed
506
- * - Recomputes if version mismatch detected
507
- * - Stores version when cache is created
508
- *
509
- * @returns Array of computed values
575
+ * Gives the values of all the bars as a plain array, oldest bar first (NaN for na). The values are computed on the
576
+ * first read and kept until the bars change; the array is shared, so copy it before changing it.
577
+ * @returns One number per bar, oldest first.
578
+ * @example
579
+ * ```js
580
+ * const values = ta.sma(close, 20).toArray();
581
+ * const shifted = seriesOf(values.map((v) => v * 1.01));
582
+ * ```
510
583
  */
511
584
  toArray() {
512
585
  // Check if cache is valid by comparing versions
@@ -534,24 +607,13 @@ export class Series {
534
607
  return this.cached;
535
608
  }
536
609
  /**
537
- * Materialize this Series by eagerly computing values and breaking closure chain
538
- *
539
- * This method addresses the closure chain memory leak issue:
540
- * - Computes all values immediately
541
- * - Creates a new Series that stores values directly
542
- * - Breaks references to parent Series objects
543
- * - Useful for complex expressions to free intermediate Series memory
544
- *
610
+ * Computes the values now and returns a Series that stores them, without links to the series it was built from.
611
+ * Use it on a long chain of methods to compute it once and free the steps in between.
612
+ * @returns A Series of the same values, stored.
545
613
  * @example
546
- * ```typescript
547
- * // Without materialize: keeps a, b, c, d in memory via closures
548
- * const result = a.add(b).mul(c).div(d).sub(e);
549
- *
550
- * // With materialize: breaks chain, frees a, b, c memory
551
- * const result = a.add(b).mul(c).materialize().div(d).sub(e);
614
+ * ```js
615
+ * const spread = high.sub(low).div(close).mul(100).materialize();
552
616
  * ```
553
- *
554
- * @returns New Series with computed values and no closure dependencies
555
617
  */
556
618
  materialize() {
557
619
  const values = this.toArray();
@@ -581,33 +643,43 @@ export class Series {
581
643
  return this.toArray();
582
644
  }
583
645
  /**
584
- * Get value at specific index
585
- * @param index - Bar index
586
- * @returns Value at that index
646
+ * Reads the value of one bar by its position: 0 is the oldest bar, `bar_index` counts the same way. This is not
647
+ * the "bars back" of `offset()`: inside a bar loop, read the current bar with `c.get(series)` and earlier bars with
648
+ * `c.get(series, k)`.
649
+ * @param index - Bar position, 0 for the oldest bar.
650
+ * @returns The value of that bar; NaN when there is no such bar.
651
+ * @example
652
+ * ```js
653
+ * const firstClose = close.get(0);
654
+ * ```
587
655
  */
588
656
  get(index) {
589
657
  const values = this.toArray();
590
658
  return values[index] ?? NaN;
591
659
  }
592
660
  /**
593
- * Get the last value (most recent bar)
594
- * @returns Most recent value
661
+ * Reads the value of the newest bar.
662
+ * @returns The value of the last bar; NaN when there are no bars.
663
+ * @example
664
+ * ```js
665
+ * const lastRsi = ta.rsi(close, 14).last();
666
+ * ```
595
667
  */
596
668
  last() {
597
669
  const values = this.toArray();
598
670
  return values[values.length - 1] ?? NaN;
599
671
  }
600
672
  /**
601
- * Get number of values
602
- * @returns Length of series
673
+ * Gives the number of bars of the series (a method, not a property).
674
+ * @returns The number of bars.
675
+ * @example
676
+ * ```js
677
+ * const bars = close.length();
678
+ * ```
603
679
  */
604
680
  length() {
605
681
  return this.dataSource.length;
606
682
  }
607
- /**
608
- * Convert series to time-value pairs for charting
609
- * @returns Array of { time, value } objects
610
- */
611
683
  /**
612
684
  * A Series holds the values of all the bars, so JavaScript operators cannot use it: without this guard
613
685
  * `close - open` is NaN, `close > open` false and `` `${rsi}` `` "[object Object]", with no error. The
@@ -622,6 +694,14 @@ export class Series {
622
694
  'a.add(b) a.sub(b) a.mul(b) a.div(b) a.mod(b) a.pow(b) a.neg(), a.gt(b) a.gte(b) a.lt(b) a.lte(b) a.eq(b) a.neq(b), ' +
623
695
  'a.and(b) a.or(b) a.not(), cond.iff(x, y), a.offset(1) for a[1]; or read numbers inside eachBar() with c.get(a).');
624
696
  }
697
+ /**
698
+ * Gives the values as `{ time, value }` points for a chart line, oldest first, leaving out the NaN bars.
699
+ * @returns The points; `time` is the bar time, in the unit of the bars.
700
+ * @example
701
+ * ```js
702
+ * const points = ta.sma(close, 20).toTimeValuePairs();
703
+ * ```
704
+ */
625
705
  toTimeValuePairs() {
626
706
  const values = this.toArray();
627
707
  const bars = this.dataSource.bars;