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
@@ -35,13 +35,28 @@ function freePosition(position: TablePosition, force_overlay: boolean, keep?: Ta
35
35
  }
36
36
 
37
37
  /**
38
- * Creates a new table (PineScript `table.new`). A live table at the same position with the same `force_overlay` is
39
- * deleted, as PineScript keeps one table per position and pane (#176, #182).
40
- *
38
+ * Creates a table fixed at a place of the pane (it does not scroll with the bars) and returns its handle; fill its
39
+ * cells with `table.cell()`. A pane holds one table per position: a live table at the same position (with the same
40
+ * `force_overlay`) is deleted. The arguments after `rows` can also be given by name in an object.
41
+ * @param position - Where the table sits: `position.top_left`, `position.top_center`, `position.top_right`,
42
+ * `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`,
43
+ * `position.bottom_center` or `position.bottom_right`.
44
+ * @param columns - Number of columns.
45
+ * @param rows - Number of rows.
46
+ * @param bgcolor - Background colour of the whole table. Default: none (transparent).
47
+ * @param frame_color - Colour of the outer frame. Default: none.
48
+ * @param frame_width - Width of the outer frame in pixels. Default: 0 (no frame).
49
+ * @param border_color - Colour of the lines between the cells. Default: none.
50
+ * @param border_width - Width of the lines between the cells in pixels. Default: 0 (no lines).
51
+ * @param force_overlay - When true, the table is drawn on the price pane even if the script has its own pane.
52
+ * Default: false.
53
+ * @returns The new table.
41
54
  * @example
42
- * ```typescript
43
- * const t = table.new(position.top_right, 2, 3, '#131722', '#787B86', 1);
44
- * table.cell(t, 0, 0, 'RSI', 0, 0, '#FFFFFF');
55
+ * ```js
56
+ * const t = table.new(position.top_right, 2, 1, { bgcolor: color.new(color.black, 20), border_width: 1 });
57
+ * eachBar((c) => {
58
+ * if (c.i === c.n - 1) table.cell(t, 0, 0, "Close", { text_color: color.white });
59
+ * });
45
60
  * ```
46
61
  */
47
62
  export function new_table(
@@ -91,7 +106,34 @@ function cellOf(caller: string, id: Table, column: number, row: number): TableCe
91
106
  }
92
107
 
93
108
  /**
94
- * Defines a cell (PineScript `table.cell`): its text and properties. Omitted properties get their default.
109
+ * Writes a cell of a table: its text and look. The properties not given get their default, so calling it again
110
+ * replaces the whole cell; use `table.cell_set_*()` to change one property. A column or row outside the table is an
111
+ * error; on a deleted table it does nothing. The arguments after `row` can also be given by name in an object.
112
+ * @param table_id - The table (from `table.new()`).
113
+ * @param column - Column of the cell, from 0 (left).
114
+ * @param row - Row of the cell, from 0 (top).
115
+ * @param text - Text of the cell; `\n` starts a new line. Default: "".
116
+ * @param width - Width of the cell in percent of the pane's width; 0 fits the text. Default: 0.
117
+ * @param height - Height of the cell in percent of the pane's height; 0 fits the text. Default: 0.
118
+ * @param text_color - Text colour. Default: dark gray (#363A45).
119
+ * @param text_halign - Horizontal alignment of the text: `text.align_left`, `text.align_center` or
120
+ * `text.align_right`. Default: `text.align_center`.
121
+ * @param text_valign - Vertical alignment of the text: `text.align_top`, `text.align_center` or
122
+ * `text.align_bottom`. Default: `text.align_center`.
123
+ * @param text_size - `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`, `size.auto`, or a size in
124
+ * points. Default: `size.auto`.
125
+ * @param bgcolor - Background colour of the cell. Default: none (the table's background shows).
126
+ * @param tooltip - Text shown when the pointer is over the cell. Default: none.
127
+ * @param text_font_family - `font.family_default` or `font.family_monospace`. Default: `font.family_default`.
128
+ * @param text_formatting - `text.format_bold`, `text.format_italic`, their sum, or `text.format_none`.
129
+ * Default: `text.format_none`.
130
+ * @example
131
+ * ```js
132
+ * const t = table.new(position.bottom_right, 2, 1);
133
+ * eachBar((c) => {
134
+ * if (c.i === c.n - 1) table.cell(t, 1, 0, str.tostring(c.close, "#.##"), { bgcolor: color.teal, text_color: color.white });
135
+ * });
136
+ * ```
95
137
  */
