openalgo-script 0.4.0 → 0.6.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 (359) hide show
  1. package/CHANGELOG.md +1050 -0
  2. package/README.md +111 -27
  3. package/dist/adapters/charts/driving.d.ts +50 -0
  4. package/dist/adapters/charts/driving.d.ts.map +1 -0
  5. package/dist/adapters/charts/driving.js +57 -0
  6. package/dist/adapters/charts/driving.js.map +1 -0
  7. package/dist/adapters/charts/run.d.ts +20 -0
  8. package/dist/adapters/charts/run.d.ts.map +1 -1
  9. package/dist/adapters/charts/run.js +89 -16
  10. package/dist/adapters/charts/run.js.map +1 -1
  11. package/dist/adapters/charts/tables.d.ts +6 -19
  12. package/dist/adapters/charts/tables.d.ts.map +1 -1
  13. package/dist/adapters/charts/tables.js.map +1 -1
  14. package/dist/adapters/charts/undrawable.d.ts +4 -0
  15. package/dist/adapters/charts/undrawable.d.ts.map +1 -0
  16. package/dist/adapters/charts/undrawable.js +47 -0
  17. package/dist/adapters/charts/undrawable.js.map +1 -0
  18. package/dist/adapters/charts/venue.d.ts +73 -0
  19. package/dist/adapters/charts/venue.d.ts.map +1 -0
  20. package/dist/adapters/charts/venue.js +104 -0
  21. package/dist/adapters/charts/venue.js.map +1 -0
  22. package/dist/core/accounting/analysis.d.ts +111 -0
  23. package/dist/core/accounting/analysis.d.ts.map +1 -0
  24. package/dist/core/accounting/analysis.js +123 -0
  25. package/dist/core/accounting/analysis.js.map +1 -0
  26. package/dist/core/accounting/equity.d.ts +33 -0
  27. package/dist/core/accounting/equity.d.ts.map +1 -1
  28. package/dist/core/accounting/equity.js +12 -0
  29. package/dist/core/accounting/equity.js.map +1 -1
  30. package/dist/core/accounting/index.d.ts +2 -0
  31. package/dist/core/accounting/index.d.ts.map +1 -1
  32. package/dist/core/accounting/index.js +1 -0
  33. package/dist/core/accounting/index.js.map +1 -1
  34. package/dist/core/accounting/report.d.ts +3 -0
  35. package/dist/core/accounting/report.d.ts.map +1 -1
  36. package/dist/core/accounting/report.js +2 -0
  37. package/dist/core/accounting/report.js.map +1 -1
  38. package/dist/core/accounting/statistics.d.ts +12 -0
  39. package/dist/core/accounting/statistics.d.ts.map +1 -1
  40. package/dist/core/accounting/statistics.js +23 -1
  41. package/dist/core/accounting/statistics.js.map +1 -1
  42. package/dist/core/backtest/case.d.ts +60 -0
  43. package/dist/core/backtest/case.d.ts.map +1 -0
  44. package/dist/core/backtest/case.js +319 -0
  45. package/dist/core/backtest/case.js.map +1 -0
  46. package/dist/core/backtest/compare.d.ts.map +1 -1
  47. package/dist/core/backtest/compare.js +2 -0
  48. package/dist/core/backtest/compare.js.map +1 -1
  49. package/dist/core/backtest/deliver.d.ts +93 -0
  50. package/dist/core/backtest/deliver.d.ts.map +1 -0
  51. package/dist/core/backtest/deliver.js +94 -0
  52. package/dist/core/backtest/deliver.js.map +1 -0
  53. package/dist/core/backtest/drive.d.ts +101 -8
  54. package/dist/core/backtest/drive.d.ts.map +1 -1
  55. package/dist/core/backtest/drive.js +114 -158
  56. package/dist/core/backtest/drive.js.map +1 -1
  57. package/dist/core/backtest/index.d.ts +26 -12
  58. package/dist/core/backtest/index.d.ts.map +1 -1
  59. package/dist/core/backtest/index.js +21 -10
  60. package/dist/core/backtest/index.js.map +1 -1
  61. package/dist/core/backtest/record.d.ts +63 -2
  62. package/dist/core/backtest/record.d.ts.map +1 -1
  63. package/dist/core/backtest/record.js +71 -4
  64. package/dist/core/backtest/record.js.map +1 -1
  65. package/dist/core/backtest/replay.d.ts.map +1 -1
  66. package/dist/core/backtest/replay.js +11 -1
  67. package/dist/core/backtest/replay.js.map +1 -1
  68. package/dist/core/backtest/simulate.d.ts +113 -1
  69. package/dist/core/backtest/simulate.d.ts.map +1 -1
  70. package/dist/core/backtest/simulate.js +123 -5
  71. package/dist/core/backtest/simulate.js.map +1 -1
  72. package/dist/core/backtest/walk.d.ts +39 -0
  73. package/dist/core/backtest/walk.d.ts.map +1 -0
  74. package/dist/core/backtest/walk.js +125 -0
  75. package/dist/core/backtest/walk.js.map +1 -0
  76. package/dist/core/catalogue/catalogue.generated.d.ts +179 -2
  77. package/dist/core/catalogue/catalogue.generated.d.ts.map +1 -1
  78. package/dist/core/catalogue/catalogue.generated.js +19 -2
  79. package/dist/core/catalogue/catalogue.generated.js.map +1 -1
  80. package/dist/core/catalogue/types.d.ts +8 -2
  81. package/dist/core/catalogue/types.d.ts.map +1 -1
  82. package/dist/core/catalogue/values.generated.d.ts +51 -0
  83. package/dist/core/catalogue/values.generated.d.ts.map +1 -1
  84. package/dist/core/check/assigned.d.ts +19 -0
  85. package/dist/core/check/assigned.d.ts.map +1 -0
  86. package/dist/core/check/assigned.js +14 -0
  87. package/dist/core/check/assigned.js.map +1 -0
  88. package/dist/core/check/call-sites.d.ts.map +1 -1
  89. package/dist/core/check/call-sites.js +9 -4
  90. package/dist/core/check/call-sites.js.map +1 -1
  91. package/dist/core/check/calls.d.ts.map +1 -1
  92. package/dist/core/check/calls.js +12 -4
  93. package/dist/core/check/calls.js.map +1 -1
  94. package/dist/core/check/check.d.ts.map +1 -1
  95. package/dist/core/check/check.js +15 -2
  96. package/dist/core/check/check.js.map +1 -1
  97. package/dist/core/check/checker.d.ts +26 -0
  98. package/dist/core/check/checker.d.ts.map +1 -1
  99. package/dist/core/check/checker.js +38 -2
  100. package/dist/core/check/checker.js.map +1 -1
  101. package/dist/core/check/constant.d.ts +16 -0
  102. package/dist/core/check/constant.d.ts.map +1 -1
  103. package/dist/core/check/constant.js +52 -0
  104. package/dist/core/check/constant.js.map +1 -1
  105. package/dist/core/check/deleted.d.ts +3 -0
  106. package/dist/core/check/deleted.d.ts.map +1 -0
  107. package/dist/core/check/deleted.js +109 -0
  108. package/dist/core/check/deleted.js.map +1 -0
  109. package/dist/core/check/expressions.js +1 -1
  110. package/dist/core/check/expressions.js.map +1 -1
  111. package/dist/core/check/library-orders.js +2 -2
  112. package/dist/core/check/library-orders.js.map +1 -1
  113. package/dist/core/check/library-output.d.ts.map +1 -1
  114. package/dist/core/check/library-output.js +1 -0
  115. package/dist/core/check/library-output.js.map +1 -1
  116. package/dist/core/check/library-prose.generated.js +12 -12
  117. package/dist/core/check/library-prose.generated.js.map +1 -1
  118. package/dist/core/check/library.d.ts +7 -0
  119. package/dist/core/check/library.d.ts.map +1 -1
  120. package/dist/core/check/library.js +1 -0
  121. package/dist/core/check/library.js.map +1 -1
  122. package/dist/core/check/surface.d.ts +9 -0
  123. package/dist/core/check/surface.d.ts.map +1 -1
  124. package/dist/core/check/surface.js +13 -0
  125. package/dist/core/check/surface.js.map +1 -1
  126. package/dist/core/emit/canonical.d.ts +22 -8
  127. package/dist/core/emit/canonical.d.ts.map +1 -1
  128. package/dist/core/emit/canonical.js +67 -6
  129. package/dist/core/emit/canonical.js.map +1 -1
  130. package/dist/core/engine/arithmetic.d.ts +6 -25
  131. package/dist/core/engine/arithmetic.d.ts.map +1 -1
  132. package/dist/core/engine/arithmetic.js +48 -3
  133. package/dist/core/engine/arithmetic.js.map +1 -1
  134. package/dist/core/engine/bar-source.d.ts +65 -0
  135. package/dist/core/engine/bar-source.d.ts.map +1 -0
  136. package/dist/core/engine/bar-source.js +58 -0
  137. package/dist/core/engine/bar-source.js.map +1 -0
  138. package/dist/core/engine/engine.d.ts +9 -11
  139. package/dist/core/engine/engine.d.ts.map +1 -1
  140. package/dist/core/engine/engine.js +24 -24
  141. package/dist/core/engine/engine.js.map +1 -1
  142. package/dist/core/engine/guard.d.ts.map +1 -1
  143. package/dist/core/engine/guard.js +10 -0
  144. package/dist/core/engine/guard.js.map +1 -1
  145. package/dist/core/engine/host.d.ts +4 -2
  146. package/dist/core/engine/host.d.ts.map +1 -1
  147. package/dist/core/engine/host.js.map +1 -1
  148. package/dist/core/engine/index.d.ts +2 -1
  149. package/dist/core/engine/index.d.ts.map +1 -1
  150. package/dist/core/engine/index.js +1 -1
  151. package/dist/core/engine/index.js.map +1 -1
  152. package/dist/core/engine/library/arrays.d.ts.map +1 -1
  153. package/dist/core/engine/library/arrays.js +8 -2
  154. package/dist/core/engine/library/arrays.js.map +1 -1
  155. package/dist/core/engine/library/binding.d.ts +2 -0
  156. package/dist/core/engine/library/binding.d.ts.map +1 -1
  157. package/dist/core/engine/library/binding.js.map +1 -1
  158. package/dist/core/engine/library/code-points.d.ts +40 -0
  159. package/dist/core/engine/library/code-points.d.ts.map +1 -0
  160. package/dist/core/engine/library/code-points.js +74 -0
  161. package/dist/core/engine/library/code-points.js.map +1 -0
  162. package/dist/core/engine/library/index.d.ts +13 -0
  163. package/dist/core/engine/library/index.d.ts.map +1 -1
  164. package/dist/core/engine/library/index.js +15 -0
  165. package/dist/core/engine/library/index.js.map +1 -1
  166. package/dist/core/engine/library/objects.d.ts.map +1 -1
  167. package/dist/core/engine/library/objects.js +1 -1
  168. package/dist/core/engine/library/objects.js.map +1 -1
  169. package/dist/core/engine/library/text.d.ts.map +1 -1
  170. package/dist/core/engine/library/text.js +51 -34
  171. package/dist/core/engine/library/text.js.map +1 -1
  172. package/dist/core/engine/load.d.ts +20 -0
  173. package/dist/core/engine/load.d.ts.map +1 -1
  174. package/dist/core/engine/load.js +62 -0
  175. package/dist/core/engine/load.js.map +1 -1
  176. package/dist/core/engine/request-body.d.ts +3 -1
  177. package/dist/core/engine/request-body.d.ts.map +1 -1
  178. package/dist/core/engine/request-body.js +3 -1
  179. package/dist/core/engine/request-body.js.map +1 -1
  180. package/dist/core/engine/request-plan.d.ts +3 -3
  181. package/dist/core/engine/request-plan.js +3 -3
  182. package/dist/core/engine/requests.d.ts +2 -2
  183. package/dist/core/engine/requests.d.ts.map +1 -1
  184. package/dist/core/engine/requests.js +5 -4
  185. package/dist/core/engine/requests.js.map +1 -1
  186. package/dist/core/engine/series.d.ts +18 -6
  187. package/dist/core/engine/series.d.ts.map +1 -1
  188. package/dist/core/engine/series.js +24 -5
  189. package/dist/core/engine/series.js.map +1 -1
  190. package/dist/core/engine/session/record.d.ts +4 -4
  191. package/dist/core/engine/session/record.js +4 -4
  192. package/dist/core/engine/verify-tables.d.ts.map +1 -1
  193. package/dist/core/engine/verify-tables.js +41 -0
  194. package/dist/core/engine/verify-tables.js.map +1 -1
  195. package/dist/core/importer/calls.d.ts +29 -0
  196. package/dist/core/importer/calls.d.ts.map +1 -0
  197. package/dist/core/importer/calls.js +267 -0
  198. package/dist/core/importer/calls.js.map +1 -0
  199. package/dist/core/importer/context.d.ts +118 -0
  200. package/dist/core/importer/context.d.ts.map +1 -0
  201. package/dist/core/importer/context.js +120 -0
  202. package/dist/core/importer/context.js.map +1 -0
  203. package/dist/core/importer/expressions.d.ts +44 -0
  204. package/dist/core/importer/expressions.d.ts.map +1 -0
  205. package/dist/core/importer/expressions.js +223 -0
  206. package/dist/core/importer/expressions.js.map +1 -0
  207. package/dist/core/importer/importer.d.ts +13 -0
  208. package/dist/core/importer/importer.d.ts.map +1 -0
  209. package/dist/core/importer/importer.js +220 -0
  210. package/dist/core/importer/importer.js.map +1 -0
  211. package/dist/core/importer/index.d.ts +20 -0
  212. package/dist/core/importer/index.d.ts.map +1 -0
  213. package/dist/core/importer/index.js +18 -0
  214. package/dist/core/importer/index.js.map +1 -0
  215. package/dist/core/importer/lexer.d.ts +68 -0
  216. package/dist/core/importer/lexer.d.ts.map +1 -0
  217. package/dist/core/importer/lexer.js +207 -0
  218. package/dist/core/importer/lexer.js.map +1 -0
  219. package/dist/core/importer/names.d.ts +41 -0
  220. package/dist/core/importer/names.d.ts.map +1 -0
  221. package/dist/core/importer/names.js +78 -0
  222. package/dist/core/importer/names.js.map +1 -0
  223. package/dist/core/importer/orders.d.ts +8 -0
  224. package/dist/core/importer/orders.d.ts.map +1 -0
  225. package/dist/core/importer/orders.js +332 -0
  226. package/dist/core/importer/orders.js.map +1 -0
  227. package/dist/core/importer/outputs.d.ts +19 -0
  228. package/dist/core/importer/outputs.d.ts.map +1 -0
  229. package/dist/core/importer/outputs.js +266 -0
  230. package/dist/core/importer/outputs.js.map +1 -0
  231. package/dist/core/importer/parser.d.ts +5 -0
  232. package/dist/core/importer/parser.d.ts.map +1 -0
  233. package/dist/core/importer/parser.js +439 -0
  234. package/dist/core/importer/parser.js.map +1 -0
  235. package/dist/core/importer/presence.d.ts +32 -0
  236. package/dist/core/importer/presence.d.ts.map +1 -0
  237. package/dist/core/importer/presence.js +146 -0
  238. package/dist/core/importer/presence.js.map +1 -0
  239. package/dist/core/importer/settle.d.ts +23 -0
  240. package/dist/core/importer/settle.d.ts.map +1 -0
  241. package/dist/core/importer/settle.js +135 -0
  242. package/dist/core/importer/settle.js.map +1 -0
  243. package/dist/core/importer/statements.d.ts +4 -0
  244. package/dist/core/importer/statements.d.ts.map +1 -0
  245. package/dist/core/importer/statements.js +339 -0
  246. package/dist/core/importer/statements.js.map +1 -0
  247. package/dist/core/importer/syntax.d.ts +182 -0
  248. package/dist/core/importer/syntax.d.ts.map +1 -0
  249. package/dist/core/importer/syntax.js +14 -0
  250. package/dist/core/importer/syntax.js.map +1 -0
  251. package/dist/core/importer/table.d.ts +52 -0
  252. package/dist/core/importer/table.d.ts.map +1 -0
  253. package/dist/core/importer/table.js +156 -0
  254. package/dist/core/importer/table.js.map +1 -0
  255. package/dist/core/importer/values.d.ts +27 -0
  256. package/dist/core/importer/values.d.ts.map +1 -0
  257. package/dist/core/importer/values.js +236 -0
  258. package/dist/core/importer/values.js.map +1 -0
  259. package/dist/core/index.d.ts +22 -5
  260. package/dist/core/index.d.ts.map +1 -1
  261. package/dist/core/index.js +19 -3
  262. package/dist/core/index.js.map +1 -1
  263. package/dist/core/stdlib/calendar/civil.d.ts.map +1 -1
  264. package/dist/core/stdlib/calendar/civil.js +5 -2
  265. package/dist/core/stdlib/calendar/civil.js.map +1 -1
  266. package/dist/core/stdlib/index.d.ts +1 -1
  267. package/dist/core/stdlib/index.d.ts.map +1 -1
  268. package/dist/core/stdlib/index.js +1 -1
  269. package/dist/core/stdlib/index.js.map +1 -1
  270. package/dist/core/stdlib/maths/index.d.ts +1 -1
  271. package/dist/core/stdlib/maths/index.d.ts.map +1 -1
  272. package/dist/core/stdlib/maths/index.js +1 -1
  273. package/dist/core/stdlib/maths/index.js.map +1 -1
  274. package/dist/core/stdlib/maths/rounding.d.ts +5 -0
  275. package/dist/core/stdlib/maths/rounding.d.ts.map +1 -1
  276. package/dist/core/stdlib/maths/rounding.js +19 -1
  277. package/dist/core/stdlib/maths/rounding.js.map +1 -1
  278. package/dist/core/version/version.generated.d.ts +1 -1
  279. package/dist/core/version/version.generated.js +1 -1
  280. package/package.json +17 -2
  281. package/spec/README.md +2 -1
  282. package/spec/errors.json +383 -23
  283. package/src/adapters/charts/driving.ts +109 -0
  284. package/src/adapters/charts/run.ts +126 -28
  285. package/src/adapters/charts/tables.ts +6 -19
  286. package/src/adapters/charts/undrawable.ts +51 -0
  287. package/src/adapters/charts/venue.ts +132 -0
  288. package/src/core/accounting/analysis.ts +188 -0
  289. package/src/core/accounting/equity.ts +38 -0
  290. package/src/core/accounting/index.ts +2 -0
  291. package/src/core/accounting/report.ts +5 -0
  292. package/src/core/accounting/statistics.ts +38 -1
  293. package/src/core/backtest/case.ts +395 -0
  294. package/src/core/backtest/compare.ts +2 -0
  295. package/src/core/backtest/deliver.ts +161 -0
  296. package/src/core/backtest/drive.ts +233 -217
  297. package/src/core/backtest/index.ts +26 -12
  298. package/src/core/backtest/record.ts +134 -5
  299. package/src/core/backtest/replay.ts +11 -1
  300. package/src/core/backtest/simulate.ts +201 -6
  301. package/src/core/backtest/walk.ts +182 -0
  302. package/src/core/catalogue/catalogue.generated.ts +19 -2
  303. package/src/core/catalogue/types.ts +8 -2
  304. package/src/core/catalogue/values.generated.ts +16 -0
  305. package/src/core/check/assigned.ts +31 -0
  306. package/src/core/check/call-sites.ts +11 -4
  307. package/src/core/check/calls.ts +13 -4
  308. package/src/core/check/check.ts +14 -2
  309. package/src/core/check/checker.ts +39 -2
  310. package/src/core/check/constant.ts +57 -0
  311. package/src/core/check/deleted.ts +135 -0
  312. package/src/core/check/expressions.ts +1 -1
  313. package/src/core/check/library-orders.ts +2 -2
  314. package/src/core/check/library-output.ts +1 -0
  315. package/src/core/check/library-prose.generated.ts +12 -12
  316. package/src/core/check/library.ts +8 -0
  317. package/src/core/check/surface.ts +16 -0
  318. package/src/core/emit/canonical.ts +67 -9
  319. package/src/core/engine/arithmetic.ts +23 -3
  320. package/src/core/engine/bar-source.ts +113 -0
  321. package/src/core/engine/engine.ts +26 -25
  322. package/src/core/engine/guard.ts +12 -1
  323. package/src/core/engine/host.ts +4 -2
  324. package/src/core/engine/index.ts +2 -1
  325. package/src/core/engine/library/arrays.ts +8 -2
  326. package/src/core/engine/library/binding.ts +2 -0
  327. package/src/core/engine/library/code-points.ts +73 -0
  328. package/src/core/engine/library/index.ts +17 -0
  329. package/src/core/engine/library/objects.ts +1 -6
  330. package/src/core/engine/library/text.ts +53 -35
  331. package/src/core/engine/load.ts +68 -0
  332. package/src/core/engine/request-body.ts +5 -7
  333. package/src/core/engine/request-plan.ts +3 -3
  334. package/src/core/engine/requests.ts +11 -8
  335. package/src/core/engine/series.ts +30 -6
  336. package/src/core/engine/session/record.ts +4 -4
  337. package/src/core/engine/verify-tables.ts +43 -0
  338. package/src/core/importer/calls.ts +274 -0
  339. package/src/core/importer/context.ts +197 -0
  340. package/src/core/importer/expressions.ts +247 -0
  341. package/src/core/importer/importer.ts +226 -0
  342. package/src/core/importer/index.ts +19 -0
  343. package/src/core/importer/lexer.ts +285 -0
  344. package/src/core/importer/names.ts +104 -0
  345. package/src/core/importer/orders.ts +307 -0
  346. package/src/core/importer/outputs.ts +245 -0
  347. package/src/core/importer/parser.ts +436 -0
  348. package/src/core/importer/presence.ts +164 -0
  349. package/src/core/importer/settle.ts +141 -0
  350. package/src/core/importer/statements.ts +331 -0
  351. package/src/core/importer/syntax.ts +144 -0
  352. package/src/core/importer/table.ts +191 -0
  353. package/src/core/importer/values.ts +241 -0
  354. package/src/core/index.ts +29 -2
  355. package/src/core/stdlib/calendar/civil.ts +5 -2
  356. package/src/core/stdlib/index.ts +1 -0
  357. package/src/core/stdlib/maths/index.ts +1 -0
  358. package/src/core/stdlib/maths/rounding.ts +23 -1
  359. package/src/core/version/version.generated.ts +1 -1
