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
package/src/str/index.ts CHANGED
@@ -21,7 +21,7 @@ import type { int, bool, float, simple_int, simple_string } from '../types/index
21
21
  * @returns The number of characters in the string
22
22
  *
23
23
  * @example
24
- * ```typescript
24
+ * ```js
25
25
  * str.length("Hello") // Returns: 5
26
26
  * str.length("") // Returns: 0
27
27
  * ```
@@ -67,7 +67,7 @@ export function length(str: simple_string): int {
67
67
  * "NaNT" for `format.volume`), except `format.mintick`, which gives "Infinity" / "-Infinity"
68
68
  *
69
69
  * @example
70
- * ```typescript
70
+ * ```js
71
71
  * str.tostring(123) // "123"
72
72
  * str.tostring(1.5, "#.##") // "1.5"
73
73
  * str.tostring(0.5, "#.00") // ".50"
@@ -138,7 +138,7 @@ export function tostring(value: any, format?: simple_string, mintick?: float, pr
138
138
  * (no-break space U+00A0, U+3000) give na
139
139
  *
140
140
  * @example
141
- * ```typescript
141
+ * ```js
142
142
  * str.tonumber("123") // Returns: 123
143
143
  * str.tonumber("45.67") // Returns: 45.67
144
144
  * str.tonumber("abc") // Returns: null
@@ -172,7 +172,7 @@ export function tonumber(str: simple_string): float | null {
172
172
  * - Indices beyond string length return empty string
173
173
  *
174
174
  * @example
175
- * ```typescript
175
+ * ```js
176
176
  * str.substring("hello world", 0, 5) // Returns: "hello"
177
177
  * str.substring("hello world", 6) // Returns: "world"
178
178
  * str.substring("test", 1, 3) // Returns: "es"
@@ -190,7 +190,7 @@ export function substring(str: simple_string, begin: simple_int, end?: simple_in
190
190
  * @returns The uppercase string
191
191
  *
192
192
  * @example
193
- * ```typescript
193
+ * ```js
194
194
  * str.upper("hello") // Returns: "HELLO"
195
195
  * str.upper("Hello World") // Returns: "HELLO WORLD"
196
196
  * str.upper("test123") // Returns: "TEST123"
@@ -207,7 +207,7 @@ export function upper(str: simple_string): string {
207
207
  * @returns The lowercase string
208
208
  *
209
209
  * @example
210
- * ```typescript
210
+ * ```js
211
211
  * str.lower("HELLO") // Returns: "hello"
212
212
  * str.lower("Hello World") // Returns: "hello world"
213
213
  * str.lower("TEST123") // Returns: "test123"
@@ -229,7 +229,7 @@ export function lower(str: simple_string): string {
229
229
  * - Empty string is contained in any string
230
230
  *
231
231
  * @example
232
- * ```typescript
232
+ * ```js
233
233
  * str.contains("hello world", "world") // Returns: true
234
234
  * str.contains("hello world", "World") // Returns: false (case-sensitive)
235
235
  * str.contains("test", "xyz") // Returns: false
@@ -252,7 +252,7 @@ export function contains(source: simple_string, str: simple_string): bool {
252
252
  * - Returns na (NaN) if substring is not found, as in PineScript
253
253
  *
254
254
  * @example
255
- * ```typescript
255
+ * ```js
256
256
  * str.pos("hello world", "world") // Returns: 6
257
257
  * str.pos("hello world", "o") // Returns: 4 (first occurrence)
258
258
  * str.pos("hello", "xyz") // Returns: NaN (na)
@@ -281,7 +281,7 @@ export function pos(source: simple_string, str: simple_string): int {
281
281
  * - fewer than N + 1 occurrences (or a negative N) leave the source unchanged
282
282
  *
283
283
  * @example
284
- * ```typescript
284
+ * ```js
285
285
  * str.replace("hello world hello", "hello", "hi") // Returns: "hi world hello"
286
286
  * str.replace("hello world hello", "hello", "hi", 1) // Returns: "hello world hi"
287
287
  * str.replace("test", "xyz", "abc") // Returns: "test"
@@ -318,7 +318,7 @@ export function replace(source: simple_string, target: simple_string, replacemen
318
318
  * - This matches PineScript's `str.replace_all()` behavior
319
319
  *
320
320
  * @example
321
- * ```typescript
321
+ * ```js
322
322
  * str.replace_all("hello world hello", "hello", "hi") // Returns: "hi world hi"
323
323
  * str.replace_all("a-b-c", "-", "_") // Returns: "a_b_c"
324
324
  * str.replace_all("test", "x", "y") // Returns: "test" (no match)
@@ -341,7 +341,7 @@ export function replace_all(source: simple_string, target: simple_string, replac
341
341
  * - Consecutive separators create empty strings in result
342
342
  *
343
343
  * @example
344
- * ```typescript
344
+ * ```js
345
345
  * str.split("a,b,c", ",") // Returns: ["a", "b", "c"]
346
346
  * str.split("hello", "") // Returns: ["h", "e", "l", "l", "o"]
347
347
  * str.split("a,,b", ",") // Returns: ["a", "", "b"]
@@ -359,7 +359,7 @@ export function split(str: simple_string, separator: simple_string): string[] {
359
359
  * @returns The concatenated string
360
360
  *
361
361
  * @example
362
- * ```typescript
362
+ * ```js
363
363
  * str.concat("hello", " ", "world") // Returns: "hello world"
364
364
  * str.concat("a", "b", "c") // Returns: "abc"
365
365
  * str.concat("test") // Returns: "test"
@@ -370,27 +370,29 @@ export function concat(...strings: simple_string[]): string {
370
370
  }
371
371
 
372
372
  /**
373
- * Formats a string with placeholders (PineScript `str.format`, Java MessageFormat syntax).
373
+ * Builds a text from a pattern and values: each `{0}`, `{1}`... is replaced by the argument at that position, e.g.
374
+ * `str.format("Close {0}, change {1,number,percent}", c.close, 0.025)`. Use it for labels and messages.
374
375
  *
375
- * @param formatStr - Pattern with `{0}`, `{1,number,#.##}`, `{0,number,percent}`, `{0,date,yyyy-MM-dd}`...
376
- * @param args - Values for the placeholders
377
- * @returns Formatted string
376
+ * @param pattern - The text with placeholders: `{0}` (a number shows as "#,##0.###"), `{1,number,#.##}` (number
377
+ * pattern), `{0,number,percent}`, `{0,number,integer}`, `{0,date,yyyy-MM-dd}` (a time in milliseconds); '...' in
378
+ * single quotes is kept as it is, '' is a quote
379
+ * @param args - The values of the placeholders, in order: numbers, texts, booleans, arrays
380
+ * @returns The text with the values in place
378
381
  *
379
382
  * @remarks
380
- * As in PineScript: numbers in `{n}` use
381
- * "#,##0.###" (1234.5678 gives "1,234.568"); number patterns round half to even on the exact
382
- * value (16 decimals at most from 1e-3 up); an array gives "[a, b]"; `'...'` is literal text and `''` a quote;
383
- * a missing argument leaves `{n}` in the text.
384
- * Dates are formatted in UTC here; PineScript uses the exchange time zone.
383
+ * - numbers in `{n}` use "#,##0.###" (1234.5678 gives "1,234.568"); number patterns round half to even on the
384
+ * exact value (16 decimals at most from 1e-3 up); an array gives "[a, b]"
385
+ * - a missing argument leaves `{n}` in the text
386
+ * - dates are written in the exchange time zone of the symbol in a script, in UTC when called outside a script
385
387
  *
386
388
  * @example
387
- * ```typescript
388
- * str.format('Close: {0,number,#.##}', 101.456) // "Close: 101.46"
389
- * str.format('{0} and {1}', 'a', 2) // "a and 2"
389
+ * ```js
390
+ * str.format("Close: {0,number,#.##}", 101.456) // Returns: "Close: 101.46"
391
+ * str.format("{0} and {1}", "a", 2) // Returns: "a and 2"
390
392
  * ```
391
393
  */
392
- export function format(formatStr: simple_string, ...args: any[]): string {
393
- return formatMessage(formatStr, args, 'Etc/UTC');
394
+ export function format(pattern: simple_string, ...args: any[]): string {
395
+ return formatMessage(pattern, args, 'Etc/UTC');
394
396
  }
395
397
 
396
398
  /**
@@ -405,7 +407,7 @@ export function format(formatStr: simple_string, ...args: any[]): string {
405
407
  * - Empty string prefix always returns true
406
408
  *
407
409
  * @example
408
- * ```typescript
410
+ * ```js
409
411
  * str.startswith("hello world", "hello") // Returns: true
410
412
  * str.startswith("hello world", "Hello") // Returns: false (case-sensitive)
411
413
  * str.startswith("test", "testing") // Returns: false
@@ -427,7 +429,7 @@ export function startswith(source: simple_string, str: simple_string): bool {
427
429
  * - Empty string suffix always returns true
428
430
  *
429
431
  * @example
430
- * ```typescript
432
+ * ```js
431
433
  * str.endswith("hello world", "world") // Returns: true
432
434
  * str.endswith("hello world", "World") // Returns: false (case-sensitive)
433
435
  * str.endswith("testing", "test") // Returns: false
@@ -449,7 +451,7 @@ export function endswith(source: simple_string, str: simple_string): bool {
449
451
  * - Unicode characters may span multiple code units
450
452
  *
451
453
  * @example
452
- * ```typescript
454
+ * ```js
453
455
  * str.charAt("hello", 0) // Returns: "h"
454
456
  * str.charAt("hello", 4) // Returns: "o"
455
457
  * str.charAt("hello", 10) // Returns: ""
@@ -470,7 +472,7 @@ export function charAt(str: simple_string, pos: simple_int): string {
470
472
  * - Preserves internal whitespace
471
473
  *
472
474
  * @example
473
- * ```typescript
475
+ * ```js
474
476
  * str.trim(" hello ") // Returns: "hello"
475
477
  * str.trim(" hello world ") // Returns: "hello world"
476
478
  * str.trim("\thello\n") // Returns: "hello"
@@ -491,7 +493,7 @@ export function trim(str: simple_string): string {
491
493
  * - Preserves trailing and internal whitespace
492
494
  *
493
495
  * @example
494
- * ```typescript
496
+ * ```js
495
497
  * str.trimLeft(" hello ") // Returns: "hello "
496
498
  * str.trimLeft(" hello") // Returns: "hello"
497
499
  * str.trimLeft("\thello") // Returns: "hello"
@@ -512,7 +514,7 @@ export function trimLeft(str: simple_string): string {
512
514
  * - Preserves leading and internal whitespace
513
515
  *
514
516
  * @example
515
- * ```typescript
517
+ * ```js
516
518
  * str.trimRight(" hello ") // Returns: " hello"
517
519
  * str.trimRight("hello ") // Returns: "hello"
518
520
  * str.trimRight("hello\t") // Returns: "hello"
@@ -534,7 +536,7 @@ export function trimRight(str: simple_string): string {
534
536
  * - Uses JavaScript regex syntax
535
537
  *
536
538
  * @example
537
- * ```typescript
539
+ * ```js
538
540
  * str.match("abc123def", "[0-9]+") // Returns: "123"
539
541
  * str.match("abc", "[0-9]+") // Returns: null (na)
540
542
  * ```
@@ -544,42 +546,6 @@ export function match(source: simple_string, regex: simple_string): simple_strin
544
546
  return found && found[0] !== '' ? found[0] : null;
545
547
  }
