oakscriptjs 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +1065 -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 +1065 -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 +1517 -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
@@ -153,32 +153,30 @@ class Series {
153
153
  cached = null;
154
154
  cachedVersion = -1;
155
155
  /**
156
- * Create a new Series
157
- *
158
- * @param data - Bar data (Bar[] or BarData)
159
- * @param extractor - Function to extract/compute value for each bar
156
+ * True for the built-in price series (`open`, `high`, `low`, `close`, `volume`, `hl2`...): their history covers
157
+ * every bar, so `offset()` may reach further than 5000 bars back.
160
158
  */
159
+ fullHistory = false;
161
160
  /**
162
- * True for PineScript's built-in price variables (open, high, low, close, volume, hl2...): their history covers
163
- * every bar, so references further than 5000 bars back are allowed (measured: close[bar_index] runs).
161
+ * Makes a Series that computes its value on each bar with a function. Scripts rarely need it: Series come from the
162
+ * builtins, `ta.*` and the Series methods.
163
+ * @param data - The bars the series is aligned to (an array of bars, or a {@link BarData}).
164
+ * @param extractor - Gives the value of bar `index`: `(bar, index, bars) => number`; NaN for na.
165
+ * @example
166
+ * ```js
167
+ * const range = new Series(close.bars, (bar) => bar.high - bar.low);
168
+ * ```
164
169
  */
165
- fullHistory = false;
166
170
  constructor(data, extractor) {
167
171
  this.dataSource = data instanceof BarData ? data : new BarData(data);
168
172
  this.extractor = extractor;
169
173
  runHooks?.created(this);
170
174
  }
171
- /**
172
- * Get the underlying bar data
173
- * @returns Bar array
174
- */
175
+ /** The bars this series is aligned to (`{ time, open, high, low, close, volume }`, oldest first). */
175
176
  get bars() {
176
177
  return this.dataSource.bars;
177
178
  }
178
- /**
179
- * Get the underlying BarData source
180
- * @returns BarData instance
181
- */
179
+ /** The {@link BarData} holding the bars this series is aligned to. */
182
180
  get barData() {
183
181
  return this.dataSource;
184
182
  }
@@ -186,10 +184,15 @@ class Series {
186
184
  // Static Factory Methods
187
185
  // ============================================
188
186
  /**
189
- * Create Series from bar data by extracting a specific field
190
- * @param bars - Bar data (Bar[] or BarData)
191
- * @param field - Field to extract ('open', 'high', 'low', 'close', 'volume')
192
- * @returns Series with extracted field values
187
+ * Makes the Series of one field of the bars (the price series). In a script, use the globals `open`, `high`, `low`,
188
+ * `close`, `volume` instead.
189
+ * @param bars - The bars (an array of bars, or a {@link BarData}).
190
+ * @param field - `'open'`, `'high'`, `'low'`, `'close'` or `'volume'`.
191
+ * @returns A Series of that field; NaN where a bar does not have it.
192
+ * @example
193
+ * ```js
194
+ * const myClose = Series.fromBars(close.bars, "close");
195
+ * ```
193
196
  */
194
197
  static fromBars(bars, field) {
195
198
  const series = new Series(bars, (bar) => bar[field] ?? NaN);
@@ -197,21 +200,31 @@ class Series {
197
200
  return series;
198
201
  }
199
202
  /**
200
- * Create a constant series (same value for all bars)
201
- * @param bars - Bar data (Bar[] or BarData)
202
- * @param value - Constant value
203
- * @returns Series with constant value
203
+ * Makes a Series with the same value on every bar. The Series methods also take plain numbers, so a script seldom
204
+ * needs it.
205
+ * @param bars - The bars to align to (an array of bars, or a {@link BarData}).
206
+ * @param value - The value of every bar (NaN for na).
207
+ * @returns A Series of `value`.
208
+ * @example
209
+ * ```js
210
+ * const level = Series.constant(close.bars, 50);
211
+ * ```
204
212
  */
205
213
  static constant(bars, value) {
206
214
  return new Series(bars, () => value);
207
215
  }
208
216
  /**
209
- * Create series from array of values
210
- * @param bars - Bar data (Bar[] or BarData) for time alignment
211
- * @param values - Array of values
212
- * @returns Series
217
+ * Makes a Series from an array with one value per bar, oldest first. In a script, `seriesOf(values)` does the same
218
+ * on the chart bars.
219
+ * @param bars - The bars the values belong to (an array of bars, or a {@link BarData}).
220
+ * @param values - One number per bar, oldest first; a missing value reads as NaN.
221
+ * @returns A Series of the values.
213
222
  * @throws When read on other bars with a different length than `values` (e.g. a series computed on the
214
- * chart bars and read inside a `request.security` expression, which runs on the higher-timeframe bars)
223
+ * chart bars and read inside a `request.security` expression, which runs on the other timeframe's bars).
224
+ * @example
225
+ * ```js
226
+ * const doubled = Series.fromArray(close.bars, close.toArray().map((v) => v * 2));
227
+ * ```
215
228
  */
216
229
  static fromArray(bars, values) {
217
230
  const origin = bars instanceof BarData ? bars.bars : bars;
@@ -228,9 +241,13 @@ class Series {
228
241
  // Arithmetic Operations
229
242
  // ============================================
230
243
  /**
231
- * Add two series or a series and a number
232
- * @param other - Series or number to add
233
- * @returns New Series representing the sum
244
+ * Adds a Series or a number to this series, bar by bar (the `+` of two values). NaN on either side gives NaN.
245
+ * @param other - Series or number to add.
246
+ * @returns A Series of the sums.
247
+ * @example
248
+ * ```js
249
+ * const mid = high.add(low).div(2);
250
+ * ```
234
251
  */
235
252
  add(other) {
236
253
  return new Series(this.dataSource, (bar, i, data) => {
@@ -240,9 +257,13 @@ class Series {
240
257
  });
241
258
  }
242
259
  /**
243
- * Subtract two series or subtract a number from a series
244
- * @param other - Series or number to subtract
245
- * @returns New Series representing the difference
260
+ * Subtracts a Series or a number from this series, bar by bar (the `-` of two values). NaN on either side gives NaN.
261
+ * @param other - Series or number to subtract.
262
+ * @returns A Series of the differences.
263
+ * @example
264
+ * ```js
265
+ * const body = close.sub(open);
266
+ * ```
246
267
  */
247
268
  sub(other) {
248
269
  return new Series(this.dataSource, (bar, i, data) => {
@@ -252,9 +273,13 @@ class Series {
252
273
  });
253
274
  }
254
275
  /**
255
- * Multiply two series or a series by a number
256
- * @param other - Series or number to multiply
257
- * @returns New Series representing the product
276
+ * Multiplies this series by a Series or a number, bar by bar (the `*` of two values). NaN on either side gives NaN.
277
+ * @param other - Series or number to multiply by.
278
+ * @returns A Series of the products.
279
+ * @example
280
+ * ```js
281
+ * const upper = ta.sma(close, 20).mul(1.02);
282
+ * ```
258
283
  */
259
284
  mul(other) {
260
285
  return new Series(this.dataSource, (bar, i, data) => {
@@ -264,9 +289,14 @@ class Series {
264
289
  });
265
290
  }
266
291
  /**
267
- * Raise the series to a power (PineScript math.pow on a series)
268
- * @param exponent - Series or number exponent
269
- * @returns New Series representing base ** exponent
292
+ * Raises this series to a power, bar by bar (`math.pow` on a series). NaN on either side gives NaN; a negative base
293
+ * with a fractional exponent gives NaN.
294
+ * @param exponent - Series or number exponent.
295
+ * @returns A Series of `this ** exponent`.
296
+ * @example
297
+ * ```js
298
+ * const squared = close.sub(ta.sma(close, 20)).pow(2);
299
+ * ```
270
300
  */
271
301
  pow(exponent) {
272
302
  return new Series(this.dataSource, (bar, i, data) => {
@@ -276,9 +306,15 @@ class Series {
276
306
  });
277
307
  }
278
308
  /**
279
- * Divide two series or a series by a number
280
- * @param other - Series or number to divide by
281
- * @returns New Series representing the quotient
309
+ * Divides this series by a Series or a number, bar by bar (the `/` of two values). A division by 0 gives
310
+ * +/-Infinity (which `na()` treats as na), 0 / 0 and NaN on either side give NaN.
311
+ * @param other - Series or number to divide by.
312
+ * @returns A Series of the quotients.
313
+ * @example
314
+ * ```js
315
+ * const range = high.sub(low);
316
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
317
+ * ```
282
318
  */
283
319
  div(other) {
284
320
  return new Series(this.dataSource, (bar, i, data) => {
@@ -288,9 +324,14 @@ class Series {
288
324
  });
289
325
  }
290
326
  /**
291
- * Modulo operation
292
- * @param other - Series or number for modulo
293
- * @returns New Series representing the remainder
327
+ * Remainder of the division of this series by a Series or a number, bar by bar (the `%` of two values). The
328
+ * remainder has the sign of this series; a divisor of 0 and NaN on either side give NaN.
329
+ * @param other - Series or number to divide by.
330
+ * @returns A Series of the remainders.
331
+ * @example
332
+ * ```js
333
+ * const everyFifth = bar_index.mod(5).eq(0);
334
+ * ```
294
335
  */
295
336
  mod(other) {
296
337
  return new Series(this.dataSource, (bar, i, data) => {
@@ -300,8 +341,12 @@ class Series {
300
341
  });
301
342
  }
302
343
  /**
303
- * Negation
304
- * @returns New Series with negated values
344
+ * Changes the sign of every value (the unary `-`). NaN stays NaN.
345
+ * @returns A Series of the opposite values.
346
+ * @example
347
+ * ```js
348
+ * const drop = close.sub(close.offset(1)).neg();
349
+ * ```
305
350
  */
306
351
  neg() {
307
352
  return new Series(this.dataSource, (bar, i, data) => {
@@ -312,9 +357,14 @@ class Series {
312
357
  // Comparison Operations (PineScript operators: tolerance 1e-10, false with na; see src/compare)
313
358
  // ============================================
314
359
  /**
315
- * Greater than comparison
316
- * @param other - Series or number to compare
317
- * @returns New Series with 1 where true, 0 where false
360
+ * Tests whether this series is above a Series or a number, bar by bar (`a > b`).
361
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
362
+ * @param other - Series or number to compare with.
363
+ * @returns A Series of 1 where it is true, 0 where it is false.
364
+ * @example
365
+ * ```js
366
+ * const up = close.gt(open);
367
+ * ```
318
368
  */
319
369
  gt(other) {
320
370
  return new Series(this.dataSource, (bar, i, data) => {
@@ -324,9 +374,14 @@ class Series {
324
374
  });
325
375
  }
326
376
  /**
327
- * Greater than or equal comparison
328
- * @param other - Series or number to compare
329
- * @returns New Series with 1 where true, 0 where false
377
+ * Tests whether this series is above or equal to a Series or a number, bar by bar (`a >= b`).
378
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
379
+ * @param other - Series or number to compare with.
380
+ * @returns A Series of 1 where it is true, 0 where it is false.
381
+ * @example
382
+ * ```js
383
+ * const atOrAbove = close.gte(ta.sma(close, 50));
384
+ * ```
330
385
  */
331
386
  gte(other) {
332
387
  return new Series(this.dataSource, (bar, i, data) => {
@@ -336,9 +391,14 @@ class Series {
336
391
  });
337
392
  }
338
393
  /**
339
- * Less than comparison
340
- * @param other - Series or number to compare
341
- * @returns New Series with 1 where true, 0 where false
394
+ * Tests whether this series is below a Series or a number, bar by bar (`a < b`).
395
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
396
+ * @param other - Series or number to compare with.
397
+ * @returns A Series of 1 where it is true, 0 where it is false.
398
+ * @example
399
+ * ```js
400
+ * const oversold = ta.rsi(close, 14).lt(30);
401
+ * ```
342
402
  */
343
403
  lt(other) {
344
404
  return new Series(this.dataSource, (bar, i, data) => {
@@ -348,9 +408,14 @@ class Series {
348
408
  });
349
409
  }
350
410
  /**
351
- * Less than or equal comparison
352
- * @param other - Series or number to compare
353
- * @returns New Series with 1 where true, 0 where false
411
+ * Tests whether this series is below or equal to a Series or a number, bar by bar (`a <= b`).
412
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
413
+ * @param other - Series or number to compare with.
414
+ * @returns A Series of 1 where it is true, 0 where it is false.
415
+ * @example
416
+ * ```js
417
+ * const atOrBelow = low.lte(ta.lowest(low, 20).offset(1));
418
+ * ```
354
419
  */
355
420
  lte(other) {
356
421
  return new Series(this.dataSource, (bar, i, data) => {
@@ -360,9 +425,14 @@ class Series {
360
425
  });
361
426
  }
362
427
  /**
363
- * Equality comparison
364
- * @param other - Series or number to compare
365
- * @returns New Series with 1 where true, 0 where false
428
+ * Tests whether this series equals a Series or a number, bar by bar (`a == b`).
429
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
430
+ * @param other - Series or number to compare with.
431
+ * @returns A Series of 1 where it is true, 0 where it is false.
432
+ * @example
433
+ * ```js
434
+ * const doji = close.eq(open);
435
+ * ```
366
436
  */
367
437
  eq(other) {
368
438
  return new Series(this.dataSource, (bar, i, data) => {
@@ -372,9 +442,14 @@ class Series {
372
442
  });
373
443
  }
374
444
  /**
375
- * Not equal comparison
376
- * @param other - Series or number to compare
377
- * @returns New Series with 1 where true, 0 where false
445
+ * Tests whether this series differs from a Series or a number, bar by bar (`a != b`).
446
+ * Values closer than 1e-10 count as equal, and a NaN on either side gives 0 (false), never NaN.
447
+ * @param other - Series or number to compare with.
448
+ * @returns A Series of 1 where it is true, 0 where it is false.
449
+ * @example
450
+ * ```js
451
+ * const moved = close.neq(close.offset(1));
452
+ * ```
378
453
  */
379
454
  neq(other) {
380
455
  return new Series(this.dataSource, (bar, i, data) => {
@@ -387,9 +462,13 @@ class Series {
387
462
  // Logical Operations (for boolean series)
388
463
  // ============================================
389
464
  /**
390
- * Logical AND
391
- * @param other - Series or number (truthy/falsy)
392
- * @returns New Series with 1 where both true, 0 otherwise
465
+ * Logical "and" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
466
+ * @param other - The other condition: a Series or a number.
467
+ * @returns A Series of 1 where both are true, 0 elsewhere.
468
+ * @example
469
+ * ```js
470
+ * const strongUp = close.gt(open).and(volume.gt(ta.sma(volume, 20)));
471
+ * ```
393
472
  */
394
473
  and(other) {
395
474
  return new Series(this.dataSource, (bar, i, data) => {
@@ -399,9 +478,13 @@ class Series {
399
478
  });
400
479
  }
401
480
  /**
402
- * Logical OR
403
- * @param other - Series or number (truthy/falsy)
404
- * @returns New Series with 1 where either true, 0 otherwise
481
+ * Logical "or" of two conditions, bar by bar. A value is true when it is not 0 and not NaN.
482
+ * @param other - The other condition: a Series or a number.
483
+ * @returns A Series of 1 where at least one is true, 0 elsewhere.
484
+ * @example
485
+ * ```js
486
+ * const extreme = ta.rsi(close, 14).gt(70).or(ta.rsi(close, 14).lt(30));
487
+ * ```
405
488
  */
406
489
  or(other) {
407
490
  return new Series(this.dataSource, (bar, i, data) => {
@@ -411,8 +494,12 @@ class Series {
411
494
  });
412
495
  }
413
496
  /**
414
- * Logical NOT
415
- * @returns New Series with inverted boolean values
497
+ * Logical "not" of a condition, bar by bar: true (not 0, not NaN) becomes 0, and 0 or NaN becomes 1.
498
+ * @returns A Series of 1 where this series is false, 0 where it is true.
499
+ * @example
500
+ * ```js
501
+ * const notUp = close.gt(open).not();
502
+ * ```
416
503
  */
417
504
  not() {
418
505
  return new Series(this.dataSource, (bar, i, data) => {
@@ -424,21 +511,16 @@ class Series {
424
511
  // Conditional Selection
425
512
  // ============================================
426
513
  /**
427
- * Bar-by-bar conditional selection (ternary operator for Series)
428
- * Uses this Series as the condition (truthy = nonzero, falsy = zero/NaN)
429
- *
430
- * @param trueValue - Value or Series to use when condition is truthy
431
- * @param falseValue - Value or Series to use when condition is falsy
432
- * @returns New Series with bar-by-bar selected values
433
- *
514
+ * Picks one of two values on each bar, with this series as the condition (the `cond ? x : y` of a series). The
515
+ * condition is true when it is not 0 and not NaN; 0 and NaN pick `falseValue`.
516
+ * @param trueValue - Series or number used where the condition is true.
517
+ * @param falseValue - Series or number used where the condition is false.
518
+ * @returns A Series of the picked values.
434
519
  * @example
435
- * ```typescript
436
- * // PineScript: result = condition ? trueVal : falseVal
437
- * const result = condition.iff(trueVal, falseVal);
438
- *
439
- * // Example: RSI formula uses ternary for edge cases
440
- * // rsi = down == 0 ? 100 : up == 0 ? 0 : formula
441
- * const rsi = down.eq(0).iff(100, up.eq(0).iff(0, formula));
520
+ * ```js
521
+ * const range = high.sub(low);
522
+ * const ratio = range.gt(0).iff(close.sub(open).div(range), 0);
523
+ * plot(ratio, "Body / range");
442
524
  * ```
443
525
  */
444
526
  iff(trueValue, falseValue) {
@@ -456,10 +538,17 @@ class Series {
456
538
  // Offset/History
457
539
  // ============================================
458
540
  /**
459
- * Access previous bars (like close[1] in PineScript)
460
- * @param offset - Number of bars back (0 or more)
461
- * @returns New Series with offset values
462
- * @throws RangeError when `offset` is negative
541
+ * Shifts the series back in time: on each bar, the value `offset` bars earlier (`close.offset(1)` is the previous
542
+ * close). The first `offset` bars, which have no earlier bar, get NaN.
543
+ * @param offset - Number of bars back: 0 (the series itself) or more, a whole number. A series computed by the
544
+ * script can look back at most 5000 bars; the price series (`close`, `high`...) any number.
545
+ * @returns A Series of the earlier values.
546
+ * @throws RangeError when `offset` is negative (it would read a future bar); an error past 5000 bars.
547
+ * @example
548
+ * ```js
549
+ * const change = close.sub(close.offset(1));
550
+ * const higherHigh = high.gt(high.offset(1));
551
+ * ```
463
552
  */
464
553
  offset(offset) {
465
554
  if (offset < 0) throw new RangeError(`Series.offset(${offset}): the offset cannot be negative (it would read a future bar).`);
@@ -476,14 +565,14 @@ class Series {
476
565
  // Computation & Access
477
566
  // ============================================
478
567
  /**
479
- * Compute all values for the series
480
- *
481
- * Now includes automatic cache invalidation:
482
- * - Checks if dataSource version has changed since cache was computed
483
- * - Recomputes if version mismatch detected
484
- * - Stores version when cache is created
485
- *
486
- * @returns Array of computed values
568
+ * Gives the values of all the bars as a plain array, oldest bar first (NaN for na). The values are computed on the
569
+ * first read and kept until the bars change; the array is shared, so copy it before changing it.
570
+ * @returns One number per bar, oldest first.
571
+ * @example
572
+ * ```js
573
+ * const values = ta.sma(close, 20).toArray();
574
+ * const shifted = seriesOf(values.map((v) => v * 1.01));
575
+ * ```
487
576
  */
488
577
  toArray() {
489
578
  if (this.cached !== null && this.cachedVersion === this.dataSource.version) {
@@ -506,24 +595,13 @@ class Series {
506
595
  return this.cached;
507
596
  }
508
597
  /**
509
- * Materialize this Series by eagerly computing values and breaking closure chain
510
- *
511
- * This method addresses the closure chain memory leak issue:
512
- * - Computes all values immediately
513
- * - Creates a new Series that stores values directly
514
- * - Breaks references to parent Series objects
515
- * - Useful for complex expressions to free intermediate Series memory
516
- *
598
+ * Computes the values now and returns a Series that stores them, without links to the series it was built from.
599
+ * Use it on a long chain of methods to compute it once and free the steps in between.
600
+ * @returns A Series of the same values, stored.
517
601
  * @example
518
- * ```typescript
519
- * // Without materialize: keeps a, b, c, d in memory via closures
520
- * const result = a.add(b).mul(c).div(d).sub(e);
521
- *
522
- * // With materialize: breaks chain, frees a, b, c memory
523
- * const result = a.add(b).mul(c).materialize().div(d).sub(e);
602
+ * ```js
603
+ * const spread = high.sub(low).div(close).mul(100).materialize();
524
604
  * ```
525
- *
526
- * @returns New Series with computed values and no closure dependencies
527
605
  */
528
606
  materialize() {
529
607
  const values = this.toArray();
@@ -552,33 +630,43 @@ class Series {
552
630
  return this.toArray();
553
631
  }
554
632
  /**
555
- * Get value at specific index
556
- * @param index - Bar index
557
- * @returns Value at that index
633
+ * Reads the value of one bar by its position: 0 is the oldest bar, `bar_index` counts the same way. This is not
634
+ * the "bars back" of `offset()`: inside a bar loop, read the current bar with `c.get(series)` and earlier bars with
635
+ * `c.get(series, k)`.
636
+ * @param index - Bar position, 0 for the oldest bar.
637
+ * @returns The value of that bar; NaN when there is no such bar.
638
+ * @example
639
+ * ```js
640
+ * const firstClose = close.get(0);
641
+ * ```
558
642
  */
559
643
  get(index) {
560
644
  const values = this.toArray();
561
645
  return values[index] ?? NaN;
562
646
  }
563
647
  /**
564
- * Get the last value (most recent bar)
565
- * @returns Most recent value
648
+ * Reads the value of the newest bar.
649
+ * @returns The value of the last bar; NaN when there are no bars.
650
+ * @example
651
+ * ```js
652
+ * const lastRsi = ta.rsi(close, 14).last();
653
+ * ```
566
654
  */
567
655
  last() {
568
656
  const values = this.toArray();
569
657
  return values[values.length - 1] ?? NaN;
570
658
  }
571
659
  /**
572
- * Get number of values
573
- * @returns Length of series
660
+ * Gives the number of bars of the series (a method, not a property).
661
+ * @returns The number of bars.
662
+ * @example
663
+ * ```js
664
+ * const bars = close.length();
665
+ * ```
574
666
  */
575
667
  length() {
576
668
  return this.dataSource.length;
577
669
  }
578
- /**
579
- * Convert series to time-value pairs for charting
580
- * @returns Array of { time, value } objects
581
- */
582
670
  /**
583
671
  * A Series holds the values of all the bars, so JavaScript operators cannot use it: without this guard
584
672
  * `close - open` is NaN, `close > open` false and `` `${rsi}` `` "[object Object]", with no error. The
@@ -594,6 +682,14 @@ class Series {
594
682
  "JavaScript operators (+ - * / % ** < > <= >= ==) do not work on a Series: use its methods, 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), 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)."
595
683
  );
596
684
  }
685
+ /**
686
+ * Gives the values as `{ time, value }` points for a chart line, oldest first, leaving out the NaN bars.
687
+ * @returns The points; `time` is the bar time, in the unit of the bars.
688
+ * @example
689
+ * ```js
690
+ * const points = ta.sma(close, 20).toTimeValuePairs();
691
+ * ```
692
+ */
597
693
  toTimeValuePairs() {
598
694
  const values = this.toArray();
599
695
  const bars = this.dataSource.bars;