dexbot 1.6.11 → 1.6.13

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 (398) hide show
  1. package/CHANGELOG.md +67 -4
  2. package/README.md +2 -1
  3. package/analysis/README.md +11 -11
  4. package/analysis/ama_fitting/README.md +5 -3
  5. package/analysis/tradingview/README.md +1 -1
  6. package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +3 -3
  7. package/claw/README.md +1 -0
  8. package/claw/docs/AI_BOT_LIBRARY_API.md +2 -0
  9. package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +4 -3
  10. package/claw/modules/chain_broadcast.ts +7 -1
  11. package/claw/modules/claw_bridge.ts +0 -3
  12. package/claw/modules/claw_launcher.ts +1 -1
  13. package/claw/modules/claw_skill_md.ts +1 -0
  14. package/claw/modules/launcher_mode_detector.ts +9 -2
  15. package/claw/package.json +3 -3
  16. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  17. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  18. package/claw/tsconfig.json +2 -2
  19. package/dist/analysis/account_resolver.d.ts +1 -1
  20. package/dist/analysis/account_resolver.js +2 -1
  21. package/dist/analysis/ama_fitting/analyze_ama_price_changes.js +3 -4
  22. package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +3 -21
  23. package/dist/analysis/ama_fitting/calibrate_convergence_er.js +0 -1
  24. package/dist/analysis/ama_fitting/fetch_lp_candles.js +55 -45
  25. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -8
  26. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +45 -24
  27. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +34 -78
  28. package/dist/analysis/analyze_args.d.ts +43 -0
  29. package/dist/analysis/analyze_args.js +61 -0
  30. package/dist/analysis/analyze_dynamic_weight.js +7 -17
  31. package/dist/analysis/analyze_kalman.js +7 -17
  32. package/dist/analysis/analyze_regime.js +7 -17
  33. package/dist/analysis/analyze_regime_windows.js +8 -18
  34. package/dist/analysis/analyze_risk_profile.js +8 -18
  35. package/dist/analysis/analyze_trade_heatmap.js +7 -17
  36. package/dist/analysis/analyze_volatility.js +7 -17
  37. package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +221 -28
  38. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +23 -103
  39. package/dist/analysis/bot_fitting/backtest_bot_fitting.d.ts +34 -4
  40. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +32 -150
  41. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
  42. package/dist/analysis/bot_fitting/shared_utils.d.ts +77 -9
  43. package/dist/analysis/bot_fitting/shared_utils.js +125 -9
  44. package/dist/analysis/bot_key_utils.d.ts +18 -10
  45. package/dist/analysis/bot_key_utils.js +7 -4
  46. package/dist/analysis/bot_usage/discover_bot_accounts.js +44 -47
  47. package/dist/analysis/bot_usage/kibana_bot_queries.d.ts +1 -21
  48. package/dist/analysis/chain_pool.d.ts +3 -1
  49. package/dist/analysis/chart_ui.d.ts +35 -3
  50. package/dist/analysis/chart_ui.js +43 -4
  51. package/dist/analysis/chart_utils.d.ts +5 -5
  52. package/dist/analysis/chart_utils.js +9 -0
  53. package/dist/analysis/fills_cache.d.ts +4 -24
  54. package/dist/analysis/fills_cache.js +3 -84
  55. package/dist/analysis/fills_source.d.ts +33 -3
  56. package/dist/analysis/fills_source.js +83 -13
  57. package/dist/analysis/grid_correction_check.d.ts +2 -24
  58. package/dist/analysis/grid_correction_check.js +27 -69
  59. package/dist/analysis/math_utils.d.ts +91 -6
  60. package/dist/analysis/math_utils.js +119 -7
  61. package/dist/analysis/pnl_report.js +29 -29
  62. package/dist/analysis/price_sources.d.ts +30 -9
  63. package/dist/analysis/price_sources.js +28 -4
  64. package/dist/analysis/resolve_bot_accounts.js +2 -2
  65. package/dist/analysis/resolve_source.d.ts +1 -1
  66. package/dist/analysis/resolve_source.js +31 -10
  67. package/dist/analysis/trade_profitability.d.ts +29 -2
  68. package/dist/analysis/trade_profitability.js +73 -147
  69. package/dist/analysis/tradingview/analyze_tradingview.d.ts +3 -8
  70. package/dist/analysis/tradingview/analyze_tradingview.js +7 -6
  71. package/dist/analysis/tradingview/grid_reset_config.d.ts +41 -32
  72. package/dist/analysis/tradingview/grid_reset_config.js +23 -11
  73. package/dist/analysis/tradingview/grid_reset_sim.d.ts +75 -9
  74. package/dist/analysis/tradingview/grid_reset_sim.js +4 -3
  75. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts +69 -4
  76. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +20 -34
  77. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
  78. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +26 -84
  79. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.d.ts +63 -1
  80. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +16 -41
  81. package/dist/analysis/trend_detection/huber_scale_variants.d.ts +9 -3
  82. package/dist/analysis/trend_detection/huber_scale_variants.js +4 -4
  83. package/dist/analysis/trend_detection/kalman_chart_generator.d.ts +30 -1
  84. package/dist/analysis/trend_detection/kalman_chart_generator.js +3 -3
  85. package/dist/analysis/trend_detection/regime_chart_generator.d.ts +31 -1
  86. package/dist/analysis/trend_detection/regime_chart_generator.js +2 -2
  87. package/dist/analysis/trend_detection/volatility_chart_generator.d.ts +33 -1
  88. package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
  89. package/dist/analysis/worker_pool.d.ts +38 -0
  90. package/dist/analysis/worker_pool.js +67 -0
  91. package/dist/credential-daemon.js +34 -20
  92. package/dist/dexbot.js +78 -59
  93. package/dist/market_adapter/ama_signal_runner.js +21 -54
  94. package/dist/market_adapter/candle_utils.d.ts +29 -19
  95. package/dist/market_adapter/candle_utils.js +35 -21
  96. package/dist/market_adapter/cli_args.d.ts +22 -0
  97. package/dist/market_adapter/cli_args.js +30 -0
  98. package/dist/market_adapter/core/asymmetric_bounds.d.ts +13 -4
  99. package/dist/market_adapter/core/config_normalizers.d.ts +3 -3
  100. package/dist/market_adapter/core/config_normalizers.js +11 -6
  101. package/dist/market_adapter/core/kibana_candles.d.ts +56 -19
  102. package/dist/market_adapter/core/kibana_candles.js +12 -9
  103. package/dist/market_adapter/core/kibana_client.d.ts +9 -1
  104. package/dist/market_adapter/core/kibana_client.js +8 -7
  105. package/dist/market_adapter/core/kibana_market_candles.d.ts +3 -2
  106. package/dist/market_adapter/core/market_adapter_service.d.ts +320 -153
  107. package/dist/market_adapter/core/market_adapter_service.js +228 -269
  108. package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
  109. package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
  110. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
  111. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
  112. package/dist/market_adapter/core/strategies/ama.d.ts +10 -2
  113. package/dist/market_adapter/core/strategies/ama.js +2 -4
  114. package/dist/market_adapter/core/strategies/ama_slope_model.d.ts +14 -1
  115. package/dist/market_adapter/core/strategies/ama_slope_model.js +12 -34
  116. package/dist/market_adapter/core/strategies/atr/calculator.d.ts +7 -2
  117. package/dist/market_adapter/core/strategies/atr/calculator.js +2 -34
  118. package/dist/market_adapter/core/strategies/collateral_manager.d.ts +7 -1
  119. package/dist/market_adapter/core/strategies/collateral_manager.js +0 -22
  120. package/dist/market_adapter/core/strategies/dynamic_weight_series.d.ts +34 -6
  121. package/dist/market_adapter/core/strategies/dynamic_weight_series.js +7 -7
  122. package/dist/market_adapter/core/strategies/regime_gate.d.ts +24 -15
  123. package/dist/market_adapter/core/strategies/regime_gate.js +4 -31
  124. package/dist/market_adapter/core/strategies/regime_interp.d.ts +7 -1
  125. package/dist/market_adapter/core/strategies/regime_interp.js +14 -22
  126. package/dist/market_adapter/core/strategies/volatility_shift.d.ts +11 -1
  127. package/dist/market_adapter/core/strategies/volatility_shift.js +0 -13
  128. package/dist/market_adapter/inputs/cex_candle_parsing.d.ts +29 -0
  129. package/dist/market_adapter/inputs/cex_candle_parsing.js +52 -0
  130. package/dist/market_adapter/inputs/fetch_book_data.d.ts +20 -4
  131. package/dist/market_adapter/inputs/fetch_book_data.js +3 -2
  132. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +160 -332
  133. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +50 -8
  134. package/dist/market_adapter/inputs/fetch_lp_data.js +47 -22
  135. package/dist/market_adapter/inputs/kibana_feed_source.d.ts +74 -15
  136. package/dist/market_adapter/inputs/kibana_feed_source.js +26 -41
  137. package/dist/market_adapter/inputs/kibana_source.d.ts +25 -3
  138. package/dist/market_adapter/inputs/kibana_source.js +2 -2
  139. package/dist/market_adapter/inputs/window_cache.d.ts +93 -156
  140. package/dist/market_adapter/inputs/window_cache.js +59 -184
  141. package/dist/market_adapter/interval_utils.d.ts +15 -5
  142. package/dist/market_adapter/interval_utils.js +16 -1
  143. package/dist/market_adapter/log_format.d.ts +6 -6
  144. package/dist/market_adapter/log_format.js +28 -21
  145. package/dist/market_adapter/lp_chart_core.d.ts +22 -1
  146. package/dist/market_adapter/lp_chart_core.js +26 -192
  147. package/dist/market_adapter/lp_chart_runner.d.ts +10 -7
  148. package/dist/market_adapter/lp_chart_runner.js +37 -19
  149. package/dist/market_adapter/lp_chart_strategy_loader.d.ts +41 -6
  150. package/dist/market_adapter/lp_chart_strategy_loader.js +8 -7
  151. package/dist/market_adapter/market_adapter.d.ts +94 -32
  152. package/dist/market_adapter/market_adapter.js +94 -120
  153. package/dist/market_adapter/utils/adapter_client.d.ts +3 -3
  154. package/dist/market_adapter/utils/adapter_client.js +2 -2
  155. package/dist/market_adapter/utils/atomic_write.d.ts +8 -1
  156. package/dist/market_adapter/utils/chain.d.ts +50 -41
  157. package/dist/market_adapter/utils/chain.js +40 -46
  158. package/dist/market_adapter/utils/data_discovery.d.ts +49 -2
  159. package/dist/market_adapter/utils/data_discovery.js +220 -27
  160. package/dist/market_adapter/utils/dynamic_grid_snapshot.d.ts +12 -3
  161. package/dist/market_adapter/utils/file_json_cache.d.ts +2 -2
  162. package/dist/market_adapter/utils/file_lock.d.ts +28 -14
  163. package/dist/market_adapter/utils/file_lock.js +36 -32
  164. package/dist/market_adapter/utils/month_shards.d.ts +84 -0
  165. package/dist/market_adapter/utils/month_shards.js +237 -0
  166. package/dist/market_adapter/utils/native_history.d.ts +9 -2
  167. package/dist/market_adapter/utils/native_history.js +35 -28
  168. package/dist/modules/account_bots.d.ts +578 -21
  169. package/dist/modules/account_bots.js +272 -267
  170. package/dist/modules/account_orders.d.ts +79 -30
  171. package/dist/modules/account_orders.js +60 -64
  172. package/dist/modules/authority_resolver.d.ts +12 -2
  173. package/dist/modules/authority_resolver.js +15 -8
  174. package/dist/modules/bitshares-native/chain_client.d.ts +39 -4
  175. package/dist/modules/bitshares-native/chain_client.js +59 -59
  176. package/dist/modules/bitshares-native/crypto/ecc.browser.js +11 -171
  177. package/dist/modules/bitshares-native/crypto/ecc.js +13 -176
  178. package/dist/modules/bitshares-native/crypto/ecc_core.d.ts +33 -0
  179. package/dist/modules/bitshares-native/crypto/ecc_core.js +213 -0
  180. package/dist/modules/bitshares-native/crypto/ecc_selector.d.ts +2 -1
  181. package/dist/modules/bitshares-native/crypto/ecc_selector.js +1 -13
  182. package/dist/modules/bitshares-native/index.d.ts +1 -1
  183. package/dist/modules/bitshares-native/lru_cache.d.ts +7 -7
  184. package/dist/modules/bitshares-native/resolvers.d.ts +18 -6
  185. package/dist/modules/bitshares-native/resolvers.js +22 -25
  186. package/dist/modules/bitshares-native/serial/lazy_buffer.d.ts +4 -0
  187. package/dist/modules/bitshares-native/serial/lazy_buffer.js +21 -0
  188. package/dist/modules/bitshares-native/serial/operations.d.ts +1 -1
  189. package/dist/modules/bitshares-native/serial/serializer.d.ts +16 -12
  190. package/dist/modules/bitshares-native/serial/serializer.js +7 -13
  191. package/dist/modules/bitshares-native/serial/types.d.ts +25 -16
  192. package/dist/modules/bitshares-native/serial/types.js +73 -108
  193. package/dist/modules/bitshares-native/signing_client.d.ts +32 -2
  194. package/dist/modules/bitshares-native/signing_client.js +19 -14
  195. package/dist/modules/bitshares-native/subscriptions.d.ts +81 -1
  196. package/dist/modules/bitshares-native/subscriptions.js +12 -12
  197. package/dist/modules/bitshares-native/transport.d.ts +5 -4
  198. package/dist/modules/bitshares-native/transport.js +5 -4
  199. package/dist/modules/bitshares-native/tx/builder.d.ts +40 -5
  200. package/dist/modules/bitshares-native/tx/builder.js +4 -3
  201. package/dist/modules/bitshares-native/tx/tx_cache.d.ts +4 -4
  202. package/dist/modules/bitshares_client.d.ts +45 -13
  203. package/dist/modules/bitshares_client.js +28 -19
  204. package/dist/modules/bot_defaults.d.ts +8 -16
  205. package/dist/modules/bot_defaults.js +1 -5
  206. package/dist/modules/bot_settings.d.ts +44 -10
  207. package/dist/modules/bot_settings.js +11 -8
  208. package/dist/modules/bots_file_lock.d.ts +6 -6
  209. package/dist/modules/bots_file_lock.js +16 -12
  210. package/dist/modules/broadcast_failure.d.ts +4 -3
  211. package/dist/modules/broadcast_failure.js +5 -4
  212. package/dist/modules/chain_keys.d.ts +42 -40
  213. package/dist/modules/chain_keys.js +53 -45
  214. package/dist/modules/chain_orders.d.ts +94 -60
  215. package/dist/modules/chain_orders.js +81 -118
  216. package/dist/modules/cli_start_onboarding.d.ts +2 -1
  217. package/dist/modules/constants.d.ts +534 -32
  218. package/dist/modules/constants.js +35 -48
  219. package/dist/modules/cr_planner.d.ts +41 -3
  220. package/dist/modules/cr_planner.js +1 -5
  221. package/dist/modules/credential_policy.d.ts +68 -8
  222. package/dist/modules/credential_policy.js +37 -31
  223. package/dist/modules/credential_session_cache.d.ts +2 -2
  224. package/dist/modules/credit_pricing.d.ts +43 -6
  225. package/dist/modules/credit_pricing.js +70 -60
  226. package/dist/modules/credit_runtime.d.ts +283 -118
  227. package/dist/modules/credit_runtime.js +500 -231
  228. package/dist/modules/crypto/browser_provider.js +6 -4
  229. package/dist/modules/crypto/node_provider.js +1 -1
  230. package/dist/modules/crypto/pure_scrypt.js +35 -4
  231. package/dist/modules/crypto/pure_secp256k1.js +72 -26
  232. package/dist/modules/crypto/sync.d.ts +9 -9
  233. package/dist/modules/crypto/sync.js +16 -9
  234. package/dist/modules/daemon_node_health.d.ts +4 -3
  235. package/dist/modules/dexbot_class.d.ts +213 -146
  236. package/dist/modules/dexbot_class.js +183 -86
  237. package/dist/modules/dexbot_cow_runtime.d.ts +160 -87
  238. package/dist/modules/dexbot_cow_runtime.js +302 -234
  239. package/dist/modules/dexbot_credential_client.d.ts +19 -18
  240. package/dist/modules/dexbot_credential_client.js +1 -2
  241. package/dist/modules/dexbot_fill_runtime.d.ts +63 -49
  242. package/dist/modules/dexbot_fill_runtime.js +53 -47
  243. package/dist/modules/dexbot_maintenance_runtime.d.ts +127 -71
  244. package/dist/modules/dexbot_maintenance_runtime.js +187 -147
  245. package/dist/modules/dexbot_startup_runtime.d.ts +11 -11
  246. package/dist/modules/dexbot_startup_runtime.js +56 -57
  247. package/dist/modules/dexbot_state_recovery.d.ts +13 -12
  248. package/dist/modules/dexbot_state_recovery.js +18 -13
  249. package/dist/modules/fund_registry.d.ts +4 -2
  250. package/dist/modules/fund_registry.js +10 -10
  251. package/dist/modules/general_settings.d.ts +30 -4
  252. package/dist/modules/graceful_shutdown.d.ts +3 -3
  253. package/dist/modules/graceful_shutdown.js +19 -13
  254. package/dist/modules/grid_price_source.d.ts +1 -1
  255. package/dist/modules/key_store.d.ts +14 -8
  256. package/dist/modules/key_store.js +7 -7
  257. package/dist/modules/launcher/adapter_requirement.d.ts +11 -5
  258. package/dist/modules/launcher/adapter_requirement.js +5 -5
  259. package/dist/modules/launcher/bot_supervisor.d.ts +106 -24
  260. package/dist/modules/launcher/bot_supervisor.js +12 -11
  261. package/dist/modules/launcher/credential_bootstrap.d.ts +1 -1
  262. package/dist/modules/launcher/credential_bootstrap.js +1 -1
  263. package/dist/modules/launcher/credential_daemon.d.ts +4 -4
  264. package/dist/modules/launcher/credential_daemon.js +2 -2
  265. package/dist/modules/launcher/credential_secret.d.ts +1 -1
  266. package/dist/modules/launcher/foreign_cred_daemon.d.ts +2 -2
  267. package/dist/modules/launcher/market_adapter_runtime.d.ts +61 -6
  268. package/dist/modules/launcher/market_adapter_watchdog.d.ts +10 -2
  269. package/dist/modules/launcher/market_adapter_watchdog.js +4 -0
  270. package/dist/modules/launcher/monolithic_runtime.d.ts +18 -16
  271. package/dist/modules/launcher/status_reporting.d.ts +22 -14
  272. package/dist/modules/launcher/supervisor_control.d.ts +8 -1
  273. package/dist/modules/launcher/supervisor_control.js +4 -2
  274. package/dist/modules/lazy_shim.d.ts +11 -0
  275. package/dist/modules/lazy_shim.js +16 -0
  276. package/dist/modules/market_adapter_whitelist.d.ts +9 -3
  277. package/dist/modules/market_adapter_whitelist.js +15 -3
  278. package/dist/modules/node_connect_policy.d.ts +3 -2
  279. package/dist/modules/node_failure_ledger.d.ts +6 -5
  280. package/dist/modules/node_manager.d.ts +41 -11
  281. package/dist/modules/node_manager.js +5 -4
  282. package/dist/modules/order/accounting.d.ts +53 -24
  283. package/dist/modules/order/accounting.js +16 -15
  284. package/dist/modules/order/async_lock.d.ts +5 -2
  285. package/dist/modules/order/async_lock.js +1 -1
  286. package/dist/modules/order/export.d.ts +7 -6
  287. package/dist/modules/order/export.js +1 -7
  288. package/dist/modules/order/format.d.ts +16 -7
  289. package/dist/modules/order/format.js +15 -3
  290. package/dist/modules/order/genesis_policy.d.ts +24 -21
  291. package/dist/modules/order/genesis_policy.js +17 -10
  292. package/dist/modules/order/grid.d.ts +144 -36
  293. package/dist/modules/order/grid.js +226 -145
  294. package/dist/modules/order/grid_reconcile.d.ts +21 -20
  295. package/dist/modules/order/grid_reconcile.js +13 -12
  296. package/dist/modules/order/grid_reconcile_internal.d.ts +85 -72
  297. package/dist/modules/order/grid_reconcile_internal.js +28 -19
  298. package/dist/modules/order/index.d.ts +264 -1
  299. package/dist/modules/order/logger.d.ts +46 -19
  300. package/dist/modules/order/logger.js +38 -49
  301. package/dist/modules/order/logger_state.d.ts +4 -4
  302. package/dist/modules/order/manager.d.ts +232 -191
  303. package/dist/modules/order/manager.js +110 -130
  304. package/dist/modules/order/processed_fill_store.d.ts +18 -3
  305. package/dist/modules/order/processed_fill_store.js +16 -27
  306. package/dist/modules/order/strategy.d.ts +21 -13
  307. package/dist/modules/order/strategy.js +5 -6
  308. package/dist/modules/order/sync_engine.d.ts +91 -33
  309. package/dist/modules/order/sync_engine.js +84 -53
  310. package/dist/modules/order/utils/math.d.ts +157 -102
  311. package/dist/modules/order/utils/math.js +77 -38
  312. package/dist/modules/order/utils/order.d.ts +310 -110
  313. package/dist/modules/order/utils/order.js +153 -126
  314. package/dist/modules/order/utils/slot.d.ts +1 -1
  315. package/dist/modules/order/utils/system.d.ts +156 -26
  316. package/dist/modules/order/utils/system.js +103 -71
  317. package/dist/modules/order/utils/validate.d.ts +89 -41
  318. package/dist/modules/order/utils/validate.js +26 -24
  319. package/dist/modules/order/utils/withPoolRef.d.ts +6 -4
  320. package/dist/modules/order/utils/withPoolRef.js +18 -12
  321. package/dist/modules/order/working_grid.d.ts +14 -13
  322. package/dist/modules/paths.d.ts +1 -4
  323. package/dist/modules/paths.js +33 -61
  324. package/dist/modules/process_discovery.d.ts +1 -1
  325. package/dist/modules/process_discovery.js +2 -1
  326. package/dist/modules/runtime.d.ts +7 -3
  327. package/dist/modules/runtime.js +4 -3
  328. package/dist/modules/runtime_settings.d.ts +14 -11
  329. package/dist/modules/runtime_settings.js +30 -24
  330. package/dist/modules/settings_merge.d.ts +29 -3
  331. package/dist/modules/settings_merge.js +56 -78
  332. package/dist/modules/socket_json_client.d.ts +9 -5
  333. package/dist/modules/storage/browser_adapter.d.ts +38 -53
  334. package/dist/modules/storage/browser_adapter.js +7 -30
  335. package/dist/modules/storage/index.d.ts +1 -1
  336. package/dist/modules/storage/node_adapter.d.ts +45 -27
  337. package/dist/modules/storage/node_adapter.js +9 -9
  338. package/dist/modules/storage/types.d.ts +5 -4
  339. package/dist/modules/types.d.ts +1090 -6
  340. package/dist/modules/types.js +4 -1
  341. package/dist/modules/utils/asset_symbols.d.ts +2 -1
  342. package/dist/modules/utils/asset_symbols.js +2 -1
  343. package/dist/modules/utils/errors.d.ts +9 -0
  344. package/dist/modules/utils/errors.js +24 -4
  345. package/dist/modules/utils/time_range.d.ts +1 -12
  346. package/dist/modules/utils/time_range.js +1 -1
  347. package/dist/modules/validate_profiles.js +53 -43
  348. package/dist/modules/version_notice.d.ts +23 -11
  349. package/dist/modules/version_notice.js +6 -4
  350. package/dist/pm2.d.ts +28 -27
  351. package/dist/pm2.js +61 -40
  352. package/dist/scripts/analyze-credit.d.ts +2 -1
  353. package/dist/scripts/analyze-credit.js +23 -24
  354. package/dist/scripts/analyze-git.js +2 -15
  355. package/dist/scripts/analyze-orders.d.ts +77 -9
  356. package/dist/scripts/analyze-orders.js +65 -40
  357. package/dist/scripts/chart_command.d.ts +27 -18
  358. package/dist/scripts/chart_command.js +15 -13
  359. package/dist/scripts/check_any.d.ts +22 -0
  360. package/dist/scripts/check_any.js +158 -0
  361. package/dist/scripts/diagnose-pool-history.js +13 -13
  362. package/dist/scripts/print_grid.js +0 -1
  363. package/dist/scripts/run-tests.js +39 -8
  364. package/dist/scripts/runner.js +1 -1
  365. package/dist/scripts/sync-version.js +0 -10
  366. package/dist/scripts/test-credit-renewal.js +30 -21
  367. package/dist/scripts/update.d.ts +10 -5
  368. package/dist/scripts/update.js +168 -103
  369. package/dist/scripts/update_dist_freshness.js +1 -0
  370. package/dist/scripts/update_layout.d.ts +61 -0
  371. package/dist/scripts/update_layout.js +180 -0
  372. package/dist/scripts/validate_bots.js +2 -1
  373. package/dist/scripts/verify-browser-bundle.js +8 -0
  374. package/dist/unlock.d.ts +28 -3
  375. package/dist/unlock.js +63 -35
  376. package/docs/BITSHARES_ONBOARDING.md +10 -12
  377. package/docs/COPY_ON_WRITE_MASTER_PLAN.md +2 -2
  378. package/docs/COW_INVARIANTS.md +13 -6
  379. package/docs/DEXBOT_COMPARISON.md +8 -8
  380. package/docs/EVOLUTION.md +10 -8
  381. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +5 -5
  382. package/docs/GRID_RECALCULATION.md +4 -2
  383. package/docs/GRID_RECONCILE.md +14 -3
  384. package/docs/LIFECYCLE.md +11 -11
  385. package/docs/MPA_CREDIT_USAGE.md +22 -3
  386. package/docs/README.md +14 -3
  387. package/docs/WORKFLOW.md +1 -0
  388. package/docs/architecture.md +31 -19
  389. package/docs/developer_guide.md +6 -6
  390. package/market_adapter/README.md +12 -5
  391. package/modules/README.md +4 -1
  392. package/package.json +9 -9
  393. package/scripts/README.md +43 -38
  394. package/tests/README.md +25 -10
  395. package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
  396. package/dist/scripts/diagnose-kibana-candles.js +0 -82
  397. package/scripts/git-viewer.sh +0 -489
  398. package/scripts/postinstall.js +0 -16