546
548
 
547
- /**
548
- * Formats a timestamp as a string according to the specified format.
549
- *
550
- * @param time - Unix timestamp in milliseconds
551
- * @param format - Format string using PineScript format specifiers
552
- * @returns Formatted date/time string
553
- *
554
- * @remarks
555
- * Supported format specifiers (case-sensitive):
556
- * - `yyyy` - 4-digit year (e.g., "2024")
557
- * - `yy` - 2-digit year (e.g., "24")
558
- * - `MMMM` - Full month name (e.g., "January")
559
- * - `MMM` - Abbreviated month name (e.g., "Jan")
560
- * - `MM` - 2-digit month (01-12)
561
- * - `M` - Month (1-12)
562
- * - `dd` - 2-digit day (01-31)
563
- * - `d` - Day (1-31)
564
- * - `HH` - 2-digit hour (00-23)
565
- * - `H` - Hour (0-23)
566
- * - `hh` - 2-digit hour (01-12)
567
- * - `h` - Hour (1-12)
568
- * - `mm` - 2-digit minute (00-59)
569
- * - `m` - Minute (0-59)
570
- * - `ss` - 2-digit second (00-59)
571
- * - `s` - Second (0-59)
572
- * - `a` - AM/PM
573
- *
574
- * @example
575
- * ```typescript
576
- * const timestamp = 1609459200000; // Jan 1, 2021 00:00:00 UTC
577
- * str.format_time(timestamp, "dd MMM yyyy") // Returns: "01 Jan 2021"
578
- * str.format_time(timestamp, "yyyy-MM-dd HH:mm:ss") // Returns: "2021-01-01 00:00:00"
579
- * str.format_time(timestamp, "MMM d, yyyy") // Returns: "Jan 1, 2021"
580
- * str.format_time(timestamp, "hh:mm a") // Returns: "12:00 AM"
581
- * ```
582
- */
583
549
  /**
584
550
  * Constructs a new string containing the source string repeated N times with an optional separator.
585
551
  *
@@ -589,7 +555,7 @@ export function match(source: simple_string, regex: simple_string): simple_strin
589
555
  * @returns The repeated string, or null (na) if source is na or count is 0 / negative / na, as in PineScript
590
556
  *
591
557
  * @example
592
- * ```typescript
558
+ * ```js
593
559
  * str.repeat("ab", 3) // Returns: "ababab"
594
560
  * str.repeat("?", 3, ",") // Returns: "?,?,?"
595
561
  * str.repeat("hello", 0) // Returns: null (na)
@@ -603,17 +569,26 @@ export function repeat(source: simple_string, count: simple_int, separator: simp
603
569
  }
604
570
 
605
571
  /**
606
- * Formats a UNIX time (ms) with a date pattern (PineScript `str.format_time`).
572
+ * Writes a time as a date text, e.g. "2024-03-15 09:30". Use it for labels, tables and log messages.
607
573
  *
608
- * @param time - UNIX time in milliseconds
609
- * @param format - Java SimpleDateFormat pattern, e.g. "yyyy-MM-dd HH:mm:ss", "dd MMM yyyy", "hh:mm a"
610
- * @param timezone - UTC/GMT offset ("UTC-5") or IANA name ("America/New_York"). PineScript's default is
611
- * the exchange time zone, which is not known here: the default is UTC
612
- * @returns Formatted date string
574
+ * @param time - The time in milliseconds since 1 January 1970 UTC, e.g. `time` on a bar or `timenow`
575
+ * @param format - Date pattern: `yyyy` year, `MM` month number, `MMM` month name, `dd` day, `EEE` weekday,
576
+ * `HH` hour (00-23), `hh` hour (01-12) with `a` (AM/PM), `mm` minutes, `ss` seconds, `SSS` milliseconds,
577
+ * `Z` / `z` time zone; 'text' in single quotes is kept as it is. Default: "yyyy-MM-dd'T'HH:mm:ssZ"
578
+ * @param timezone - Time zone of the text: a UTC offset ("UTC-5", "GMT+2") or a zone name ("America/New_York").
579
+ * Default in a script: the exchange time zone of the symbol; called outside a script: "Etc/UTC"
580
+ * @returns The date text
613
581
  *
614
582
  * @remarks
615
583
  * Letters: y, M, d, E, u, D, w,
616
584
  * H, k, K, h, a, m, s, S, Z, z; 'text' is literal text.
585
+ *
586
+ * @example
587
+ * ```js
588
+ * eachBar((c) => {
589
+ * if (c.i === c.n - 1) label.new(c.i, c.high, str.format_time(c.get(time), "yyyy-MM-dd HH:mm"));
590
+ * });
591
+ * ```
617
592
  */
