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
@@ -1,114 +1,163 @@
1
1
  /**
2
- * @fileoverview PineScript constant namespaces of the script API: `shape.*`, `location.*`, `size.*`, `xloc.*`,
3
- * `yloc.*`, `extend.*`, `display.*`, `format.*`, `font.*`, `scale.*`, `order.*`, `currency.*`, `adjustment.*`, and the
4
- * `label.style_*`, `line.style_*`, `dayofweek.*` members. Each constant is the string (or number) the functions take,
5
- * so `plotshape(cond, "Buy", { style: shape.triangleup })` and `{ style: "triangleup" }` are the same.
2
+ * @fileoverview Constant groups of the script API: `shape.*`, `location.*`, `size.*`, `xloc.*`, `yloc.*`, `extend.*`,
3
+ * `display.*`, `format.*`, `font.*`, `scale.*`, `order.*`, `currency.*`, `adjustment.*`, and the `label.style_*`,
4
+ * `line.style_*`, `dayofweek.*` members. Each constant is the string (or number) the functions take, so
5
+ * `plotshape(cond, "Buy", { style: shape.triangleup })` and `{ style: "triangleup" }` are the same.
6
6
  *
7
7
  * @module script/constants
8
8
  */
9
9
 
10
- /** PineScript `shape.*`: the shapes of plotshape(). */
10
+ /** Shapes of `plotshape()` (`style` option): `plotshape(cond, "Buy", { style: shape.triangleup })`. */
11
11
  export const shape = {
12
+ /** A diagonal cross (x). */
12
13
  xcross: 'xcross',
14
+ /** An upright cross (+). */
13
15
  cross: 'cross',
16
+ /** A triangle pointing up. */
14
17
  triangleup: 'triangleup',
18
+ /** A triangle pointing down. */
15
19
  triangledown: 'triangledown',
20
+ /** A small flag. */
16
21
  flag: 'flag',
22
+ /** A filled circle. */
17
23
  circle: 'circle',
24
+ /** An arrow pointing up. */
18
25
  arrowup: 'arrowup',
26
+ /** An arrow pointing down. */
19
27
  arrowdown: 'arrowdown',
28
+ /** A label box pointing up (its tip at the value): put it below the bar. */
20
29
  labelup: 'labelup',
30
+ /** A label box pointing down (its tip at the value): put it above the bar. */
21
31
  labeldown: 'labeldown',
32
+ /** A filled square. */
22
33
  square: 'square',
34
+ /** A filled diamond. */
23
35
  diamond: 'diamond',
24
36
  } as const;
25
37
 
26
- /** PineScript `location.*`: where plotshape() / plotchar() draw. */
38
+ /** Where `plotshape()` and `plotchar()` draw their marker (`location` option). */
27
39
  export const location = {
40
+ /** Above the high of the bar (the default of `plotshape()` and `plotchar()`). */
28
41
  abovebar: 'abovebar',
42
+ /** Below the low of the bar. */
29
43
  belowbar: 'belowbar',
44
+ /** At the top edge of the pane. */
30
45
  top: 'top',
46
+ /** At the bottom edge of the pane. */
31
47
  bottom: 'bottom',
48
+ /** At the value of the series itself, read as a price (bars where it is na or 0 get no marker). */
32
49
  absolute: 'absolute',
33
50
  } as const;
34
51
 
35
- /** PineScript `size.*`: sizes of shapes, characters, labels and table texts. */
52
+ /** Sizes of shapes, characters, labels and table texts (`size` / `text_size` options). */
36
53
  export const size = {
54
+ /** Size chosen by the chart to fit the space. */
37
55
  auto: 'auto',
56
+ /** The smallest size. */
38
57
  tiny: 'tiny',
58
+ /** Small size. */
39
59
  small: 'small',
60
+ /** Normal size. */
40
61
  normal: 'normal',
62
+ /** Large size. */
41
63
  large: 'large',
64
+ /** The largest size. */
42
65
  huge: 'huge',
43
66
  } as const;
