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
@@ -0,0 +1,207 @@
1
+ # Indicators without code
2
+
3
+ An indicator can be described in a JSON file instead of a script: its settings, its calculations written as formulas
4
+ (as in a spreadsheet), what it draws and when it signals. No JavaScript is needed. See it on a chart while you write
5
+ it with [`oakscript preview`](#see-it-on-a-chart).
6
+
7
+ ```json
8
+ {
9
+ "title": "Average cross",
10
+ "inputs": {
11
+ "fast": { "default": 9, "title": "Fast length", "min": 1 },
12
+ "slow": { "default": 21, "title": "Slow length", "min": 2 }
13
+ },
14
+ "values": {
15
+ "fastLine": "ema(close, fast)",
16
+ "slowLine": "ema(close, slow)"
17
+ },
18
+ "plots": [
19
+ { "title": "Fast", "value": "fastLine", "color": "teal", "width": 2 },
20
+ { "title": "Slow", "value": "slowLine", "color": "orange", "width": 2 }
21
+ ],
22
+ "fills": [{ "between": ["Fast", "Slow"], "color": "blue", "opacity": 0.1 }],
23
+ "signals": [
24
+ { "title": "Buy", "when": "crossover(fastLine, slowLine)", "position": "below", "color": "green", "alert": "Fast crossed above slow" },
25
+ { "title": "Sell", "when": "crossunder(fastLine, slowLine)", "position": "above", "color": "red", "alert": true }
26
+ ]
27
+ }
28
+ ```
29
+
30
+ In JavaScript, `defineIndicatorFromJson` makes it a ready-to-use indicator, like `defineIndicator` (see
31
+ [Your first indicator](first-indicator.md)):
32
+
33
+ ```javascript
34
+ import { defineIndicatorFromJson } from 'oakscriptjs/script';
35
+ import { readFileSync } from 'node:fs';
36
+
37
+ const cross = defineIndicatorFromJson(readFileSync('average-cross.json', 'utf8'));
38
+ cross.calculate(bars, { fast: 5 }); // the settings are named as in "inputs"
39
+ ```
40
+
41
+ ## The fields
42
+
43
+ | Field | What it is |
44
+ |---|---|
45
+ | `title` | The name of the indicator (required) |
46
+ | `shortTitle` | A shorter name for the legend |
47
+ | `pane` | `"price"` (on the price chart), `"separate"` (its own pane) or `"auto"` (default: chosen from the values) |
48
+ | `inputs` | The settings, by name: `"length": 14`, or `"length": { "default": 14, "title": "Length", "min": 1, "max": 200 }` |
49
+ | `values` | Named calculations, in order: `"fastLine": "ema(close, fast)"`; each one can use the settings and the values before it |
50
+ | `plots` | The lines: `{ "title", "value", "color", "width", "style", "colorWhen" }` |
51
+ | `levels` | Fixed horizontal lines: `{ "title", "value", "color", "style" }` |
52
+ | `fills` | A colour between two plots or two levels: `{ "between": ["Upper", "Lower"], "color", "opacity" }` |
53
+ | `signals` | Shapes where a condition is true: `{ "title", "when", "shape", "position", "color", "text", "size", "alert" }` |
54
+ | `background` | The background colour where a condition is true: `{ "when", "color", "opacity", "title" }` |
55
+ | `barColors` | The colour of the candles where a condition is true: `{ "when", "color", "opacity", "title" }` |
56
+
57
+ Details:
58
+
59
+ - **Settings.** The type comes from the default: a whole number, a decimal number, `true`/`false`, a text. Add
60
+ `"options": ["EMA", "SMA"]` for a list to choose from, `"step"`, `"tooltip"`, `"group"`. `"type": "source"` makes a
61
+ price to choose (`"default": "close"`; also open, high, low, hl2, hlc3, ohlc4), `"type": "color"` a colour.
62
+ A text setting is compared in a formula: `kind == "EMA" ? ema(close, fast) : sma(close, fast)`.
63
+ - **Colours.** A name (aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red,
64
+ silver, teal, white, yellow), `"#ff0000"` or `"rgba(255, 0, 0, 0.5)"`. `opacity` goes from 0 (invisible) to 1
65
+ (solid). A plot coloured by a condition: `"colorWhen": { "when": "slowLine > slowLine[1]", "color": "green", "else": "red" }`.
66
+ - **Plot styles.** `line` (default), `histogram`, `columns`, `area`, `circles`, `cross`, `stepline`; `width` 1 to 4.
67
+ Plots without a colour take different colours.
68
+ - **Signals.** `position`: `above` or `below` the bar, `top` or `bottom` of the pane. `shape`: triangleup,
69
+ triangledown, arrowup, arrowdown, circle, square, diamond, cross, xcross, flag, labelup, labeldown (default: a
70
+ triangle pointing away from the bar). `alert`: `true` or a message; an app lists it in its alert dialog.
71
+ - **A plot's title is a name too**: a signal can use `"when": "close > fast"` for a plot titled "Fast" (lowercase,
72
+ spaces become `_`).
73
+
74
+ ## Formulas
75
+
76
+ A formula is a calculation written as text. It works on whole lines of values (one per bar), so `close - open` is the
77
+ body of every candle.
78
+
79
+ | Write | Means |
80
+ |---|---|
81
+ | `close`, `open`, `high`, `low`, `volume`, `hl2`, `hlc3`, `ohlc4`, `hlcc4`, `bar_index` | the prices of each bar, the bar number |
82
+ | `+ - * / % ^` | arithmetic (`^`: power) |
83
+ | `> < >= <= == !=` (`=` and `<>` also work) | comparisons: 1 where true, 0 where false |
84
+ | `and`, `or`, `not` (`&&`, `\|\|`, `!` also work) | combine conditions |
85
+ | `condition ? a : b` | `a` where the condition is true, else `b` |
86
+ | `close[1]` | the value one bar before (`close[5]`: five bars before) |
87
+ | `length`, `fastLine`... | a setting, a value of `values`, or a plot's title |
88
+
89
+ The functions (any case: `EMA(close, 9)` works too):
90
+
91
+ | Function | Gives |
92
+ |---|---|
93
+ | `sma(source, length)` | simple moving average |
94
+ | `average(source, length)` | simple moving average (same as sma) |
95
+ | `ema(source, length)` | exponential moving average |
96
+ | `wma(source, length)` | weighted moving average |
97
+ | `rma(source, length)` | Wilder's moving average (the RSI's smoothing) |
98
+ | `hma(source, length)` | Hull moving average |
99
+ | `vwma(source, length)` | volume-weighted moving average |
100
+ | `rsi(source, length)` | relative strength index (0 to 100) |
101
+ | `stdev(source, length)` | standard deviation |
102
+ | `cci(source, length)` | commodity channel index |
103
+ | `mfi(source, length)` | money flow index (uses the volume) |
104
+ | `linreg(source, length)` | linear regression value on the last bar |
105
+ | `sum(source, length)` | sum of the last `length` values |
106
+ | `highest(source, length)` | highest value of the last `length` bars |
107
+ | `lowest(source, length)` | lowest value of the last `length` bars |
108
+ | `atr(length)` | average true range |
109
+ | `tr()` | true range |
110
+ | `change(source, bars = 1)` | difference with the value `bars` bars before |
111
+ | `roc(source, bars)` | rate of change in percent |
112
+ | `previous(source, bars = 1)` | the value `bars` bars before (same as source[bars]) |
113
+ | `crossover(a, b)` | 1 on the bar where a crosses above b |
114
+ | `crossunder(a, b)` | 1 on the bar where a crosses below b |
115
+ | `crossesAbove(a, b)` | same as crossover |
116
+ | `crossesBelow(a, b)` | same as crossunder |
117
+ | `cross(a, b)` | 1 on the bar where a and b cross either way |
118
+ | `rising(source, bars)` | 1 when the value rose on each of the last `bars` bars |
119
+ | `falling(source, bars)` | 1 when the value fell on each of the last `bars` bars |
120
+ | `abs(x)` | value without its sign |
121
+ | `sqrt(x)` | square root |
122
+ | `log(x)` | natural logarithm |
123
+ | `exp(x)` | e to the power x |
124
+ | `round(x)` | nearest whole number |
125
+ | `floor(x)` | whole number below |
126
+ | `ceil(x)` | whole number above |
127
+ | `sign(x)` | 1, 0 or -1 |
128
+ | `pow(x, y)` | x to the power y (also x ^ y) |
129
+ | `max(a, b, ...)` | the largest value |
130
+ | `min(a, b, ...)` | the smallest value |
131
+ | `nz(x, replacement = 0)` | x, or the replacement where x has no value |
132
+ | `na(x)` | 1 where x has no value |
133
+ | `iff(condition, a, b)` | a where the condition is true, else b |
134
+
135
+ A length (the second argument of `sma`, `rsi`...) is a fixed whole number: a number or a setting, not a calculation.
136
+ A mistake shows where it is:
137
+
138
+ ```text
139
+ values.fastLine: formula "ema(close, fst)": there is no value "fst". Did you mean fast?
140
+ ema(close, fst)
141
+ ^
142
+ ```
143
+
144
+ Formulas also work in a script: `plot(formula("ema(close, 5) - ema(close, 35)"), "EWO")`, with the script's own
145
+ values as `formula("rsi(close, length) > 70", { length })`.
146
+
147
+ ## More examples
148
+
149
+ An RSI in its own pane with its levels and a zone:
150
+
151
+ ```json
152
+ {
153
+ "title": "My RSI",
154
+ "inputs": { "length": { "default": 14, "title": "Length", "min": 1 } },
155
+ "values": { "rsi": "rsi(close, length)" },
156
+ "plots": [{ "title": "RSI", "value": "rsi", "color": "purple" }],
157
+ "levels": [
158
+ { "title": "Overbought", "value": 70, "color": "red" },
159
+ { "title": "Oversold", "value": 30, "color": "green" }
160
+ ],
161
+ "fills": [{ "between": ["Overbought", "Oversold"], "color": "purple", "opacity": 0.08 }],
162
+ "background": [{ "when": "rsi > 70", "color": "red" }, { "when": "rsi < 30", "color": "green" }]
163
+ }
164
+ ```
165
+
166
+ Bands around an average, candles coloured by the trend, and breakouts:
167
+
168
+ ```json
169
+ {
170
+ "title": "Average bands",
171
+ "pane": "price",
172
+ "inputs": {
173
+ "length": { "default": 20, "title": "Length", "min": 1 },
174
+ "width": { "default": 2.0, "title": "Width", "min": 0.5, "step": 0.5 },
175
+ "kind": { "default": "SMA", "title": "Average", "options": ["SMA", "EMA"] }
176
+ },
177
+ "values": {
178
+ "middle": "kind == \"EMA\" ? ema(close, length) : sma(close, length)",
179
+ "upper": "middle + width * stdev(close, length)",
180
+ "lower": "middle - width * stdev(close, length)"
181
+ },
182
+ "plots": [
183
+ { "title": "Middle", "value": "middle", "colorWhen": { "when": "middle > middle[1]", "color": "green", "else": "red" } },
184
+ { "title": "Upper", "value": "upper", "color": "gray" },
185
+ { "title": "Lower", "value": "lower", "color": "gray" }
186
+ ],
187
+ "fills": [{ "between": ["Upper", "Lower"], "color": "blue", "opacity": 0.08 }],
188
+ "signals": [
189
+ { "title": "Above the band", "when": "crossover(close, upper)", "shape": "arrowup", "position": "below", "color": "green", "alert": true },
190
+ { "title": "Below the band", "when": "crossunder(close, lower)", "shape": "arrowdown", "position": "above", "color": "red", "alert": true }
191
+ ],
192
+ "barColors": [{ "when": "close > middle", "color": "teal" }, { "when": "close < middle", "color": "maroon" }]
193
+ }
194
+ ```
195
+
196
+ ## See it on a chart
197
+
198
+ ```bash
199
+ npx oakscript preview average-cross.json
200
+ ```
201
+
202
+ It prints an address to open in a browser: the indicator on a chart of sample prices (or yours: `--bars prices.csv`
203
+ with the columns time, open, high, low, close, volume), its settings in a form beside the chart, and a message above it
204
+ when something is wrong. Each time the file is saved, the chart is drawn again. It works with scripts too
205
+ (`oakscript preview my-indicator.js`, the format of the [recipes](recipes.md)). The chart comes from the
206
+ `lightweight-charts` package when it is installed in the project, else from the internet. Fills between lines are not
207
+ drawn in the preview; levels, plots, signals, backgrounds and bar colours are.
@@ -0,0 +1,202 @@
1
+ # Recipes
2
+
3
+ Eleven indicators to copy and adapt. Each recipe is a script: put it inside `defineIndicator(() => { ... })` (see
4
+ [Your first indicator](first-indicator.md)), or paste it as it is in an app that runs oakscriptjs scripts (the names
5
+ `indicator`, `input`, `plot`, `ta`, `close`... are given by the app). Every recipe here is run by the library's tests.
6
+
7
+ | # | Recipe | You learn |
8
+ |---|---|---|
9
+ | 1 | [Average that changes colour with the trend](#1-average-that-changes-colour-with-the-trend) | a colour per bar |
10
+ | 2 | [Two averages crossing, with arrows and alerts](#2-two-averages-crossing-with-arrows-and-alerts) | signals, alerts |
11
+ | 3 | [RSI with overbought and oversold levels](#3-rsi-with-overbought-and-oversold-levels) | own pane, levels, fill |
12
+ | 4 | [Bands around an average](#4-bands-around-an-average) | math on lines, fill between lines |
13
+ | 5 | [Breakout of the highest high](#5-breakout-of-the-highest-high) | previous bar's value |
14
+ | 6 | [Candles coloured by the trend](#6-candles-coloured-by-the-trend) | bar colours |
15
+ | 7 | [Background in overbought zones](#7-background-in-overbought-zones) | background colours |
16
+ | 8 | [Volume spikes](#8-volume-spikes) | markers with text |
17
+ | 9 | [Trailing stop](#9-trailing-stop) | a value carried from bar to bar |
18
+ | 10 | [Labels on swing highs and lows](#10-labels-on-swing-highs-and-lows) | drawings |
19
+ | 11 | [Bullish RSI divergence](#11-bullish-rsi-divergence) | comparing two swings |
20
+
21
+ ## 1. Average that changes colour with the trend
22
+
23
+ ```js
24
+ indicator("Trend average", { overlay: true });
25
+ const length = input.int(50, "Length", { minval: 1 });
26
+ const average = ta.ema(close, length);
27
+ const rising = average.gt(average.offset(1)); // higher than on the previous bar
28
+ plot(average, "Average", { color: color.when(rising, color.green, color.red), linewidth: 2 });
29
+ ```
30
+
31
+ ## 2. Two averages crossing, with arrows and alerts
32
+
33
+ ```js
34
+ indicator("Average cross", { overlay: true });
35
+ const fastLength = input.int(9, "Fast length", { minval: 1 });
36
+ const slowLength = input.int(21, "Slow length", { minval: 1 });
37
+ const fast = ta.ema(close, fastLength);
38
+ const slow = ta.ema(close, slowLength);
39
+ plot(fast, "Fast", { color: color.teal });
40
+ plot(slow, "Slow", { color: color.orange });
41
+
42
+ const crossUp = ta.crossover(fast, slow);
43
+ const crossDown = ta.crossunder(fast, slow);
44
+ plotshape(crossUp, "Buy", { style: shape.triangleup, location: location.belowbar, color: color.green, size: size.small });
45
+ plotshape(crossDown, "Sell", { style: shape.triangledown, location: location.abovebar, color: color.red, size: size.small });
46
+
47
+ // An app lists these in its alert dialog.
48
+ alertcondition(crossUp, "Buy", "The fast average crossed above the slow one");
49
+ alertcondition(crossDown, "Sell", "The fast average crossed below the slow one");
50
+ ```
51
+
52
+ ## 3. RSI with overbought and oversold levels
53
+
54
+ Without `overlay: true`, the indicator has its own pane.
55
+
56
+ ```js
57
+ indicator("My RSI");
58
+ const length = input.int(14, "Length", { minval: 1 });
59
+ const upper = input.int(70, "Overbought", { minval: 50, maxval: 100 });
60
+ const lower = input.int(30, "Oversold", { minval: 0, maxval: 50 });
61
+ plot(ta.rsi(close, length), "RSI", { color: color.purple });
62
+ const top = hline(upper, "Overbought", { color: color.red });
63
+ const bottom = hline(lower, "Oversold", { color: color.green });
64
+ fill(top, bottom, { color: color.new(color.purple, 90), title: "Zone" });
65
+ ```
66
+
67
+ ## 4. Bands around an average
68
+
69
+ ```js
70
+ indicator("Average bands", { overlay: true });
71
+ const length = input.int(20, "Length", { minval: 1 });
72
+ const width = input.float(2, "Width (standard deviations)", { minval: 0.1, step: 0.1 });
73
+ const middle = ta.sma(close, length);
74
+ const distance = ta.stdev(close, length).mul(width);
75
+ plot(middle, "Middle", { color: color.blue });
76
+ const up = plot(middle.add(distance), "Upper", { color: color.gray });
77
+ const down = plot(middle.sub(distance), "Lower", { color: color.gray });
78
+ fill(up, down, { color: color.new(color.blue, 92), title: "Band" });
79
+ ```
80
+
81
+ ## 5. Breakout of the highest high
82
+
83
+ `.offset(1)` is the value on the previous bar: a breakout is a close above the highest high of the bars *before*.
84
+
85
+ ```js
86
+ indicator("Breakout", { overlay: true });
87
+ const length = input.int(20, "Length", { minval: 1 });
88
+ const highest = ta.highest(high, length).offset(1);
89
+ const lowest = ta.lowest(low, length).offset(1);
90
+ plot(highest, "Highest high", { color: color.green });
91
+ plot(lowest, "Lowest low", { color: color.red });
92
+ plotshape(close.gt(highest), "Breakout up", { style: shape.arrowup, location: location.belowbar, color: color.green });
93
+ plotshape(close.lt(lowest), "Breakout down", { style: shape.arrowdown, location: location.abovebar, color: color.red });
94
+ ```
95
+
96
+ ## 6. Candles coloured by the trend
97
+
98
+ ```js
99
+ indicator("Trend candles", { overlay: true });
100
+ const length = input.int(50, "Length", { minval: 1 });
101
+ const average = ta.sma(close, length);
102
+ plot(average, "Average", { color: color.gray });
103
+ barcolor(color.when(close.gt(average), color.green, color.red));
104
+ ```
105
+
106
+ ## 7. Background in overbought zones
107
+
108
+ `color.when(condition, colour)` gives no colour where the condition is false.
109
+
110
+ ```js
111
+ indicator("RSI zones");
112
+ const rsi = ta.rsi(close, input.int(14, "Length", { minval: 1 }));
113
+ plot(rsi, "RSI", { color: color.purple });
114
+ bgcolor(color.when(rsi.gt(70), color.new(color.red, 85)), { title: "Overbought" });
115
+ bgcolor(color.when(rsi.lt(30), color.new(color.green, 85)), { title: "Oversold" });
116
+ ```
117
+
118
+ ## 8. Volume spikes
119
+
120
+ ```js
121
+ indicator("Volume spikes", { overlay: true });
122
+ const length = input.int(20, "Average length", { minval: 1 });
123
+ const factor = input.float(2, "Spike factor", { minval: 1, step: 0.5 });
124
+ const spike = volume.gt(ta.sma(volume, length).mul(factor));
125
+ plot(ta.sma(close, length), "Average", { color: color.gray });
126
+ plotshape(spike, "Spike", { style: shape.labelup, location: location.belowbar, text: "Vol", color: color.blue, textcolor: color.white });
127
+ ```
128
+
129
+ ## 9. Trailing stop
130
+
131
+ A stop that only moves in the trade's direction depends on its own previous value: compute it bar by bar with
132
+ `eachBar`. Inside, values are plain numbers; what the callback returns becomes a line of values.
133
+
134
+ ```js
135
+ indicator("ATR trailing stop", { overlay: true });
136
+ const length = input.int(14, "ATR length", { minval: 1 });
137
+ const factor = input.float(3, "ATR factor", { minval: 0.5, step: 0.5 });
138
+ const atr = ta.atr(length);
139
+
140
+ let stop = NaN; // the stop of the previous bar
141
+ let long = true; // long or short
142
+ const trail = eachBar((c) => {
143
+ const distance = c.get(atr) * factor;
144
+ if (Number.isNaN(distance)) return NaN;
145
+ if (Number.isNaN(stop)) stop = c.close - distance;
146
+ if (long) {
147
+ if (c.close < stop) { long = false; stop = c.close + distance; }
148
+ else stop = Math.max(stop, c.close - distance);
149
+ } else {
150
+ if (c.close > stop) { long = true; stop = c.close - distance; }
151
+ else stop = Math.min(stop, c.close + distance);
152
+ }
153
+ return stop;
154
+ });
155
+ plot(trail, "Stop", { color: color.orange, style: plot.style_linebr });
156
+ ```
157
+
158
+ ## 10. Labels on swing highs and lows
159
+
160
+ A swing high is a high with `strength` lower highs on each side, so it is known `strength` bars later: the label goes
161
+ back to its bar (`c.i - strength`). An indicator keeps 50 labels unless `max_labels_count` says more.
162
+
163
+ ```js
164
+ indicator("Swing labels", { overlay: true, max_labels_count: 500 });
165
+ const strength = input.int(5, "Swing strength", { minval: 1 });
166
+ const swingHigh = ta.pivothigh(high, strength, strength);
167
+ const swingLow = ta.pivotlow(low, strength, strength);
168
+ plot(ta.sma(close, 20), "Average", { color: color.gray });
169
+ eachBar((c) => {
170
+ const h = c.get(swingHigh);
171
+ const l = c.get(swingLow);
172
+ if (!Number.isNaN(h)) label.new(c.i - strength, h, "H", { style: label.style_label_down, color: color.red, textcolor: color.white });
173
+ if (!Number.isNaN(l)) label.new(c.i - strength, l, "L", { style: label.style_label_up, color: color.green, textcolor: color.white });
174
+ });
175
+ ```
176
+
177
+ ## 11. Bullish RSI divergence
178
+
179
+ The price makes a lower low while the RSI makes a higher low: selling slows down. Each swing low of the RSI is
180
+ compared with the previous one.
181
+
182
+ ```js
183
+ indicator("RSI divergence");
184
+ const rsi = ta.rsi(close, input.int(14, "RSI length", { minval: 1 }));
185
+ const strength = input.int(5, "Swing strength", { minval: 1 });
186
+ const swingLow = ta.pivotlow(rsi, strength, strength);
187
+ plot(rsi, "RSI", { color: color.purple });
188
+
189
+ let previousRsi = NaN;
190
+ let previousPrice = NaN;
191
+ const divergence = eachBar((c) => {
192
+ if (Number.isNaN(c.get(swingLow))) return 0;
193
+ const rsiLow = c.get(rsi, strength); // the swing is `strength` bars back
194
+ const priceLow = c.get(low, strength);
195
+ const found = rsiLow > previousRsi && priceLow < previousPrice ? 1 : 0;
196
+ previousRsi = rsiLow;
197
+ previousPrice = priceLow;
198
+ return found;
199
+ });
200
+ plotshape(divergence, "Bullish divergence", { style: shape.labelup, location: location.bottom, text: "Div", color: color.green, textcolor: color.white, offset: -strength });
201
+ alertcondition(divergence, "Bullish divergence", "Bullish RSI divergence");
202
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oakscriptjs",
3
- "version": "0.24.0",
3
+ "version": "0.25.1",
4
4
  "type": "module",
5
5
  "description": "PineScript v6 compatible technical analysis library for JavaScript/TypeScript",
6
6
  "main": "./dist/cjs/index.js",
@@ -67,6 +67,7 @@
67
67
  "bin",
68
68
  "src",
69
69
  "skills",
70
+ "docs",
70
71
  "README.md",
71
72
  "AGENTS.md",
72
73
  "LICENSE"
@@ -193,6 +193,14 @@ const run = executeScript(() => {
193
193
  // run.result.plots.plot0 -> [{ time, value }, ...]; run.inputConfig, run.plotConfig, run.strategyConfig...
194
194
  ```
195
195
 
196
+ `defineIndicator(body)` does the same once for all: `metadata`, `inputConfig`, `defaultInputs`, `plotConfig`... read
197
+ from the script, `calculate(bars, inputs)` and `createRun(inputs)` to run it, with strict mode on (a wrong input name
198
+ or value, an unknown option, a text that is not a colour throw a `ScriptError` that says what to change). For a user
199
+ who is new to coding, point to `docs/first-indicator.md`, `docs/recipes.md` and `docs/no-code.md` (an indicator in JSON
200
+ with formulas, `defineIndicatorFromJson`) of the package, and to `oakscript preview <file>` (a live chart in the
201
+ browser). Plain-English names (`calc.average`, `calc.crossesAbove`...) and `formula("...")` exist beside the Pine ones;
202
+ write the Pine names for a port.
203
+
196
204
  Bars are `{ time, open, high, low, close, volume }`, oldest first, `time` in UNIX seconds. The chart context
197
205
  (4th argument) gives the timeframe, time zone and session that `request.security`, `time()` and
198
206
  `timeframe.*` need. A strategy also needs an order engine: see [strategies](reference/strategies.md#the-host-side).
@@ -41,8 +41,9 @@ The run stops at the first error; the message names the call. The ones you will
41
41
  | `TypeError: A Series is not a text...` | a Series in a text: `` `RSI ${rsi}` ``, `"RSI " + rsi` | in a loop: `str.tostring(c.get(rsi))` |
42
42
  | `ta.sma() was called inside eachBar(): a ta.* call computes the whole Series at once...` | a `ta.*` function in a bar callback | compute it before the loop, read it with `c.get(x)`; a Pine call in an `if` block: `callsite.*` |
43
43
  | `TypeError: ta.smaa does not exist. Did you mean ta.sma?` (also `math.`, `str.`, `color.`, `input.`, `request.`) | a misspelt or Pine-only member | a suggested name, [pine-differences.md](pine-differences.md) |
44
- | `TypeError: series.toArray is not a function` | a number or NaN passed where a Series is expected: `plot(5)`, `ta.sma(c.close, 10)` | a Series; a constant level: `hline(5)` |
45
- | `xyz is not defined` | a global that does not exist (Pine-only name such as `shape`, `location`, `size`, `xloc`, `bar_index`) | [pine-differences.md](pine-differences.md): strings for the constants, `c.i` for `bar_index` |
44
+ | `TypeError: series.toArray is not a function` | a number or NaN passed where a Series is expected: `ta.sma(c.close, 10)` | a Series (`plot(5)` plots a flat line; a level: `hline(5)`) |
45
+ | `xyz is not defined` | a global that does not exist: a misspelt name, or a Pine-only name (`shape`, `location`, `size`, `xloc` and `bar_index` do exist) | the name it suggests, [pine-differences.md](pine-differences.md) |
46
+ | `ScriptError: ...` | a mistake found in strict mode (`defineIndicator`, or `executeScript(..., { strict: true })`): an unknown input or option, a value out of range, a text that is not a colour | the change the message says |
46
47
  | `TypeError: Cannot read properties of undefined (reading 'toArray')` | a missing Series argument, e.g. `ta.stoch(close, high, low)` | pass it: `ta.stoch(close, high, low, 14)` |
47
48
  | `TypeError: label.new(): unknown argument "textcolour". Did you mean textcolor?` | a misspelt named argument of a drawing | the name it suggests |
48
49
  | `TypeError: input.int(): unknown argument "min". Did you mean minval?` | an input option that is not Pine's (`min`, `max`, `choices`) | Pine's name: `minval`, `maxval`, `options` |