openalgo-script 0.2.0 → 0.5.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 (357) hide show
  1. package/CHANGELOG.md +1198 -0
  2. package/README.md +127 -40
  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 +83 -16
  10. package/dist/adapters/charts/run.js.map +1 -1
  11. package/dist/adapters/charts/surfaces.d.ts +17 -0
  12. package/dist/adapters/charts/surfaces.d.ts.map +1 -1
  13. package/dist/adapters/charts/tables.js +82 -0
  14. package/dist/adapters/charts/tables.js.map +1 -1
  15. package/dist/adapters/charts/venue.d.ts +73 -0
  16. package/dist/adapters/charts/venue.d.ts.map +1 -0
  17. package/dist/adapters/charts/venue.js +104 -0
  18. package/dist/adapters/charts/venue.js.map +1 -0
  19. package/dist/adapters/codemirror/commands.d.ts +14 -0
  20. package/dist/adapters/codemirror/commands.d.ts.map +1 -0
  21. package/dist/adapters/codemirror/commands.js +52 -0
  22. package/dist/adapters/codemirror/commands.js.map +1 -0
  23. package/dist/adapters/codemirror/completion.d.ts +15 -0
  24. package/dist/adapters/codemirror/completion.d.ts.map +1 -0
  25. package/dist/adapters/codemirror/completion.js +64 -0
  26. package/dist/adapters/codemirror/completion.js.map +1 -0
  27. package/dist/adapters/codemirror/contract.d.ts +156 -0
  28. package/dist/adapters/codemirror/contract.d.ts.map +1 -0
  29. package/dist/adapters/codemirror/contract.js +42 -0
  30. package/dist/adapters/codemirror/contract.js.map +1 -0
  31. package/dist/adapters/codemirror/index.d.ts +43 -0
  32. package/dist/adapters/codemirror/index.d.ts.map +1 -0
  33. package/dist/adapters/codemirror/index.js +8 -0
  34. package/dist/adapters/codemirror/index.js.map +1 -0
  35. package/dist/adapters/codemirror/lint.d.ts +17 -0
  36. package/dist/adapters/codemirror/lint.d.ts.map +1 -0
  37. package/dist/adapters/codemirror/lint.js +62 -0
  38. package/dist/adapters/codemirror/lint.js.map +1 -0
  39. package/dist/adapters/codemirror/positions.d.ts +16 -0
  40. package/dist/adapters/codemirror/positions.d.ts.map +1 -0
  41. package/dist/adapters/codemirror/positions.js +65 -0
  42. package/dist/adapters/codemirror/positions.js.map +1 -0
  43. package/dist/adapters/codemirror/stream.d.ts +23 -0
  44. package/dist/adapters/codemirror/stream.d.ts.map +1 -0
  45. package/dist/adapters/codemirror/stream.js +70 -0
  46. package/dist/adapters/codemirror/stream.js.map +1 -0
  47. package/dist/adapters/codemirror/tokens.d.ts +44 -0
  48. package/dist/adapters/codemirror/tokens.d.ts.map +1 -0
  49. package/dist/adapters/codemirror/tokens.js +34 -0
  50. package/dist/adapters/codemirror/tokens.js.map +1 -0
  51. package/dist/adapters/codemirror/tooltips.d.ts +35 -0
  52. package/dist/adapters/codemirror/tooltips.d.ts.map +1 -0
  53. package/dist/adapters/codemirror/tooltips.js +108 -0
  54. package/dist/adapters/codemirror/tooltips.js.map +1 -0
  55. package/dist/core/accounting/analysis.d.ts +111 -0
  56. package/dist/core/accounting/analysis.d.ts.map +1 -0
  57. package/dist/core/accounting/analysis.js +123 -0
  58. package/dist/core/accounting/analysis.js.map +1 -0
  59. package/dist/core/accounting/charges.d.ts +136 -0
  60. package/dist/core/accounting/charges.d.ts.map +1 -0
  61. package/dist/core/accounting/charges.js +362 -0
  62. package/dist/core/accounting/charges.js.map +1 -0
  63. package/dist/core/accounting/equity.d.ts +191 -0
  64. package/dist/core/accounting/equity.d.ts.map +1 -0
  65. package/dist/core/accounting/equity.js +167 -0
  66. package/dist/core/accounting/equity.js.map +1 -0
  67. package/dist/core/accounting/index.d.ts +46 -0
  68. package/dist/core/accounting/index.d.ts.map +1 -0
  69. package/dist/core/accounting/index.js +37 -0
  70. package/dist/core/accounting/index.js.map +1 -0
  71. package/dist/core/accounting/markers.d.ts +43 -0
  72. package/dist/core/accounting/markers.d.ts.map +1 -0
  73. package/dist/core/accounting/markers.js +64 -0
  74. package/dist/core/accounting/markers.js.map +1 -0
  75. package/dist/core/accounting/monthly.d.ts +52 -0
  76. package/dist/core/accounting/monthly.d.ts.map +1 -0
  77. package/dist/core/accounting/monthly.js +99 -0
  78. package/dist/core/accounting/monthly.js.map +1 -0
  79. package/dist/core/accounting/report.d.ts +41 -0
  80. package/dist/core/accounting/report.d.ts.map +1 -0
  81. package/dist/core/accounting/report.js +65 -0
  82. package/dist/core/accounting/report.js.map +1 -0
  83. package/dist/core/accounting/shapes.d.ts +70 -0
  84. package/dist/core/accounting/shapes.d.ts.map +1 -0
  85. package/dist/core/accounting/shapes.js +21 -0
  86. package/dist/core/accounting/shapes.js.map +1 -0
  87. package/dist/core/accounting/statistics.d.ts +87 -0
  88. package/dist/core/accounting/statistics.d.ts.map +1 -0
  89. package/dist/core/accounting/statistics.js +268 -0
  90. package/dist/core/accounting/statistics.js.map +1 -0
  91. package/dist/core/accounting/trades.d.ts +118 -0
  92. package/dist/core/accounting/trades.d.ts.map +1 -0
  93. package/dist/core/accounting/trades.js +186 -0
  94. package/dist/core/accounting/trades.js.map +1 -0
  95. package/dist/core/backtest/case.d.ts +60 -0
  96. package/dist/core/backtest/case.d.ts.map +1 -0
  97. package/dist/core/backtest/case.js +319 -0
  98. package/dist/core/backtest/case.js.map +1 -0
  99. package/dist/core/backtest/compare.d.ts +38 -0
  100. package/dist/core/backtest/compare.d.ts.map +1 -0
  101. package/dist/core/backtest/compare.js +191 -0
  102. package/dist/core/backtest/compare.js.map +1 -0
  103. package/dist/core/backtest/declaration.d.ts +52 -0
  104. package/dist/core/backtest/declaration.d.ts.map +1 -0
  105. package/dist/core/backtest/declaration.js +48 -0
  106. package/dist/core/backtest/declaration.js.map +1 -0
  107. package/dist/core/backtest/deliver.d.ts +93 -0
  108. package/dist/core/backtest/deliver.d.ts.map +1 -0
  109. package/dist/core/backtest/deliver.js +94 -0
  110. package/dist/core/backtest/deliver.js.map +1 -0
  111. package/dist/core/backtest/drive.d.ts +87 -0
  112. package/dist/core/backtest/drive.d.ts.map +1 -0
  113. package/dist/core/backtest/drive.js +312 -0
  114. package/dist/core/backtest/drive.js.map +1 -0
  115. package/dist/core/backtest/index.d.ts +58 -0
  116. package/dist/core/backtest/index.d.ts.map +1 -0
  117. package/dist/core/backtest/index.js +47 -0
  118. package/dist/core/backtest/index.js.map +1 -0
  119. package/dist/core/backtest/range.d.ts +84 -0
  120. package/dist/core/backtest/range.d.ts.map +1 -0
  121. package/dist/core/backtest/range.js +90 -0
  122. package/dist/core/backtest/range.js.map +1 -0
  123. package/dist/core/backtest/record.d.ts +299 -0
  124. package/dist/core/backtest/record.d.ts.map +1 -0
  125. package/dist/core/backtest/record.js +243 -0
  126. package/dist/core/backtest/record.js.map +1 -0
  127. package/dist/core/backtest/replay.d.ts +48 -0
  128. package/dist/core/backtest/replay.d.ts.map +1 -0
  129. package/dist/core/backtest/replay.js +137 -0
  130. package/dist/core/backtest/replay.js.map +1 -0
  131. package/dist/core/backtest/resting.d.ts +62 -0
  132. package/dist/core/backtest/resting.d.ts.map +1 -0
  133. package/dist/core/backtest/resting.js +59 -0
  134. package/dist/core/backtest/resting.js.map +1 -0
  135. package/dist/core/backtest/settings.d.ts +117 -0
  136. package/dist/core/backtest/settings.d.ts.map +1 -0
  137. package/dist/core/backtest/settings.js +207 -0
  138. package/dist/core/backtest/settings.js.map +1 -0
  139. package/dist/core/backtest/simulate.d.ts +258 -0
  140. package/dist/core/backtest/simulate.d.ts.map +1 -0
  141. package/dist/core/backtest/simulate.js +335 -0
  142. package/dist/core/backtest/simulate.js.map +1 -0
  143. package/dist/core/catalogue/catalogue.generated.d.ts +66 -0
  144. package/dist/core/catalogue/catalogue.generated.d.ts.map +1 -1
  145. package/dist/core/catalogue/catalogue.generated.js +6 -0
  146. package/dist/core/catalogue/catalogue.generated.js.map +1 -1
  147. package/dist/core/catalogue/values.generated.d.ts +24 -0
  148. package/dist/core/catalogue/values.generated.d.ts.map +1 -1
  149. package/dist/core/check/index.d.ts +2 -1
  150. package/dist/core/check/index.d.ts.map +1 -1
  151. package/dist/core/check/index.js +1 -1
  152. package/dist/core/check/index.js.map +1 -1
  153. package/dist/core/check/library-orders.js +2 -2
  154. package/dist/core/check/library-orders.js.map +1 -1
  155. package/dist/core/check/library-prose.generated.d.ts +16 -0
  156. package/dist/core/check/library-prose.generated.d.ts.map +1 -0
  157. package/dist/core/check/library-prose.generated.js +353 -0
  158. package/dist/core/check/library-prose.generated.js.map +1 -0
  159. package/dist/core/check/surface.d.ts +24 -0
  160. package/dist/core/check/surface.d.ts.map +1 -1
  161. package/dist/core/check/surface.js +29 -0
  162. package/dist/core/check/surface.js.map +1 -1
  163. package/dist/core/emit/canonical.d.ts +22 -8
  164. package/dist/core/emit/canonical.d.ts.map +1 -1
  165. package/dist/core/emit/canonical.js +67 -6
  166. package/dist/core/emit/canonical.js.map +1 -1
  167. package/dist/core/emit/defaults.d.ts +35 -16
  168. package/dist/core/emit/defaults.d.ts.map +1 -1
  169. package/dist/core/emit/defaults.js +66 -0
  170. package/dist/core/emit/defaults.js.map +1 -1
  171. package/dist/core/emit/index.d.ts +2 -0
  172. package/dist/core/emit/index.d.ts.map +1 -1
  173. package/dist/core/emit/index.js +2 -0
  174. package/dist/core/emit/index.js.map +1 -1
  175. package/dist/core/engine/arithmetic.d.ts +6 -25
  176. package/dist/core/engine/arithmetic.d.ts.map +1 -1
  177. package/dist/core/engine/arithmetic.js +48 -3
  178. package/dist/core/engine/arithmetic.js.map +1 -1
  179. package/dist/core/engine/index.d.ts +2 -2
  180. package/dist/core/engine/index.d.ts.map +1 -1
  181. package/dist/core/engine/index.js +2 -2
  182. package/dist/core/engine/index.js.map +1 -1
  183. package/dist/core/engine/library/arrays.d.ts.map +1 -1
  184. package/dist/core/engine/library/arrays.js +8 -2
  185. package/dist/core/engine/library/arrays.js.map +1 -1
  186. package/dist/core/engine/library/code-points.d.ts +40 -0
  187. package/dist/core/engine/library/code-points.d.ts.map +1 -0
  188. package/dist/core/engine/library/code-points.js +74 -0
  189. package/dist/core/engine/library/code-points.js.map +1 -0
  190. package/dist/core/engine/library/index.d.ts +5 -0
  191. package/dist/core/engine/library/index.d.ts.map +1 -1
  192. package/dist/core/engine/library/index.js +5 -0
  193. package/dist/core/engine/library/index.js.map +1 -1
  194. package/dist/core/engine/library/text.d.ts.map +1 -1
  195. package/dist/core/engine/library/text.js +51 -34
  196. package/dist/core/engine/library/text.js.map +1 -1
  197. package/dist/core/engine/load.d.ts +35 -1
  198. package/dist/core/engine/load.d.ts.map +1 -1
  199. package/dist/core/engine/load.js +63 -0
  200. package/dist/core/engine/load.js.map +1 -1
  201. package/dist/core/engine/verify-tables.d.ts.map +1 -1
  202. package/dist/core/engine/verify-tables.js +41 -0
  203. package/dist/core/engine/verify-tables.js.map +1 -1
  204. package/dist/core/index.d.ts +43 -4
  205. package/dist/core/index.d.ts.map +1 -1
  206. package/dist/core/index.js +23 -2
  207. package/dist/core/index.js.map +1 -1
  208. package/dist/core/stdlib/index.d.ts +1 -1
  209. package/dist/core/stdlib/index.d.ts.map +1 -1
  210. package/dist/core/stdlib/index.js +1 -1
  211. package/dist/core/stdlib/index.js.map +1 -1
  212. package/dist/core/stdlib/maths/index.d.ts +1 -1
  213. package/dist/core/stdlib/maths/index.d.ts.map +1 -1
  214. package/dist/core/stdlib/maths/index.js +1 -1
  215. package/dist/core/stdlib/maths/index.js.map +1 -1
  216. package/dist/core/stdlib/maths/rounding.d.ts +5 -0
  217. package/dist/core/stdlib/maths/rounding.d.ts.map +1 -1
  218. package/dist/core/stdlib/maths/rounding.js +19 -1
  219. package/dist/core/stdlib/maths/rounding.js.map +1 -1
  220. package/dist/core/version/version.generated.d.ts +1 -1
  221. package/dist/core/version/version.generated.js +1 -1
  222. package/dist/editor/complete.d.ts +40 -0
  223. package/dist/editor/complete.d.ts.map +1 -0
  224. package/dist/editor/complete.js +206 -0
  225. package/dist/editor/complete.js.map +1 -0
  226. package/dist/editor/diagnose.d.ts +18 -0
  227. package/dist/editor/diagnose.d.ts.map +1 -0
  228. package/dist/editor/diagnose.js +70 -0
  229. package/dist/editor/diagnose.js.map +1 -0
  230. package/dist/editor/format.d.ts +11 -0
  231. package/dist/editor/format.d.ts.map +1 -0
  232. package/dist/editor/format.js +50 -0
  233. package/dist/editor/format.js.map +1 -0
  234. package/dist/editor/highlight.d.ts +28 -0
  235. package/dist/editor/highlight.d.ts.map +1 -0
  236. package/dist/editor/highlight.js +65 -0
  237. package/dist/editor/highlight.js.map +1 -0
  238. package/dist/editor/hover.d.ts +40 -0
  239. package/dist/editor/hover.d.ts.map +1 -0
  240. package/dist/editor/hover.js +147 -0
  241. package/dist/editor/hover.js.map +1 -0
  242. package/dist/editor/index.d.ts +60 -0
  243. package/dist/editor/index.d.ts.map +1 -0
  244. package/dist/editor/index.js +7 -0
  245. package/dist/editor/index.js.map +1 -0
  246. package/dist/editor/kinds.d.ts +27 -0
  247. package/dist/editor/kinds.d.ts.map +1 -0
  248. package/dist/editor/kinds.js +118 -0
  249. package/dist/editor/kinds.js.map +1 -0
  250. package/dist/editor/layout.d.ts +47 -0
  251. package/dist/editor/layout.d.ts.map +1 -0
  252. package/dist/editor/layout.js +135 -0
  253. package/dist/editor/layout.js.map +1 -0
  254. package/dist/editor/manifest.d.ts +47 -0
  255. package/dist/editor/manifest.d.ts.map +1 -0
  256. package/dist/editor/manifest.js +93 -0
  257. package/dist/editor/manifest.js.map +1 -0
  258. package/dist/editor/reading.d.ts +41 -0
  259. package/dist/editor/reading.d.ts.map +1 -0
  260. package/dist/editor/reading.js +51 -0
  261. package/dist/editor/reading.js.map +1 -0
  262. package/dist/editor/scan.d.ts +26 -0
  263. package/dist/editor/scan.d.ts.map +1 -0
  264. package/dist/editor/scan.js +139 -0
  265. package/dist/editor/scan.js.map +1 -0
  266. package/dist/editor/scope.d.ts +19 -0
  267. package/dist/editor/scope.d.ts.map +1 -0
  268. package/dist/editor/scope.js +99 -0
  269. package/dist/editor/scope.js.map +1 -0
  270. package/dist/editor/signature.d.ts +38 -0
  271. package/dist/editor/signature.d.ts.map +1 -0
  272. package/dist/editor/signature.js +112 -0
  273. package/dist/editor/signature.js.map +1 -0
  274. package/dist/editor/site.d.ts +46 -0
  275. package/dist/editor/site.d.ts.map +1 -0
  276. package/dist/editor/site.js +184 -0
  277. package/dist/editor/site.js.map +1 -0
  278. package/dist/editor/spacing.d.ts +29 -0
  279. package/dist/editor/spacing.d.ts.map +1 -0
  280. package/dist/editor/spacing.js +116 -0
  281. package/dist/editor/spacing.js.map +1 -0
  282. package/package.json +42 -3
  283. package/spec/README.md +2 -1
  284. package/spec/errors.json +141 -1
  285. package/src/adapters/charts/driving.ts +109 -0
  286. package/src/adapters/charts/run.ts +120 -28
  287. package/src/adapters/charts/surfaces.ts +17 -0
  288. package/src/adapters/charts/tables.ts +88 -0
  289. package/src/adapters/charts/venue.ts +132 -0
  290. package/src/adapters/codemirror/commands.ts +53 -0
  291. package/src/adapters/codemirror/completion.ts +74 -0
  292. package/src/adapters/codemirror/contract.ts +156 -0
  293. package/src/adapters/codemirror/index.ts +66 -0
  294. package/src/adapters/codemirror/lint.ts +66 -0
  295. package/src/adapters/codemirror/positions.ts +79 -0
  296. package/src/adapters/codemirror/stream.ts +87 -0
  297. package/src/adapters/codemirror/tokens.ts +64 -0
  298. package/src/adapters/codemirror/tooltips.ts +113 -0
  299. package/src/core/accounting/analysis.ts +188 -0
  300. package/src/core/accounting/charges.ts +452 -0
  301. package/src/core/accounting/equity.ts +314 -0
  302. package/src/core/accounting/index.ts +51 -0
  303. package/src/core/accounting/markers.ts +95 -0
  304. package/src/core/accounting/monthly.ts +137 -0
  305. package/src/core/accounting/report.ts +94 -0
  306. package/src/core/accounting/shapes.ts +73 -0
  307. package/src/core/accounting/statistics.ts +387 -0
  308. package/src/core/accounting/trades.ts +313 -0
  309. package/src/core/backtest/case.ts +395 -0
  310. package/src/core/backtest/compare.ts +246 -0
  311. package/src/core/backtest/declaration.ts +97 -0
  312. package/src/core/backtest/deliver.ts +161 -0
  313. package/src/core/backtest/drive.ts +468 -0
  314. package/src/core/backtest/index.ts +64 -0
  315. package/src/core/backtest/range.ts +137 -0
  316. package/src/core/backtest/record.ts +470 -0
  317. package/src/core/backtest/replay.ts +168 -0
  318. package/src/core/backtest/resting.ts +125 -0
  319. package/src/core/backtest/settings.ts +280 -0
  320. package/src/core/backtest/simulate.ts +499 -0
  321. package/src/core/catalogue/catalogue.generated.ts +6 -0
  322. package/src/core/catalogue/values.generated.ts +6 -0
  323. package/src/core/check/index.ts +3 -0
  324. package/src/core/check/library-orders.ts +2 -2
  325. package/src/core/check/library-prose.generated.ts +367 -0
  326. package/src/core/check/surface.ts +34 -0
  327. package/src/core/emit/canonical.ts +67 -9
  328. package/src/core/emit/defaults.ts +70 -0
  329. package/src/core/emit/index.ts +2 -0
  330. package/src/core/engine/arithmetic.ts +23 -3
  331. package/src/core/engine/index.ts +2 -2
  332. package/src/core/engine/library/arrays.ts +8 -2
  333. package/src/core/engine/library/code-points.ts +73 -0
  334. package/src/core/engine/library/index.ts +6 -0
  335. package/src/core/engine/library/text.ts +53 -35
  336. package/src/core/engine/load.ts +84 -2
  337. package/src/core/engine/verify-tables.ts +43 -0
  338. package/src/core/index.ts +100 -2
  339. package/src/core/stdlib/index.ts +1 -0
  340. package/src/core/stdlib/maths/index.ts +1 -0
  341. package/src/core/stdlib/maths/rounding.ts +23 -1
  342. package/src/core/version/version.generated.ts +1 -1
  343. package/src/editor/complete.ts +266 -0
  344. package/src/editor/diagnose.ts +71 -0
  345. package/src/editor/format.ts +85 -0
  346. package/src/editor/highlight.ts +110 -0
  347. package/src/editor/hover.ts +199 -0
  348. package/src/editor/index.ts +65 -0
  349. package/src/editor/kinds.ts +157 -0
  350. package/src/editor/layout.ts +189 -0
  351. package/src/editor/manifest.ts +119 -0
  352. package/src/editor/reading.ts +69 -0
  353. package/src/editor/scan.ts +162 -0
  354. package/src/editor/scope.ts +122 -0
  355. package/src/editor/signature.ts +150 -0
  356. package/src/editor/site.ts +233 -0
  357. package/src/editor/spacing.ts +132 -0