96
138
  export function cell(
97
139
  table_id: Table,
@@ -126,39 +168,219 @@ export function cell(
126
168
  });
127
169
  }
128
170
 
129
- function setCell<K extends keyof TableCell>(name: string, field: K) {
130
- return (table_id: Table, column: number, row: number, value: TableCell[K]): void => {
131
- if (isDeleted(table_id)) return;
132
- cellOf(`table.${name}()`, table_id, column, row)[field] = value;
133
- };
134
- }
135
-
136
- /** Sets the text of a cell. */
137
- export const cell_set_text = setCell('cell_set_text', 'text');
138
- /** Sets the width of a cell (% of the chart width; 0 fits the text). */
139
- export const cell_set_width = setCell('cell_set_width', 'width');
140
- /** Sets the height of a cell (% of the chart height; 0 fits the text). */
141
- export const cell_set_height = setCell('cell_set_height', 'height');
142
- /** Sets the text colour of a cell. */
143
- export const cell_set_text_color = setCell('cell_set_text_color', 'text_color');
144
- /** Sets the horizontal text alignment of a cell. */
145
- export const cell_set_text_halign = setCell('cell_set_text_halign', 'text_halign');
146
- /** Sets the vertical text alignment of a cell. */
147
- export const cell_set_text_valign = setCell('cell_set_text_valign', 'text_valign');
148
- /** Sets the text size of a cell. */
149
- export const cell_set_text_size = setCell('cell_set_text_size', 'text_size');
150
- /** Sets the background colour of a cell. */
151
- export const cell_set_bgcolor = setCell('cell_set_bgcolor', 'bgcolor');
152
- /** Sets the tooltip of a cell. */
153
- export const cell_set_tooltip = setCell('cell_set_tooltip', 'tooltip');
154
- /** Sets the font family of a cell. */
155
- export const cell_set_text_font_family = setCell('cell_set_text_font_family', 'text_font_family');
156
- /** Sets the text formatting of a cell (`text.format_*` flags). */
157
- export const cell_set_text_formatting = setCell('cell_set_text_formatting', 'text_formatting');
158
-
159
- /**
160
- * Merges the cells from start_column / start_row to end_column / end_row (PineScript `table.merge_cells`). The
161
- * merged cell shows the content of the start cell.
171
+ /** Sets one property of a cell (the cell is created when it was not defined); nothing on a deleted table. */
172
+ function setCell<K extends keyof TableCell>(name: string, field: K, table_id: Table, column: number, row: number, value: TableCell[K]): void {
173
+ if (isDeleted(table_id)) return;
174
+ cellOf(`table.${name}()`, table_id, column, row)[field] = value;
175
+ }
176
+
177
+ /**
178
+ * Changes the text of a cell and keeps its other properties. A cell that was not written yet is created.
179
+ * A column or row outside the table is an error; on a deleted table it does nothing.
180
+ * @param table_id - The table.
181
+ * @param column - Column of the cell, from 0.
182
+ * @param row - Row of the cell, from 0.
183
+ * @param value - New text; `\n` starts a new line.
184
+ * @example
185
+ * ```js
186
+ * const t = table.new(position.top_right, 1, 1);
187
+ * eachBar((c) => {
188
+ * if (c.i === c.n - 1) table.cell_set_text(t, 0, 0, "Close " + str.tostring(c.close, "#.##"));
189
+ * });
190
+ * ```
191
+ */
192
+ export function cell_set_text(table_id: Table, column: number, row: number, value: TableCell['text']): void {
193
+ setCell('cell_set_text', 'text', table_id, column, row, value);
194
+ }
195
+
196
+ /**
197
+ * Changes the width of a cell. A cell that was not written yet is created.
198
+ * @param table_id - The table.
199
+ * @param column - Column of the cell, from 0.
200
+ * @param row - Row of the cell, from 0.
201
+ * @param value - Width in percent of the pane's width; 0 fits the text.
202
+ * @example
203
+ * ```js
204
+ * const t = table.new(position.top_right, 1, 1);
205
+ * table.cell(t, 0, 0, "Trend");
206
+ * table.cell_set_width(t, 0, 0, 10);
207
+ * ```
208
+ */
209
+ export function cell_set_width(table_id: Table, column: number, row: number, value: TableCell['width']): void {
210
+ setCell('cell_set_width', 'width', table_id, column, row, value);
211
+ }
212
+
213
+ /**
214
+ * Changes the height of a cell. A cell that was not written yet is created.
215
+ * @param table_id - The table.
216
+ * @param column - Column of the cell, from 0.
217
+ * @param row - Row of the cell, from 0.
218
+ * @param value - Height in percent of the pane's height; 0 fits the text.
219
+ * @example
220
+ * ```js
221
+ * const t = table.new(position.top_right, 1, 1);
222
+ * table.cell(t, 0, 0, "Trend");
223
+ * table.cell_set_height(t, 0, 0, 5);
224
+ * ```
225
+ */
226
+ export function cell_set_height(table_id: Table, column: number, row: number, value: TableCell['height']): void {
227
+ setCell('cell_set_height', 'height', table_id, column, row, value);
228
+ }
229
+
230
+ /**
231
+ * Changes the text colour of a cell. A cell that was not written yet is created.
232
+ * @param table_id - The table.
233
+ * @param column - Column of the cell, from 0.
234
+ * @param row - Row of the cell, from 0.
235
+ * @param value - New text colour (`color.red`, `"#FF0000"`...).
236
+ * @example
237
+ * ```js
238
+ * const t = table.new(position.top_right, 1, 1);
239
+ * eachBar((c) => {
240
+ * if (c.i === c.n - 1) table.cell_set_text_color(t, 0, 0, c.close > c.open ? color.green : color.red);
241
+ * });
242
+ * ```
243
+ */
244
+ export function cell_set_text_color(table_id: Table, column: number, row: number, value: TableCell['text_color']): void {
245
+ setCell('cell_set_text_color', 'text_color', table_id, column, row, value);
246
+ }
247
+
248
+ /**
249
+ * Changes the horizontal alignment of the text of a cell. A cell that was not written yet is created.
250
+ * @param table_id - The table.
251
+ * @param column - Column of the cell, from 0.
252
+ * @param row - Row of the cell, from 0.
253
+ * @param value - `text.align_left`, `text.align_center` or `text.align_right`.
254
+ * @example
255
+ * ```js
256
+ * const t = table.new(position.top_right, 1, 1);
257
+ * table.cell(t, 0, 0, "Volume", { width: 15 });
258
+ * table.cell_set_text_halign(t, 0, 0, text.align_left);
259
+ * ```
260
+ */
261
+ export function cell_set_text_halign(table_id: Table, column: number, row: number, value: TableCell['text_halign']): void {
262
+ setCell('cell_set_text_halign', 'text_halign', table_id, column, row, value);
263
+ }
264
+
265
+ /**
266
+ * Changes the vertical alignment of the text of a cell. A cell that was not written yet is created.
267
+ * @param table_id - The table.
268
+ * @param column - Column of the cell, from 0.
269
+ * @param row - Row of the cell, from 0.
270
+ * @param value - `text.align_top`, `text.align_center` or `text.align_bottom`.
271
+ * @example
272
+ * ```js
273
+ * const t = table.new(position.top_right, 1, 1);
274
+ * table.cell(t, 0, 0, "Volume", { height: 8 });
275
+ * table.cell_set_text_valign(t, 0, 0, text.align_bottom);
276
+ * ```
277
+ */
278
+ export function cell_set_text_valign(table_id: Table, column: number, row: number, value: TableCell['text_valign']): void {
279
+ setCell('cell_set_text_valign', 'text_valign', table_id, column, row, value);
280
+ }
281
+
282
+ /**
283
+ * Changes the text size of a cell. A cell that was not written yet is created.
284
+ * @param table_id - The table.
285
+ * @param column - Column of the cell, from 0.
286
+ * @param row - Row of the cell, from 0.
287
+ * @param value - `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`, `size.auto`, or a size in points.
288
+ * @example
289
+ * ```js
290
+ * const t = table.new(position.top_right, 1, 1);
291
+ * table.cell(t, 0, 0, "Signal");
292
+ * table.cell_set_text_size(t, 0, 0, size.large);
293
+ * ```
294
+ */
295
+ export function cell_set_text_size(table_id: Table, column: number, row: number, value: TableCell['text_size']): void {
296
+ setCell('cell_set_text_size', 'text_size', table_id, column, row, value);
297
+ }
298
+
299
+ /**
300
+ * Changes the background colour of a cell. A cell that was not written yet is created.
301
+ * @param table_id - The table.
302
+ * @param column - Column of the cell, from 0.
303
+ * @param row - Row of the cell, from 0.
304
+ * @param value - New background colour.
305
+ * @example
306
+ * ```js
307
+ * const t = table.new(position.top_right, 1, 1);
308
+ * eachBar((c) => {
309
+ * if (c.i === c.n - 1) table.cell_set_bgcolor(t, 0, 0, c.close > c.open ? color.teal : color.maroon);
310
+ * });
311
+ * ```
312
+ */
313
+ export function cell_set_bgcolor(table_id: Table, column: number, row: number, value: TableCell['bgcolor']): void {
314
+ setCell('cell_set_bgcolor', 'bgcolor', table_id, column, row, value);
315
+ }
316
+
317
+ /**
318
+ * Changes the tooltip of a cell: the text shown when the pointer is over it. A cell that was not written yet is
319
+ * created.
320
+ * @param table_id - The table.
321
+ * @param column - Column of the cell, from 0.
322
+ * @param row - Row of the cell, from 0.
323
+ * @param value - Tooltip text.
324
+ * @example
325
+ * ```js
326
+ * const t = table.new(position.top_right, 1, 1);
327
+ * table.cell(t, 0, 0, "ATR");
328
+ * table.cell_set_tooltip(t, 0, 0, "Average true range over 14 bars");
329
+ * ```
330
+ */
331
+ export function cell_set_tooltip(table_id: Table, column: number, row: number, value: TableCell['tooltip']): void {
332
+ setCell('cell_set_tooltip', 'tooltip', table_id, column, row, value);
333
+ }
334
+
335
+ /**
336
+ * Changes the font of a cell's text. A cell that was not written yet is created.
337
+ * @param table_id - The table.
338
+ * @param column - Column of the cell, from 0.
339
+ * @param row - Row of the cell, from 0.
340
+ * @param value - `font.family_default` or `font.family_monospace`.
341
+ * @example
342
+ * ```js
343
+ * const t = table.new(position.top_right, 1, 1);
344
+ * table.cell(t, 0, 0, "123.45");
345
+ * table.cell_set_text_font_family(t, 0, 0, font.family_monospace);
346
+ * ```
347
+ */
348
+ export function cell_set_text_font_family(table_id: Table, column: number, row: number, value: TableCell['text_font_family']): void {
349
+ setCell('cell_set_text_font_family', 'text_font_family', table_id, column, row, value);
350
+ }
351
+
352
+ /**
353
+ * Makes a cell's text bold, italic, both or plain. A cell that was not written yet is created.
354
+ * @param table_id - The table.
355
+ * @param column - Column of the cell, from 0.
356
+ * @param row - Row of the cell, from 0.
357
+ * @param value - `text.format_bold`, `text.format_italic`, their sum, or `text.format_none`.
358
+ * @example
359
+ * ```js
360
+ * const t = table.new(position.top_right, 1, 1);
361
+ * table.cell(t, 0, 0, "Summary");
362
+ * table.cell_set_text_formatting(t, 0, 0, text.format_bold + text.format_italic);
363
+ * ```
364
+ */
365
+ export function cell_set_text_formatting(table_id: Table, column: number, row: number, value: TableCell['text_formatting']): void {
366
+ setCell('cell_set_text_formatting', 'text_formatting', table_id, column, row, value);
367
+ }
368
+
369
+ /**
370
+ * Joins a block of cells into one cell, for a title over several columns for example. The merged cell shows the
371
+ * content and look of its start (top-left) cell. Merging the same block again does nothing. A cell outside the table,
372
+ * or an end before the start, is an error; on a deleted table it does nothing.
373
+ * @param table_id - The table.
374
+ * @param start_column - Column of the top-left cell of the block, from 0.
375
+ * @param start_row - Row of the top-left cell of the block, from 0.
376
+ * @param end_column - Column of the bottom-right cell of the block (not before `start_column`).
377
+ * @param end_row - Row of the bottom-right cell of the block (not before `start_row`).
378
+ * @example
379
+ * ```js
380
+ * const t = table.new(position.top_right, 2, 2);
381
+ * table.cell(t, 0, 0, "Daily stats");
382
+ * table.merge_cells(t, 0, 0, 1, 0); // the title spans both columns
383
+ * ```
162
384
  */