44
67
 
45
- /**
46
- * PineScript `xloc.*`: the x coordinate of drawings (bar index or UNIX time in ms). `xloc.pane` is not a Pine value: the x
47
- * coordinates are percents of the pane's width (0 left edge, 100 right edge), so the drawing stays in place while the
48
- * user scrolls and zooms.
49
- */
68
+ /** What the x coordinates of a drawing (label, line, box, polyline) are (`xloc` option). */
50
69
  export const xloc = {
70
+ /** Bar indexes: 0 is the first bar, `bar_index` the current one (the default). */
51
71
  bar_index: 'bar_index',
72
+ /** UNIX times in milliseconds, such as `time` or `time_close`. */
52
73
  bar_time: 'bar_time',
74
+ /** Percents of the pane's width (0 left edge, 100 right edge): the drawing stays in place while the user scrolls and zooms. */
53
75
  pane: 'pane',
54
76
  } as const;
55
77
 
56
- /** PineScript `yloc.*`: the y position of labels. */
78
+ /** Vertical placement of a label (`label.new(..., { yloc })`). */
57
79
  export const yloc = {
80
+ /** At the y value given to the label (the default). */
58
81
  price: 'price',
82
+ /** Above the high of the bar at the label's x; the y value is not used. */
59
83
  abovebar: 'abovebar',
84
+ /** Below the low of the bar at the label's x; the y value is not used. */
60
85
  belowbar: 'belowbar',
61
86
  } as const;
62
87
 
63
- /** PineScript `extend.*`: line extension. */
88
+ /** How far a line (or a box's top and bottom) is drawn past its two points (`extend` option). */
64
89
  export const extend = {
90
+ /** Not extended: drawn between its two points only (the default). */
65
91
  none: 'none',
92
+ /** Extended to the left edge of the chart, past the first point. */
66
93
  left: 'left',
94
+ /** Extended to the right edge of the chart, past the second point. */
67
95
  right: 'right',
96
+ /** Extended in both directions. */
68
97
  both: 'both',
69
98
  } as const;
70
99
 
71
- /** PineScript `display.*`: where a plot or input is shown. */
100
+ /** Where a plot or an input value is shown (`display` option). */
72
101
  export const display = {
102
+ /** Not shown anywhere; the values still exist for alerts and other scripts. */
73
103
  none: 'none',
104
+ /** Shown everywhere: on the pane, in the status line and in the data window. */
74
105
  all: 'all',
106
+ /** Shown only in the data window. */
75
107
  data_window: 'data_window',
108
+ /** Shown only in the status line of the script. */
76
109
  status_line: 'status_line',
110
+ /** Drawn only on the pane, not in the status line or the data window. */
77
111
  pane: 'pane',
78
112
  } as const;
79
113
 
80
- /** PineScript `format.*`: the number format of indicator() and str.tostring(). */
114
+ /** Number formats: the `format` option of `indicator()` and the format of `str.tostring()`. */
81
115
  export const format = {
116
+ /** The same format as the chart symbol. */
82
117
  inherit: 'inherit',
118
+ /** A price, with the precision of the script or of the symbol. */
83
119
  price: 'price',
120
+ /** A volume, shortened with K, M, B, T (12345 shows as "12.345K"). */
84
121
  volume: 'volume',
122
+ /** A percentage: two decimals and a "%" sign; the value is not multiplied by 100. */
85
123
  percent: 'percent',
124
+ /** Rounded to the tick size of the symbol (`syminfo.mintick`). */
86
125
  mintick: 'mintick',
87
126
  } as const;
88
127
 
89
- /** PineScript `font.*`: font families of labels, boxes and table cells. */
128
+ /** Font families of labels, boxes and table cells (`text_font_family` option). */
90
129
  export const font = {
130
+ /** The default font of the chart. */
91
131
  family_default: 'default',
132
+ /** A monospace font: every character has the same width. */
92
133
  family_monospace: 'monospace',
93
134
  } as const;