@@ -6,25 +6,23 @@ import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strate
6
6
  import { computeHuberWindowSlopePct } from '../../market_adapter/core/strategies/ama_slope_model.js';
7
7
  import { applyAsymmetricBounds } from '../../market_adapter/core/asymmetric_bounds.js';
8
8
  import { range } from '../math_utils.js';
9
- import { parseListOrRange, loadLpData, fmt } from './shared_utils.js';
9
+ import { loadLpData, fmt, loadAmaStrategies, consumeBacktestArg, slopeResetThresholdPct, DEFAULT_FEE_ROUNDTRIP_PCT, DEFAULT_MIN_SPREAD_FACTOR, DEFAULT_BTS_CREATE_FEE, DEFAULT_BTS_CANCEL_FEE, DEFAULT_BTS_MAKER_CREATE_FACTOR, DEFAULT_TX_FEE_PRICE, DEFAULT_REPOSITION_PCT, SLOPE_TRIGGER_FACTOR, SLOPE_MAX_PCT, SLOPE_LOOKBACK_BARS } from './shared_utils.js';
10
10
  import { getStorage } from '../../modules/storage/index.js';
11
11
  import { PATHS } from '../../modules/paths.js';
12
12
  import { GRID_LIMITS, MARKET_ADAPTER } from '../../modules/constants.js';