163
385
  export function merge_cells(
164
386
  table_id: Table,
@@ -181,8 +403,20 @@ export function merge_cells(
181
403
  }
182
404
 
183
405
  /**
184
- * Removes the cells from start_column / start_row to end_column / end_row (PineScript `table.clear`; default end:
185
- * the start cell), and the merges that start in that range.
406
+ * Empties a block of cells (text and look), and removes the merges that start inside it. Without an end, only the
407
+ * start cell is cleared. A cell outside the table is an error; on a deleted table it does nothing.
408
+ * @param table_id - The table.
409
+ * @param start_column - Column of the top-left cell of the block, from 0.
410
+ * @param start_row - Row of the top-left cell of the block, from 0.
411
+ * @param end_column - Column of the bottom-right cell of the block. Default: `start_column`.
412
+ * @param end_row - Row of the bottom-right cell of the block. Default: `start_row`.
413
+ * @example
414
+ * ```js
415
+ * const t = table.new(position.top_right, 3, 3);
416
+ * eachBar((c) => {
417
+ * if (c.i === c.n - 1) table.clear(t, 0, 0, 2, 2); // empty all nine cells
418
+ * });
419
+ * ```
186
420
  */
187
421
  export function clear(
188
422
  table_id: Table,
@@ -202,39 +436,131 @@ export function clear(
202
436
  if (table_id.merges) table_id.merges = table_id.merges.filter((m) => !inside(m.start_column, m.start_row));
203
437
  }
204
438
 
205
- function setTable<K extends keyof Table>(field: K) {
206
- return (table_id: Table, value: Table[K]): void => {
207
- if (isDeleted(table_id)) return;
208
- table_id[field] = value;
209
- };
439
+ /** Sets one property of a table; nothing on a deleted table. */
440
+ function setTable<K extends keyof Table>(table_id: Table, field: K, value: Table[K]): void {
441
+ if (isDeleted(table_id)) return;
442
+ table_id[field] = value;
210
443
  }
211
444
 
212
445
  /**
213
- * Moves the table to another position (`position.*`). A live table at that position with the same `force_overlay` is
214
- * deleted (#182).
446
+ * Moves the table to another place of the pane. A live table already at that position (with the same
447
+ * `force_overlay`) is deleted. Does nothing on a deleted table.
448
+ * @param table_id - The table.
449
+ * @param position - New place: `position.top_left`, `position.top_center`, `position.top_right`,
450
+ * `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`,
451
+ * `position.bottom_center` or `position.bottom_right`.
452
+ * @example
453
+ * ```js
454
+ * const where = input.string("Top right", "Table", { options: ["Top right", "Bottom left"] });
455
+ * const t = table.new(position.top_right, 1, 1);
456
+ * if (where === "Bottom left") table.set_position(t, position.bottom_left);
457
+ * ```
215
458
  */
216
459
  export function set_position(table_id: Table, position: TablePosition): void {
217
460
  if (isDeleted(table_id)) return;
218
461
  freePosition(position, table_id.force_overlay ?? false, table_id);
219
462
  table_id.position = position;
220
463
  }
221
- /** Sets the background colour of the table. */
222
- export const set_bgcolor = setTable('bgcolor');
223
- /** Sets the colour of the outer frame. */
224
- export const set_frame_color = setTable('frame_color');
225
- /** Sets the width of the outer frame. */
226
- export const set_frame_width = setTable('frame_width');
227
- /** Sets the colour of the cell borders. */
228
- export const set_border_color = setTable('border_color');
229
- /** Sets the width of the cell borders. */
230
- export const set_border_width = setTable('border_width');
231
-
232
- /** Deletes the table (PineScript `table.delete`). */
464
+
465
+ /**
466
+ * Changes the background colour of the whole table (cells with their own background keep it). Does nothing on a
467
+ * deleted table.
468
+ * @param table_id - The table.
469
+ * @param value - New background colour.
470
+ * @example
471
+ * ```js
472
+ * const t = table.new(position.top_right, 1, 1);
473
+ * eachBar((c) => {
474
+ * if (c.i === c.n - 1) table.set_bgcolor(t, c.close > c.open ? color.new(color.green, 80) : color.new(color.red, 80));
475
+ * });
476
+ * ```
477
+ */
478
+ export function set_bgcolor(table_id: Table, value: Table['bgcolor']): void {
479
+ setTable(table_id, 'bgcolor', value);
480
+ }
481
+
482
+ /**
483
+ * Changes the colour of the outer frame of the table. Does nothing on a deleted table.
484
+ * @param table_id - The table.
485
+ * @param value - New frame colour.
486
+ * @example
487
+ * ```js
488
+ * const t = table.new(position.top_right, 1, 1, { frame_width: 1 });
489
+ * table.set_frame_color(t, color.gray);
490
+ * ```
491
+ */
492
+ export function set_frame_color(table_id: Table, value: Table['frame_color']): void {
493
+ setTable(table_id, 'frame_color', value);
494
+ }
495
+
496
+ /**
497
+ * Changes the width of the outer frame of the table. Does nothing on a deleted table.
498
+ * @param table_id - The table.
499
+ * @param value - New frame width in pixels; 0 hides the frame.
500
+ * @example
501
+ * ```js
502
+ * const t = table.new(position.top_right, 1, 1, { frame_color: color.gray });
503
+ * table.set_frame_width(t, 2);
504
+ * ```
505
+ */
506
+ export function set_frame_width(table_id: Table, value: Table['frame_width']): void {
507
+ setTable(table_id, 'frame_width', value);
508
+ }
509
+
510
+ /**
511
+ * Changes the colour of the lines between the cells. Does nothing on a deleted table.
512
+ * @param table_id - The table.
513
+ * @param value - New border colour.
514
+ * @example
515
+ * ```js
516
+ * const t = table.new(position.top_right, 2, 2, { border_width: 1 });
517
+ * table.set_border_color(t, color.silver);
518
+ * ```
519
+ */
520
+ export function set_border_color(table_id: Table, value: Table['border_color']): void {
521
+ setTable(table_id, 'border_color', value);
522
+ }
523
+
524
+ /**
525
+ * Changes the width of the lines between the cells. Does nothing on a deleted table.
526
+ * @param table_id - The table.
527
+ * @param value - New border width in pixels; 0 hides the lines.
528
+ * @example
529
+ * ```js
530
+ * const t = table.new(position.top_right, 2, 2, { border_color: color.silver });
531
+ * table.set_border_width(t, 1);
532
+ * ```
533
+ */
534
+ export function set_border_width(table_id: Table, value: Table['border_width']): void {
535
+ setTable(table_id, 'border_width', value);
536
+ }
537
+
538
+ /**
539
+ * Removes the table from the pane and from `table.all`. Afterwards the table functions do nothing on it; deleting it
540
+ * again does nothing.
541
+ * @param table_id - The table to delete.
542
+ * @example
543
+ * ```js
544
+ * const show = input.bool(true, "Show table");
545
+ * const t = table.new(position.top_right, 1, 1);
546
+ * if (!show) table.delete(t);
547
+ * ```
548
+ */
233
549
  export function delete_table(table_id: Table): void {
234
550
  remove('table', table_id);
235
551
  }
236
552
 
237
- /** All live tables, in creation order (PineScript `table.all`). A new array. */
553
+ /**
554
+ * Returns the tables that are still on the pane, oldest first. The array is a copy.
555
+ * (In a script, `table.all` is read as a value, without a call.)
556
+ * @returns The live tables in creation order.
557
+ * @example
558
+ * ```js
559
+ * eachBar((c) => {
560
+ * if (c.i === c.n - 1) for (const t of table.all) table.set_bgcolor(t, color.new(color.black, 50));
561
+ * });
562
+ * ```
563
+ */
238
564
  export function all(): Table[] {
239
565
  return allDrawings('table');
240
566
  }
package/src/text/index.ts CHANGED
@@ -1,30 +1,28 @@
1
1
  /**
2
- * Text namespace
3
- * PineScript `text.format_*` constants for `label.set_text_formatting` and `box.set_text_formatting`, and the
4
- * `text.align_*` / `text.wrap_*` constants.
2
+ * Text namespace: the `text.format_*` constants of `label.set_text_formatting()` and `box.set_text_formatting()`
3
+ * (`text_formatting` option), and the `text.align_*` / `text.wrap_*` constants of labels, boxes and table cells.
5
4
  *
6
- * The format values are flags that add up, as in PineScript (`text.format_bold + text.format_italic`).
7
- * They are the values PineScript uses for drawn labels and boxes.
5
+ * The format values are flags that add up: `text.format_bold + text.format_italic` is bold italic text.
8
6
  */
9
7
 
10
- /** No formatting. */
8
+ /** Plain text: neither bold nor italic (the default). */
11
9
  export const format_none = 0;
12
- /** Bold text. */
10
+ /** Bold text; add `text.format_italic` for bold italic. */
13
11
  export const format_bold = 1;
14
- /** Italic text. */
12
+ /** Italic text; add `text.format_bold` for bold italic. */
15
13
  export const format_italic = 2;
16
14
 
17
- /** Text aligned to the left (`table.cell`, `box.new`, `label.new` textalign). */
15
+ /** Text aligned to the left (`text_halign` of `table.cell()` and `box.new()`, `textalign` of `label.new()`). */
18
16
  export const align_left = 'left' as const;
19
- /** Text centred horizontally or vertically. */
17
+ /** Text centred, horizontally or vertically. */
20
18
  export const align_center = 'center' as const;
21
19
  /** Text aligned to the right. */
22
20
  export const align_right = 'right' as const;
23
- /** Text aligned to the top (`table.cell`, `box.new` text_valign). */
21
+ /** Text aligned to the top (`text_valign` of `table.cell()` and `box.new()`). */
24
22
  export const align_top = 'top' as const;
25
- /** Text aligned to the bottom. */
23
+ /** Text aligned to the bottom (`text_valign`). */
26
24
  export const align_bottom = 'bottom' as const;
27
- /** Text wrapped to the box width (`box.new` text_wrap). */
25
+ /** Text wrapped onto several lines to fit the width of the box (`text_wrap` of `box.new()`). */
28
26
  export const wrap_auto = 'auto' as const;
29
- /** Text not wrapped. */
27
+ /** Text not wrapped: each line of the text stays on one line. */
30
28
  export const wrap_none = 'none' as const;
@@ -355,31 +355,38 @@ export interface OHLC {
355
355
  export const na = null;
356
356
  export type na = null;
357
357
 
358
- /**
359
- * X-axis location constants
360
- * Used to specify whether x-coordinates are bar indices, timestamps or places on the pane
361
- */
358
+ /** What the x coordinates of a drawing (label, line, box, polyline) are (`xloc` option). */
362
359
  export const xloc = {
363
- /** X-coordinates are bar indices */
360
+ /** Bar indexes: 0 is the first bar, `bar_index` the current one (the default). */
364
361
  bar_index: 'bar_index' as const,
365
- /** X-coordinates are UNIX timestamps in milliseconds */
362
+ /** UNIX times in milliseconds, such as `time` or `time_close`. */
366
363
  bar_time: 'bar_time' as const,
367
- /** X-coordinates are percents of the pane's width: 0 left edge, 100 right edge (not a PineScript value) */
364
+ /** Percents of the pane's width (0 left edge, 100 right edge): the drawing stays in place while the user scrolls and zooms. */
368
365
  pane: 'pane' as const,
369
366
  };
370
367
 
371
368
  /**
372
- * Table position constants (PineScript `position.*`), for `table.new` and `table.set_position`
369
+ * Where a table sits in the pane: the first argument of `table.new()` and of `table.set_position()`
370
+ * (`table.new(position.top_right, 2, 3)`).
373
371
  */
374
372
  export const position = {
373
+ /** The top-left corner of the pane. */
375
374
  top_left: 'top_left' as const,
375
+ /** The middle of the top edge. */
376
376
  top_center: 'top_center' as const,
377
+ /** The top-right corner of the pane. */
377
378
  top_right: 'top_right' as const,
379
+ /** The middle of the left edge. */
378
380
  middle_left: 'middle_left' as const,
381
+ /** The centre of the pane. */
379
382
  middle_center: 'middle_center' as const,
383
+ /** The middle of the right edge. */
380
384
  middle_right: 'middle_right' as const,
385
+ /** The bottom-left corner of the pane. */
381
386
  bottom_left: 'bottom_left' as const,
387
+ /** The middle of the bottom edge. */
382
388
  bottom_center: 'bottom_center' as const,
389
+ /** The bottom-right corner of the pane. */
383
390
  bottom_right: 'bottom_right' as const,
384
391
  };
385
392