94
135
 
95
- /** PineScript `scale.*`: the price scale of indicator() (`scale` option). */
136
+ /** Price scale a script is attached to (`indicator(..., { scale })`). */
96
137
  export const scale = {
138
+ /** The right price scale. */
97
139
  right: 'right',
140
+ /** The left price scale. */
98
141
  left: 'left',
142
+ /** No price scale of its own (for overlay scripts). */
99
143
  none: 'none',
100
144
  } as const;
101
145
 
102
- /** PineScript `order.*`: sort order of array.sort() / array.sort_indices() / matrix.sort(). */
146
+ /** Sort order of `array.sort()`, `array.sort_indices()` and `matrix.sort()`. */
103
147
  export const order = {
148
+ /** Smallest value first (the default). */
104
149
  ascending: 'ascending',
150
+ /** Largest value first. */
105
151
  descending: 'descending',
106
152
  } as const;
107
153
 
108
- /** PineScript `adjustment.*`: price adjustment of a ticker. */
154
+ /** Price adjustment of a ticker. Kept for compatibility: no function of the library reads it yet. */
109
155
  export const adjustment = {
156
+ /** Prices not adjusted. */
110
157
  none: 'none',
158
+ /** Prices adjusted for splits. */
111
159
  splits: 'splits',
160
+ /** Prices adjusted for dividends. */
112
161
  dividends: 'dividends',
113
162
  } as const;
114
163
 