@@ -0,0 +1,182 @@
1
+ /**
2
+ * One walk of a backtest's bars, with the venue answering between them.
3
+ *
4
+ * Split from `drive.ts`, which sets a run up and writes its record, because the
5
+ * walk is the one part of a backtest that runs once per bar and it is read on its
6
+ * own: the order in which a frame is delivered, a bar appended and its fills
7
+ * folded is the whole of what makes a backtest reproducible.
8
+ */
9
+ import type { RecordedFill } from '../accounting/index.js';
10
+ import type { BarResult, Engine, HostBar, OrderIntent, Value } from '../engine/index.js';
11
+ import type { Delivered, Destination } from './deliver.js';
12
+ import type { ReportWindow } from './range.js';
13
+ import { diagnosticIn } from './record.js';
14
+ import type { RecordedBar, RecordedDiagnostic } from './record.js';
15
+
16
+ /** One line of the script's log, `conformance.md` section 4. */
17
+ export interface LogLine {
18
+ readonly barIndex: number;
19
+ readonly time: number | null;
20
+ readonly value: Value;
21
+ }
22
+
23
+ /** What one walk of the bars produced. */
24
+ export interface Walked {
25
+ readonly fills: readonly RecordedFill[];
26
+ readonly frames: readonly Delivered[];
27
+ readonly diagnostics: readonly RecordedDiagnostic[];
28
+ readonly rows: readonly (readonly Value[])[];
29
+ readonly log: readonly LogLine[];
30
+ }
31
+
32
+ /**
33
+ * The bars, in order, with the venue answering between them.
34
+ *
35
+ * The one ordering rule: deliver, then append, then collect what the append
36
+ * reported, then ask the venue what this bar did. A driver that asked the venue
37
+ * before appending would price a fill against a bar the strategy had not seen.
38
+ */
39
+ export function walk(
40
+ engine: Engine,
41
+ destination: Destination,
42
+ bars: readonly RecordedBar[],
43
+ covered: ReportWindow,
44
+ keep: { readonly rows?: boolean; readonly log?: boolean },
45
+ ): Walked {
46
+ const fills: RecordedFill[] = [];
47
+ const rows: (readonly Value[])[] = [];
48
+ const log: LogLine[] = [];
49
+ const frames: Delivered[] = [];
50
+ const diagnostics: RecordedDiagnostic[] = [];
51
+ const intents = new Map<number, OrderIntent>();
52
+ const refs = new Map<number, string>();
53
+ const sizes = new Map<number, number>();
54
+
55
+ let pending: readonly Delivered[] = [];
56
+ let seq = 0;
57
+
58
+ // The bars are a settled history, so the fold is handed all of them before
59
+ // bar 0 as `Engine.run` hands them. A `"lookahead"` read is the one reading
60
+ // that can tell (`compiled-program.md` 2.16.2): over history it reads the
61
+ // bucket a bar is inside in full, and a driver that fed the fold one bar at
62
+ // a time would answer the bucket so far, which is the live chart's reading.
63
+ const handed = bars.slice(0, covered.total).map(hostBarOf);
64
+ engine.history(handed);
65
+
66
+ for (let index = 0; index < covered.total; index += 1) {
67
+ const bar = bars[index];
68
+ const held = handed[index];
69
+ if (bar === undefined || held === undefined) continue;
70
+
71
+ for (const one of pending) {
72
+ const ref = one.frame.orderRef;
73
+ if (typeof ref === 'string') refs.set(one.frame.intentId, ref);
74
+ engine.deliver(one.frame);
75
+ frames.push(one);
76
+ }
77
+ pending = [];
78
+
79
+ const result = engine.append(
80
+ held,
81
+ { isConfirmed: true, isRealtime: false },
82
+ covered.total,
83
+ );
84
+
85
+ for (const intent of intentsIn(result)) intents.set(intent.intentId, intent);
86
+ seq = settle(result, index, bar, { intents, refs, sizes }, fills, seq);
87
+
88
+ if (result.diagnostic !== undefined) {
89
+ diagnostics.push(diagnosticIn(result.diagnostic, index));
90
+ break;
91
+ }
92
+ if (keep.rows === true) rows.push([...result.columns]);
93
+ if (keep.log === true) {
94
+ for (const one of result.effects) {
95
+ if (one.effect === 'log') log.push({ barIndex: index, time: bar.time, value: one.args[0] ?? null });
96
+ }
97
+ }
98
+
99
+ pending = destination.answers(index);
100
+ }
101
+
102
+ return { fills, frames, diagnostics, rows, log };
103
+ }
104
+
105
+ /** A recorded bar as the engine is handed one. */
106
+ function hostBarOf(bar: RecordedBar): HostBar {
107
+ return {
108
+ time: bar.time, open: bar.open, high: bar.high, low: bar.low, close: bar.close,
109
+ volume: bar.volume, oi: bar.oi,
110
+ };
111
+ }
112
+
113
+ /** What the run is holding while it folds one bar's frames into fills. */
114
+ interface Books {
115
+ readonly intents: ReadonlyMap<number, OrderIntent>;
116
+ readonly refs: ReadonlyMap<number, string>;
117
+ readonly sizes: Map<number, number>;
118
+ }
119
+
120
+ /**
121
+ * The fills one bar's fold settled.
122
+ *
123
+ * **The position sizes either side of step 6 are kept here and not read back
124
+ * from the engine**, because the engine reports a leg's net and a fill settles
125
+ * against one position reference: the two are different numbers whenever a leg
126
+ * holds more than one position, which is every flip. They are folded in the
127
+ * order the frames were folded in, from the same deltas, so they are the same
128
+ * arithmetic the position book did rather than a second reading of its result.
129
+ * When `FrameOutcome` carries them, these two lines come from the engine and
130
+ * this fold keeps only the sequence number.
131
+ *
132
+ * A refused frame settles nothing and is not a fill. Today the fold reports the
133
+ * refusal as a word rather than a code, so nothing here can write it into the
134
+ * record as a diagnostic; that is OS7018 and OS7019, and both are deferred.
135
+ */
136
+ function settle(
137
+ result: BarResult,
138
+ index: number,
139
+ bar: RecordedBar,
140
+ books: Books,
141
+ fills: RecordedFill[],
142
+ from: number,
143
+ ): number {
144
+ let seq = from;
145
+ for (const outcome of result.frames) {
146
+ if (outcome.refused !== undefined) continue;
147
+ if (outcome.delta <= 0 || outcome.price === null) continue;
148
+ const intent = books.intents.get(outcome.intentId);
149
+ if (intent === undefined || intent.side === null) continue;
150
+
151
+ const before = books.sizes.get(intent.positionRef) ?? 0;
152
+ const after = before + (intent.side === 'sell' ? -outcome.delta : outcome.delta);
153
+ books.sizes.set(intent.positionRef, after);
154
+ seq += 1;
155
+
156
+ fills.push({
157
+ seq,
158
+ intentId: outcome.intentId,
159
+ orderRef: books.refs.get(outcome.intentId) ?? '',
160
+ tag: intent.tag,
161
+ positionRef: intent.positionRef,
162
+ side: intent.side,
163
+ units: outcome.delta,
164
+ price: outcome.price,
165
+ // The bar the fold happened at, which is the bar the position exists
166
+ // from. Where the fill itself traded is the bar before it or this bar's
167
+ // own open, and neither is a bar the strategy could have acted on it in.
168
+ barIndex: index,
169
+ barTime: bar.time,
170
+ refSizeBefore: before,
171
+ refSizeAfter: after,
172
+ });
173
+ }
174
+ return seq;
175
+ }
176
+
177
+ /** Every intent one bar handed over, in the order the bar applied them. */
178
+ function intentsIn(result: BarResult): readonly OrderIntent[] {
179
+ const out: OrderIntent[] = [];
180
+ for (const effect of result.effects) for (const intent of effect.intents) out.push(intent);
181
+ return out;
182
+ }
@@ -19,6 +19,7 @@ export const STAGE_LABELS = {
19
19
  check: "checker",
20
20
  runtime: "engine",
21
21
  host: "host",
22
+ import: "importer",
22
23
  } as const satisfies Readonly<Record<Stage, string>>;
