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,395 @@
1
+ /**
2
+ * A run record, turned into the files a conformance case is made of.
3
+ *
4
+ * **The suite is harvested rather than written.** A case invented by hand tests
5
+ * what somebody imagined a run does; a case taken from a run tests what a run
6
+ * actually did. `conformance.md` section 2 says a case is one directory of named
7
+ * files, and a record already holds every one of them: the script it ran, the
8
+ * bars it ran over, the settings it ran with, and what came back. So this is a
9
+ * projection and not a computation. Nothing here folds money, re-reads a report
10
+ * or decides anything a run did not already decide.
11
+ *
12
+ * **Text out, and no I/O.** Core writes no files, so this returns the bytes and
13
+ * the caller puts them where it likes. That also makes it testable without a
14
+ * disk and usable from a browser, which is where most runs happen.
15
+ *
16
+ * **What it refuses, it refuses loudly.** A case with a missing file is worse
17
+ * than no case: it fails on somebody else's engine and the blame lands on them.
18
+ * So a record that cannot make a whole case does not make a partial one, and a
19
+ * record that cannot make a faithful file does not guess at one: the script it
20
+ * has no text for and the instrument fact its host never stated are both
21
+ * refusals, never a hole and never a default.
22
+ *
23
+ * **Every setting of the run has a place in the case, and the table below says
24
+ * which.** A run once harvested to a case that said nothing about the digit
25
+ * count its money was rounded to, the charge schedule its host supplied or the
26
+ * window its report was about, so a second engine ran under other values and
27
+ * took the blame. `CARRIED` names the file each field of the settings is
28
+ * carried in, the type refuses to compile when the settings gain a field with
29
+ * no row, and a record whose settings hold a field the table does not know is
30
+ * refused by name rather than written into a case that ran under something it
31
+ * does not state.
32
+ */
33
+ import { diagnosticFor } from '../diagnostics/index.js';
34
+ import type { Diagnostic } from '../diagnostics/index.js';
35
+ import { canonicalNumber, canonicalise } from '../emit/index.js';
36
+ import type { Instrument } from '../engine/index.js';
37
+ import type { RecordedBar, RecordedFrame, RunRecord } from './record.js';
38
+ import type { BacktestSettings, Tolerance } from './settings.js';
39
+
40
+ /** The files of one case, keyed by the name `conformance.md` section 2 gives them. */
41
+ export type CaseFiles = Readonly<Record<string, string>>;
42
+
43
+ /** Why a record could not become a case. */
44
+ export interface CaseRefusal {
45
+ readonly ok: false;
46
+ readonly reason: string;
47
+ /**
48
+ * The catalogue code the refusal is filed under, or null.
49
+ *
50
+ * A refusal about a setting of the run is the catalogue's, OS6021, because
51
+ * it is the same refusal the run makes of a setting it cannot be carried out
52
+ * under. The others here are about what the record holds, a text it never
53
+ * carried, a bar list it only points at, a fact its host never stated, and
54
+ * no catalogue entry is about those.
55
+ */
56
+ readonly code: string | null;
57
+ }
58
+
59
+ export interface CaseWritten {
60
+ readonly ok: true;
61
+ readonly files: CaseFiles;
62
+ }
63
+
64
+ export type CaseResult = CaseWritten | CaseRefusal;
65
+
66
+ /** What the caller has to say, because a record cannot know it. */
67
+ export interface CaseIdentity {
68
+ /**
69
+ * The directory path under the suite root, which is also the case id.
70
+ *
71
+ * The caller's because a record does not know where it will live, and
72
+ * `case.json` repeats it on purpose so a directory that is moved without its
73
+ * id being changed is caught by the suite rather than by a confused reader.
74
+ */
75
+ readonly id: string;
76
+ /** One sentence. It is the failure message a runner prints. */
77
+ readonly description: string;
78
+ /** Section 7's categories. Defaults to the one a strategy run belongs to. */
79
+ readonly category?: string;
80
+ }
81
+
82
+ /**
83
+ * The channels a strategy run can be held to.
84
+ *
85
+ * Only what the record carries folded, and only what another engine could
86
+ * produce independently. A case asserts the channels it names and no others, so
87
+ * a change to drawing output cannot break a case about money.
88
+ */
89
+ const STRATEGY_ASSERTS = ['diagnostics', 'orders', 'trades', 'performance'] as const;
90
+
91
+ /**
92
+ * `conformance.md` section 6: the loosest bounds a conformance case may declare.
93
+ *
94
+ * Core reads no page, so the two figures are written here, and
95
+ * `tests/backtest/case-settings.test.ts` reads them out of section 6 and holds
96
+ * these to the page, which is the arrangement `stdlib.md` section 20's figures
97
+ * are under. Past either bound a run may still be a useful comparison; what it
98
+ * is not is a case, so the projection makes no file of it.
99
+ */
100
+ export const TOLERANCE_CAP: { readonly rel: number; readonly abs: number } = {
101
+ rel: 1e-9,
102
+ abs: 1e-12,
103
+ };
104
+
105
+ /**
106
+ * Where each setting of a run is carried in a case, sections 2 and 3.
107
+ *
108
+ * Every field of the settings has a row and the type makes a field without one
109
+ * a compile error, so a setting the record gains cannot be left out of a case
110
+ * by forgetting. `fill` is the simulated destination's policy, and what it
111
+ * decided is the frames, which are input: an engine handed `frames.csv` folds
112
+ * them and fills nothing itself.
113
+ */
114
+ const CARRIED: Readonly<Record<keyof BacktestSettings, string>> = {
115
+ contract: 'instrument.json, and backtest.json for the digit count',
116
+ costs: 'backtest.json',
117
+ range: 'backtest.json',
118
+ inputs: 'settings.json',
119
+ now: 'case.json',
120
+ tolerance: 'case.json',
121
+ fill: 'frames.csv, as the frames it decided',
122
+ };
123
+
124
+ /**
125
+ * Where a refusal about a setting points: nowhere in the script.
126
+ *
127
+ * A run setting is what the host stated before the first bar, and a caret
128
+ * under a line of the strategy would blame the one party that did not choose
129
+ * it. The run's own settings check states the same position for the same reason.
130
+ */
131
+ const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
132
+
133
+ /**
134
+ * The files for one case, or the reason there are none.
135
+ *
136
+ * `now` is written only when the run pinned one. A case that names it when the
137
+ * script never asked pins a clock the run did not depend on, and the next
138
+ * reader has to work out whether it mattered.
139
+ */
140
+ export function caseFilesFrom(record: RunRecord, identity: CaseIdentity): CaseResult {
141
+ if (record.sourceText === null) {
142
+ return refused(
143
+ 'the record carries no source text, so the case would have no script.os: ' +
144
+ 'record it with sourceText, or re-run the script to record one that has it',
145
+ );
146
+ }
147
+ if (record.bars.form !== 'inline') {
148
+ return refused(
149
+ 'the record points at its bars instead of holding them, and a case holds every ' +
150
+ 'byte of its own input: record it with form "inline"',
151
+ );
152
+ }
153
+ if (identity.id.trim() === '') return refused('a case needs an id');
154
+ if (identity.description.trim() === '') {
155
+ return refused('a case needs a one-sentence description, which is its failure message');
156
+ }
157
+ const instrument = instrumentOf(record);
158
+ if (!instrument.ok) return instrument;
159
+ const settings = settingsRefusal(record.settings);
160
+ if (settings !== null) return settings;
161
+
162
+ const files: Record<string, string> = {
163
+ 'case.json': json({
164
+ id: identity.id,
165
+ category: identity.category ?? 'strategy',
166
+ profile: 'strategy',
167
+ languageVersion: Number(record.languageVersion),
168
+ description: identity.description,
169
+ asserts: [...STRATEGY_ASSERTS],
170
+ ...(record.settings.now === null ? {} : { now: record.settings.now }),
171
+ tolerance: record.settings.tolerance,
172
+ }),
173
+ 'script.os': endsWithNewline(record.sourceText),
174
+ 'bars.csv': barsCsv(record.bars.rows),
175
+ // `conformance.md` section 4: performance is a list of one flat object
176
+ // holding the summary statistics and nothing nested. The trades are their
177
+ // own channel and are not repeated inside it, because a figure stated
178
+ // twice in one case is a figure that can disagree with itself. The equity
179
+ // curve, the monthly table and the markers are not written at all: the
180
+ // first two are not conformance channels, being derived from fills and
181
+ // closes the case already fixes, and a marker is a chart output the
182
+ // `markers` channel owns, which a case about money does not assert.
183
+ 'expected.json': json({
184
+ diagnostics: record.diagnostics,
185
+ orders: record.orders,
186
+ trades: record.report.trades,
187
+ performance: [record.report.summary],
188
+ }),
189
+ // Section 2: the record of `host-interface.md` 4.1, which is what the
190
+ // engine was handed and not the money layer's contract. The suite's
191
+ // defaults for an absent file are boring on purpose and are not this run's
192
+ // instrument, so the file is always written.
193
+ 'instrument.json': json(instrument.value),
194
+ // Section 3: what the report was folded under and the script never states.
195
+ // Always written, because every run rounds money to some digit count, and a
196
+ // strategy case that left it out would be run under whatever count a runner
197
+ // assumed. `costs` is null for the declaration's own schedule and `range`
198
+ // carries null for a bound nobody stated, so the file says what the run ran
199
+ // under in every case rather than leaving a default to a runner.
200
+ 'backtest.json': json({
201
+ digits: record.settings.contract.digits,
202
+ costs: record.settings.costs,
203
+ range: record.settings.range,
204
+ }),
205
+ };
206
+
207
+ // Without this the case is unpassable, on every engine including the one that
208
+ // wrote it. `conformance.md` section 3 ends "a case with no `frames.csv` is
209
+ // handed no frames at all", and what `expected.json` asserts through the
210
+ // orders channel is what came of the frames: a status, a cumulative quantity,
211
+ // an average fill price. An engine handed none of them folds nothing and
212
+ // disagrees with every row, and the failure reads as a defect in that engine.
213
+ //
214
+ // Written only when the run had frames, because an empty file and an absent
215
+ // one mean the same thing here and the absent one says it in fewer bytes.
216
+ if (record.frames.length > 0) files['frames.csv'] = framesCsv(record.frames);
217
+
218
+ // Written only when the run had inputs to write. An empty settings.json says
219
+ // "these are the values" about nothing, and section 2 reads an absent one as
220
+ // every input taking its declared default, which is what actually happened.
221
+ if (Object.keys(record.settings.inputs).length > 0) {
222
+ files['settings.json'] = json(record.settings.inputs);
223
+ }
224
+
225
+ return { ok: true, files };
226
+ }
227
+
228
+ /** A refusal about what the record holds, which no catalogue entry is about. */
229
+ function refused(reason: string): CaseRefusal {
230
+ return { ok: false, reason, code: null };
231
+ }
232
+
233
+ /**
234
+ * The instrument record a case can state faithfully, or why there is none.
235
+ *
236
+ * Two refusals, and both are the same refusal: the file `conformance.md`
237
+ * section 2 names is the record of `host-interface.md` 4.1, and a record that
238
+ * cannot produce that record cannot produce the file.
239
+ *
240
+ * A record written before version 3 carries none, because the facts beside
241
+ * the contract were handed to the engine and written down nowhere. And 4.1
242
+ * requires one fact of every host, `hasVolume`, which is the one the engine
243
+ * does not refuse a run without: a run whose host never stated it ran with the
244
+ * flag absent, so a file stating it would hand a second engine a different
245
+ * study than the one the expected output came from, and a file omitting it is
246
+ * not a 4.1 record. The other rules that page states about a record, a session
247
+ * with no timezone to read it in, are refused at load, so a run that happened
248
+ * cannot carry one.
249
+ */
250
+ function instrumentOf(
251
+ record: RunRecord,
252
+ ): { readonly ok: true; readonly value: Instrument } | CaseRefusal {
253
+ if (record.instrument === null) {
254
+ return refused(
255
+ 'the record carries no instrument record, so the case would have no instrument.json: ' +
256
+ 'it was written before record version 3, and re-running the script records one',
257
+ );
258
+ }
259
+ if (typeof record.instrument.hasVolume !== 'boolean') {
260
+ return refused(
261
+ 'the run was handed no hasVolume, which host-interface.md 4.1 requires of every ' +
262
+ 'host, so instrument.json cannot be the record that page defines: run the script ' +
263
+ 'with the instrument facts stated',
264
+ );
265
+ }
266
+ return { ok: true, value: record.instrument };
267
+ }
268
+
269
+ /**
270
+ * Whether every setting the run was carried out under has a place in the case,
271
+ * and whether its tolerance is one the suite accepts.
272
+ *
273
+ * The first question is asked of the record rather than of the type, because a
274
+ * record read back from JSON is whatever was written: a field this projection
275
+ * has no file for is refused with its name, never passed over into a case that
276
+ * then ran under a value it does not state.
277
+ */
278
+ function settingsRefusal(settings: BacktestSettings): CaseRefusal | null {
279
+ for (const key of Object.keys(settings)) {
280
+ if (key in CARRIED) continue;
281
+ return refused(
282
+ `the record's settings carry ${key}, which no file of conformance.md section 2 has a ` +
283
+ 'place for, so a case written from it would run under a setting it does not state: ' +
284
+ 'give the setting a file on that page and a row in this projection first',
285
+ );
286
+ }
287
+ return toleranceRefusal(settings.tolerance);
288
+ }
289
+
290
+ /**
291
+ * A tolerance past the cap is a comparison somebody may find useful and is not
292
+ * a case, `conformance.md` section 6.
293
+ *
294
+ * Refused here, where the case is written, and not only where one is read,
295
+ * because a directory the suite will not accept fails every runner it meets
296
+ * and the blame lands on the engine under test. The run's own settings check
297
+ * refuses a bound with no reason and a bound below zero before a record
298
+ * exists; the cap is the one rule about a tolerance that is the suite's rather
299
+ * than the run's, so it is the one asked here.
300
+ */
301
+ function toleranceRefusal(tolerance: Tolerance): CaseRefusal | null {
302
+ const { abs, rel } = tolerance;
303
+ if (!Number.isFinite(abs) || !Number.isFinite(rel)) {
304
+ return settingRefusal('a bound is not a finite number');
305
+ }
306
+ if (abs <= TOLERANCE_CAP.abs && rel <= TOLERANCE_CAP.rel) return null;
307
+ return settingRefusal(
308
+ `a bound of ${canonicalNumber(abs)} absolute and ${canonicalNumber(rel)} relative is ` +
309
+ 'past the cap conformance.md section 6 puts on a conformance case, ' +
310
+ `${canonicalNumber(TOLERANCE_CAP.abs)} absolute and ${canonicalNumber(TOLERANCE_CAP.rel)} ` +
311
+ 'relative, so this run is a comparison and not a case',
312
+ );
313
+ }
314
+
315
+ /** A refusal about the comparison tolerance, filed under the run's own code for a setting. */
316
+ function settingRefusal(problem: string): CaseRefusal {
317
+ const diagnostic = diagnosticFor('OS6021', NO_POSITION, {
318
+ setting: 'The comparison tolerance',
319
+ problem,
320
+ });
321
+ return { ok: false, reason: diagnostic.message, code: diagnostic.code };
322
+ }
323
+
324
+ /**
325
+ * Through the canonical writer, so a case's bytes are the record's bytes.
326
+ *
327
+ * There is one canonical writer in this repository and this is not a second
328
+ * one: key order and number form are fixed for everybody, which is what lets a
329
+ * case be compared as text at all.
330
+ */
331
+ function json(value: unknown): string {
332
+ return `${canonicalise(value)}\n`;
333
+ }
334
+
335
+ /** `conformance.md` section 3: header, one row per bar, oldest first, `none` for absent. */
336
+ function barsCsv(rows: readonly RecordedBar[]): string {
337
+ const lines = ['time,open,high,low,close,volume'];
338
+ for (const bar of rows) {
339
+ lines.push(
340
+ [bar.time, bar.open, bar.high, bar.low, bar.close, bar.volume].map(cell).join(','),
341
+ );
342
+ }
343
+ return `${lines.join('\n')}\n`;
344
+ }
345
+
346
+ /**
347
+ * `conformance.md` section 3: the fields of a frame, in the order that page names.
348
+ *
349
+ * A projection and not a translation. The record already holds an intent as an
350
+ * ordinal rather than as this engine's own id, for the reason the same section
351
+ * gives: a case cannot know the id another engine minted and must not depend on
352
+ * its spelling.
353
+ *
354
+ * The instant is written on every row, `none` where the destination stated
355
+ * none, through the same cell writer as an absent price. A case whose frames
356
+ * carried instants and whose file did not would assert an `updatedAt` that its
357
+ * own input cannot reproduce, which is what the column closes.
358
+ */
359
+ function framesCsv(frames: readonly RecordedFrame[]): string {
360
+ const lines = ['afterBar,intent,status,filledQty,avgFillPrice,orderRef,text,time'];
361
+ for (const frame of frames) {
362
+ lines.push(
363
+ [
364
+ canonicalNumber(frame.afterBar),
365
+ canonicalNumber(frame.intent),
366
+ frame.status,
367
+ canonicalNumber(frame.filledQty),
368
+ cell(frame.avgFillPrice),
369
+ frame.orderRef ?? '',
370
+ frame.text ?? '',
371
+ cell(frame.time),
372
+ ].join(','),
373
+ );
374
+ }
375
+ return `${lines.join('\n')}\n`;
376
+ }
377
+
378
+ /**
379
+ * One cell, through the one number writer.
380
+ *
381
+ * An absent field is `none` rather than empty, because an empty cell between two
382
+ * commas is indistinguishable from a file somebody's editor trimmed, and a
383
+ * language whose central idea is the absent value cannot be vague about it. A
384
+ * case is compared as text, so the number is written by the rule of
385
+ * `language.md` 5.5 and by the same function that writes every other number in
386
+ * the repository, never by a second spelling that agrees until it does not.
387
+ */
388
+ function cell(value: number | null): string {
389
+ return value === null || !Number.isFinite(value) ? 'none' : canonicalNumber(value);
390
+ }
391
+
392
+ /** A text file ends with a newline, so appending to it never joins two lines. */
393
+ function endsWithNewline(text: string): string {
394
+ return text.endsWith('\n') ? text : `${text}\n`;
395
+ }
@@ -70,6 +70,8 @@ const COMPARED: readonly string[] = [
70
70
  'maxDrawdown',
71
71
  'maxDrawdownPercent',
72
72
  'longestDrawdownBars',
73
+ 'maxRunUp',
74
+ 'maxRunUpPercent',
73
75
  'averageBarsHeld',
74
76
  'barsInMarket',
75
77
  'barCount',
@@ -0,0 +1,161 @@
1
+ /**
2
+ * A case's rows and this engine's frames, and the destination between them.
3
+ *
4
+ * Both directions are here because they are one correspondence: a case names an
5
+ * order by an ordinal and an engine knows it by an id, so `Delivery` reads an
6
+ * ordinal into the id of the intent the run placed, and `framedAs` writes an id
7
+ * back out as the ordinal a case file prints. Split between two files, the two
8
+ * halves of it drift, and a suite whose rows say one thing and whose records
9
+ * say another is one nobody can read.
10
+ *
11
+ * `conformance.md` section 3: `frames.csv` supplies order frames the way
12
+ * `bars.csv` supplies bars, so a strategy case asserts the fold against input
13
+ * the engine did not choose. `simulate.ts` is the other destination this module
14
+ * has, the one that reads a bar and works out what a venue would have said.
15
+ * This one works nothing out. It holds the rows a case supplied, hands over the
16
+ * ones each boundary names, and the whole of its behaviour is the mapping
17
+ * between a row and a frame.
18
+ *
19
+ * **An ordinal is what a case can name, and an id is not.** A row names the nth
20
+ * intent the run placed, because no case can know the id an engine minted, so
21
+ * the intents are counted here in the order the run handed them over and the
22
+ * ordinal is read against that count. A row naming an ordinal the run never
23
+ * placed is delivered all the same, carrying an id no run mints: step 1 of
24
+ * `stdlib.md` 17.8 refuses a frame naming no row of the ledger, and that
25
+ * refusal is the ledger's to make. A destination that dropped the row instead
26
+ * would answer nothing at all where section 3 hands an engine a frame about an
27
+ * order its ledger does not hold, and the case would pass by the frame never
28
+ * having arrived.
29
+ *
30
+ * **What a row does not carry, this does not invent.** `host-interface.md` 7.2
31
+ * lets a frame say which instrument and product the destination booked the
32
+ * order under, and the file has no column for either, so neither is stated and
33
+ * the row keeps what its placement put there. The instant is the one the file
34
+ * does carry: a row states one or states `none`, and one that states none
35
+ * leaves `updatedAt` where the placement put it, which is what the fold does
36
+ * with a frame whose destination stated no instant.
37
+ */
38
+ import type { OrderFrame, OrderIntent, RoutedEffect } from '../engine/index.js';
39
+ import type { RecordedFrame } from './record.js';
40
+
41
+ /** One frame, the boundary it was handed over at, and the row it came from. */
42
+ export interface Delivered {
43
+ readonly frame: OrderFrame;
44
+ readonly afterBar: number;
45
+ /**
46
+ * The row a case supplied, or null where the run's own destination answered
47
+ * the frame and there is no row until the record is written.
48
+ */
49
+ readonly row: RecordedFrame | null;
50
+ }
51
+
52
+ /** Where a run's orders go, and what it is told between two of its bars. */
53
+ export interface Destination {
54
+ /** Step 9: what the strategy decided, on the bar it decided it. */
55
+ route(effect: RoutedEffect, barIndex: number): void;
56
+ /** What this destination hands over at the boundary after `barIndex`. */
57
+ answers(barIndex: number): readonly Delivered[];
58
+ /** Every intent it was handed, in the order it was handed them. */
59
+ readonly intents: readonly OrderIntent[];
60
+ }
61
+
62
+ /**
63
+ * An id no run mints, which a row naming no intent of this run is delivered
64
+ * under.
65
+ *
66
+ * Ids are counted from one by the ledger that mints them, so nothing below one
67
+ * can name a row and the fold refuses the frame by the rule it refuses every
68
+ * other unknown one by. The alternative was an id of a real row chosen by some
69
+ * rule of this file's own, which would fold a case's frame into whichever order
70
+ * happened to be near it.
71
+ */
72
+ const NO_INTENT = -1;
73
+
74
+ /**
75
+ * A destination holding one case's frames, ready at the boundaries they name.
76
+ *
77
+ * Built before the run rather than during it, because the rows are input: what
78
+ * a boundary hands over was decided by whoever wrote the case and not by
79
+ * anything this run does. The one thing it learns as the run goes is which
80
+ * intent an ordinal names, which only the run can say.
81
+ */
82
+ export class Delivery implements Destination {
83
+ readonly intents: OrderIntent[] = [];
84
+ /** The rows of each boundary, in file order, which section 3 fixes as the delivery order. */
85
+ private readonly rows = new Map<number, RecordedFrame[]>();
86
+
87
+ constructor(frames: readonly RecordedFrame[]) {
88
+ for (const row of frames) {
89
+ const at = this.rows.get(row.afterBar);
90
+ if (at === undefined) this.rows.set(row.afterBar, [row]);
91
+ else at.push(row);
92
+ }
93
+ }
94
+
95
+ /** Nothing is decided by an order arriving here: it is counted, and that is all. */
96
+ route(effect: RoutedEffect): void {
97
+ for (const intent of effect.intents) this.intents.push(intent);
98
+ }
99
+
100
+ answers(barIndex: number): readonly Delivered[] {
101
+ const due = this.rows.get(barIndex) ?? [];
102
+ return due.map((row) => ({
103
+ frame: frameOf(row, this.intents[row.intent - 1]),
104
+ afterBar: barIndex,
105
+ row,
106
+ }));
107
+ }
108
+ }
109
+
110
+ /** One row of a case, as the frame `host-interface.md` 7.2 describes. */
111
+ function frameOf(row: RecordedFrame, intent: OrderIntent | undefined): OrderFrame {
112
+ return {
113
+ intentId: intent?.intentId ?? NO_INTENT,
114
+ status: row.status,
115
+ filledQty: row.filledQty,
116
+ avgFillPrice: row.avgFillPrice,
117
+ orderRef: row.orderRef,
118
+ text: row.text,
119
+ time: row.time,
120
+ };
121
+ }
122
+
123
+ /**
124
+ * The ordinal of every intent, which is how a case names one.
125
+ *
126
+ * One, two, three in the order the run placed them, because no engine can know
127
+ * the id another minted and a case that named one would only ever be readable
128
+ * by the engine that wrote it.
129
+ */
130
+ export function ordinalsOf(intents: readonly OrderIntent[]): ReadonlyMap<number, number> {
131
+ const out = new Map<number, number>();
132
+ for (const intent of intents) {
133
+ if (!out.has(intent.intentId)) out.set(intent.intentId, out.size + 1);
134
+ }
135
+ return out;
136
+ }
137
+
138
+ /**
139
+ * One delivered frame, in the columns a case file prints.
140
+ *
141
+ * The instant travels with it. `stdlib.md` 17.7 folds `updatedAt` from a
142
+ * frame's `time`, so a driver that dropped the field here wrote a record whose
143
+ * ledger no engine could fold from the record's own frames: it would have
144
+ * nothing to move that field to and would leave it at `placedAt`.
145
+ */
146
+ export function framedAs(
147
+ frame: OrderFrame,
148
+ afterBar: number,
149
+ ordinals: ReadonlyMap<number, number>,
150
+ ): RecordedFrame {
151
+ return {
152
+ afterBar,
153
+ intent: ordinals.get(frame.intentId) ?? 0,
154
+ status: frame.status,
155
+ filledQty: frame.filledQty,
156
+ avgFillPrice: frame.avgFillPrice ?? null,
157
+ orderRef: frame.orderRef ?? null,
158
+ text: frame.text ?? null,
159
+ time: frame.time ?? null,
160
+ };
161
+ }