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
@@ -13,12 +13,9 @@ import type { Bar } from '../types/index.js';
13
13
  export type SeriesExtractor = (bar: Bar, index: number, data: Bar[]) => number;
14
14
 
15
15
  /**
16
- * BarData - Versioned wrapper around Bar[] array for cache invalidation
17
- *
18
- * This class wraps a Bar[] array and tracks a version number that increments
19
- * whenever the data is mutated. Series objects reference this to detect when
20
- * their cached values need to be recomputed.
21
- *
16
+ * Host-side: an array of bars with a version number that grows on every change, so the Series made on it know when
17
+ * their cached values are stale. Scripts do not use it: their Series are made on the chart's bars.
18
+ *
22
19
  * @example
23
20
  * ```typescript
24
21
  * const barData = new BarData(bars);
@@ -145,35 +142,6 @@ export class BarData {
145
142
  }
146
143
  }
147
144
 
148
- /**
149
- * Series - represents a time-series of values with lazy evaluation
150
- *
151
- * Series are lazy - they don't compute values until needed.
152
- * Operations on Series return new Series with composed extractors.
153
- *
154
- * Now supports automatic cache invalidation via BarData versioning
155
- * and memory-efficient closure chain breaking via materialize().
156
- *
157
- * @example
158
- * ```typescript
159
- * // Basic usage (backward compatible)
160
- * const close = Series.fromBars(bars, 'close');
161
- * const open = Series.fromBars(bars, 'open');
162
- * const range = close.sub(open); // Creates new Series, doesn't compute yet
163
- * const values = range.toArray(); // Now computes for all bars
164
- *
165
- * // With automatic cache invalidation
166
- * const barData = new BarData(bars);
167
- * const close = Series.fromBars(barData, 'close');
168
- * const values1 = close.toArray(); // Computes and caches
169
- * barData.push(newBar); // Version increments
170
- * const values2 = close.toArray(); // Detects stale cache, recomputes
171
- *
172
- * // Breaking closure chains for memory efficiency
173
- * const complex = a.add(b).mul(c).div(d).sub(e);
174
- * const materialized = complex.materialize(); // Breaks closure chain
175
- * ```
176
- */
177
145
  /** Largest history reference of a script series (PineScript max_bars_back limit). */
178
146
  export const MAX_BARS_BACK = 5000;
179
147
 
@@ -204,6 +172,22 @@ export function setSeriesRunHooks(hooks: SeriesRunHooks | null): void {
204
172
  runHooks = hooks;
205
173
  }
206
174
 