13
- const { ensureDir, readJSON, writeJSON } = getStorage();
13
+ const { ensureDir, writeJSON } = getStorage();
14
14
  // Default mirrors production sizing: createOrderGrid sizes EVERY rail slot in
15
15
  // bounds (modules/order/grid.ts), it does not cap per side. Infinity = all
16
16
  // slots; --active-orders N restores a bounded run.
17
17
  const DEFAULT_ACTIVE_ORDERS = Infinity;
18
- const DEFAULT_FEE_ROUNDTRIP_PCT = 0.20;
19
- // BTS operation fees — same model as backtest_ama_sweep. Every placement
20
- // (initial grid, reset rebuild, per-cycle slot refill) pays a maker create
21
- // fee, every cancel pays a cancel fee. Fees are converted into percentage
22
- // points against --bts-fee-capital so they deduct from net capture in the
23
- // same units as the rest of the accounting.
24
- const DEFAULT_BTS_CREATE_FEE = 0.48260;
25
- const DEFAULT_BTS_CANCEL_FEE = 0.00482;
26
- const DEFAULT_BTS_MAKER_CREATE_FACTOR = 0.10;
27
- const DEFAULT_TX_FEE_PRICE = 1.0;
18
+ // BTS operation fees are shared with backtest_ama_sweep via shared_utils; the
19
+ // fee-capital base (the notional the fee percentage is measured against) is
20
+ // bot_fitting-specific.
21
+ //
22
+ // Every placement (initial grid, reset rebuild, per-cycle slot refill) pays a
23
+ // maker create fee, every cancel pays a cancel fee. Fees are converted into
24
+ // percentage points against --bts-fee-capital so they deduct from net capture
25
+ // in the same units as the rest of the accounting.
28
26
  const DEFAULT_BTS_FEE_CAPITAL = 10000;