@@ -0,0 +1,125 @@
1
+ /**
2
+ * One resting order against one bar: did it trade, and at what price.
3
+ *
4
+ * **A bar is four prices and no path.** Whether the high came before the low is
5
+ * not in the data, so every rule here is one that does not need to know, and
6
+ * the places where knowing would matter are decided against the strategy rather
7
+ * than guessed in its favour. That is the whole discipline of this file: a
8
+ * backtest that is generous about fills is a backtest that reports money the
9
+ * market never offered.
10
+ *
11
+ * Three rules, each with the case it exists for:
12
+ *
13
+ * - **Touched is not traded through.** A limit resting exactly at the low of a
14
+ * bar may or may not have been filled: the print happened, somebody was
15
+ * filled at that price, and whether it was this order depends on a queue no
16
+ * bar records. The default is that it was not (`limitNeedsThrough`), and a
17
+ * host that has decided otherwise for its own market says so in the policy.
18
+ * - **A gap fills at the open, not at the level.** A stop triggered by a bar
19
+ * that opened beyond it was not filled at its trigger, and a backtest that
20
+ * says it was is reporting the one price that was never available. The open
21
+ * is the first price there was.
22
+ * - **A limit fills at its own price or better, and never worse.** Where the
23
+ * bar opened already through a limit, the open is the better price and the
24
+ * fill is there; a limit is never worsened by slippage, because a limit that
25
+ * is worsened is not a limit.
26
+ *
27
+ * A stop limit is both, in one bar and in that order: the trigger has to be
28
+ * reached and the limit has to be traded through, and where only the trigger is
29
+ * reached the order becomes a resting limit and waits.
30
+ */
31
+ import type { OrderSide, OrderType } from '../engine/index.js';
32
+ import type { FillPolicy } from './settings.js';
33
+ import type { RecordedBar } from './record.js';
34
+
35
+ /** A resting order, as the venue holds one. */
36
+ export interface RestingOrder {
37
+ readonly side: OrderSide;
38
+ readonly type: OrderType;
39
+ /** The price a limit may not be worse than, absent on a plain stop. */
40
+ readonly limit: number | null;
41
+ /** The price a stop is triggered at, absent on a plain limit. */
42
+ readonly trigger: number | null;
43
+ }
44
+
45
+ /** What one bar did to one resting order. */
46
+ export type RestOutcome =
47
+ | { readonly filled: false; readonly triggered: boolean }
48
+ | {
49
+ readonly filled: true;
50
+ readonly price: number;
51
+ /** Whether the price is the bar's open, which is a gap through the level. */
52
+ readonly atOpen: boolean;
53
+ /** Whether slippage applies, which a limit never takes. */
54
+ readonly slips: boolean;
55
+ };
56
+
57
+ const NOTHING: RestOutcome = { filled: false, triggered: false };
58
+
59
+ /**
60
+ * One order against one bar.
61
+ *
62
+ * A bar missing any of the four prices decides nothing: an incomplete bar is
63
+ * not evidence that a level was reached and it is not evidence that it was not.
64
+ */
65
+ export function testResting(
66
+ order: RestingOrder,
67
+ bar: RecordedBar,
68
+ policy: FillPolicy,
69
+ ): RestOutcome {
70
+ const open = bar.open;
71
+ const high = bar.high;
72
+ const low = bar.low;
73
+ if (open === null || high === null || low === null || bar.close === null) return NOTHING;
74
+
75
+ if (order.type === 'limit') return limitAgainst(order, open, high, low, policy);
76
+
77
+ const trigger = order.trigger;
78
+ if (trigger === null) return NOTHING;
79
+ const reached = order.side === 'buy' ? high >= trigger : low <= trigger;
80
+ if (!reached) return NOTHING;
81
+
82
+ if (order.type === 'stop') {
83
+ const gapped = order.side === 'buy' ? open >= trigger : open <= trigger;
84
+ const price = gapped && policy.stopFillsAtOpenOnGap ? open : trigger;
85
+ return { filled: true, price, atOpen: gapped && policy.stopFillsAtOpenOnGap, slips: true };
86
+ }
87
+
88
+ // A stop limit that triggered is a limit for the rest of this bar. Where the
89
+ // limit is not traded through it keeps resting, and the caller is told the
90
+ // trigger was reached so that the order rests as a limit from here on.
91
+ const asLimit = limitAgainst(order, open, high, low, policy);
92
+ return asLimit.filled ? asLimit : { filled: false, triggered: true };
93
+ }
94
+
95
+ /**
96
+ * A limit against one bar.
97
+ *
98
+ * `limitNeedsThrough` is the difference between a strict comparison and a loose
99
+ * one, and it is the only knob in this file that changes a fill into no fill
100
+ * rather than one price into another.
101
+ */
102
+ function limitAgainst(
103
+ order: RestingOrder,
104
+ open: number,
105
+ high: number,
106
+ low: number,
107
+ policy: FillPolicy,
108
+ ): RestOutcome {
109
+ const limit = order.limit;
110
+ if (limit === null) return NOTHING;
111
+
112
+ if (order.side === 'buy') {
113
+ const traded = policy.limitNeedsThrough ? low < limit : low <= limit;
114
+ if (!traded) return NOTHING;
115
+ // The open already below the limit is the better price, and it is the first
116
+ // price the bar had.
117
+ const gapped = open < limit;
118
+ return { filled: true, price: gapped ? open : limit, atOpen: gapped, slips: false };
119
+ }
120
+
121
+ const traded = policy.limitNeedsThrough ? high > limit : high >= limit;
122
+ if (!traded) return NOTHING;
123
+ const gapped = open > limit;
124
+ return { filled: true, price: gapped ? open : limit, atOpen: gapped, slips: false };
125
+ }
@@ -0,0 +1,280 @@
1
+ /**
2
+ * What the host chose, which is the half of a run that is not the program.
3
+ *
4
+ * **The dividing rule, and everything in this module follows from it: the
5
+ * record stores what the host chose, and what the program states is stored
6
+ * once, as the program.** So there is no capital here, no slippage and no
7
+ * commission: the declaration states those and the declaration is in the
8
+ * record. A charge schedule the host supplied is here because the host supplied
9
+ * it; a schedule derived from the declaration is derived again on replay.
10
+ *
11
+ * **The fill policy carries its own version** precisely so that a record made
12
+ * before the policy changed replays as it ran, rather than being quietly re-run
13
+ * under today's rules and reported as the same study. A policy that could not
14
+ * say which revision it was would make every stored run a claim about whichever
15
+ * engine happened to read it last.
16
+ *
17
+ * `fillOn` is deliberately not here: it is the declaration's, and the
18
+ * declaration is the program.
19
+ *
20
+ * Two refusals live in this module when the behaviour lands. A setting the run
21
+ * cannot be carried out under is OS6021, and a supplied schedule beside a
22
+ * declared commission that is not the default is OS6023, because two cost
23
+ * models stated at once is a number nobody can explain afterwards.
24
+ */
25
+ import { scheduleFromDeclaration, scheduleProblem } from '../accounting/index.js';
26
+ import type { ChargeSchedule, Contract } from '../accounting/index.js';
27
+ import { diagnosticFor } from '../diagnostics/index.js';
28
+ import type { Diagnostic } from '../diagnostics/index.js';
29
+ import type { Value } from '../engine/index.js';
30
+ import type { RunDeclaration } from './declaration.js';
31
+
32
+ /**
33
+ * Where a refusal about a setting points.
34
+ *
35
+ * Nowhere in the script. A run setting is what the host stated before the first
36
+ * bar, and a caret under a line of the strategy would blame the one party that
37
+ * did not choose it. The money layer states the same position for the same
38
+ * reason.
39
+ */
40
+ const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
41
+
42
+ /** The window of the supplied bars a report is about. */
43
+ export interface DateRange {
44
+ /** Inclusive, UTC ms; null means the first bar supplied. */
45
+ readonly from: number | null;
46
+ /** Inclusive; null means the last. */
47
+ readonly to: number | null;
48
+ }
49
+
50
+ /** How a resting order is decided against a bar, and who holds a level. */
51
+ export interface FillPolicy {
52
+ /** A limit fills only when the bar traded through it. */
53
+ readonly limitNeedsThrough: boolean;
54
+ readonly stopFillsAtOpenOnGap: boolean;
55
+ /** Who holds a bracket's levels; 'destination' until 17.9 exists. */
56
+ readonly levels: 'destination' | 'engine';
57
+ /** This policy's own revision, so an old record replays as it ran. */
58
+ readonly version: number;
59
+ }
60
+
61
+ /**
62
+ * How closely two numbers have to agree, and why they are allowed not to.
63
+ *
64
+ * Exact by default. A bound that is not zero without a reason beside it is
65
+ * refused, because a tolerance with no reason is a failed comparison somebody
66
+ * turned off. The reason a tolerance exists at all is a second implementation,
67
+ * never this one's own re-run: a rerun of the same record on this engine is
68
+ * bit-identical or it is a defect.
69
+ */
70
+ export interface Tolerance {
71
+ readonly abs: number;
72
+ readonly rel: number;
73
+ /** Required whenever either bound is non-zero. */
74
+ readonly reason: string | null;
75
+ }
76
+
77
+ /** Everything the host decided about one run. */
78
+ export interface BacktestSettings {
79
+ readonly range: DateRange;
80
+ readonly contract: Contract;
81
+ /** Null derives one from the declaration. */
82
+ readonly costs: ChargeSchedule | null;
83
+ readonly fill: FillPolicy;
84
+ readonly inputs: Readonly<Record<string, Value>>;
85
+ readonly now: number | null;
86
+ readonly tolerance: Tolerance;
87
+ }
88
+
89
+ /**
90
+ * How a resting order is decided when the host states nothing.
91
+ *
92
+ * Conservative on both counts, because a backtest that is wrong is wrong in the
93
+ * strategy's favour by default: a limit is only filled where the bar traded
94
+ * through it, so an order resting exactly at the extreme of a bar is not
95
+ * credited with a fill nobody can prove happened, and a stop that gapped is
96
+ * filled at the open rather than at its trigger, which is the price a trader
97
+ * would actually have been given.
98
+ *
99
+ * `levels` is `destination` because there is nowhere else for it to be: a
100
+ * bracket reaches a destination as a protective instruction attached to a tag
101
+ * and the engine holds no level of its own. The other spelling exists so that a
102
+ * record made today says which of the two it ran under.
103
+ *
104
+ * `version` is this policy's own revision. It travels in the record so that a
105
+ * run stored before the rules changed replays as it ran rather than being
106
+ * quietly re-decided under today's.
107
+ */
108
+ export const DEFAULT_FILL: FillPolicy = {
109
+ limitNeedsThrough: true,
110
+ stopFillsAtOpenOnGap: true,
111
+ levels: 'destination',
112
+ version: 1,
113
+ };
114
+
115
+ /** Exact, which is what a comparison is until somebody writes down why it is not. */
116
+ export const EXACT: Tolerance = { abs: 0, rel: 0, reason: null };
117
+
118
+ /** The whole window of whatever bars were supplied. */
119
+ export const WHOLE_RANGE: DateRange = { from: null, to: null };
120
+
121
+ /**
122
+ * The settings a run takes when the host states only the contract.
123
+ *
124
+ * Every default here is the absence of a choice rather than a choice made on
125
+ * the host's behalf: the whole of the bars supplied, no schedule, exact
126
+ * comparison, no inputs overridden and no clock. A host that wants any of it
127
+ * different says so, and what it said is what the record stores.
128
+ *
129
+ * **The contract is the first argument and cannot be in the second.** The
130
+ * second was `Partial<BacktestSettings>`, which names a `contract` field that
131
+ * this function then ignored in favour of the positional one: a caller passing
132
+ * a contract there silently got the other, and a test written that way passed
133
+ * while proving nothing. Excluding the field makes it a compiler error at the
134
+ * call rather than a wrong answer at the end of a run.
135
+ */
136
+ export function settingsFor(
137
+ contract: Contract,
138
+ chosen: Omit<Partial<BacktestSettings>, 'contract'> = {},
139
+ ): BacktestSettings {
140
+ return {
141
+ range: chosen.range ?? WHOLE_RANGE,
142
+ contract,
143
+ costs: chosen.costs ?? null,
144
+ fill: chosen.fill ?? DEFAULT_FILL,
145
+ inputs: chosen.inputs ?? {},
146
+ now: chosen.now ?? null,
147
+ tolerance: chosen.tolerance ?? EXACT,
148
+ };
149
+ }
150
+
151
+ /**
152
+ * What a run cannot be carried out under, before its first bar.
153
+ *
154
+ * Four questions, and every one of them is a figure nobody could explain
155
+ * afterwards rather than a tidiness rule:
156
+ *
157
+ * - **Two cost models at once**, OS6023. A supplied schedule and a declared
158
+ * commission describe the same money. Applied together they charge it twice
159
+ * and applied one at a time they charge whichever an engine preferred, which
160
+ * is a rule nobody wrote down.
161
+ * - **A schedule that cannot be evaluated**, OS6021, which `scheduleProblem`
162
+ * decides, because the schedule is the money layer's and the rule for it is
163
+ * written once, there. **Whichever schedule the run will be charged under**,
164
+ * which is the declaration's own when the host supplied none: asking only
165
+ * about a supplied one left every refusal in the money layer unreachable on
166
+ * the path almost every run takes.
167
+ * - **A quantity in a unit this destination cannot fill**, OS6021. A backtest
168
+ * fills in units and works out no running equity, so a quantity in cash or in
169
+ * a percentage of equity is one it cannot convert, and a lot needs a lot size
170
+ * the instrument may not state.
171
+ * - **A tolerance with a bound and no reason**, OS6021. A comparison allowed to
172
+ * pass by a margin nobody justified is a failed comparison somebody switched
173
+ * off, and the reason a tolerance exists at all is a second implementation:
174
+ * a rerun of one record on this engine is bit-identical or it is a defect.
175
+ *
176
+ * Nothing has been computed when this is asked, so a refusal costs one run
177
+ * rather than a report a reader has to be told to distrust.
178
+ */
179
+ export function checkSettings(
180
+ settings: BacktestSettings,
181
+ declared: RunDeclaration,
182
+ ): Diagnostic | null {
183
+ if (settings.costs !== null && declared.isStrategy && declared.commission !== 0) {
184
+ return diagnosticFor('OS6023', NO_POSITION, {
185
+ commission: declared.commission,
186
+ commissionType: declared.commissionType,
187
+ });
188
+ }
189
+
190
+ // Whichever schedule the run will actually be charged under, which is the
191
+ // declaration's own when the host supplied none. Guarding this on
192
+ // `settings.costs !== null` left every refusal in the money layer unreachable
193
+ // on the default path: a declared commission of -5 was charged as a credit
194
+ // and turned a loss into a gain, with nothing raised anywhere.
195
+ const schedule = settings.costs ?? scheduleForDeclaration(declared, settings.contract);
196
+ if (schedule !== null) {
197
+ const problem = scheduleProblem(schedule, settings.contract);
198
+ if (problem !== null) return problem;
199
+ }
200
+
201
+ const sizing = sizingProblem(declared, settings.contract);
202
+ if (sizing !== null) return sizing;
203
+
204
+ return toleranceProblem(settings.tolerance);
205
+ }
206
+
207
+ /** The schedule a run with no host schedule is charged under, or none. */
208
+ function scheduleForDeclaration(
209
+ declared: RunDeclaration,
210
+ contract: Contract,
211
+ ): ChargeSchedule | null {
212
+ if (!declared.isStrategy) return null;
213
+ return scheduleFromDeclaration(
214
+ declared.commission,
215
+ declared.commissionType,
216
+ declared.slippage,
217
+ contract.currency,
218
+ contract.digits,
219
+ );
220
+ }
221
+
222
+ /**
223
+ * Whether this destination can fill the unit the strategy sizes in.
224
+ *
225
+ * **A quantity is stated in the declaration's own unit and the destination is
226
+ * the party that converts it** (`host-interface.md` 7.1). This one filled every
227
+ * order at the number the script wrote, whatever unit it was written in, so a
228
+ * strategy sizing in lots on a lot of sixty five traded one sixty fifth of what
229
+ * it asked for and every money figure in the record was out by that factor,
230
+ * with nothing refused and nothing said. A record like that is worse than a
231
+ * refused run twice over: somebody trades on the number, and a second engine is
232
+ * handed it as a conformance case and taught the wrong quantity.
233
+ *
234
+ * So: units and lots are converted, and the two that need a running equity this
235
+ * destination does not hold are refused by name. A refusal costs one run. The
236
+ * alternative cost a whole report that looked right.
237
+ */
238
+ function sizingProblem(declared: RunDeclaration, contract: Contract): Diagnostic | null {
239
+ if (!declared.isStrategy) return null;
240
+ if (declared.qtyType === 'units' || declared.qtyType === '') return null;
241
+ if (declared.qtyType === 'lots') {
242
+ if (contract.lotSize !== null && contract.lotSize > 0) return null;
243
+ return diagnosticFor('OS6021', NO_POSITION, {
244
+ setting: 'A quantity stated in lots',
245
+ problem:
246
+ 'this instrument states no lot size, so there is nothing to convert a lot into',
247
+ });
248
+ }
249
+ return diagnosticFor('OS6021', NO_POSITION, {
250
+ setting: 'A quantity stated in ' + declared.qtyType,
251
+ problem:
252
+ 'a backtest fills in units and works out no running equity to size against, so it ' +
253
+ 'cannot convert one. State the quantity in units or in lots',
254
+ });
255
+ }
256
+
257
+ /**
258
+ * A bound, and the reason it is there.
259
+ *
260
+ * Both bounds are checked rather than the first one found, because a tolerance
261
+ * carrying two bounds and one reason is two allowances and one justification.
262
+ */
263
+ function toleranceProblem(tolerance: Tolerance): Diagnostic | null {
264
+ const stated = tolerance.reason !== null && tolerance.reason.trim() !== '';
265
+ const bounded = tolerance.abs !== 0 || tolerance.rel !== 0;
266
+ if (bounded && !stated) {
267
+ return diagnosticFor('OS6021', NO_POSITION, {
268
+ setting: 'The comparison tolerance',
269
+ problem: 'a bound of ' + String(tolerance.abs) + ' absolute and ' + String(tolerance.rel) +
270
+ ' relative is stated with no reason beside it',
271
+ });
272
+ }
273
+ if (tolerance.abs < 0 || tolerance.rel < 0) {
274
+ return diagnosticFor('OS6021', NO_POSITION, {
275
+ setting: 'The comparison tolerance',
276
+ problem: 'a bound below zero admits nothing and refuses what is exact',
277
+ });
278
+ }
279
+ return null;
280
+ }