618
593
  export function format_time(time: simple_int, format: simple_string = "yyyy-MM-dd'T'HH:mm:ssZ", timezone: simple_string = 'Etc/UTC'): string {
619
594
  return formatDate(time, format, timezone);
@@ -35,68 +35,115 @@ export const STRATEGY_CONSTANTS = {
35
35
 
36
36
  /** Arguments of the PineScript `strategy()` declaration, after `title`. */
37
37
  export interface StrategyOptions {
38
+ /** Short name shown in the legend. Default: the title. */
38
39
  shorttitle?: string;
40
+ /** true: drawn over the price chart; false: in its own pane. Default: false. */
39
41
  overlay?: boolean;
42
+ /** Number format of the values: `format.price`, `format.volume`, `format.percent`... Default: the symbol's price format. */
40
43
  format?: string;
44
+ /** Decimals shown for the values. Default: the symbol's. */
41
45
  precision?: number;
46
+ /** Price scale the strategy uses: `scale.right`, `scale.left` or `scale.none`. */
42
47
  scale?: string;
48
+ /** Most entries allowed in the same direction; 0 and 1 both allow one. Default: 0. */
43
49
  pyramiding?: number;
50
+ /** true: runs the bar again after an order fills inside it, so new orders can be sent at once. Default: false. */
44
51
  calc_on_order_fills?: boolean;
52
+ /** true: runs on every price update of the live bar, not only when it closes. Default: false. */
45
53
  calc_on_every_tick?: boolean;
46
54
  /** Runs the strategy on each tick of the history bars too (the reference app's "On history bar tick"). */
47
55
  calc_on_every_history_tick?: boolean;
56
+ /** History length kept for the script's Series. Default: worked out from the script. */
48
57
  max_bars_back?: number;
58
+ /** Ticks the price must go past a limit price before the limit order fills. Default: 0. */
49
59
  backtest_fill_limits_assumption?: number;
60
+ /** Unit of `default_qty_value`: `strategy.fixed` (contracts / shares), `strategy.cash` or `strategy.percent_of_equity`. Default: `strategy.percent_of_equity`. */
50
61
  default_qty_type?: StrategyQtyType;
62
+ /** Size of an order that gives no `qty`, in the unit of `default_qty_type`. Default: 100. */
51
63
  default_qty_value?: number;
64
+ /** Money the account starts with, in the account currency. Default: 100000. */
52
65
  initial_capital?: number;
53
66
  /** Account currency; default: the symbol currency. */
54
67
  currency?: string;
55
68
  /** Ticks added to market and stop fills. */
56
69
  slippage?: number;
70
+ /** How `commission_value` is read: `strategy.commission.percent` (of the order value), `.cash_per_contract` or `.cash_per_order`. Default: percent. */
57
71
  commission_type?: StrategyCommissionType;
72
+ /** Commission per order, in the unit of `commission_type`. Default: 0. */
58
73
  commission_value?: number;
74
+ /** true: orders fill at the close of the bar that sent them instead of the next bar's open. Default: false. */
59
75
  process_orders_on_close?: boolean;
76
+ /** Which entry an exit closes first: "FIFO" (the oldest) or "ANY" (the one named by `from_entry`). Default: "FIFO". */
60
77
  close_entries_rule?: 'FIFO' | 'ANY';
78
+ /** Share of a long position's value the account must hold, in percent (100: no leverage). Default: 100. */
61
79
  margin_long?: number;
80
+ /** Share of a short position's value the account must hold, in percent. Default: 100. */
62
81
  margin_short?: number;
82
+ /** true: outputs are drawn in the order the script writes them. Default: false. */
63
83
  explicit_plot_zorder?: boolean;
84
+ /** Most `line` drawings kept on the chart (the oldest go). Default: 50. */
64
85
  max_lines_count?: number;
86
+ /** Most labels kept on the chart (the oldest go). Default: 50. */
65
87
  max_labels_count?: number;
88
+ /** Most boxes kept on the chart (the oldest go). Default: 50. */
66
89
  max_boxes_count?: number;
90
+ /** Runs only on the last this many bars; 0 runs on all. Default: 0. */
67
91
  calc_bars_count?: number;
92
+ /** Yearly risk-free rate, in percent, for the Sharpe and Sortino ratios. Default: 2. */
68
93
  risk_free_rate?: number;
94
+ /** true: fills orders with the bars of a lower timeframe inside each bar, for more exact fills. Default: false. */
69
95
  use_bar_magnifier?: boolean;
96
+ /** true: on synthetic charts (Heikin Ashi...) orders fill at the real prices. Default: false. */
70
97
  fill_orders_on_standard_ohlc?: boolean;
98
+ /** Most polylines kept on the chart (the oldest go). Default: 50. */
71
99
  max_polylines_count?: number;
72
100
  }
73
101
 
74
102
  /** Strategy properties passed to the engine: the declared values, else the PineScript defaults. */
75
103
  export interface StrategyProperties {
104
+ /** Most entries in the same direction (0 and 1 allow one). */
76
105
  pyramiding: number;
106
+ /** Runs the bar again after a fill inside it. */
77
107
  calc_on_order_fills: boolean;
108
+ /** Runs on every price update of the live bar. */
78
109
  calc_on_every_tick: boolean;
110
+ /** Runs on each price step of the history bars. */
79
111
  calc_on_every_history_tick: boolean;
112
+ /** Ticks the price must pass a limit price by before it fills. */
80
113
  backtest_fill_limits_assumption: number;
114
+ /** Unit of `default_qty_value`. */
81
115
  default_qty_type: StrategyQtyType;
116
+ /** Size of an order that gives no `qty`. */
82
117
  default_qty_value: number;
118
+ /** Money the account starts with. */
83
119
  initial_capital: number;
84
120
  /** undefined: the symbol currency. */
85
121
  currency?: string;
122
+ /** Ticks added against you to market and stop fills. */
86
123
  slippage: number;
124
+ /** How `commission_value` is read. */
87
125
  commission_type: StrategyCommissionType;
126
+ /** Commission per order. */
88
127
  commission_value: number;
128
+ /** Orders fill at the close of the bar that sent them. */
89
129
  process_orders_on_close: boolean;
130
+ /** "FIFO": exits close the oldest entry first; "ANY": the entry they name. */
90
131
  close_entries_rule: 'FIFO' | 'ANY';
132
+ /** Margin of long positions, in percent of their value. */
91
133
  margin_long: number;
134
+ /** Margin of short positions, in percent of their value. */
92
135
  margin_short: number;
136
+ /** Runs on the last this many bars; 0: all. */
93
137
  calc_bars_count: number;
138
+ /** Yearly risk-free rate in percent (Sharpe, Sortino). */
94
139
  risk_free_rate: number;
140
+ /** Fills orders with lower-timeframe bars. */
95
141
  use_bar_magnifier: boolean;
142
+ /** Fills at real prices on synthetic charts. */
96
143
  fill_orders_on_standard_ohlc: boolean;
97
144
  }
98
145
 
99
- /** PineScript defaults of the `strategy()` arguments (v6 reference). */
146
+ /** Default values of the `strategy()` options, used for every option a strategy leaves out. */
100
147
  export const STRATEGY_DEFAULTS: StrategyProperties = {
101
148
  pyramiding: 0,
102
149
  calc_on_order_fills: false,
@@ -122,55 +169,93 @@ export const STRATEGY_DEFAULTS: StrategyProperties = {
122
169
 
123
170
  /** `strategy.entry()` / `strategy.order()` arguments after `id` and `direction`. */
124
171
  export interface StrategyEntryOptions {
172
+ /** Contracts / shares to trade. Default: from `default_qty_type` and `default_qty_value`. */
125
173
  qty?: number;
174
+ /** Limit price: the order fills at this price or better. Left out with `stop` too: a market order. */
126
175
  limit?: number;
176
+ /** Stop price: the order fills once the price reaches it. With `limit`: a stop-limit order. */
127
177
  stop?: number;
178
+ /** Name of a one-cancels-all group the order belongs to. */
128
179
  oca_name?: string;
180
+ /** What a fill in the group does to the others: `strategy.oca.cancel`, `.reduce` or `.none`. Default: none. */
129
181
  oca_type?: StrategyOcaType;
182
+ /** Text shown with the order's fill on the chart and in the trade list. */
130
183
  comment?: string;
184
+ /** Text of the alert sent when the order fills. */
131
185
  alert_message?: string;
186
+ /** true: the order's fill sends no alert. Default: false. */
132
187
  disable_alert?: boolean;
133
188
  }
134
189
 
135
190
  /** `strategy.exit()` arguments after `id`. `profit`, `loss`, `trail_points` and `trail_offset` are in ticks. */
136
191
  export interface StrategyExitOptions {
192
+ /** Id of the entry to exit. Default: every open entry. */
137
193
  from_entry?: string;
194
+ /** Contracts / shares to exit. Default: the whole position (see `qty_percent`). */
138
195
  qty?: number;
196
+ /** Share of the position to exit, in percent. Default: 100. */
139
197
  qty_percent?: number;
198
+ /** Take-profit distance from the entry price, in ticks. */
140
199
  profit?: number;
200
+ /** Take-profit price. */
141
201
  limit?: number;
202
+ /** Stop-loss distance from the entry price, in ticks. */
142
203
  loss?: number;
204
+ /** Stop-loss price. */
143
205
  stop?: number;
206
+ /** Price at which the trailing stop starts. */
144
207
  trail_price?: number;
208
+ /** Distance from the entry, in ticks, at which the trailing stop starts. */
145
209
  trail_points?: number;
210
+ /** Distance the trailing stop keeps from the best price, in ticks. */
146
211
  trail_offset?: number;
212
+ /** Name of the one-cancels-all group of the exit's orders. Default: one group per exit. */
147
213
  oca_name?: string;
214
+ /** Text shown with the exit fill. */
148
215
  comment?: string;
216
+ /** Text shown when the take-profit fills. Default: `comment`. */
149
217
  comment_profit?: string;
218
+ /** Text shown when the stop-loss fills. Default: `comment`. */
150
219
  comment_loss?: string;
220
+ /** Text shown when the trailing stop fills. Default: `comment`. */
151
221
  comment_trailing?: string;
222
+ /** Text of the alert sent when the exit fills. */
152
223
  alert_message?: string;
224
+ /** Alert text when the take-profit fills. Default: `alert_message`. */
153
225
  alert_profit?: string;
226
+ /** Alert text when the stop-loss fills. Default: `alert_message`. */
154
227
  alert_loss?: string;
228
+ /** Alert text when the trailing stop fills. Default: `alert_message`. */
155
229
  alert_trailing?: string;
230
+ /** true: the exit sends no alert. Default: false. */
156
231
  disable_alert?: boolean;
157
232
  }
158
233
 
159
234
  /** `strategy.close()` arguments after `id`. */
160
235
  export interface StrategyCloseOptions {
236
+ /** Text shown with the closing fill. */
161
237
  comment?: string;
238
+ /** Contracts / shares to close. Default: all of the entry. */
162
239
  qty?: number;
240
+ /** Share to close, in percent. Default: 100. */
163
241
  qty_percent?: number;
242
+ /** Text of the alert sent when it fills. */
164
243
  alert_message?: string;
244
+ /** true: closes at the current bar's close instead of the next bar's open. Default: false. */
165
245
  immediately?: boolean;
246
+ /** true: sends no alert. Default: false. */
166
247
  disable_alert?: boolean;
167
248
  }
168
249
 
169
250
  /** `strategy.close_all()` arguments. */
170
251
  export interface StrategyCloseAllOptions {
252
+ /** Text shown with the closing fill. */
171
253
  comment?: string;
254
+ /** Text of the alert sent when it fills. */
172
255
  alert_message?: string;
256
+ /** true: closes at the current bar's close instead of the next bar's open. Default: false. */
173
257
  immediately?: boolean;
258
+ /** true: sends no alert. Default: false. */
174
259
  disable_alert?: boolean;
175
260
  }
176
261
 
@@ -189,25 +274,41 @@ export type StrategyRiskRule =
189
274
  * comments are undefined when the engine does not provide them.
190
275
  */
191
276
  export interface StrategyTrade {
277
+ /** Id of the entry order. */
192
278
  entry_id: string;
279
+ /** Price the entry filled at. */
193
280
  entry_price: number;
281
+ /** Bar number of the entry (0 = first bar). */
194
282
  entry_bar_index: number;
283
+ /** Time of the entry, in UNIX milliseconds. */
195
284
  entry_time: number;
196
285
  /** undefined when the order had no comment, or when the engine does not keep it apart from the id. */
197
286
  entry_comment?: string;
198
287
  /** Signed quantity: positive for a long trade, negative for a short trade. */
199
288
  size: number;
289
+ /** Profit of the trade in the account currency (negative for a loss), after commission. */
200
290
  profit: number;
291
+ /** Profit of the trade in percent of its entry value. */
201
292
  profit_percent: number;
293
+ /** Commission paid for the trade. */
202
294
  commission: number;
295
+ /** Largest open profit the trade reached, in the account currency. */
203
296
  max_runup: number;
297
+ /** Largest open profit in percent. */
204
298
  max_runup_percent: number;
299
+ /** Largest open loss the trade reached, in the account currency. */
205
300
  max_drawdown: number;
301
+ /** Largest open loss in percent. */
206
302
  max_drawdown_percent: number;
303
+ /** Id of the order that closed the trade. */
207
304
  exit_id?: string;
305
+ /** Price the exit filled at. */
208
306
  exit_price?: number;
307
+ /** Bar number of the exit. */
209
308
  exit_bar_index?: number;
309
+ /** Time of the exit, in UNIX milliseconds. */
210
310
  exit_time?: number;
311
+ /** Comment of the exit order. */
211
312
  exit_comment?: string;
212
313
  }
213
314