23
24
 
24
25
  export const ENTRIES = {
@@ -95,6 +96,8 @@ export const ENTRIES = {
95
96
  OS3022: { code: "OS3022", title: "Two inputs share a settings key", severity: "error", stage: "check", since: 1, autofix: false, message: "This input is keyed by its title {name}, which is already the name of the input at line {line}.", fix: "Give this one a title of its own, or rename the input at line {line}.", placeholders: ["name", "line"] },
96
97
  OS3023: { code: "OS3023", title: "An order names a leg this file does not declare", severity: "error", stage: "check", since: 1, autofix: false, message: "{name} was given a {argument}, and this file declares none.", fix: "Leave {argument} out. A file that declares no leg has exactly one, and every order acts on it.", placeholders: ["name", "argument"] },
97
98
  OS3024: { code: "OS3024", title: "An input written in place has an empty title", severity: "error", stage: "check", since: 1, autofix: false, message: "An input() assigned to no name is named by its title, and this one's title is empty.", fix: "Give the title something to say: input(2, \"Precision\").", placeholders: [] },
99
+ OS3025: { code: "OS3025", title: "A setting is part of a larger expression here", severity: "error", stage: "check", since: 1, autofix: false, message: "{option} is fixed before the first bar and holds an input() only as the whole of its value, so it cannot hold an expression over one.", fix: "Declare the setting as the value itself, with an input() written as the whole of {option}, or write the value out as a literal.", placeholders: ["option"] },
100
+ OS3026: { code: "OS3026", title: "This option cannot be a setting", severity: "error", stage: "check", since: 1, autofix: false, message: "{option} is written into the compiled program as a plain value, so a setting cannot choose it in this format version.", fix: "Write the option out as a string literal and drop the input(): style = \"step\", for instance, is one of the values OS3008 names.", placeholders: ["option"] },
98
101
  OS4001: { code: "OS4001", title: "History index is not usable", severity: "error", stage: "runtime", since: 1, autofix: false, message: "[{index}] is not a whole number of bars at or above zero.", fix: "Wrap the index in floor() or round(), and clamp a computed index with max(0, n).", placeholders: ["index"] },
99
102
  OS4002: { code: "OS4002", title: "History index is deeper than the retained depth", severity: "error", stage: "runtime", since: 1, autofix: false, message: "[{index}] reaches past the retained depth of {depth} bars.", fix: "Raise the depth in one place: limits(history = {suggested}).", placeholders: ["index", "depth", "suggested"] },
100
103
  OS4003: { code: "OS4003", title: "A whole number was required here", severity: "error", stage: "runtime", since: 1, autofix: false, message: "{name}'s {argument} was {found} on this bar; a whole number was required.", fix: "Round the value before passing it: floor() towards zero, round() to nearest.", placeholders: ["name", "argument", "found"] },
@@ -129,7 +132,7 @@ export const ENTRIES = {
129
132
  OS6009: { code: "OS6009", title: "The request failed", severity: "error", stage: "host", since: 1, autofix: false, message: "The host could not fetch {symbol} at {timeframe}: {reason}.", fix: "Act on {reason} in the host: it is a connection, permission or quota problem. Where the value can be derived from the chart's own bars, derive it and drop the request.", placeholders: ["symbol", "timeframe", "reason"] },
130
133
  OS6010: { code: "OS6010", title: "The engine was given no bars", severity: "error", stage: "host", since: 1, autofix: false, message: "There are no bars for {symbol} at {timeframe}, so the script cannot run.", fix: "Choose an instrument and interval that have history, or widen the chart's date range until bars exist.", placeholders: ["symbol", "timeframe"] },
131
134
  OS6011: { code: "OS6011", title: "The bars are not in order", severity: "error", stage: "host", since: 1, autofix: false, message: "Bar {index} is dated {time}, which is not after bar {previous}.", fix: "Reload the history; if the same bar repeats, the feed is sending duplicates and the host has to sort and deduplicate the bars before the engine runs.", placeholders: ["index", "time", "previous"] },
132
- OS6012: { code: "OS6012", title: "An instrument fact is not known", severity: "error", stage: "host", since: 1, autofix: false, message: "The host did not supply {fact} for {symbol}.", fix: "Supply {fact} in the host's instrument record, or stop depending on it: round with a number the script chooses rather than chart.tickSize.", placeholders: ["fact", "symbol"] },
135
+ OS6012: { code: "OS6012", title: "An instrument fact is not known", severity: "error", stage: "host", since: 1, autofix: false, message: "The host did not supply {fact} for {symbol}.", fix: "Supply {fact} in the host's instrument record for {symbol}, or leave the session out of the record, which makes every session read absent rather than wrong.", placeholders: ["fact", "symbol"] },
133
136
  OS6013: { code: "OS6013", title: "The request changed after the first bar", severity: "error", stage: "runtime", since: 1, autofix: false, message: "This request asked for {first} on bar 0 and for {found} on bar {bar}.", fix: "Compute the symbol and the timeframe from literals or inputs, not from bar data.", placeholders: ["first", "found", "bar"] },
134
137
  OS6014: { code: "OS6014", title: "The feed does not offer this timeframe", severity: "error", stage: "host", since: 1, autofix: false, message: "The host has no {timeframe} data for {symbol}; it offers {available}.", fix: "Request one of {available}, or derive the interval you want from a lower one the host does serve.", placeholders: ["timeframe", "symbol", "available"] },
135
138
  OS6015: { code: "OS6015", title: "The requested timeframe does not fold into the chart's", severity: "error", stage: "host", since: 1, autofix: false, message: "{requested} is not a whole multiple of {chart}.", fix: "Request a multiple of {chart}, for example {suggestion}.", placeholders: ["requested", "chart", "suggestion"] },
@@ -138,9 +141,11 @@ export const ENTRIES = {
138
141
  OS6018: { code: "OS6018", title: "The compiled program is malformed", severity: "error", stage: "host", since: 1, autofix: false, message: "The program failed verification at {location}: {reason}.", fix: "Recompile the script from its source; a program that fails verification came from a broken compiler or was edited after it was written, and neither is repairable by hand.", placeholders: ["location", "reason"] },
139
142
  OS6019: { code: "OS6019", title: "A host setting fails the input's validation", severity: "error", stage: "host", since: 1, autofix: false, message: "The host supplied {value} for {key}, and {validation}.", fix: "Correct the value in the settings dialog, or widen the input's own min, max or options so the value is allowed.", placeholders: ["value", "key", "validation"] },
140
143
  OS6020: { code: "OS6020", title: "The report window holds no bars", severity: "error", stage: "host", since: 1, autofix: false, message: "The window from {from} to {to} holds none of the {count} bars supplied.", fix: "Widen the window until it covers bars, or supply the bars it covers. Both bounds are inclusive and are compared against the times of the bars supplied rather than against a calendar, so a window that falls inside a gap in the data is empty however wide it looks.", placeholders: ["from", "to", "count"] },
141
- OS6021: { code: "OS6021", title: "A run setting cannot be applied as stated", severity: "error", stage: "host", since: 1, autofix: false, message: "{setting} cannot be applied: {problem}.", fix: "State the setting so it can be carried out: declare a charge line after every line it is levied on, supply the tick size a slippage in ticks is measured in, or write down the reason a tolerance needs a bound. Nothing has been computed at the point this is refused, so correcting the setting and running again costs one run.", placeholders: ["setting", "problem"] },
144
+ OS6021: { code: "OS6021", title: "A run setting cannot be applied as stated", severity: "error", stage: "host", since: 1, autofix: false, message: "{setting} cannot be applied: {problem}.", fix: "State the setting so it can be carried out: declare a charge line after every line it is levied on, supply the tick size a slippage in ticks is measured in, write down the reason a tolerance needs a bound, or bring a bound past the cap back inside it. Nothing has been computed at the point this is refused, so correcting the setting and running again costs one run.", placeholders: ["setting", "problem"] },
142
145
  OS6022: { code: "OS6022", title: "The bars are not the bars the record was made from", severity: "error", stage: "host", since: 1, autofix: false, message: "The bars supplied hash to {found}, and the record was made from {expected}.", fix: "Replay against the bars the record names. Where the revision is the point, make a second record over the revised bars and compare the two runs, rather than overwriting one run with the other under one name.", placeholders: ["found", "expected"] },
143
146
  OS6023: { code: "OS6023", title: "Two cost models are stated at once", severity: "error", stage: "host", since: 1, autofix: false, message: "A charge schedule was supplied, and the declaration states a commission of {commission} in {commissionType}.", fix: "Supply the schedule and leave the declaration's commission at its default of zero, or state the commission in the declaration and supply no schedule. A schedule is the one of the two that can carry a floor, a cap, a charge levied on a charge, and a cost that falls on one side of the trade only.", placeholders: ["commission", "commissionType"] },
147
+ OS6024: { code: "OS6024", title: "The host cannot draw something this study declares", severity: "error", stage: "host", since: 1, autofix: false, message: "This host cannot draw {what}: {limit}.", fix: "Declare what this host draws, or run the study on a host that draws {what}.", placeholders: ["what", "limit"] },
148
+ OS6025: { code: "OS6025", title: "A bar has no time", severity: "error", stage: "host", since: 1, autofix: false, message: "Bar {index} was handed over with no time.", fix: "State the bar's open instant in whole milliseconds since the Unix epoch, UTC, and leave out a bar the host cannot date rather than handing it over without a time.", placeholders: ["index"] },
144
149
  OS7001: { code: "OS7001", title: "Only a strategy can do that", severity: "error", stage: "check", since: 1, autofix: false, message: "{name} is available only in a file declared with strategy().", fix: "Change study(...) on line {line} to strategy(...), or replace {name} with signal(\"...\") to mark the bar without trading.", placeholders: ["name", "line"] },
145
150
  OS7002: { code: "OS7002", title: "An order argument is absent", severity: "error", stage: "runtime", since: 1, autofix: false, message: "{name}'s {argument} is absent on this bar.", fix: "Guard the call with isNone({argument}), or supply a fallback with orElse() where one is genuinely correct.", placeholders: ["name", "argument"] },
146
151
  OS7003: { code: "OS7003", title: "An order function inside a request expression", severity: "error", stage: "check", since: 1, autofix: false, message: "{name} inside a request expression would place an order from another instrument's bars.", fix: "Read the value with the request, and place the order at the top level from the result.", placeholders: ["name"] },
@@ -179,6 +184,18 @@ export const ENTRIES = {
179
184
  OS8017: { code: "OS8017", title: "The condition is constant", severity: "warning", stage: "check", since: 1, autofix: false, message: "This condition is {value} on every bar.", fix: "Restore the test that was meant, or delete the branch that never runs.", placeholders: ["value"] },
180
185
  OS8018: { code: "OS8018", title: "An input is never used", severity: "warning", stage: "check", since: 1, autofix: false, message: "The input {title} is declared at line {line} and never read.", fix: "Use the name the input assigns, or delete the input() and its dialog row.", placeholders: ["title", "line"] },
181
186
  OS8019: { code: "OS8019", title: "A deleted object is still held", severity: "warning", stage: "check", since: 1, autofix: false, message: "{name} still holds the {kind} deleted at line {line}.", fix: "Assign none to the name on the same path as the delete, and where the object came out of an array, remove the element as well as deleting the object.", placeholders: ["name", "kind", "line"] },
187
+ OS9001: { code: "OS9001", title: "Version not read by the importer", severity: "error", stage: "import", since: 1, autofix: false, message: "The importer reads scripts written for version 5 or 6 of the source dialect, and this one {found}.", fix: "Bring the script up to version 5 or 6 in the source dialect, or translate it by hand, and import it again.", placeholders: ["found"] },
188
+ OS9002: { code: "OS9002", title: "Construct with no equivalent", severity: "error", stage: "import", since: 1, autofix: false, message: "The importer cannot translate {construct}, so the statement holding it was kept as a comment.", fix: "Translate that statement by hand from the comment that holds it: the rest of the script was translated around it.", placeholders: ["construct"] },
189
+ OS9003: { code: "OS9003", title: "Built-in with no mapping", severity: "error", stage: "import", since: 1, autofix: false, message: "{name} is not one of the built-ins the importer maps, so the statement holding it was kept as a comment.", fix: "Find the OpenScript library call that does the same job, check how it treats its first bars and absent values, and write the statement by hand.", placeholders: ["name"] },
190
+ OS9004: { code: "OS9004", title: "Declaration the importer cannot place", severity: "error", stage: "import", since: 1, autofix: false, message: "The importer writes the declaration from one indicator or strategy call at the top level, and this script {found}.", fix: "Give the source script exactly one indicator or strategy declaration at the top level, and import it again.", placeholders: ["found"] },
191
+ OS9005: { code: "OS9005", title: "Reads a statement that was not translated", severity: "error", stage: "import", since: 1, autofix: false, message: "{name} is declared by the statement at line {line}, which was not translated, so this statement that reads it was kept as a comment too.", fix: "Translate the statement at line {line} by hand first, then this one, which was kept as a comment only because it reads {name}.", placeholders: ["name", "line"] },
192
+ OS9006: { code: "OS9006", title: "Argument with no equivalent", severity: "error", stage: "import", since: 1, autofix: false, message: "{argument} changes what {call} computes or trades, and OpenScript has no equivalent the importer can write.", fix: "Translate the call by hand, deciding what OpenScript should do in place of {argument}, or remove {argument} from the source script if the script does not depend on it.", placeholders: ["argument", "call"] },
193
+ OS9007: { code: "OS9007", title: "Warmup and absent bars follow OpenScript", severity: "warning", stage: "import", since: 1, autofix: false, message: "{call} became {target} here and wherever else the script calls it, and {target} follows OpenScript's own warmup, arithmetic and handling of absent values, which the original is not guaranteed to share.", fix: "Compare the first bars of the translation with the original, and where the script depends on them, write the value as an explicit recurrence held in var, seeded the way the original seeds it.", placeholders: ["call", "target"] },
194
+ OS9008: { code: "OS9008", title: "Whole-number division in version 5", severity: "warning", stage: "import", since: 1, autofix: false, message: "Both sides of this division are whole-number constants, which version 5 of the source dialect divides without a fractional part, and OpenScript keeps the fraction.", fix: "If the script relied on the whole-number result, wrap the translated division in trunc(); otherwise leave it as it is.", placeholders: [] },
195
+ OS9009: { code: "OS9009", title: "Presentation argument left out", severity: "warning", stage: "import", since: 1, autofix: false, message: "{argument} changes how {call} is displayed or what it may allocate, not what it computes, and OpenScript has no equivalent, so the translation leaves it out.", fix: "Nothing needs to change unless the display mattered; where it did, restyle the translated call with the arguments OpenScript does have, such as its colour, width and style.", placeholders: ["argument", "call"] },
196
+ OS9010: { code: "OS9010", title: "Order placed under OpenScript's order model", severity: "warning", stage: "import", since: 1, autofix: false, message: "{call} was translated into OpenScript's order calls here and wherever else the script uses it, and OpenScript decides when an order fills, how a reversal and a repeated entry are handled and when an exit level starts to apply by its own order model.", fix: "Backtest the translation beside the original and compare the two trade lists before relying on it, and adjust the order calls by hand where they part.", placeholders: ["call"] },
197
+ OS9011: { code: "OS9011", title: "Equality over two absent values", severity: "warning", stage: "import", since: 1, autofix: false, message: "Both sides of {operator} can be absent on the same bar, and OpenScript counts two absent values as equal, which the original is not guaranteed to do.", fix: "If the original relied on two absent values never comparing equal, guard the translated comparison with isNone() on either side.", placeholders: ["operator"] },
198
+ OS9012: { code: "OS9012", title: "The translation does not compile", severity: "error", stage: "import", since: 1, autofix: false, message: "The OpenScript compiler refused the translation of this statement with {code}, so the statement was kept as a comment.", fix: "Look up {code} in this catalogue and translate the statement by hand in the form that code's fix describes.", placeholders: ["code"] },
182
199
  } as const satisfies Readonly<Record<string, CatalogueEntry>>;
183
200
 
184
201
  /** Every code the compiler and the engines may emit. Nothing else is one. */
@@ -6,8 +6,14 @@
6
6
  /** What a diagnostic does to the run. An error stops it; a warning never does. */
7
7
  export type Severity = 'error' | 'warning';
8
8
 
9
- /** Which part of the pipeline raised a diagnostic. */
10
- export type Stage = 'lex' | 'parse' | 'check' | 'runtime' | 'host';
9
+ /**
10
+ * Which part of the pipeline raised a diagnostic.
11
+ *
12
+ * `import` is not a stage of compiling OpenScript: it is the importer, which
13
+ * reads a script written in another chart language and whose findings point
14
+ * into that script rather than into an OpenScript file.
15
+ */
16
+ export type Stage = 'lex' | 'parse' | 'check' | 'runtime' | 'host' | 'import';
11
17
 
12
18
  /**
13
19
  * What a compiler puts into a message slot.
@@ -87,6 +87,8 @@ export interface DiagnosticValues {
87
87
  readonly OS3022: { readonly name: PlaceholderValue; readonly line: PlaceholderValue };
88
88
  readonly OS3023: { readonly name: PlaceholderValue; readonly argument: PlaceholderValue };
89
89
  readonly OS3024: Record<string, never>;
90
+ readonly OS3025: { readonly option: PlaceholderValue };
91
+ readonly OS3026: { readonly option: PlaceholderValue };
90
92
  readonly OS4001: { readonly index: PlaceholderValue };
91
93
  readonly OS4002: { readonly index: PlaceholderValue; readonly depth: PlaceholderValue; readonly suggested: PlaceholderValue };
92
94
  readonly OS4003: { readonly name: PlaceholderValue; readonly argument: PlaceholderValue; readonly found: PlaceholderValue };
@@ -133,6 +135,8 @@ export interface DiagnosticValues {
133
135
  readonly OS6021: { readonly setting: PlaceholderValue; readonly problem: PlaceholderValue };
134
136
  readonly OS6022: { readonly found: PlaceholderValue; readonly expected: PlaceholderValue };
135
137
  readonly OS6023: { readonly commission: PlaceholderValue; readonly commissionType: PlaceholderValue };
138
+ readonly OS6024: { readonly what: PlaceholderValue; readonly limit: PlaceholderValue };
139
+ readonly OS6025: { readonly index: PlaceholderValue };
136
140
  readonly OS7001: { readonly name: PlaceholderValue; readonly line: PlaceholderValue };
137
141
  readonly OS7002: { readonly name: PlaceholderValue; readonly argument: PlaceholderValue };
138
142
  readonly OS7003: { readonly name: PlaceholderValue };
@@ -171,4 +175,16 @@ export interface DiagnosticValues {
171
175
  readonly OS8017: { readonly value: PlaceholderValue };
172
176
  readonly OS8018: { readonly title: PlaceholderValue; readonly line: PlaceholderValue };
173
177
  readonly OS8019: { readonly name: PlaceholderValue; readonly kind: PlaceholderValue; readonly line: PlaceholderValue };
178
+ readonly OS9001: { readonly found: PlaceholderValue };
179
+ readonly OS9002: { readonly construct: PlaceholderValue };
180
+ readonly OS9003: { readonly name: PlaceholderValue };
181
+ readonly OS9004: { readonly found: PlaceholderValue };
182
+ readonly OS9005: { readonly name: PlaceholderValue; readonly line: PlaceholderValue };
183
+ readonly OS9006: { readonly argument: PlaceholderValue; readonly call: PlaceholderValue };
184
+ readonly OS9007: { readonly call: PlaceholderValue; readonly target: PlaceholderValue };
185
+ readonly OS9008: Record<string, never>;
186
+ readonly OS9009: { readonly argument: PlaceholderValue; readonly call: PlaceholderValue };
187
+ readonly OS9010: { readonly call: PlaceholderValue };
188
+ readonly OS9011: { readonly operator: PlaceholderValue };
189
+ readonly OS9012: { readonly code: PlaceholderValue };
174
190
  }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The names some line of the file gives a value, wherever that line is.
3
+ *
4
+ * `warmup.ts` answers `never` for a value that is absent on every bar, and
5
+ * OS8009 is built on it. The checker reads a name's warmup at the point of use,
6
+ * in source order, which is right for a plain name: it is recomputed every bar
7
+ * and cannot be read before this bar's line writes it. It is not right for a
8
+ * `var`, which is one name over the whole run: a line near the top can read the
9
+ * value a line near the bottom wrote on the bar before, which is the shape of
10
+ * every rollover that carries a running figure into a held one and restarts it.
11
+ *
12
+ * So the checker asks this set before it calls a `var` never. An assignment of
13
+ * the literal `none` is not counted, because it gives the name nothing, and a
14
+ * placeholder `var x = none` that nothing else writes stays exactly what OS8009
15
+ * was written for.
16
+ */
17
+ import type { AstNode, Script } from '../ast/index.js';
18
+ import { childrenOf, withoutGrouping } from '../ast/index.js';
19
+
20
+ export function namesGivenAValue(script: Script): ReadonlySet<string> {
21
+ const found = new Set<string>();
22
+ collect(script, found);
23
+ return found;
24
+ }
25
+
26
+ function collect(node: AstNode, found: Set<string>): void {
27
+ if (node.kind === 'assignment' && withoutGrouping(node.value).kind !== 'noneLiteral') {
28
+ found.add(node.target.text);
29
+ }
30
+ for (const child of childrenOf(node)) collect(child, found);
31
+ }
@@ -21,7 +21,7 @@ import { bindArguments } from './arguments.js';
21
21
  import { remember, resolveLibraryCall, unresolved } from './calls.js';
22
22
  import type { Checker, Placement } from './checker.js';
23
23
  import type { CheckedCall, InputKind, RequestMode } from './checked.js';
24
- import { calleeNameOf, isCompileTimeConstant, isSourceName } from './constant.js';
24
+ import { calleeNameOf, isCompileTimeConstant, isSourceName, readsInputInPart } from './constant.js';
25
25
  import { checkExpression } from './expressions.js';
26
26
  import { ensureChecked } from './functions.js';
27
27
  import { allowHandle, refuseHandle } from './handles.js';
@@ -59,7 +59,10 @@ export function resolveCall(checker: Checker, call: Call, placement: Placement):
59
59
  checker.report('OS2010', call.span, {
60
60
  name: checker.textOf(call.callee.span),
61
61
  type: typeText(UNKNOWN),
62
- suggestion: checker.suggestionFor(checker.textOf(call.callee.span)),
62
+ suggestion: checker.callSuggestionFor(
63
+ checker.textOf(call.callee.span),
64
+ call.args.map((one) => checker.textOf(one.span)),
65
+ ),
63
66
  });
64
67
  return record(checker, call, unresolved(call, 'call'));
65
68
  }
@@ -220,8 +223,12 @@ function checkInput(checker: Checker, call: Call, checked: CheckedCall): Checked
220
223
  const options = argumentOf(checked, 'options');
221
224
  const title = literalOf(checked, 'title');
222
225
 
223
- if (value !== undefined && !isSourceName(value.value) && !isCompileTimeConstant(checker, value.value)) {
224
- checker.report('OS3003', value.span, { option: 'value' });
226
+ if (value !== undefined && !isSourceName(value.value)) {
227
+ if (!isCompileTimeConstant(checker, value.value)) {
228
+ checker.report('OS3003', value.span, { option: 'value' });
229
+ } else if (readsInputInPart(checker, value.value, false)) {
230
+ checker.report('OS3025', value.span, { option: 'value' });
231
+ }
225
232
  }
226
233
  if (kind !== undefined && PLANNED_KINDS.has(kind)) {
227
234
  checker.report('OS2001', call.span, {
@@ -19,7 +19,7 @@ import type { ParameterShape } from './arguments.js';
19
19
  import type { Checker, Placement } from './checker.js';
20
20
  import { reportStrategyOnly } from './checker.js';
21
21
  import type { CheckedCall } from './checked.js';
22
- import { isCompileTimeConstant } from './constant.js';
22
+ import { isCompileTimeConstant, readsInputInPart } from './constant.js';
23
23
  import { refuseHandle } from './handles.js';
24
24
  import type { LibraryEntry } from './library.js';
25
25
  import { literalNumber, literalString } from './literals.js';
@@ -244,8 +244,14 @@ export function validateArguments(
244
244
  });
245
245
  }
246
246
 
247
- if (entry.constant.includes(parameter.name) && !isCompileTimeConstant(checker, argument.value)) {
248
- checker.report('OS3003', span, { option: parameter.name });
247
+ if (entry.constant.includes(parameter.name)) {
248
+ if (!isCompileTimeConstant(checker, argument.value)) {
249
+ checker.report('OS3003', span, { option: parameter.name });
250
+ } else if (readsInputInPart(checker, argument.value, entry.name !== 'input')) {
251
+ checker.report('OS3025', span, { option: parameter.name });
252
+ } else if (entry.written.includes(parameter.name) && readsInputInPart(checker, argument.value, false)) {
253
+ checker.report('OS3026', span, { option: parameter.name });
254
+ }
249
255
  }
250
256
  }
251
257
 
@@ -331,7 +337,10 @@ export function resolveLibraryCall(
331
337
  checker.report('OS2010', call.span, {
332
338
  name,
333
339
  type: held === undefined ? typeText(UNKNOWN) : typeText(held.returns),
334
- suggestion: checker.suggestionFor(name),
340
+ suggestion: checker.callSuggestionFor(
341
+ name,
342
+ call.args.map((one) => checker.textOf(one.span)),
343
+ ),
335
344
  });
336
345
  return unresolved(call, name);
337
346
  }
@@ -21,11 +21,12 @@ import type { SourceFile } from '../source/index.js';
21
21
  import { Checker, TOP_LEVEL, finaliseStorage } from './checker.js';
22
22
  import type { CheckedScript } from './checked.js';
23
23
  import { checkDeclaration, checkLimits, readHeader } from './declaration.js';
24
+ import { reportDeletedStillHeld } from './deleted.js';
24
25
  import { checkFunctionDeclarations, checkRemainingFunctions } from './functions.js';
25
26
  import { reportRepeatedAlertIds } from './outputs.js';
26
27
  import { checkStatement } from './statements.js';
27
28
  import { reportUnplaceableTags } from './tags.js';
28
- import { elementOf } from './types.js';
29
+ import { elementOf, isHandle } from './types.js';
29
30
 
30
31
  export function check(file: SourceFile, script: Script, sink: DiagnosticSink): CheckedScript {
31
32
  const checker = new Checker(file, script, sink);
@@ -54,6 +55,7 @@ export function check(file: SourceFile, script: Script, sink: DiagnosticSink): C
54
55
  finaliseStorage(checker);
55
56
  reportUnknownElementTypes(checker);
56
57
  reportNamesNeverRead(checker);
58
+ reportDeletedStillHeld(checker);
57
59
  reportRepeatedTitles(checker);
58
60
  reportInputKeys(checker);
59
61
  reportRepeatedAlertIds(checker);
@@ -78,12 +80,22 @@ function reportUnknownElementTypes(checker: Checker): void {
78
80
  }
79
81
  }
80
82
 
81
- /** OS8010 and OS8018: a name, or an input, that nothing in the file reads. */
83
+ /**
84
+ * OS8010 and OS8018: a name, or an input, that nothing in the file reads.
85
+ *
86
+ * A name bound to a declaration handle is exempt, as the entry's cause says: a
87
+ * handle is a compile-time binding, the declaration draws whether or not it is
88
+ * named, and so nothing is left in the bar loop for the warning to be about.
89
+ * `language.md` 5.4 gives `plot`, `plotCandles`, `fill` and `level` the one kind
90
+ * of value, so the exemption is the type rather than a list of the two calls
91
+ * `stdlib.md` 14.2 happens to name.
92
+ */
82
93
  function reportNamesNeverRead(checker: Checker): void {
83
94
  for (const binding of checker.bindings) {
84
95
  if (binding.isRead) continue;
85
96
  if (binding.kind === 'library' || binding.kind === 'function') continue;
86
97
  if (binding.kind === 'parameter' || binding.kind === 'loop') continue;
98
+ if (isHandle(binding.type)) continue;
87
99
 
88
100
  if (binding.input !== undefined) {
89
101
  const input = checker.inputs[binding.input];
@@ -35,13 +35,14 @@ import type {
35
35
  Mutable,
36
36
  Storage,
37
37
  } from './checked.js';
38
+ import { namesGivenAValue } from './assigned.js';
38
39
  import { conditionalCalls } from './conditional.js';
39
- import { libraryNames } from './surface.js';
40
+ import { libraryFunctionNames, libraryNames } from './surface.js';
40
41
  import { closestName } from './suggest.js';
41
42
  import type { Type } from './types.js';
42
43
  import { UNKNOWN } from './types.js';
43
44
  import type { Warmup } from './warmup.js';
44
- import { BAR_ZERO } from './warmup.js';
45
+ import { BAR_ZERO, weaken } from './warmup.js';
45
46
 
46
47
  /**
47
48
  * One nesting of names, `language.md` 12.1.
@@ -143,6 +144,13 @@ export class Checker {
143
144
 
144
145
  /** Every call site a bar can pass without evaluating, `language.md` 11.4. */
145
146
  private readonly conditional: ReadonlySet<Call>;
147
+ /**
148
+ * The names some line gives a value, read before a `var` is called never.
149
+ *
150
+ * Built the first time it is asked for, because it is one walk of the whole
151
+ * tree and almost every file never reads a `var` that is still never.
152
+ */
153
+ private givenAValue: ReadonlySet<string> | undefined = undefined;
146
154
 
147
155
  constructor(file: SourceFile, script: Script, sink: DiagnosticSink) {
148
156
  this.file = file;
@@ -300,6 +308,21 @@ export class Checker {
300
308
  return this.types.get(expression) ?? UNKNOWN;
301
309
  }
302
310
 
311
+ /**
312
+ * The warmup a read of this name sees, here.
313
+ *
314
+ * A `var` that is still never at this line but that a later line gives a
315
+ * value is not never: the later line wrote it on the bar before, or on an
316
+ * earlier pass of the same loop. The honest answer is a floor of bar 0,
317
+ * which claims nothing and withdraws the claim OS8009 is built on, where an
318
+ * exact bar would need the later line's warmup before it has been read.
319
+ */
320
+ warmupOfRead(binding: Binding): Warmup {
321
+ if (binding.warmup.kind !== 'never' || binding.persistence === 'none') return binding.warmup;
322
+ this.givenAValue ??= namesGivenAValue(this.script);
323
+ return this.givenAValue.has(binding.name) ? weaken(BAR_ZERO) : binding.warmup;
324
+ }
325
+
303
326
  warmupOf(expression: Expression): Warmup {
304
327
  return this.warmups.get(expression) ?? BAR_ZERO;
305
328
  }
@@ -318,6 +341,20 @@ export class Checker {
318
341
  return closestName(written, this.namesInScope());
319
342
  }
320
343
 
344
+ /**
345
+ * OS2010's suggestion: the closest function, called with what was written.
346
+ *
347
+ * The catalogue documents the slot as the library function whose name is
348
+ * closest, with its first argument filled in, so `volume(20)` is offered a
349
+ * function over `volume` and the arguments the reader already wrote,
350
+ * rather than a name that cannot be called either.
351
+ */
352
+ callSuggestionFor(written: string, args: readonly string[]): string {
353
+ const inStrategy = this.declaration?.form === 'strategy';
354
+ const name = closestName(written, libraryFunctionNames(inStrategy));
355
+ return `${name}(${[written, ...args].join(', ')})`;
356
+ }
357
+
321
358
  /** A state region for one call site, `compiled-program.md` 2.11. */
322
359
  takeStateId(): number {
323
360
  const id = this.stateCount;
@@ -92,6 +92,63 @@ export function isCompileTimeConstant(checker: Checker, expression: Expression):
92
92
  }
93
93
  }
94
94
 
95
+ /**
96
+ * Whether a constant reads a setting as part of something larger, OS3025.
97
+ *
98
+ * `compiled-program.md` 2.3 gives a field fixed before bar 0 two forms: the
99
+ * effective value, which the compiler folds, and `{ "input": "<key>" }`, one
100
+ * setting resolved at load. `precision = input(2, "Decimals") + 1` and
101
+ * `opacity = shade ? 1 : 0` are neither: the compiler cannot fold them, because
102
+ * the setting's value is not known until the host resolves it, and the program
103
+ * has no form for an expression evaluated at load. Nothing refused them, so the
104
+ * emitter met them with no way to write them and reported a defect in itself.
105
+ *
106
+ * `wholeOnly` is false for an input's own options: a default, a bound or a
107
+ * step is what the settings dialog shows before anybody has chosen, and a
108
+ * setting that took its default from another setting would have none to show.
109
+ */
110
+ export function readsInputInPart(
111
+ checker: Checker,
112
+ expression: Expression,
113
+ wholeOnly: boolean,
114
+ ): boolean {
115
+ if (!readsInput(checker, expression)) return false;
116
+ return !wholeOnly || !isWholeInput(checker, expression);
117
+ }
118
+
119
+ /** Whether a setting is read anywhere inside the expression. */
120
+ function readsInput(checker: Checker, expression: Expression): boolean {
121
+ if (isWholeInput(checker, expression)) return true;
122
+ const inner = withoutGrouping(expression);
123
+ switch (inner.kind) {
124
+ case 'arrayLiteral':
125
+ return inner.elements.some((element) => readsInput(checker, element));
126
+ case 'unary':
127
+ return readsInput(checker, inner.operand);
128
+ case 'binary':
129
+ return readsInput(checker, inner.left) || readsInput(checker, inner.right);
130
+ case 'ternary':
131
+ return (
132
+ readsInput(checker, inner.condition) ||
133
+ readsInput(checker, inner.whenTrue) ||
134
+ readsInput(checker, inner.whenFalse)
135
+ );
136
+ case 'call':
137
+ return inner.args.some((argument) => readsInput(checker, argument.value));
138
+ default:
139
+ return false;
140
+ }
141
+ }
142
+
143
+ /** One setting and nothing else: an `input()` call, or a name that holds one. */
144
+ function isWholeInput(checker: Checker, expression: Expression): boolean {
145
+ const inner = withoutGrouping(expression);
146
+ if (inner.kind === 'call') return calleeName(inner.callee) === 'input';
147
+ if (inner.kind !== 'nameReference') return false;
148
+ const binding = checker.lookup(inner.name);
149
+ return binding !== undefined && inputHeldBy(binding) !== undefined;
150
+ }
151
+
95
152
  /**
96
153
  * Whether the expression names one of the price series an `input` may default
97
154
  * to, `stdlib.md` 13.1.