@@ -121,54 +170,209 @@ const CURRENCIES = [
121
170
 
122
171
  type CurrencyCode = (typeof CURRENCIES)[number];
123
172
 
124
- /** PineScript `currency.*`: currency codes (`strategy(..., { currency })`, `syminfo.currency`); `currency.NONE` is na. */
125
- export const currency = {
173
+ /** The members of `currency`: one constant per currency code, its value the code itself. */
174
+ export interface CurrencyConstants {
175
+ /** United Arab Emirates dirham. */
176
+ readonly AED: 'AED';
177
+ /** Argentine peso. */
178
+ readonly ARS: 'ARS';
179
+ /** Australian dollar. */
180
+ readonly AUD: 'AUD';
181
+ /** Bangladeshi taka. */
182
+ readonly BDT: 'BDT';
183
+ /** Bahraini dinar. */
184
+ readonly BHD: 'BHD';
185
+ /** Brazilian real. */
186
+ readonly BRL: 'BRL';
187
+ /** Bitcoin. */
188
+ readonly BTC: 'BTC';
189
+ /** Canadian dollar. */
190
+ readonly CAD: 'CAD';
191
+ /** Swiss franc. */
192
+ readonly CHF: 'CHF';
193
+ /** Chilean peso. */
194
+ readonly CLP: 'CLP';
195
+ /** Chinese yuan. */
196
+ readonly CNY: 'CNY';
197
+ /** Colombian peso. */
198
+ readonly COP: 'COP';
199
+ /** Czech koruna. */
200
+ readonly CZK: 'CZK';
201
+ /** Danish krone. */
202
+ readonly DKK: 'DKK';
203
+ /** Egyptian pound. */
204
+ readonly EGP: 'EGP';
205
+ /** Ether. */
206
+ readonly ETH: 'ETH';
207
+ /** Euro. */
208
+ readonly EUR: 'EUR';
209
+ /** Pound sterling. */
210
+ readonly GBP: 'GBP';
211
+ /** Georgian lari. */
212
+ readonly GEL: 'GEL';
213
+ /** Hong Kong dollar. */
214
+ readonly HKD: 'HKD';
215
+ /** Hungarian forint. */
216
+ readonly HUF: 'HUF';
217
+ /** Indonesian rupiah. */
218
+ readonly IDR: 'IDR';
219
+ /** Israeli new shekel. */
220
+ readonly ILS: 'ILS';
221
+ /** Indian rupee. */
222
+ readonly INR: 'INR';
223
+ /** Icelandic krona. */
224
+ readonly ISK: 'ISK';
225
+ /** Japanese yen. */
226
+ readonly JPY: 'JPY';
227
+ /** Kenyan shilling. */
228
+ readonly KES: 'KES';
229
+ /** South Korean won. */
230
+ readonly KRW: 'KRW';
231
+ /** Kuwaiti dinar. */
232
+ readonly KWD: 'KWD';
233
+ /** Sri Lankan rupee. */
234
+ readonly LKR: 'LKR';
235
+ /** Moroccan dirham. */
236
+ readonly MAD: 'MAD';
237
+ /** Mexican peso. */
238
+ readonly MXN: 'MXN';
239
+ /** Malaysian ringgit. */
240
+ readonly MYR: 'MYR';
241
+ /** Nigerian naira. */
242
+ readonly NGN: 'NGN';
243
+ /** Norwegian krone. */
244
+ readonly NOK: 'NOK';
245
+ /** New Zealand dollar. */
246
+ readonly NZD: 'NZD';
247
+ /** Peruvian sol. */
248
+ readonly PEN: 'PEN';
249
+ /** Philippine peso. */
250
+ readonly PHP: 'PHP';
251
+ /** Pakistani rupee. */
252
+ readonly PKR: 'PKR';
253
+ /** Polish zloty. */
254
+ readonly PLN: 'PLN';
255
+ /** Qatari riyal. */
256
+ readonly QAR: 'QAR';
257
+ /** Romanian leu. */
258
+ readonly RON: 'RON';
259
+ /** Serbian dinar. */
260
+ readonly RSD: 'RSD';
261
+ /** Russian ruble. */
262
+ readonly RUB: 'RUB';
263
+ /** Saudi riyal. */
264
+ readonly SAR: 'SAR';
265
+ /** Swedish krona. */
266
+ readonly SEK: 'SEK';
267
+ /** Singapore dollar. */
268
+ readonly SGD: 'SGD';
269
+ /** Thai baht. */
270
+ readonly THB: 'THB';
271
+ /** Tunisian dinar. */
272
+ readonly TND: 'TND';
273
+ /** Turkish lira. */
274
+ readonly TRY: 'TRY';
275
+ /** New Taiwan dollar. */
276
+ readonly TWD: 'TWD';
277
+ /** United States dollar. */
278
+ readonly USD: 'USD';
279
+ /** Tether (USDT). */
280
+ readonly USDT: 'USDT';
281
+ /** Venezuelan bolivar. */
282
+ readonly VES: 'VES';
283
+ /** Vietnamese dong. */
284
+ readonly VND: 'VND';
285
+ /** South African rand. */
286
+ readonly ZAR: 'ZAR';
287
+ /** No currency (na). */
288
+ readonly NONE: 'NONE';
289
+ }
290
+
291
+ /**
292
+ * Currency codes, such as the `currency` option of `strategy()` or the value of `syminfo.currency`
293
+ * (`currency.USD` is "USD"). `currency.NONE` means no currency (na).
294
+ */
295
+ export const currency: CurrencyConstants = {
126
296
  ...(Object.fromEntries(CURRENCIES.map((c) => [c, c])) as { readonly [K in CurrencyCode]: K }),
127
297
  NONE: 'NONE',
128
298
  } as const;
129
299
 
130
- /** PineScript `label.style_*`. */
300
+ /** Label styles, available as `label.style_*` (`label.new(..., { style })`). */
131
301
  export const labelStyles = {
302
+ /** No shape: the text only. */
132
303
  style_none: 'none',
304
+ /** A diagonal cross (x) on the point. */
133
305
  style_xcross: 'xcross',
306
+ /** An upright cross (+) on the point. */
134
307
  style_cross: 'cross',
308
+ /** A triangle pointing up. */
135
309
  style_triangleup: 'triangleup',
310
+ /** A triangle pointing down. */
136
311
  style_triangledown: 'triangledown',
312
+ /** A small flag. */
137
313
  style_flag: 'flag',
314
+ /** A filled circle. */
138
315
  style_circle: 'circle',
316
+ /** An arrow pointing up. */
139
317
  style_arrowup: 'arrowup',
318
+ /** An arrow pointing down. */
140
319
  style_arrowdown: 'arrowdown',
320
+ /** A text box pointing up: its tip on the point, the box below it. */
141
321
  style_label_up: 'label_up',
322
+ /** A text box pointing down: its tip on the point, the box above it. */
142
323
  style_label_down: 'label_down',
324
+ /** A text box pointing left: its tip on the point, the box to the right. */
143
325
  style_label_left: 'label_left',
326
+ /** A text box pointing right: its tip on the point, the box to the left. */
144
327
  style_label_right: 'label_right',
328
+ /** A text box whose lower-left corner is on the point (the box above and to the right). */
145
329
  style_label_lower_left: 'label_lower_left',
330
+ /** A text box whose lower-right corner is on the point (the box above and to the left). */
146
331
  style_label_lower_right: 'label_lower_right',
332
+ /** A text box whose upper-left corner is on the point (the box below and to the right). */
147
333
  style_label_upper_left: 'label_upper_left',
334
+ /** A text box whose upper-right corner is on the point (the box below and to the left). */
148
335
  style_label_upper_right: 'label_upper_right',
336
+ /** A text box centred on the point, without a tip. */
149
337
  style_label_center: 'label_center',
338
+ /** A filled square. */
150
339
  style_square: 'square',
340
+ /** A filled diamond. */
151
341
  style_diamond: 'diamond',
342
+ /** The text only, with an outline in the label colour. */
152
343
  style_text_outline: 'text_outline',
153
344
  } as const;
154
345
 
155
- /** PineScript `line.style_*` (lines, boxes, polylines). */
346
+ /** Line styles, available as `line.style_*` (lines, boxes and polylines). */
156
347
  export const lineStyles = {
348
+ /** A solid line (the default). */
157
349
  style_solid: 'solid',
350
+ /** A dotted line. */
158
351
  style_dotted: 'dotted',
352
+ /** A dashed line. */
159
353
  style_dashed: 'dashed',
354
+ /** A solid line with an arrowhead at its first point. */
160
355
  style_arrow_left: 'arrow_left',
356
+ /** A solid line with an arrowhead at its second point. */
161
357
  style_arrow_right: 'arrow_right',
358
+ /** A solid line with an arrowhead at both ends. */
162
359
  style_arrow_both: 'arrow_both',
163
360
  } as const;
164
361
 
165
- /** PineScript `dayofweek.*`: the values of `dayofweek` (1 = Sunday ... 7 = Saturday). */
362
+ /** Days of the week as `dayofweek()` gives them, available as `dayofweek.*` (1 = Sunday ... 7 = Saturday). */
166
363
  export const daysOfWeek = {
364
+ /** Sunday (1). */
167
365
  sunday: 1,
366
+ /** Monday (2). */
168
367
  monday: 2,
368
+ /** Tuesday (3). */
169
369
  tuesday: 3,
370
+ /** Wednesday (4). */
170
371
  wednesday: 4,
372
+ /** Thursday (5). */
171
373
  thursday: 5,
374
+ /** Friday (6). */
172
375
  friday: 6,
376
+ /** Saturday (7). */
173
377
  saturday: 7,
174
378
  } as const;
@@ -0,0 +1,221 @@
1
+ /**
2
+ * `defineIndicator()`: a script body made into a ready-to-use indicator. The body is written once (indicator(),
3
+ * input.*(), plot()...); its settings (inputs and their defaults, plots, levels, alerts, title, pane) are read from it
4
+ * and its calculation runs on any bars. Not a PineScript function: a shortcut over executeScript() / createScriptRun().
5
+ */
6
+ import type { Bar } from '../types/index.js';
7
+ import type { IndicatorResult } from '../types/metadata.js';
8
+ import type {
9
+ ArrowConfig,
10
+ BarColorConfig,
11
+ CandleConfig,
12
+ FillConfig,
13
+ HLineConfig,
14
+ InputConfig,
15
+ PlotConfig,
16
+ ShapeConfig,
17
+ } from '../runtime/types.js';
18
+ import {
19
+ createScriptRun,
20
+ executeScript,
21
+ ScriptError,
22
+ type AlertConditionConfig,
23
+ type ChartContext,
24
+ type ScriptOutput,
25
+ type ScriptRunResult,
26
+ } from './index.js';
27
+
28
+ export interface DefineIndicatorOptions {
29
+ /**
30
+ * Strict mode (default true): a wrong input value or name, an unknown option of plot()... or a text that is not a
31
+ * colour throws a `ScriptError` that says what to change. false: the PineScript behaviour (mistakes pass silently).
32
+ */
33
+ strict?: boolean;
34
+ /**
35
+ * Called with each warning of a calculation (drawings deleted over their maximum count...). Default: `console.warn`.
36
+ * Give `() => {}` to keep them quiet; `run()` returns them too.
37
+ */
38
+ onWarning?: ((message: string) => void) | null;
39
+ /**
40
+ * Colours of the plots that give none (default true: each takes the next colour of `DEFAULT_PALETTE`; false: all blue,
41
+ * as in PineScript; or a list of colours).
42
+ */
43
+ palette?: boolean | readonly string[];
44
+ /** Plot ids: 'index' (default: plot0, plot1...) or 'title' (made from the titles: "Fast MA" gives fast_ma). */
45
+ plotIds?: 'index' | 'title';
46
+ }
47
+
48
+ /** What a calculation returns: the plots by plot id, markers, colours and drawings (`IndicatorResult`). */
49
+ export type IndicatorCalculation = ScriptRunResult['result'];
50
+
51
+ /** A live calculation: each update calculates the bars from the forming bar of the previous one when it can. */
52
+ export interface IndicatorLiveRun {
53
+ update(bars: Bar[], chart?: ChartContext): IndicatorCalculation & { from: number };
54
+ /** Why the updates calculate every bar (null: they calculate the newest bars only). */
55
+ readonly fullRunReason: string | null;
56
+ }
57
+
58
+ /** An indicator made by {@link defineIndicator}. */
59
+ export interface IndicatorDefinition {
60
+ /** A name made from the title ("My RSI" gives "my-rsi"), e.g. for a registry. */
61
+ readonly id: string;
62
+ /** Title, short title, pane (`overlay`: true on the price chart, false in its own pane), price format. */
63
+ readonly metadata: ScriptRunResult['metadata'] & { shortTitle: string };
64
+ /** The inputs, as the settings dialog lists them. */
65
+ readonly inputConfig: InputConfig[];
66
+ /** The default value of each input, by input id. */
67
+ readonly defaultInputs: Record<string, unknown>;
68
+ readonly plotConfig: PlotConfig[];
69
+ readonly hlineConfig: HLineConfig[];
70
+ readonly fillConfig: FillConfig[];
71
+ readonly shapeConfig: ShapeConfig[];
72
+ readonly arrowConfig: ArrowConfig[];
73
+ readonly barColorConfig: BarColorConfig[];
74
+ readonly candleConfig: CandleConfig[];
75
+ /** The alertcondition() declarations; a calculation gives the bars on which each one is true (`alerts`). */
76
+ readonly alertConditions: AlertConditionConfig[];
77
+ /** The outputs in declaration order. */
78
+ readonly outputs: ScriptOutput[];
79
+ /**
80
+ * The indicator on `bars` (oldest first, time in UNIX seconds). `inputs`: the values to change, by input id (the
81
+ * others keep their default).
82
+ */
83
+ calculate(bars: Bar[], inputs?: Record<string, unknown>, chart?: ChartContext): IndicatorCalculation;
84
+ /** Everything a run produced: the calculation (`result`), the declarations and the warnings. */
85
+ run(bars: Bar[], inputs?: Record<string, unknown>, chart?: ChartContext): ScriptRunResult;
86
+ /** A live calculation for a chart whose last bar changes (see `createScriptRun`). */
87
+ createRun(inputs?: Record<string, unknown>, chart?: ChartContext): IndicatorLiveRun;
88
+ }
89
+
90
+ /**
91
+ * The sample bars on which the declarations are read: 200 days of prices around 5000, far from the 0-100 range of
92
+ * oscillators, so that indicator(..., { pane: 'auto' }) tells a price line from an oscillator.
93
+ */
94
+ const PROBE_BARS: Bar[] = Array.from({ length: 200 }, (_, i) => {
95
+ const close = 5000 + 300 * Math.sin(i / 9) + 120 * Math.sin(i / 3.1) + i;
96
+ const open = 5000 + 300 * Math.sin((i - 1) / 9) + 120 * Math.sin((i - 1) / 3.1) + i - 1;
97
+ return { time: 86400 * (i + 1), open, high: Math.max(open, close) + 25, low: Math.min(open, close) - 25, close, volume: 1e6 + 2e5 * Math.sin(i / 5) };
98
+ });
99
+
100
+ const slug = (title: string): string =>
101
+ title.toLowerCase().normalize('NFKD').replace(/[̀-ͯ]/g, '').replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') || 'indicator';
102
+
103
+ /**
104
+ * Host-side: makes an indicator of a script body, for a program that runs scripts (a script does not call it):
105
+ *
106
+ * ```js
107
+ * const ewo = defineIndicator(() => {
108
+ * indicator('Elliott Wave Oscillator');
109
+ * const fast = input.int(5, 'Fast length', { minval: 1 });
110
+ * const slow = input.int(35, 'Slow length', { minval: 1 });
111
+ * plot(ta.ema(close, fast).sub(ta.ema(close, slow)), 'EWO', { style: plot.style_histogram });
112
+ * });
113
+ * ewo.inputConfig; // the two inputs, for a settings dialog
114
+ * ewo.calculate(bars, { fast_length: 8 }).plots.plot0;
115
+ * ```
116
+ *
117
+ * The declarations are read from a first run on two sample bars, the first time they are asked for. Strict mode is on
118
+ * by default (see {@link DefineIndicatorOptions}).
119
+ * @param body - The script: a function that runs its statements (`indicator()`, inputs, plots...).
120
+ * @param options - Strict mode and the other run options. Default: `{}`.
121
+ * @returns The indicator: its declarations (title, inputs, outputs) and `calculate` / `run` / `createRun`.
122
+ */
123
+ export function defineIndicator(body: () => void, options: DefineIndicatorOptions = {}): IndicatorDefinition {
124
+ if (typeof body !== 'function') {
125
+ throw new TypeError('defineIndicator(): give the script as a function, e.g. defineIndicator(() => { indicator("My indicator"); plot(close); }).');
126
+ }
127
+ const strict = options.strict ?? true;
128
+ const runOptions = { strict, palette: options.palette ?? true, plotIds: options.plotIds ?? 'index' } as const;
129
+ const warn = options.onWarning === undefined ? (m: string) => console.warn(m) : options.onWarning;
130
+ let probe: ScriptRunResult | null = null;
131
+ const declared = (): ScriptRunResult => {
132
+ if (probe) return probe;
133
+ let run: ScriptRunResult;
134
+ try {
135
+ run = executeScript(body, PROBE_BARS, {}, {}, runOptions);
136
+ } catch (err) {
137
+ if (err instanceof ScriptError) throw err;
138
+ const why = err instanceof Error ? err.message : String(err);
139
+ throw new Error(`defineIndicator(): the script could not run on sample bars, so its settings are unknown: ${why}`, { cause: err });
140
+ }
141
+ if (run.strategyConfig) throw new Error('defineIndicator(): this script declares strategy(); write indicator("...") instead.');
142
+ if (run.metadata.title === 'Untitled' && run.inputConfig.length + run.plotConfig.length > 0) {
143
+ throw new Error('defineIndicator(): start the script with indicator("Its title"), e.g. indicator("My RSI").');
144
+ }
145
+ probe = run;
146
+ return run;
147
+ };
148
+ const report = <R extends ScriptRunResult>(run: R): R => {
149
+ const d = declared();
150
+ // pane 'auto': the pane chosen on the sample bars, whatever the bars of the run
151
+ if (d.metadata.pane === 'auto') {
152
+ run.metadata.overlay = d.metadata.overlay;
153
+ if (run.result.metadata) run.result.metadata.overlay = d.metadata.overlay;
154
+ }
155
+ if (warn) for (const w of run.warnings) warn(`${d.metadata.title}: ${w}`);
156
+ return run;
157
+ };
158
+ const execute = (bars: Bar[], inputs: Record<string, unknown> = {}, chart: ChartContext = {}): ScriptRunResult => {
159
+ declared();
160
+ return report(executeScript(body, bars, inputs, chart, runOptions));
161
+ };
162
+ return {
163
+ get id() {
164
+ return slug(declared().metadata.title);
165
+ },
166
+ get metadata() {
167
+ const m = declared().metadata;
168
+ return { ...m, shortTitle: m.shortTitle ?? m.title };
169
+ },
170
+ get inputConfig() {
171
+ return declared().inputConfig;
172
+ },
173
+ get defaultInputs() {
174
+ return { ...declared().defaultInputs };
175
+ },
176
+ get plotConfig() {
177
+ return declared().plotConfig;
178
+ },
179
+ get hlineConfig() {
180
+ return declared().hlineConfig;
181
+ },
182
+ get fillConfig() {
183
+ return declared().fillConfig;
184
+ },
185
+ get shapeConfig() {
186
+ return declared().shapeConfig;
187
+ },
188
+ get arrowConfig() {
189
+ return declared().arrowConfig;
190
+ },
191
+ get barColorConfig() {
192
+ return declared().barColorConfig;
193
+ },
194
+ get candleConfig() {
195
+ return declared().candleConfig;
196
+ },
197
+ get alertConditions() {
198
+ return declared().alertConfig;
199
+ },
200
+ get outputs() {
201
+ return declared().outputs;
202
+ },
203
+ calculate: (bars, inputs, chart) => execute(bars, inputs, chart).result,
204
+ run: execute,
205
+ createRun(inputs = {}, chart = {}) {
206
+ declared();
207
+ const live = createScriptRun(body, inputs, chart, runOptions);
208
+ return {
209
+ update(bars: Bar[], next?: ChartContext) {
210
+ const u = report(live.update(bars, next));
211
+ return Object.assign(u.result, { from: u.from });
212
+ },
213
+ get fullRunReason() {
214
+ return live.fullRunReason;
215
+ },
216
+ };
217
+ },
218
+ };
219
+ }
220
+
221
+ export type { IndicatorResult };
@@ -77,7 +77,8 @@ function closest(name: string, names: readonly string[]): string[] {
77
77
  };
78
78
  return names
79
79
  .map((k) => ({ k, d: dist(n, k) }))
80
- .filter(({ k, d }) => d <= Math.max(2, Math.floor(name.length / 3)) || k.includes(n) || n.includes(k))
80
+ // a name that shares nothing with the key (as many edits as its length: "id" for "min") is not close
81
+ .filter(({ k, d }) => (d <= Math.max(2, Math.floor(name.length / 3)) && d < k.length) || k.includes(n) || n.includes(k))
81
82
  .sort((a, b) => a.d - b.d)
82
83
  .slice(0, 3)
83
84
  .map(({ k }) => k);