29
27
  // ── Production grid-reset triggers (MARKET_ADAPTER, modules/constants.ts) ────
30
28
  //
@@ -33,7 +31,6 @@ const DEFAULT_BTS_FEE_CAPITAL = 10000;
33
31
  // from that recorded center; the center re-records only when a trigger fires
34
32
  // (ratchet semantics — see analyze_ama_price_changes.trackRepositions).
35
33
  // --reposition-pct overrides the default.
36
- const DEFAULT_REPOSITION_PCT = MARKET_ADAPTER.AMA_DELTA_THRESHOLD_PERCENT;
37
34
  //
38
35
  // Trigger B — AMA slope delta, plus the grid price offset, are BOTH gated in
39
36
  // production behind the per-bot asymmetricBounds whitelist
@@ -47,16 +44,11 @@ const DEFAULT_REPOSITION_PCT = MARKET_ADAPTER.AMA_DELTA_THRESHOLD_PERCENT;
47
44
  // where slopePct is the Huber-robust per-bar AMA slope over
48
45
  // DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS (computeHuberWindowSlopePct) and the
49
46
  // baseline mirrors botState.gridRangeScalingAmaSlope (re-seeded every reset).
50
- const SLOPE_TRIGGER_FACTOR = MARKET_ADAPTER.AMA_SLOPE_DELTA_THRESHOLD_PERCENT;
51
- const SLOPE_MAX_PCT = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT;
52
- const SLOPE_LOOKBACK_BARS = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS;
53
47
  const SLOPE_NEUTRAL_ZONE_PCT = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_NEUTRAL_ZONE_PCT;
54
48
  // Spread-gap floor knobs (GRID_LIMITS): the effective target spread is
55
49
  // clamped up to incrementPercent × MIN_SPREAD_FACTOR and the gap is never
56
50
  // narrower than MIN_SPREAD_ORDERS slots — mirrored from calculateGapSlots
57
51
  // (modules/order/utils/math.ts).
58
- const DEFAULT_MIN_SPREAD_FACTOR = GRID_LIMITS.MIN_SPREAD_FACTOR;
59
- // Inventory-risk penalty weights (in score points)
60
52
  const RISK_W_DURATION = 1.0; // avg open bars
61
53
  const RISK_W_PEAK_OPEN = 2.0; // peak simultaneous open orders
62
54
  const RISK_W_IMBALANCE = 1.2; // avg |openBuy-openSell|