175
+ /**
176
+ * A series: one number per bar, oldest bar first, NaN where the value is missing (na). `close`, `open`, `high`,
177
+ * `low`, `volume`, the `ta.*` results and `input.source()` are Series.
178
+ *
179
+ * JavaScript operators do not work on a Series (`close - open` throws): use its methods, which work on all the bars
180
+ * at once and return a new Series. The other operand can be a Series or a plain number. Comparisons and logic give
181
+ * a Series of 1 (true) and 0 (false). Inside `eachBar()`, read the number of the current bar with `c.get(series)`.
182
+ * Values are computed when first read, then kept.
183
+ *
184
+ * @example
185
+ * ```js
186
+ * const body = close.sub(open);
187
+ * const up = close.gt(open);
188
+ * plot(up.iff(body, body.neg()), "Body size");
189
+ * ```
190
+ */
207
191
  export class Series {
208
192
  private extractor: SeriesExtractor;
209
193
  private dataSource: BarData;
@@ -211,17 +195,21 @@ export class Series {
211
195
  private cachedVersion: number = -1;
212
196
 
213
197
  /**
214
- * Create a new Series
215
- *
216
- * @param data - Bar data (Bar[] or BarData)
217
- * @param extractor - Function to extract/compute value for each bar
218
- */
219
- /**
220
- * True for PineScript's built-in price variables (open, high, low, close, volume, hl2...): their history covers
221
- * every bar, so references further than 5000 bars back are allowed (measured: close[bar_index] runs).
198
+ * True for the built-in price series (`open`, `high`, `low`, `close`, `volume`, `hl2`...): their history covers
199
+ * every bar, so `offset()` may reach further than 5000 bars back.
222
200
  */
223
201
  fullHistory = false;
224
202
 
203
+ /**
204
+ * Makes a Series that computes its value on each bar with a function. Scripts rarely need it: Series come from the
205
+ * builtins, `ta.*` and the Series methods.
206
+ * @param data - The bars the series is aligned to (an array of bars, or a {@link BarData}).
207
+ * @param extractor - Gives the value of bar `index`: `(bar, index, bars) => number`; NaN for na.
208
+ * @example
209
+ * ```js
210
+ * const range = new Series(close.bars, (bar) => bar.high - bar.low);
211
+ * ```
212
+ */
225
213
  constructor(data: Bar[] | BarData, extractor: SeriesExtractor) {
226
214
  // Support both Bar[] and BarData for backward compatibility
227
215
  this.dataSource = data instanceof BarData ? data : new BarData(data);
@@ -229,18 +217,12 @@ export class Series {
229
217
  runHooks?.created(this);
230
218
  }
231
219
 
232
- /**
233
- * Get the underlying bar data
234
- * @returns Bar array
235
- */
220
+ /** The bars this series is aligned to (`{ time, open, high, low, close, volume }`, oldest first). */
236
221
  get bars(): Bar[] {
237
222
  return this.dataSource.bars;
238
223
  }
239
224
 
240
- /**
241
- * Get the underlying BarData source
242
- * @returns BarData instance
243
- */
225
+ /** The {@link BarData} holding the bars this series is aligned to. */
244
226
  get barData(): BarData {
245
227
  return this.dataSource;
246
228
  }
@@ -250,10 +232,15 @@ export class Series {
250
232
  // ============================================
251
233
 
252
234
  /**
253
- * Create Series from bar data by extracting a specific field
254
- * @param bars - Bar data (Bar[] or BarData)
255
- * @param field - Field to extract ('open', 'high', 'low', 'close', 'volume')
256
- * @returns Series with extracted field values
235
+ * Makes the Series of one field of the bars (the price series). In a script, use the globals `open`, `high`, `low`,
236
+ * `close`, `volume` instead.
237
+ * @param bars - The bars (an array of bars, or a {@link BarData}).
238
+ * @param field - `'open'`, `'high'`, `'low'`, `'close'` or `'volume'`.
239
+ * @returns A Series of that field; NaN where a bar does not have it.
240
+ * @example
241
+ * ```js
242
+ * const myClose = Series.fromBars(close.bars, "close");
243
+ * ```
257
244
  */
258
245
  static fromBars(bars: Bar[] | BarData, field: 'open' | 'high' | 'low' | 'close' | 'volume'): Series {
259
246
  const series = new Series(bars, (bar) => bar[field] ?? NaN);
@@ -262,22 +249,32 @@ export class Series {
262
249
  }
263
250
 
264
251
  /**
265
- * Create a constant series (same value for all bars)
266
- * @param bars - Bar data (Bar[] or BarData)
267
- * @param value - Constant value
268
- * @returns Series with constant value
252
+ * Makes a Series with the same value on every bar. The Series methods also take plain numbers, so a script seldom
253
+ * needs it.
254
+ * @param bars - The bars to align to (an array of bars, or a {@link BarData}).
255
+ * @param value - The value of every bar (NaN for na).
256
+ * @returns A Series of `value`.
257
+ * @example
258
+ * ```js
259
+ * const level = Series.constant(close.bars, 50);
260
+ * ```
269
261
  */
270
262
  static constant(bars: Bar[] | BarData, value: number): Series {
271
263
  return new Series(bars, () => value);
272
264
  }
273
265
 
274
266
  /**
275
- * Create series from array of values
276
- * @param bars - Bar data (Bar[] or BarData) for time alignment
277
- * @param values - Array of values
278
- * @returns Series
267
+ * Makes a Series from an array with one value per bar, oldest first. In a script, `seriesOf(values)` does the same
268
+ * on the chart bars.
269
+ * @param bars - The bars the values belong to (an array of bars, or a {@link BarData}).
270
+ * @param values - One number per bar, oldest first; a missing value reads as NaN.
271
+ * @returns A Series of the values.
279
272
  * @throws When read on other bars with a different length than `values` (e.g. a series computed on the
280
- * chart bars and read inside a `request.security` expression, which runs on the higher-timeframe bars)
273
+ * chart bars and read inside a `request.security` expression, which runs on the other timeframe's bars).
274
+ * @example
275
+ * ```js
276
+ * const doubled = Series.fromArray(close.bars, close.toArray().map((v) => v * 2));
277
+ * ```
281
278
  */
282
279
  static fromArray(bars: Bar[] | BarData, values: number[]): Series {
283
280
  const origin = bars instanceof BarData ? bars.bars : bars;
@@ -297,9 +294,13 @@ export class Series {
297
294
  // ============================================
298
295
 
299
296
  /**
300
- * Add two series or a series and a number
301
- * @param other - Series or number to add
302
- * @returns New Series representing the sum
297
+ * Adds a Series or a number to this series, bar by bar (the `+` of two values). NaN on either side gives NaN.
298
+ * @param other - Series or number to add.
299
+ * @returns A Series of the sums.
300
+ * @example
301
+ * ```js
302
+ * const mid = high.add(low).div(2);
303
+ * ```
303
304
  */
304
305
  add(other: Series | number): Series {
305
306
  return new Series(this.dataSource, (bar, i, data) => {
@@ -310,9 +311,13 @@ export class Series {
310
311
  }
311
312
 
312
313
  /**
313
- * Subtract two series or subtract a number from a series
314
- * @param other - Series or number to subtract
315
- * @returns New Series representing the difference
314
+ * Subtracts a Series or a number from this series, bar by bar (the `-` of two values). NaN on either side gives NaN.
315
+ * @param other - Series or number to subtract.
316
+ * @returns A Series of the differences.
317
+ * @example
318
+ * ```js
319
+ * const body = close.sub(open);
320
+ * ```
316
321
  */
317
322
  sub(other: Series | number): Series {
318
323
  return new Series(this.dataSource, (bar, i, data) => {
@@ -323,9 +328,13 @@ export class Series {
323
328
  }
324
329
 
325
330
  /**
326
- * Multiply two series or a series by a number
327
- * @param other - Series or number to multiply
328
- * @returns New Series representing the product
331
+ * Multiplies this series by a Series or a number, bar by bar (the `*` of two values). NaN on either side gives NaN.
332
+ * @param other - Series or number to multiply by.
333
+ * @returns A Series of the products.
334
+ * @example
335
+ * ```js
336
+ * const upper = ta.sma(close, 20).mul(1.02);
337
+ * ```
329
338
  */
330
339
  mul(other: Series | number): Series {
331
340
  return new Series(this.dataSource, (bar, i, data) => {
@@ -336,9 +345,14 @@ export class Series {
336
345
  }
337
346
 
338
347
  /**
339
- * Raise the series to a power (PineScript math.pow on a series)
340
- * @param exponent - Series or number exponent
341
- * @returns New Series representing base ** exponent
348
+ * Raises this series to a power, bar by bar (`math.pow` on a series). NaN on either side gives NaN; a negative base
349
+ * with a fractional exponent gives NaN.
350
+ * @param exponent - Series or number exponent.
351
+ * @returns A Series of `this ** exponent`.
352
+ * @example
353
+ * ```js
354
+ * const squared = close.sub(ta.sma(close, 20)).pow(2);
355
+ * ```
342
356
  */
343
357
  pow(exponent: Series | number): Series {
344
358
  return new Series(this.dataSource, (bar, i, data) => {
@@ -349,9 +363,15 @@ export class Series {
349
363
  }
350
364
 
351
365
  /**
352
- * Divide two series or a series by a number
353
- * @param other - Series or number to divide by
354
- * @returns New Series representing the quotient
366
+ * Divides this series by a Series or a number, bar by bar (the `/` of two values). A division by 0 gives
367
+ * +/-Infinity (which `na()` treats as na), 0 / 0 and NaN on either side give NaN.
368
+ * @param other - Series or number to divide by.
369
+ * @returns A Series of the quotients.
370
+ * @example
371
+ * ```js
372
+ * const range = high.sub(low);
373
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
374
+ * ```
355
375
  */
356
376
  div(other: Series | number): Series {
357
377
  return new Series(this.dataSource, (bar, i, data) => {
@@ -362,9 +382,14 @@ export class Series {
362
382
  }
363
383
 
364
384
  /**
365
- * Modulo operation
366
- * @param other - Series or number for modulo
367
- * @returns New Series representing the remainder
385
+ * Remainder of the division of this series by a Series or a number, bar by bar (the `%` of two values). The
386
+ * remainder has the sign of this series; a divisor of 0 and NaN on either side give NaN.
387
+ * @param other - Series or number to divide by.
388
+ * @returns A Series of the remainders.
389
+ * @example
390
+ * ```js
391
+ * const everyFifth = bar_index.mod(5).eq(0);
392
+ * ```
368
393
  */
369
394
  mod(other: Series | number): Series {
370
395
  return new Series(this.dataSource, (bar, i, data) => {
@@ -375,8 +400,12 @@ export class Series {
375
400
  }
376
401
 
377
402
  /**
378
- * Negation
379
- * @returns New Series with negated values
403
+ * Changes the sign of every value (the unary `-`). NaN stays NaN.
404
+ * @returns A Series of the opposite values.
405
+ * @example
406
+ * ```js
407
+ * const drop = close.sub(close.offset(1)).neg();
408
+ * ```
380
409
  */
381
410
  neg(): Series {
382
411
  return new Series(this.dataSource, (bar, i, data) => {
@@ -389,9 +418,14 @@ export class Series {
389
418
  // ============================================
390
419
 
391
420
  /**
392
- * Greater than comparison
393
- * @param other - Series or number to compare
394
- * @returns New Series with 1 where true, 0 where false
421
+ * Tests whether this series is above a Series or a number, bar by bar (`a > b`).
422
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
423
+ * @param other - Series or number to compare with.
424
+ * @returns A Series of 1 where it is true, 0 where it is false.
425
+ * @example
426
+ * ```js
427
+ * const up = close.gt(open);
428
+ * ```
395
429
  */
396
430
  gt(other: Series | number): Series {
397
431
  return new Series(this.dataSource, (bar, i, data) => {
@@ -402,9 +436,14 @@ export class Series {
402
436
  }
403
437
 
404
438
  /**
405
- * Greater than or equal comparison
406
- * @param other - Series or number to compare
407
- * @returns New Series with 1 where true, 0 where false
439
+ * Tests whether this series is above or equal to a Series or a number, bar by bar (`a >= b`).
440
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
441
+ * @param other - Series or number to compare with.
442
+ * @returns A Series of 1 where it is true, 0 where it is false.
443
+ * @example
444
+ * ```js
445
+ * const atOrAbove = close.gte(ta.sma(close, 50));
446
+ * ```
408
447
  */
409
448
  gte(other: Series | number): Series {
410
449
  return new Series(this.dataSource, (bar, i, data) => {
@@ -415,9 +454,14 @@ export class Series {
415
454
  }
416
455
 
417
456
  /**
418
- * Less than comparison
419
- * @param other - Series or number to compare
420
- * @returns New Series with 1 where true, 0 where false
457
+ * Tests whether this series is below a Series or a number, bar by bar (`a < b`).
458
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
459
+ * @param other - Series or number to compare with.
460
+ * @returns A Series of 1 where it is true, 0 where it is false.
461
+ * @example
462
+ * ```js
463
+ * const oversold = ta.rsi(close, 14).lt(30);
464
+ * ```
421
465
  */
422
466
  lt(other: Series | number): Series {
423
467
  return new Series(this.dataSource, (bar, i, data) => {
@@ -428,9 +472,14 @@ export class Series {
428
472
  }
429
473
 
430
474
  /**
431
- * Less than or equal comparison
432
- * @param other - Series or number to compare
433
- * @returns New Series with 1 where true, 0 where false
475
+ * Tests whether this series is below or equal to a Series or a number, bar by bar (`a <= b`).
476
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
477
+ * @param other - Series or number to compare with.
478
+ * @returns A Series of 1 where it is true, 0 where it is false.
479
+ * @example
480
+ * ```js
481
+ * const atOrBelow = low.lte(ta.lowest(low, 20).offset(1));
482
+ * ```
434
483
  */
435
484
  lte(other: Series | number): Series {
436
485
  return new Series(this.dataSource, (bar, i, data) => {
@@ -441,9 +490,14 @@ export class Series {
441
490
  }
442
491
 
443
492
  /**
444
- * Equality comparison
445
- * @param other - Series or number to compare
446
- * @returns New Series with 1 where true, 0 where false
493
+ * Tests whether this series equals a Series or a number, bar by bar (`a == b`).
494
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
495
+ * @param other - Series or number to compare with.
496
+ * @returns A Series of 1 where it is true, 0 where it is false.
497
+ * @example
498
+ * ```js
499
+ * const doji = close.eq(open);
500
+ * ```
447
501
  */
448
502
  eq(other: Series | number): Series {
449
503
  return new Series(this.dataSource, (bar, i, data) => {
@@ -454,9 +508,14 @@ export class Series {
454
508
  }
455
509
 
456
510
  /**
457
- * Not equal comparison
458
- * @param other - Series or number to compare
459
- * @returns New Series with 1 where true, 0 where false
511
+ * Tests whether this series differs from a Series or a number, bar by bar (`a != b`).
512
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
513
+ * @param other - Series or number to compare with.
514
+ * @returns A Series of 1 where it is true, 0 where it is false.
515
+ * @example
516
+ * ```js
517
+ * const moved = close.neq(close.offset(1));
518
+ * ```
460
519
  */
461
520
  neq(other: Series | number): Series {
462
521
  return new Series(this.dataSource, (bar, i, data) => {
@@ -471,9 +530,13 @@ export class Series {
471
530
  // ============================================
472
531
 
473
532
  /**
474
- * Logical AND
475
- * @param other - Series or number (truthy/falsy)
476
- * @returns New Series with 1 where both true, 0 otherwise
533
+ * Logical "and" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
534
+ * @param other - The other condition: a Series or a number.
535
+ * @returns A Series of 1 where both are true, 0 elsewhere.
536
+ * @example
537
+ * ```js
538
+ * const strongUp = close.gt(open).and(volume.gt(ta.sma(volume, 20)));
539
+ * ```
477
540
  */
478
541
  and(other: Series | number): Series {
479
542
  return new Series(this.dataSource, (bar, i, data) => {
@@ -484,9 +547,13 @@ export class Series {
484
547
  }
485
548
 
486
549
  /**
487
- * Logical OR
488
- * @param other - Series or number (truthy/falsy)
489
- * @returns New Series with 1 where either true, 0 otherwise
550
+ * Logical "or" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
551
+ * @param other - The other condition: a Series or a number.
552
+ * @returns A Series of 1 where at least one is true, 0 elsewhere.
553
+ * @example
554
+ * ```js
555
+ * const extreme = ta.rsi(close, 14).gt(70).or(ta.rsi(close, 14).lt(30));
556
+ * ```
490
557
  */
491
558
  or(other: Series | number): Series {
492
559
  return new Series(this.dataSource, (bar, i, data) => {
@@ -497,8 +564,12 @@ export class Series {
497
564
  }
498
565
 
499
566
  /**
500
- * Logical NOT
501
- * @returns New Series with inverted boolean values
567
+ * Logical "not" of a condition, bar by bar: true (not 0, not NaN) becomes 0, and 0 or NaN becomes 1.
568
+ * @returns A Series of 1 where this series is false, 0 where it is true.
569
+ * @example
570
+ * ```js
571
+ * const notUp = close.gt(open).not();
572
+ * ```
502
573
  */
503
574
  not(): Series {
504
575
  return new Series(this.dataSource, (bar, i, data) => {
@@ -512,21 +583,16 @@ export class Series {
512
583
  // ============================================
513
584
 
514
585
  /**
515
- * Bar-by-bar conditional selection (ternary operator for Series)
516
- * Uses this Series as the condition (truthy = nonzero, falsy = zero/NaN)
517
- *
518
- * @param trueValue - Value or Series to use when condition is truthy
519
- * @param falseValue - Value or Series to use when condition is falsy
520
- * @returns New Series with bar-by-bar selected values
521
- *
586
+ * Picks one of two values on each bar, with this series as the condition (the `cond ? x : y` of a series). The
587
+ * condition is true when it is not 0 and not NaN; 0 and NaN pick `falseValue`.
588
+ * @param trueValue - Series or number used where the condition is true.
589
+ * @param falseValue - Series or number used where the condition is false.
590
+ * @returns A Series of the picked values.
522
591
  * @example
523
- * ```typescript
524
- * // PineScript: result = condition ? trueVal : falseVal
525
- * const result = condition.iff(trueVal, falseVal);
526
- *
527
- * // Example: RSI formula uses ternary for edge cases
528
- * // rsi = down == 0 ? 100 : up == 0 ? 0 : formula
529
- * const rsi = down.eq(0).iff(100, up.eq(0).iff(0, formula));
592
+ * ```js
593
+ * const range = high.sub(low);
594
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
595
+ * plot(ratio, "Body / range");
530
596
  * ```
531
597
  */
532
598
  iff(trueValue: Series | number, falseValue: Series | number): Series {
@@ -548,10 +614,17 @@ export class Series {
548
614
  // ============================================
549
615
 
550
616
  /**
551
- * Access previous bars (like close[1] in PineScript)
552
- * @param offset - Number of bars back (0 or more)
553
- * @returns New Series with offset values
554
- * @throws RangeError when `offset` is negative
617
+ * Shifts the series back in time: on each bar, the value `offset` bars earlier (`close.offset(1)` is the previous
618
+ * close). The first `offset` bars, which have no earlier bar, get NaN.
619
+ * @param offset - Number of bars back: 0 (the series itself) or more, a whole number. A series computed by the
620
+ * script can look back at most 5000 bars; the price series (`close`, `high`...) any number.
621
+ * @returns A Series of the earlier values.
622
+ * @throws RangeError when `offset` is negative (it would read a future bar); an error past 5000 bars.
623
+ * @example
624
+ * ```js
625
+ * const change = close.sub(close.offset(1));
626
+ * const higherHigh = high.gt(high.offset(1));
627
+ * ```
555
628
  */
556
629
  offset(offset: number): Series {
557
630
  if (offset < 0) throw new RangeError(`Series.offset(${offset}): the offset cannot be negative (it would read a future bar).`);
@@ -571,14 +644,14 @@ export class Series {
571
644
  // ============================================
572
645
 
573
646
  /**
574
- * Compute all values for the series
575
- *
576
- * Now includes automatic cache invalidation:
577
- * - Checks if dataSource version has changed since cache was computed
578
- * - Recomputes if version mismatch detected
579
- * - Stores version when cache is created
580
- *
581
- * @returns Array of computed values
647
+ * Gives the values of all the bars as a plain array, oldest bar first (NaN for na). The values are computed on the
648
+ * first read and kept until the bars change; the array is shared, so copy it before changing it.
649
+ * @returns One number per bar, oldest first.
650
+ * @example
651
+ * ```js
652
+ * const values = ta.sma(close, 20).toArray();
653
+ * const shifted = seriesOf(values.map((v) => v * 1.01));
654
+ * ```
582
655
  */
583
656
  toArray(): number[] {
584
657
  // Check if cache is valid by comparing versions
@@ -606,24 +679,13 @@ export class Series {
606
679
  }
607
680
 
608
681
  /**
609
- * Materialize this Series by eagerly computing values and breaking closure chain
610
- *
611
- * This method addresses the closure chain memory leak issue:
612
- * - Computes all values immediately
613
- * - Creates a new Series that stores values directly
614
- * - Breaks references to parent Series objects
615
- * - Useful for complex expressions to free intermediate Series memory
616
- *
682
+ * Computes the values now and returns a Series that stores them, without links to the series it was built from.
683
+ * Use it on a long chain of methods to compute it once and free the steps in between.
684
+ * @returns A Series of the same values, stored.
617
685
  * @example
618
- * ```typescript
619
- * // Without materialize: keeps a, b, c, d in memory via closures
620
- * const result = a.add(b).mul(c).div(d).sub(e);
621
- *
622
- * // With materialize: breaks chain, frees a, b, c memory
623
- * const result = a.add(b).mul(c).materialize().div(d).sub(e);
686
+ * ```js
687
+ * const spread = high.sub(low).div(close).mul(100).materialize();
624
688
  * ```
625
- *
626
- * @returns New Series with computed values and no closure dependencies
627
689
  */
628
690
  materialize(): Series {
629
691
  const values = this.toArray();
@@ -656,9 +718,15 @@ export class Series {
656
718
  }
657
719
 
658
720
  /**
659
- * Get value at specific index
660
- * @param index - Bar index
661
- * @returns Value at that index
721
+ * Reads the value of one bar by its position: 0 is the oldest bar, `bar_index` counts the same way. This is not
722
+ * the "bars back" of `offset()`: inside a bar loop, read the current bar with `c.get(series)` and earlier bars with
723
+ * `c.get(series, k)`.
724
+ * @param index - Bar position, 0 for the oldest bar.
725
+ * @returns The value of that bar; NaN when there is no such bar.
726
+ * @example
727
+ * ```js
728
+ * const firstClose = close.get(0);
729
+ * ```
662
730
  */
663
731
  get(index: number): number {
664
732
  const values = this.toArray();
@@ -666,8 +734,12 @@ export class Series {
666
734
  }
667
735
 
668
736
  /**
669
- * Get the last value (most recent bar)
670
- * @returns Most recent value
737
+ * Reads the value of the newest bar.
738
+ * @returns The value of the last bar; NaN when there are no bars.
739
+ * @example
740
+ * ```js
741
+ * const lastRsi = ta.rsi(close, 14).last();
742
+ * ```
671
743
  */
672
744
  last(): number {
673
745
  const values = this.toArray();
@@ -675,17 +747,17 @@ export class Series {
675
747
  }
676
748
 
677
749
  /**
678
- * Get number of values
679
- * @returns Length of series
750
+ * Gives the number of bars of the series (a method, not a property).
751
+ * @returns The number of bars.
752
+ * @example
753
+ * ```js
754
+ * const bars = close.length();
755
+ * ```
680
756
  */
681
757
  length(): number {
682
758
  return this.dataSource.length;
683
759
  }
684
760
 
685
- /**
686
- * Convert series to time-value pairs for charting
687
- * @returns Array of { time, value } objects
688
- */
689
761
  /**
690
762
  * A Series holds the values of all the bars, so JavaScript operators cannot use it: without this guard
691
763
  * `close - open` is NaN, `close > open` false and `` `${rsi}` `` "[object Object]", with no error. The
@@ -705,6 +777,14 @@ export class Series {
705
777
  );
706
778
  }
707
779
 
780
+ /**
781
+ * Gives the values as `{ time, value }` points for a chart line, oldest first, leaving out the NaN bars.
782
+ * @returns The points; `time` is the bar time, in the unit of the bars.
783
+ * @example
784
+ * ```js
785
+ * const points = ta.sma(close, 20).toTimeValuePairs();
786
+ * ```
787
+ */
708
788
  toTimeValuePairs(): Array<{ time: any; value: number }> {
709
789
  const values = this.toArray();
710
790
  const bars = this.dataSource.bars;