@@ -98,59 +90,19 @@ function parseArgs() {
98
90
  const val = args[i + 1];
99
91
  if (!val)
100
92
  continue;
93
+ if (consumeBacktestArg(arg, val, out, {
94
+ spreadValues: DEFAULT_SPREAD_VALUES,
95
+ incrementValues: DEFAULT_INCREMENT_VALUES,
96
+ ratioValues: DEFAULT_RATIO_VALUES,
97
+ })) {
98
+ i++;
99
+ continue;
100
+ }
101
101
  switch (arg) {
102
- case '--data':
103
- out.dataPath = path.resolve(val);
104
- i++;
105
- break;
106
- case '--results':
107
- out.resultsPath = path.resolve(val);
108
- i++;
109
- break;
110
- case '--spread':
111
- out.spreadValues = parseListOrRange(val, DEFAULT_SPREAD_VALUES);
112
- i++;
113
- break;
114
- case '--increment':
115
- out.incrementValues = parseListOrRange(val, DEFAULT_INCREMENT_VALUES);
116
- i++;
117
- break;
118
- case '--ratio':
119
- out.ratioValues = parseListOrRange(val, DEFAULT_RATIO_VALUES);
120
- i++;
121
- break;
122
102
  case '--active-orders':
123
103
  out.activeOrders = Number(val);
124
104
  i++;
125
105
  break;
126
- case '--fee':
127
- out.feeRoundtripPct = Number(val);
128
- i++;
129
- break;
130
- case '--min-spread-factor':
131
- out.minSpreadFactor = Number(val);
132
- i++;
133
- break;
134
- case '--reposition-pct':
135
- out.repositionPct = Number(val);
136
- i++;
137
- break;
138
- case '--bts-create-fee':
139
- out.btsCreateFee = Number(val);
140
- i++;
141
- break;
142
- case '--bts-cancel-fee':
143
- out.btsCancelFee = Number(val);
144
- i++;
145
- break;
146
- case '--maker-create-factor':
147
- out.makerCreateFactor = Number(val);
148
- i++;
149
- break;
150
- case '--tx-fee-price':
151
- out.txFeePrice = Number(val);
152
- i++;
153
- break;
154
106
  case '--bts-fee-capital':
155
107
  out.btsFeeCapital = Number(val);
156
108
  i++;
@@ -217,28 +169,6 @@ function resolveAutoResultsPath(dataPath) {
217
169
  return newest.file;
218
170
  return path.join(PATHS.ANALYSIS.RESULTS_DIR, `optimization_results_${base}.json`);
219
171
  }
220
- function loadAmaStrategies(resultsPath) {
221
- const json = readJSON(resultsPath);
222
- const amas = (json.meta?.amas ?? {});
223
- const labels = { AMA1: 'AMA1', AMA2: 'AMA2', AMA3: 'AMA3', AMA4: 'AMA4' };
224
- const out = [];
225
- for (const [key, val] of Object.entries(amas)) {
226
- const a = val;
227
- if (!a || !Number.isFinite(a.er) || !Number.isFinite(a.fast) || !Number.isFinite(a.slow))
228
- continue;
229
- out.push({
230
- id: key,
231
- name: labels[key] ?? key,
232
- er: a.er,
233
- fast: a.fast,
234
- slow: a.slow,
235
- });
236
- }
237
- if (out.length !== 4) {
238
- throw new Error(`Expected 4 AMA strategies in results meta.amas, found ${out.length}`);
239
- }
240
- return out;
241
- }
242
172
  /**
243
173
  * Spread-gap width in slots — direct port of calculateGapSlots
244
174
  * (modules/order/utils/math.ts:1228). The effective target spread is clamped
@@ -336,60 +266,6 @@ function buildProductionGrid(center, spreadPct, incrementPctFrac, maxMinRatio, a
336
266
  sellStartIdx,
337
267
  };
338
268
  }
339
- /**
340
- * Slot-rotation simulation with FIXED chain prices, mirroring production
341
- * anchor-&-refill (strategy.ts) on top of the createOrderGrid geometry:
342
- *
343
- * - Initial grid built once at the post-warmup AMA (createOrderGrid geometry
344
- * via buildProductionGrid); order prices never follow AMA afterwards. When
345
- * asymmetricBounds is enabled the placement center is shifted by the
346
- * slope-ratio offset (#3) exactly like the live startPrice offset.
347
- * - Rotation economics (#1/#2/#15): a FILLED BUY at rail node k instantly
348
- * re-offers that base at the ADJACENT MASTER-RAIL NODE k+1; when that
349
- * refill sells, the rotation books exactly one rail hop minus the
350
- * round-trip fee, and the freed quote re-bids the node k-1 below. Slots
351
- * cycle on small oscillations between resets — the earnings model of a
352
- * refilled live slot, not a cross-gap differential.
353
- * - Inventory-funded selling: an INITIAL (unlinked) sell can only execute
354
- * against held inventory — it books its proceeds against the weighted
355
- * average entry of the position (the live bot funds sells from real
356
- * balances; unfundable sells stay open and retry, never shorting). Base
357
- * bought in the SAME bar does not fund unlinked sales until the next bar
358
- * (live balance updates propagate on the next maintenance tick).
359
- * - Same-bar guard: newly armed/refill orders carry a one-bar cooldown, so
360
- * a slot that just cycled cannot fill again inside the completion bar.
361
- * - Grid reset fires on EITHER production trigger:
362
- * (A) |AMA − recordedCenter| / recordedCenter ≥ repositionThresholdPct
363
- * (ratchet: recordedCenter updates only when the trigger fires),
364
- * (B) slope-delta reset, ONLY when asymmetricBounds (whitelist gate):
365
- * |slopePct_now − slopeBaseline| ≥ slopeDeltaThresholdPct
366
- * (= AMA_SLOPE_DELTA_THRESHOLD_PERCENT/100 ×
367
- * DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT), where slopePct is the
368
- * DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS average-slope series and
369
- * slopeBaseline mirrors botState.gridRangeScalingAmaSlope — seeded
370
- * at start and re-seeded on every reset.
371
- * On reset ALL unfilled orders are canceled (counted); bought-and-held
372
- * base REMAINS as inventory (live resync never market-sells) and merges
373
- * into the weighted-average-entry pool; a fresh grid is placed at the
374
- * new center.
375
- * - Scoring uses REALIZED rotations and op fees ONLY. The end-of-run
376
- * inventory position (units × mark-vs-avg-entry) is reported as an
377
- * informational field — it is real carried risk, but including unrealized
378
- * bag marks in the ranking objective let trend-following combos dump
379
- * phantom paper profit into the score.
380
- * - BTS operation fees (#9): every order placement (initial grid + every
381
- * armed refill/rebid) pays makerCreateFee × btsCreateFee, every reset
382
- * cancel pays btsCancelFee; totals are converted to percentage points
383
- * against the --bts-fee-capital reference and deducted from net capture.
384
- *
385
- * Differences from production (by design, documented):
386
- * #6 bar-granularity triggers and reset-vs-fill ordering (OHLC sim);
387
- * #7 all-or-nothing fills on raw hi/lo touch (no partial fills/dust/queue);
388
- * #8 unit sizes / percentage-point accounting instead of capital-weighted
389
- * dynamic sizing;
390
- * #10 pool-price fills (no book depth/slippage);
391
- * #11 no consolidation/dust-cancel/COW/collision mechanics.
392
- */
393
269
  function simulateForParams(candles, amaValues, params) {
394
270
  const { spreadPct, incrementPct, maxMinRatio, activeOrders, feeRoundtripPct, repositionThresholdPct, asymmetricBounds, risk, btsCreateFee, btsCancelFee, makerCreateFactor, txFeePrice, btsFeeCapital } = params;
395
271
  // Warmup follows production AMA seeding/convergence (getAmaWarmupBars)
@@ -397,7 +273,7 @@ function simulateForParams(candles, amaValues, params) {
397
273
  // start on SMA-warmup values when the fitted ER period is large.
398
274
  const skip = Math.min(params.warmupBars, Math.max(0, candles.length - 2));
399
275
  const singleLegFeePct = feeRoundtripPct / 2;
400
- const slopeDeltaThresholdPct = (SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT;
276
+ const slopeDeltaThresholdPct = slopeResetThresholdPct();
401
277
  const makerCreateFeeBts = btsCreateFee * makerCreateFactor;
402
278
  const stepUpFrac = 1 + incrementPct; // one-rail-step rotation distance
403
279
  // Optional gate/band overrides. `slopePersistBars` follows the production
@@ -405,10 +281,12 @@ function simulateForParams(candles, amaValues, params) {
405
281
  // reset path matches live whenever asymmetricBounds enables trigger B.
406
282
  // slopePersistBars persistence gate on trigger B
407
283
  // bandTilt apply applyAsymmetricBounds to the built band
408
- const slopeLookbackBars = Number.isFinite(params.slopeLookbackBars) && params.slopeLookbackBars >= 2
409
- ? Math.round(params.slopeLookbackBars) : SLOPE_LOOKBACK_BARS;
410
- const slopePersistBars = Number.isFinite(params.slopePersistBars) && params.slopePersistBars >= 1
411
- ? Math.round(params.slopePersistBars)
284
+ const slopeLookbackRaw = Number(params.slopeLookbackBars);
285
+ const slopeLookbackBars = Number.isFinite(slopeLookbackRaw) && slopeLookbackRaw >= 2
286
+ ? Math.round(slopeLookbackRaw) : SLOPE_LOOKBACK_BARS;
287
+ const slopePersistRaw = Number(params.slopePersistBars);
288
+ const slopePersistBars = Number.isFinite(slopePersistRaw) && slopePersistRaw >= 1
289
+ ? Math.round(slopePersistRaw)
412
290
  : (MARKET_ADAPTER.AMA_SLOPE_PERSIST_ENABLED === true && Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS) >= 1
413
291
  ? Math.round(Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS)) : 1);
414
292
  const bandTilt = params.bandTilt === true;
@@ -748,7 +626,11 @@ function run() {
748
626
  const loaded = loadLpData(cfg.dataPath);
749
627
  const candles = loaded.candles;
750
628
  const closes = candles.map((c) => c.close);
751
- const strategies = loadAmaStrategies(cfg.resultsPath);
629
+ const strategies = loadAmaStrategies(cfg.resultsPath, {
630
+ requireAllFields: true,
631
+ exactCount: 4,
632
+ labels: { AMA1: 'AMA1', AMA2: 'AMA2', AMA3: 'AMA3', AMA4: 'AMA4' },
633
+ });
752
634
  if (!Number.isFinite(cfg.repositionPct) || cfg.repositionPct <= 0) {
753
635
  throw new Error(`Invalid reposition threshold: ${cfg.repositionPct}`);
754
636
  }
@@ -766,7 +648,7 @@ function run() {
766
648
  console.log(` Spread floor: spread >= ${cfg.minSpreadFactor} x increment`);
767
649
  console.log(` Reset (A): AMA drift >= ${cfg.repositionPct}% from recorded center${cfg.repositionPct === DEFAULT_REPOSITION_PCT ? ' (AMA_DELTA_THRESHOLD_PERCENT)' : ''}`);
768
650
  console.log(` Asym. bounds: ${cfg.asymmetricBounds ? 'ON — slope reset (B) + grid price offset enabled (whitelist semantics)' : 'OFF — typical non-whitelisted bot (production default)'}`);
769
- console.log(` Reset (B): |slope - slope@lastReset| >= ${(SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT}% (${SLOPE_TRIGGER_FACTOR}% x ${SLOPE_MAX_PCT}, lookback ${SLOPE_LOOKBACK_BARS})${cfg.asymmetricBounds ? '' : ' [gated off]'}`);
651
+ console.log(` Reset (B): |slope - slope@lastReset| >= ${slopeResetThresholdPct()}% (${SLOPE_TRIGGER_FACTOR}% x ${SLOPE_MAX_PCT}, lookback ${SLOPE_LOOKBACK_BARS})${cfg.asymmetricBounds ? '' : ' [gated off]'}`);
770
652
  console.log(` Tx fees: create=${fmt(cfg.btsCreateFee * cfg.makerCreateFactor, 5)} BTS, cancel=${fmt(cfg.btsCancelFee, 5)} BTS, 1 BTS=${fmt(cfg.txFeePrice, 2)} units, capital=${fmt(cfg.btsFeeCapital, 0)}`);
771
653
  console.log(` Risk W: duration=${cfg.riskWDuration}, peakOpen=${cfg.riskWPeakOpen}, imbalance=${cfg.riskWImbalance}, cancel=${cfg.riskWCancel}`);
772
654
  console.log(` Combos/AMA: ${totalCombos}\n`);
@@ -862,7 +744,7 @@ function run() {
862
744
  baseScore: 'totalNetCaptureAfterFeesPct * (fillEfficiency / 100)',
863
745
  gridModel: 'persistent fixed chain prices (createOrderGrid port): master rail at sqrt(1±inc) offsets bounded by [center/ratio, center*ratio], gapSlots spread zone centered on center; prices never follow AMA after placement',
864
746
  spreadParam: 'targetSpreadPercent for calculateGapSlots (floored at increment * MIN_SPREAD_FACTOR)',
865
- resetTriggers: `(A) AMA drift >= ${cfg.repositionPct}% from recorded center (ratchet)${cfg.asymmetricBounds ? ` OR (B) |slope - slopeAtLastReset| >= ${(SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT}% over ${SLOPE_LOOKBACK_BARS}-bar average slope (baseline re-seeded on every reset)` : ' (trigger B gated off — asymmetricBounds whitelist)'}`,
747
+ resetTriggers: `(A) AMA drift >= ${cfg.repositionPct}% from recorded center (ratchet)${cfg.asymmetricBounds ? ` OR (B) |slope - slopeAtLastReset| >= ${slopeResetThresholdPct()}% over ${SLOPE_LOOKBACK_BARS}-bar average slope (baseline re-seeded on every reset)` : ' (trigger B gated off — asymmetricBounds whitelist)'}`,
866
748
  gridPriceOffset: cfg.asymmetricBounds ? 'slope-ratio offset applied to placement center (direction * min(|slope|/maxSlopePct,1) * targetSpread/2) on every grid build' : 'disabled (asymmetricBounds whitelist)',
867
749
  repositionAccounting: 'unfilled orders canceled + counted (incl armed refills/rebids); bought-and-held base carries across resets in a weighted-average-entry inventory pool (resync never market-sells); end-of-run inventory mark is informational and excluded from scoring',
868
750
  cycleEconomics: 'slot rotation: filled buy re-offers one rail step up; that refill selling books ~increment% minus round-trip fee and the freed quote re-bids one step down; unlinked initial-grid sells only execute against held inventory at weighted-average entry (no shorting)',
@@ -33,8 +33,9 @@ import fs from 'node:fs';
33
33
  import path from 'node:path';
34
34
  import { pathToFileURL } from 'node:url';
35
35
  import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strategies/ama.js';
36
- import { loadCandles } from '../trend_detection/backtest_ama_slope_huber.js';
37
36
  import { parseListOrRange } from './shared_utils.js';
37
+ import { printHelpHeader, helpRow } from '../analyze_args.js';
38
+ import { median, percentile, fmtNum as fmt, loadCandleSeries } from '../math_utils.js';
38
39
  import { simulatePersistentGrid, WEIGHT_PROFILES } from './backtest_ama_sweep.js';
39
40
  import { getStorage } from '../../modules/storage/index.js';
40
41
  import { PATHS } from '../../modules/paths.js';
@@ -157,23 +158,19 @@ function parseArgs() {
157
158
  return out;
158
159
  }
159
160
  function printHelp() {
160
- console.log('Lookback -> fill-model drawdown backtest (paired persistent-grid simulation)');
161
- console.log('');
162
- console.log('Usage: node dist/analysis/bot_fitting/backtest_lookback_drawdown.js [options]');
163
- console.log('');
164
- console.log('Options:');
165
- console.log(' --data <path> LP candle file or shard directory (required)');
166
- console.log(` --ama <AMA1..AMA4> Built-in AMA preset (default: ${MA.DEFAULT_AMA_KEY})`);
167
- console.log(` --lookbacks <spec> Lookbacks to compare: 12,14,16,20 (default: ${DEFAULT_LOOKBACKS.join(',')})`);
168
- console.log(` --spread <spec> Spread grid (default: ${DEFAULT_SPREADS.join(',')})`);
169
- console.log(` --increment <spec> Increment grid (default: ${DEFAULT_INCREMENTS.join(',')})`);
170
- console.log(` --ratio <spec> Max/min ratio grid (default: ${DEFAULT_RATIOS.join(',')})`);
171
- console.log(' --max-orders <n> Size cap per side (default: 20)');
172
- console.log(' --fee <pct> Round-trip fee % (default: 0.20)');
173
- console.log(' --capital <n> Notional capital per side (default: 10000)');
174
- console.log(` --reposition <pct> AMA drift % to re-center (default: ${MA.AMA_DELTA_THRESHOLD_PERCENT})`);
175
- console.log(' --no-asymmetric-bounds Disable trigger B + slope offset (default: enabled)');
176
- console.log(' --out <path> JSON output path');
161
+ printHelpHeader('Lookback -> fill-model drawdown backtest (paired persistent-grid simulation)', 'bot_fitting/backtest_lookback_drawdown.js');
162
+ console.log(helpRow('--data <path>', 'LP candle file or shard directory (required)', 25));
163
+ console.log(helpRow('--ama <AMA1..AMA4>', `Built-in AMA preset (default: ${MA.DEFAULT_AMA_KEY})`, 25));
164
+ console.log(helpRow('--lookbacks <spec>', `Lookbacks to compare: 12,14,16,20 (default: ${DEFAULT_LOOKBACKS.join(',')})`, 25));
165
+ console.log(helpRow('--spread <spec>', `Spread grid (default: ${DEFAULT_SPREADS.join(',')})`, 25));
166
+ console.log(helpRow('--increment <spec>', `Increment grid (default: ${DEFAULT_INCREMENTS.join(',')})`, 25));
167
+ console.log(helpRow('--ratio <spec>', `Max/min ratio grid (default: ${DEFAULT_RATIOS.join(',')})`, 25));
168
+ console.log(helpRow('--max-orders <n>', 'Size cap per side (default: 20)', 25));
169
+ console.log(helpRow('--fee <pct>', 'Round-trip fee % (default: 0.20)', 25));
170
+ console.log(helpRow('--capital <n>', 'Notional capital per side (default: 10000)', 25));
171
+ console.log(helpRow('--reposition <pct>', `AMA drift % to re-center (default: ${MA.AMA_DELTA_THRESHOLD_PERCENT})`, 25));
172
+ console.log(helpRow('--no-asymmetric-bounds', 'Disable trigger B + slope offset (default: enabled)', 25));
173
+ console.log(helpRow('--out <path>', 'JSON output path', 25));
177
174
  }
178
175
  function resolveAma(name) {
179
176
  const presets = MA.AMAS;
@@ -182,22 +179,10 @@ function resolveAma(name) {
182
179
  throw new Error(`Unknown AMA preset '${name}' (expected ${Object.keys(presets).join(', ')})`);
183
180
  return { name, er: preset.erPeriod, fast: preset.fastPeriod, slow: preset.slowPeriod };
184
181
  }
185
- function median(values) {
186
- const s = values.slice().sort((a, b) => a - b);
187
- const mid = s.length >> 1;
188
- return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
189
- }
190
- function percentile(values, pct) {
191
- const s = values.slice().sort((a, b) => a - b);
192
- return s[Math.min(s.length - 1, Math.max(0, Math.floor((pct / 100) * (s.length - 1))))];
193
- }
194
- function fmt(x, d = 2) {
195
- return Number.isFinite(Number(x)) ? Number(x).toFixed(d) : 'n/a';
196
- }
197
182
  function run() {
198
183
  const cfg = parseArgs();
199
184
  const amaDef = resolveAma(cfg.amaName);
200
- const { candles, files } = loadCandles(cfg.dataPath);
185
+ const { candles, files } = loadCandleSeries(cfg.dataPath);
201
186
  const closes = candles.map((c) => c.close);
202
187
  const amaValues = calculateAMA(closes, { erPeriod: amaDef.er, fastPeriod: amaDef.fast, slowPeriod: amaDef.slow });
203
188
  const warmupBars = getAmaWarmupBars(amaDef.er, amaDef.slow, 0, amaDef.fast);
@@ -260,7 +245,7 @@ function run() {
260
245
  for (const [label, field, mul, d] of fields) {
261
246
  let row = ` ${label.padEnd(20).slice(0, 20)} |`;
262
247
  for (const lb of cfg.lookbacks) {
263
- row += fmt(median(keys.map((k) => byLookback[lb][k][field] * mul)), d).padStart(12);
248
+ row += fmt(median(keys.map((k) => byLookback[lb][k][field] * mul)) ?? Number.NaN, d).padStart(12);
264
249
  }
265
250
  console.log(row);
266
251
  }
@@ -272,8 +257,8 @@ function run() {
272
257
  const deltas = keys.map((k) => byLookback[lb][k].maxDrawdownPct - ref[k].maxDrawdownPct);
273
258
  const lower = deltas.filter((d) => d < 0).length;
274
259
  console.log(` ${String(lb).padStart(2)} | ` +
275
- `${fmt(median(deltas)).padStart(12)} | ` +
276
- `${fmt(percentile(deltas, 25))} .. ${fmt(percentile(deltas, 75))} | ` +
260
+ `${fmt(median(deltas) ?? Number.NaN).padStart(12)} | ` +
261
+ `${fmt(percentile(deltas, 25, { interpolation: 'nearest', rounding: 'floor', empty: Number.NaN }))} .. ${fmt(percentile(deltas, 75, { interpolation: 'nearest', rounding: 'floor', empty: Number.NaN }))} | ` +
277
262
  `${lower}/${deltas.length} (${fmt((100 * lower) / deltas.length, 0)}%)`);
278
263
  }
279
264
  console.log('\n Reminder: the sign is pool-dependent and the absolute level is model-shaped');
@@ -1,15 +1,83 @@
1
- declare function parseListOrRange(spec: any, fallback: any): any;
1
+ /**
2
+ * Shared utilities for bot-fitting scripts.
3
+ *
4
+ * Also owns the backtest defaults/fee model shared by backtest_ama_sweep and
5
+ * backtest_bot_fitting, which previously declared byte-identical local copies.
6
+ */
7
+ declare const DEFAULT_FEE_ROUNDTRIP_PCT = 0.2;
8
+ declare const DEFAULT_MIN_SPREAD_FACTOR: number;
9
+ declare const DEFAULT_BTS_CREATE_FEE = 0.4826;
10
+ declare const DEFAULT_BTS_CANCEL_FEE = 0.00482;
11
+ declare const DEFAULT_BTS_MAKER_CREATE_FACTOR = 0.1;
12
+ declare const DEFAULT_TX_FEE_PRICE = 1;
13
+ declare const DEFAULT_REPOSITION_PCT: number;
14
+ declare const SLOPE_TRIGGER_FACTOR: number;
15
+ declare const SLOPE_MAX_PCT: number;
16
+ declare const SLOPE_LOOKBACK_BARS: number;
17
+ /** Slope-reset threshold in percent: (trigger factor/100) * max slope pct. */
18
+ declare function slopeResetThresholdPct(): number;
19
+ interface AmaStrategy {
20
+ id: string;
21
+ name: string;
22
+ er: number;
23
+ fast: number;
24
+ slow: number;
25
+ }
26
+ interface LoadAmaStrategiesOptions {
27
+ /** Require `fast` and `slow` to be finite too (default: only `er`). */
28
+ requireAllFields?: boolean;
29
+ /** Throw unless exactly this many strategies parse (default: no exact check). */
30
+ exactCount?: number | null;
31
+ /** Sort by `id` ascending. */
32
+ sort?: boolean;
33
+ /** Display-name lookup; when omitted use the entry's `label` field. */
34
+ labels?: Record<string, string> | null;
35
+ }
36
+ /**
37
+ * Read the `meta.amas` preset table from an optimization results file and map
38
+ * it to strategy descriptors. Single home for the loader previously copied into
39
+ * backtest_bot_fitting and backtest_ama_sweep; the options preserve each
40
+ * caller's validation and naming rules.
41
+ */
42
+ declare function loadAmaStrategies(resultsPath: string, options?: LoadAmaStrategiesOptions): AmaStrategy[];
43
+ declare function parseListOrRange(spec: string | null | undefined, fallback: number[]): number[];
2
44
  declare function loadLpData(filePath: string): {
3
45
  candles: {
4
46
  timestamp: number;
5
- open: any;
6
- high: any;
7
- low: any;
8
- close: any;
9
- volume: any;
47
+ open: number;
48
+ high: number;
49
+ low: number;
50
+ close: number;
51
+ volume: number;
10
52
  }[];
11
- meta: any;
53
+ meta: Record<string, unknown> | null;
12
54
  };
13
- declare function fmt(x: number, d?: number): string;
14
- export { parseListOrRange, loadLpData, fmt };
55
+ declare const fmt: (x: number, d?: number) => string;
56
+ interface BacktestArgTarget {
57
+ dataPath?: string | null;
58
+ resultsPath?: string | null;
59
+ spreadValues?: number[];
60
+ incrementValues?: number[];
61
+ ratioValues?: number[];
62
+ feeRoundtripPct?: number;
63
+ minSpreadFactor?: number;
64
+ repositionPct?: number;
65
+ btsCreateFee?: number;
66
+ btsCancelFee?: number;
67
+ makerCreateFactor?: number;
68
+ txFeePrice?: number;
69
+ }
70
+ interface BacktestListDefaults {
71
+ spreadValues: number[];
72
+ incrementValues: number[];
73
+ ratioValues: number[];
74
+ }
75
+ /**
76
+ * Consume one flag shared by backtest_ama_sweep and backtest_bot_fitting.
77
+ * Returns true when `arg` was handled, so the caller advances past its value.
78
+ * Both `--reposition` and `--reposition-pct` spellings are accepted so the two
79
+ * tools converge without breaking either CLI.
80
+ */
81
+ declare function consumeBacktestArg(arg: string, val: string, out: BacktestArgTarget, defaults: BacktestListDefaults): boolean;
82
+ export { parseListOrRange, loadLpData, fmt, loadAmaStrategies, consumeBacktestArg, slopeResetThresholdPct, DEFAULT_FEE_ROUNDTRIP_PCT, DEFAULT_MIN_SPREAD_FACTOR, DEFAULT_BTS_CREATE_FEE, DEFAULT_BTS_CANCEL_FEE, DEFAULT_BTS_MAKER_CREATE_FACTOR, DEFAULT_TX_FEE_PRICE, DEFAULT_REPOSITION_PCT, SLOPE_TRIGGER_FACTOR, SLOPE_MAX_PCT, SLOPE_LOOKBACK_BARS, };
15
83
  //# sourceMappingURL=shared_utils.d.ts.map
@@ -1,16 +1,74 @@
1
1
  'use strict';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
2
4
  import { getStorage } from '../../modules/storage/index.js';
3
5
  const { readJSON } = getStorage();
4
- import { normalizeCandle } from '../math_utils.js';
6
+ import { normalizeCandle, fmtNum, loadCandleFile } from '../math_utils.js';
7
+ import { GRID_LIMITS, MARKET_ADAPTER } from '../../modules/constants.js';
5
8
  /**
6
9
  * Shared utilities for bot-fitting scripts.
10
+ *
11
+ * Also owns the backtest defaults/fee model shared by backtest_ama_sweep and
12
+ * backtest_bot_fitting, which previously declared byte-identical local copies.
7
13
  */
14
+ // Transaction / fee model shared by the two bot-fitting backtests.
15
+ const DEFAULT_FEE_ROUNDTRIP_PCT = 0.20;
16
+ const DEFAULT_MIN_SPREAD_FACTOR = GRID_LIMITS.MIN_SPREAD_FACTOR;
17
+ const DEFAULT_BTS_CREATE_FEE = 0.48260;
18
+ const DEFAULT_BTS_CANCEL_FEE = 0.00482;
19
+ const DEFAULT_BTS_MAKER_CREATE_FACTOR = 0.10;
20
+ const DEFAULT_TX_FEE_PRICE = 1.0;
21
+ // AMA reset trigger shared by both backtests.
22
+ const DEFAULT_REPOSITION_PCT = MARKET_ADAPTER.AMA_DELTA_THRESHOLD_PERCENT;
23
+ const SLOPE_TRIGGER_FACTOR = MARKET_ADAPTER.AMA_SLOPE_DELTA_THRESHOLD_PERCENT;
24
+ const SLOPE_MAX_PCT = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT;
25
+ const SLOPE_LOOKBACK_BARS = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS;
26
+ /** Slope-reset threshold in percent: (trigger factor/100) * max slope pct. */
27
+ function slopeResetThresholdPct() {
28
+ return (SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT;
29
+ }
30
+ /**
31
+ * Read the `meta.amas` preset table from an optimization results file and map
32
+ * it to strategy descriptors. Single home for the loader previously copied into
33
+ * backtest_bot_fitting and backtest_ama_sweep; the options preserve each
34
+ * caller's validation and naming rules.
35
+ */
36
+ function loadAmaStrategies(resultsPath, options = {}) {
37
+ const { requireAllFields = false, exactCount = null, sort = false, labels = null } = options;
38
+ const json = readJSON(resultsPath);
39
+ const amas = json.meta?.amas;
40
+ if (!amas)
41
+ throw new Error('No meta.amas found in results file.');
42
+ const out = [];
43
+ for (const [key, val] of Object.entries(amas)) {
44
+ const v = val;
45
+ if (!v || !Number.isFinite(Number(v.er)))
46
+ continue;
47
+ if (requireAllFields && (!Number.isFinite(Number(v.fast)) || !Number.isFinite(Number(v.slow))))
48
+ continue;
49
+ out.push({
50
+ id: key,
51
+ name: labels ? (labels[key] ?? key) : String(v.label || key),
52
+ er: Number(v.er),
53
+ fast: Number(v.fast),
54
+ slow: Number(v.slow),
55
+ });
56
+ }
57
+ if (sort)
58
+ out.sort((a, b) => a.id.localeCompare(b.id));
59
+ if (exactCount != null && out.length !== exactCount) {
60
+ throw new Error(`Expected ${exactCount} AMA strategies in results meta.amas, found ${out.length}`);
61
+ }
62
+ if (out.length === 0)
63
+ throw new Error('No valid AMA strategies found');
64
+ return out;
65
+ }
8
66
  function toCandles(arr) {
9
67
  // Canonical accessor transform (market_adapter/candle_utils via math_utils)
10
68
  // instead of hand-rolled array indexing.
11
69
  return arr
12
70
  .map((c) => normalizeCandle(c))
13
- .filter(Boolean)
71
+ .filter((c) => c != null)
14
72
  .map((c) => ({
15
73
  timestamp: c.time * 1000,
16
74
  open: c.open,
@@ -36,13 +94,71 @@ function parseListOrRange(spec, fallback) {
36
94
  return vals.length ? vals : fallback;
37
95
  }
38
96
  function loadLpData(filePath) {
39
- const json = readJSON(filePath);
40
- return { candles: toCandles(json.candles ?? json), meta: json.meta ?? null };
97
+ // Shape detection (flat / {candles} / {data}) lives in math_utils.loadCandleFile.
98
+ // loadCandleFile returns an empty list for a missing file; keep the historical
99
+ // readJSON hard failure so a typo'd --data path is not silently treated as empty.
100
+ //
101
+ // Two intentional deltas vs the old inline readJSON+toCandles version:
102
+ // (a) a `{candles:[...]}` file without `meta` now yields the whole file object
103
+ // as `meta` (loadCandleFile's fallback). Both backtest consumers ignore
104
+ // `meta`, so this is inert today.
105
+ // (b) `{data:[...]}` files now load instead of crashing in toCandles (which
106
+ // called `.map` on the non-array object).
107
+ if (!filePath || !fs.existsSync(filePath))
108
+ throw new Error(`Data file not found: ${filePath}`);
109
+ const { candles, meta } = loadCandleFile(filePath);
110
+ return { candles: toCandles(candles), meta };
41
111
  }
42
- function fmt(x, d = 2) {
43
- if (!Number.isFinite(x))
44
- return ' n/a';
45
- return Number(x).toFixed(d);
112
+ // Strict (no Number() coercion) and padded fallback, matching the historical
113
+ // shared_utils output contract; formatting itself lives in math_utils.fmtNum.
114
+ const fmt = (x, d = 2) => fmtNum(x, d, { fallback: ' n/a', strict: true });
115
+ /**
116
+ * Consume one flag shared by backtest_ama_sweep and backtest_bot_fitting.
117
+ * Returns true when `arg` was handled, so the caller advances past its value.
118
+ * Both `--reposition` and `--reposition-pct` spellings are accepted so the two
119
+ * tools converge without breaking either CLI.
120
+ */
121
+ function consumeBacktestArg(arg, val, out, defaults) {
122
+ switch (arg) {
123
+ case '--data':
124
+ out.dataPath = path.resolve(val);
125
+ return true;
126
+ case '--results':
127
+ out.resultsPath = path.resolve(val);
128
+ return true;
129
+ case '--spread':
130
+ out.spreadValues = parseListOrRange(val, defaults.spreadValues);
131
+ return true;
132
+ case '--increment':
133
+ out.incrementValues = parseListOrRange(val, defaults.incrementValues);
134
+ return true;
135
+ case '--ratio':
136
+ out.ratioValues = parseListOrRange(val, defaults.ratioValues);
137
+ return true;
138
+ case '--fee':
139
+ out.feeRoundtripPct = Number(val);
140
+ return true;
141
+ case '--min-spread-factor':
142
+ out.minSpreadFactor = Number(val);
143
+ return true;
144
+ case '--reposition':
145
+ case '--reposition-pct':
146
+ out.repositionPct = Number(val);
147
+ return true;
148
+ case '--bts-create-fee':
149
+ out.btsCreateFee = Number(val);
150
+ return true;
151
+ case '--bts-cancel-fee':
152
+ out.btsCancelFee = Number(val);
153
+ return true;
154
+ case '--maker-create-factor':
155
+ out.makerCreateFactor = Number(val);
156
+ return true;
157
+ case '--tx-fee-price':
158
+ out.txFeePrice = Number(val);
159
+ return true;
160
+ default: return false;
161
+ }
46
162
  }
47
- export { parseListOrRange, loadLpData, fmt };
163
+ export { parseListOrRange, loadLpData, fmt, loadAmaStrategies, consumeBacktestArg, slopeResetThresholdPct, DEFAULT_FEE_ROUNDTRIP_PCT, DEFAULT_MIN_SPREAD_FACTOR, DEFAULT_BTS_CREATE_FEE, DEFAULT_BTS_CANCEL_FEE, DEFAULT_BTS_MAKER_CREATE_FACTOR, DEFAULT_TX_FEE_PRICE, DEFAULT_REPOSITION_PCT, SLOPE_TRIGGER_FACTOR, SLOPE_MAX_PCT, SLOPE_LOOKBACK_BARS, };
48
164
  //# sourceMappingURL=shared_utils.js.map