@reefclaw/connect 0.1.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 (415) hide show
  1. package/assets/bridge/bridge.d.ts +96 -0
  2. package/assets/bridge/bridge.js +969 -0
  3. package/assets/bridge/config.d.ts +43 -0
  4. package/assets/bridge/config.js +176 -0
  5. package/assets/bridge/connector.d.ts +52 -0
  6. package/assets/bridge/connector.js +293 -0
  7. package/assets/bridge/event-replay-buffer.d.ts +43 -0
  8. package/assets/bridge/event-replay-buffer.js +109 -0
  9. package/assets/bridge/gateway/event-parser.d.ts +209 -0
  10. package/assets/bridge/gateway/event-parser.js +793 -0
  11. package/assets/bridge/gateway/gateway-config.d.ts +39 -0
  12. package/assets/bridge/gateway/gateway-config.js +86 -0
  13. package/assets/bridge/gateway/gateway-http-client.d.ts +50 -0
  14. package/assets/bridge/gateway/gateway-http-client.js +165 -0
  15. package/assets/bridge/gateway/gateway-ws-client.d.ts +116 -0
  16. package/assets/bridge/gateway/gateway-ws-client.js +417 -0
  17. package/assets/bridge/gateway/poller.d.ts +146 -0
  18. package/assets/bridge/gateway/poller.js +505 -0
  19. package/assets/bridge/gateway/tool-discovery.d.ts +25 -0
  20. package/assets/bridge/gateway/tool-discovery.js +198 -0
  21. package/assets/bridge/index.d.ts +2 -0
  22. package/assets/bridge/index.js +253 -0
  23. package/assets/bridge/logger.d.ts +2 -0
  24. package/assets/bridge/logger.js +2 -0
  25. package/assets/bridge/provider.d.ts +156 -0
  26. package/assets/bridge/provider.js +2 -0
  27. package/assets/bridge/providers/emergency-commands.d.ts +54 -0
  28. package/assets/bridge/providers/emergency-commands.js +235 -0
  29. package/assets/bridge/providers/gateway.d.ts +322 -0
  30. package/assets/bridge/providers/gateway.js +2302 -0
  31. package/assets/bridge/providers/mock.d.ts +37 -0
  32. package/assets/bridge/providers/mock.js +385 -0
  33. package/assets/bridge/providers/onboarding-commands.d.ts +83 -0
  34. package/assets/bridge/providers/onboarding-commands.js +213 -0
  35. package/assets/bridge/providers/risk-calculator.d.ts +96 -0
  36. package/assets/bridge/providers/risk-calculator.js +369 -0
  37. package/assets/bridge/setup.d.ts +32 -0
  38. package/assets/bridge/setup.js +226 -0
  39. package/assets/bridge/types.d.ts +584 -0
  40. package/assets/bridge/types.js +50 -0
  41. package/assets/bridge/utils/reconnect.d.ts +6 -0
  42. package/assets/bridge/utils/reconnect.js +6 -0
  43. package/assets/bridge/utils/skill-signing.d.ts +51 -0
  44. package/assets/bridge/utils/skill-signing.js +138 -0
  45. package/assets/bridge/utils/skill-version.d.ts +17 -0
  46. package/assets/bridge/utils/skill-version.js +71 -0
  47. package/assets/plugin/audit/mode-transition-audit.d.ts +11 -0
  48. package/assets/plugin/audit/mode-transition-audit.js +29 -0
  49. package/assets/plugin/balance-utils.d.ts +36 -0
  50. package/assets/plugin/balance-utils.js +98 -0
  51. package/assets/plugin/ccxt/binance-ban-gate.d.ts +47 -0
  52. package/assets/plugin/ccxt/binance-ban-gate.js +409 -0
  53. package/assets/plugin/ccxt/binance-private.d.ts +325 -0
  54. package/assets/plugin/ccxt/binance-private.js +1415 -0
  55. package/assets/plugin/ccxt/binance-public.d.ts +18 -0
  56. package/assets/plugin/ccxt/binance-public.js +147 -0
  57. package/assets/plugin/config/agent-config-client.d.ts +55 -0
  58. package/assets/plugin/config/agent-config-client.js +145 -0
  59. package/assets/plugin/config/agent-config-poller.d.ts +25 -0
  60. package/assets/plugin/config/agent-config-poller.js +100 -0
  61. package/assets/plugin/config/brackets-config.d.ts +22 -0
  62. package/assets/plugin/config/brackets-config.js +58 -0
  63. package/assets/plugin/config/gate-store.d.ts +18 -0
  64. package/assets/plugin/config/gate-store.js +61 -0
  65. package/assets/plugin/config/plugin-config-io.d.ts +175 -0
  66. package/assets/plugin/config/plugin-config-io.js +84 -0
  67. package/assets/plugin/config/position-review-config.d.ts +35 -0
  68. package/assets/plugin/config/position-review-config.js +105 -0
  69. package/assets/plugin/config/tool-gate.d.ts +53 -0
  70. package/assets/plugin/config/tool-gate.js +125 -0
  71. package/assets/plugin/config/user-data-stream-config.d.ts +85 -0
  72. package/assets/plugin/config/user-data-stream-config.js +224 -0
  73. package/assets/plugin/exchange-adapter.d.ts +49 -0
  74. package/assets/plugin/exchange-adapter.js +4 -0
  75. package/assets/plugin/index.d.ts +30 -0
  76. package/assets/plugin/index.js +1989 -0
  77. package/assets/plugin/ingest/pending-entry-metadata.d.ts +52 -0
  78. package/assets/plugin/ingest/pending-entry-metadata.js +182 -0
  79. package/assets/plugin/ingest/position-auto-capture.d.ts +98 -0
  80. package/assets/plugin/ingest/position-auto-capture.js +394 -0
  81. package/assets/plugin/ingest/position-decisions-client.d.ts +318 -0
  82. package/assets/plugin/ingest/position-decisions-client.js +296 -0
  83. package/assets/plugin/ingest/reconcile-db-vs-exchange.d.ts +13 -0
  84. package/assets/plugin/ingest/reconcile-db-vs-exchange.js +114 -0
  85. package/assets/plugin/ingest/reconciler-cleanup.d.ts +37 -0
  86. package/assets/plugin/ingest/reconciler-cleanup.js +147 -0
  87. package/assets/plugin/ingest/rest-gap-filler.d.ts +191 -0
  88. package/assets/plugin/ingest/rest-gap-filler.js +565 -0
  89. package/assets/plugin/ingest/touched-symbols-store.d.ts +25 -0
  90. package/assets/plugin/ingest/touched-symbols-store.js +96 -0
  91. package/assets/plugin/ingest/trade-store-client.d.ts +40 -0
  92. package/assets/plugin/ingest/trade-store-client.js +125 -0
  93. package/assets/plugin/ingest/ws-ingest.d.ts +43 -0
  94. package/assets/plugin/ingest/ws-ingest.js +126 -0
  95. package/assets/plugin/learning/setup-family.d.ts +21 -0
  96. package/assets/plugin/learning/setup-family.js +103 -0
  97. package/assets/plugin/lifecycle/install-signal-handlers.d.ts +33 -0
  98. package/assets/plugin/lifecycle/install-signal-handlers.js +112 -0
  99. package/assets/plugin/lifecycle/shutdown-coordinator.d.ts +43 -0
  100. package/assets/plugin/lifecycle/shutdown-coordinator.js +131 -0
  101. package/assets/plugin/live/bracket-id.d.ts +18 -0
  102. package/assets/plugin/live/bracket-id.js +81 -0
  103. package/assets/plugin/live/bracket-ledger.d.ts +54 -0
  104. package/assets/plugin/live/bracket-ledger.js +267 -0
  105. package/assets/plugin/live/bracket-manager.d.ts +82 -0
  106. package/assets/plugin/live/bracket-manager.js +478 -0
  107. package/assets/plugin/live/bracket-params.d.ts +22 -0
  108. package/assets/plugin/live/bracket-params.js +124 -0
  109. package/assets/plugin/live/bracket-reconciler.d.ts +95 -0
  110. package/assets/plugin/live/bracket-reconciler.js +573 -0
  111. package/assets/plugin/live/bracket-types.d.ts +102 -0
  112. package/assets/plugin/live/bracket-types.js +8 -0
  113. package/assets/plugin/live/deposit-tracker.d.ts +62 -0
  114. package/assets/plugin/live/deposit-tracker.js +97 -0
  115. package/assets/plugin/live/emergency-controls.d.ts +32 -0
  116. package/assets/plugin/live/emergency-controls.js +226 -0
  117. package/assets/plugin/live/exchange-errors.d.ts +12 -0
  118. package/assets/plugin/live/exchange-errors.js +130 -0
  119. package/assets/plugin/live/exchange-info-cache.d.ts +35 -0
  120. package/assets/plugin/live/exchange-info-cache.js +119 -0
  121. package/assets/plugin/live/fact-subscriber.d.ts +78 -0
  122. package/assets/plugin/live/fact-subscriber.js +182 -0
  123. package/assets/plugin/live/intent-journal.d.ts +42 -0
  124. package/assets/plugin/live/intent-journal.js +122 -0
  125. package/assets/plugin/live/listen-key-manager.d.ts +70 -0
  126. package/assets/plugin/live/listen-key-manager.js +169 -0
  127. package/assets/plugin/live/live-adapter.d.ts +264 -0
  128. package/assets/plugin/live/live-adapter.js +1665 -0
  129. package/assets/plugin/live/live-balance-enricher.d.ts +32 -0
  130. package/assets/plugin/live/live-balance-enricher.js +104 -0
  131. package/assets/plugin/live/live-bracket-api.d.ts +13 -0
  132. package/assets/plugin/live/live-bracket-api.js +20 -0
  133. package/assets/plugin/live/live-state-store.d.ts +194 -0
  134. package/assets/plugin/live/live-state-store.js +450 -0
  135. package/assets/plugin/live/local-signal-service.d.ts +57 -0
  136. package/assets/plugin/live/local-signal-service.js +146 -0
  137. package/assets/plugin/live/local-strategy-evaluator.d.ts +62 -0
  138. package/assets/plugin/live/local-strategy-evaluator.js +127 -0
  139. package/assets/plugin/live/microstructure-assembler.d.ts +54 -0
  140. package/assets/plugin/live/microstructure-assembler.js +148 -0
  141. package/assets/plugin/live/order-poller.d.ts +29 -0
  142. package/assets/plugin/live/order-poller.js +125 -0
  143. package/assets/plugin/live/position-state-store.d.ts +83 -0
  144. package/assets/plugin/live/position-state-store.js +237 -0
  145. package/assets/plugin/live/proposal-decision-listener.d.ts +64 -0
  146. package/assets/plugin/live/proposal-decision-listener.js +288 -0
  147. package/assets/plugin/live/proposal-manager.d.ts +76 -0
  148. package/assets/plugin/live/proposal-manager.js +140 -0
  149. package/assets/plugin/live/rate-limiter.d.ts +47 -0
  150. package/assets/plugin/live/rate-limiter.js +159 -0
  151. package/assets/plugin/live/reconciler.d.ts +39 -0
  152. package/assets/plugin/live/reconciler.js +175 -0
  153. package/assets/plugin/live/setup-buckets.d.ts +7 -0
  154. package/assets/plugin/live/setup-buckets.js +33 -0
  155. package/assets/plugin/live/slippage-tracker.d.ts +45 -0
  156. package/assets/plugin/live/slippage-tracker.js +78 -0
  157. package/assets/plugin/live/stop-watcher.d.ts +34 -0
  158. package/assets/plugin/live/stop-watcher.js +158 -0
  159. package/assets/plugin/live/user-data-active-probe.d.ts +54 -0
  160. package/assets/plugin/live/user-data-active-probe.js +180 -0
  161. package/assets/plugin/live/user-data-stream-controller.d.ts +200 -0
  162. package/assets/plugin/live/user-data-stream-controller.js +579 -0
  163. package/assets/plugin/live/user-data-stream-ws.d.ts +22 -0
  164. package/assets/plugin/live/user-data-stream-ws.js +63 -0
  165. package/assets/plugin/live/user-data-stream.d.ts +243 -0
  166. package/assets/plugin/live/user-data-stream.js +704 -0
  167. package/assets/plugin/logger.d.ts +2 -0
  168. package/assets/plugin/logger.js +2 -0
  169. package/assets/plugin/mfe.d.ts +21 -0
  170. package/assets/plugin/mfe.js +68 -0
  171. package/assets/plugin/onboarding/mode-ladder.d.ts +1 -0
  172. package/assets/plugin/onboarding/mode-ladder.js +3 -0
  173. package/assets/plugin/onboarding/runtime.d.ts +71 -0
  174. package/assets/plugin/onboarding/runtime.js +153 -0
  175. package/assets/plugin/openclaw.plugin.json +91 -0
  176. package/assets/plugin/paper-adapter.d.ts +24 -0
  177. package/assets/plugin/paper-adapter.js +91 -0
  178. package/assets/plugin/persistence/state-manager.d.ts +42 -0
  179. package/assets/plugin/persistence/state-manager.js +164 -0
  180. package/assets/plugin/pinned-plan.d.ts +9 -0
  181. package/assets/plugin/pinned-plan.js +23 -0
  182. package/assets/plugin/risk/pre-trade-check.d.ts +38 -0
  183. package/assets/plugin/risk/pre-trade-check.js +345 -0
  184. package/assets/plugin/risk/pre-trade-types.d.ts +60 -0
  185. package/assets/plugin/risk/pre-trade-types.js +3 -0
  186. package/assets/plugin/shadow/shadow-tracker.d.ts +36 -0
  187. package/assets/plugin/shadow/shadow-tracker.js +151 -0
  188. package/assets/plugin/shadow/types.d.ts +42 -0
  189. package/assets/plugin/shadow/types.js +20 -0
  190. package/assets/plugin/shared/indicators-extended.d.ts +52 -0
  191. package/assets/plugin/shared/indicators-extended.js +291 -0
  192. package/assets/plugin/shared/indicators.d.ts +15 -0
  193. package/assets/plugin/shared/indicators.js +114 -0
  194. package/assets/plugin/signals/conditions/registry.d.ts +16 -0
  195. package/assets/plugin/signals/conditions/registry.js +1274 -0
  196. package/assets/plugin/signals/conditions/types.d.ts +1 -0
  197. package/assets/plugin/signals/conditions/types.js +4 -0
  198. package/assets/plugin/signals/direction-rules.d.ts +3 -0
  199. package/assets/plugin/signals/direction-rules.js +24 -0
  200. package/assets/plugin/signals/entry-rules.d.ts +6 -0
  201. package/assets/plugin/signals/entry-rules.js +33 -0
  202. package/assets/plugin/signals/serialize-context.d.ts +4 -0
  203. package/assets/plugin/signals/serialize-context.js +39 -0
  204. package/assets/plugin/signals/stop-rules.d.ts +3 -0
  205. package/assets/plugin/signals/stop-rules.js +48 -0
  206. package/assets/plugin/signals/strategy-adapter.d.ts +14 -0
  207. package/assets/plugin/signals/strategy-adapter.js +122 -0
  208. package/assets/plugin/signals/types.d.ts +1 -0
  209. package/assets/plugin/signals/types.js +8 -0
  210. package/assets/plugin/simulator/exchange-simulator.d.ts +93 -0
  211. package/assets/plugin/simulator/exchange-simulator.js +684 -0
  212. package/assets/plugin/simulator/fill-engine.d.ts +53 -0
  213. package/assets/plugin/simulator/fill-engine.js +276 -0
  214. package/assets/plugin/simulator/paper-market-feed.d.ts +26 -0
  215. package/assets/plugin/simulator/paper-market-feed.js +104 -0
  216. package/assets/plugin/simulator/realistic-fills.d.ts +59 -0
  217. package/assets/plugin/simulator/realistic-fills.js +175 -0
  218. package/assets/plugin/simulator/types.d.ts +219 -0
  219. package/assets/plugin/simulator/types.js +43 -0
  220. package/assets/plugin/strategy/builtin-strategies.d.ts +2 -0
  221. package/assets/plugin/strategy/builtin-strategies.js +109 -0
  222. package/assets/plugin/strategy/condition-registry.d.ts +3 -0
  223. package/assets/plugin/strategy/condition-registry.js +153 -0
  224. package/assets/plugin/strategy/evaluator.d.ts +67 -0
  225. package/assets/plugin/strategy/evaluator.js +93 -0
  226. package/assets/plugin/tools/assessment-validation.d.ts +118 -0
  227. package/assets/plugin/tools/assessment-validation.js +415 -0
  228. package/assets/plugin/tools/attach-brackets.d.ts +34 -0
  229. package/assets/plugin/tools/attach-brackets.js +363 -0
  230. package/assets/plugin/tools/audit-bracket-protection.d.ts +49 -0
  231. package/assets/plugin/tools/audit-bracket-protection.js +527 -0
  232. package/assets/plugin/tools/cancel-all-orders.d.ts +7 -0
  233. package/assets/plugin/tools/cancel-all-orders.js +5 -0
  234. package/assets/plugin/tools/cancel-order.d.ts +10 -0
  235. package/assets/plugin/tools/cancel-order.js +14 -0
  236. package/assets/plugin/tools/check-position-health.d.ts +46 -0
  237. package/assets/plugin/tools/check-position-health.js +194 -0
  238. package/assets/plugin/tools/clear-exchange-credentials.d.ts +24 -0
  239. package/assets/plugin/tools/clear-exchange-credentials.js +70 -0
  240. package/assets/plugin/tools/close-position.d.ts +22 -0
  241. package/assets/plugin/tools/close-position.js +449 -0
  242. package/assets/plugin/tools/create-order.d.ts +54 -0
  243. package/assets/plugin/tools/create-order.js +338 -0
  244. package/assets/plugin/tools/exit-gate.d.ts +58 -0
  245. package/assets/plugin/tools/exit-gate.js +162 -0
  246. package/assets/plugin/tools/fetch-balance.d.ts +5 -0
  247. package/assets/plugin/tools/fetch-balance.js +4 -0
  248. package/assets/plugin/tools/fetch-ohlcv.d.ts +11 -0
  249. package/assets/plugin/tools/fetch-ohlcv.js +8 -0
  250. package/assets/plugin/tools/fetch-open-orders.d.ts +7 -0
  251. package/assets/plugin/tools/fetch-open-orders.js +4 -0
  252. package/assets/plugin/tools/fetch-positions.d.ts +7 -0
  253. package/assets/plugin/tools/fetch-positions.js +4 -0
  254. package/assets/plugin/tools/fetch-ticker.d.ts +11 -0
  255. package/assets/plugin/tools/fetch-ticker.js +5 -0
  256. package/assets/plugin/tools/get-agent-profile.d.ts +4 -0
  257. package/assets/plugin/tools/get-agent-profile.js +6 -0
  258. package/assets/plugin/tools/get-analytics.d.ts +6 -0
  259. package/assets/plugin/tools/get-analytics.js +7 -0
  260. package/assets/plugin/tools/get-backtest.d.ts +12 -0
  261. package/assets/plugin/tools/get-backtest.js +91 -0
  262. package/assets/plugin/tools/get-basis.d.ts +7 -0
  263. package/assets/plugin/tools/get-basis.js +7 -0
  264. package/assets/plugin/tools/get-bracket-config.d.ts +11 -0
  265. package/assets/plugin/tools/get-bracket-config.js +24 -0
  266. package/assets/plugin/tools/get-cascade-risk.d.ts +7 -0
  267. package/assets/plugin/tools/get-cascade-risk.js +8 -0
  268. package/assets/plugin/tools/get-crypto-metrics.d.ts +18 -0
  269. package/assets/plugin/tools/get-crypto-metrics.js +45 -0
  270. package/assets/plugin/tools/get-cvd.d.ts +6 -0
  271. package/assets/plugin/tools/get-cvd.js +6 -0
  272. package/assets/plugin/tools/get-divergences.d.ts +6 -0
  273. package/assets/plugin/tools/get-divergences.js +6 -0
  274. package/assets/plugin/tools/get-funding-context.d.ts +6 -0
  275. package/assets/plugin/tools/get-funding-context.js +16 -0
  276. package/assets/plugin/tools/get-liquidation-levels.d.ts +7 -0
  277. package/assets/plugin/tools/get-liquidation-levels.js +7 -0
  278. package/assets/plugin/tools/get-liquidation-pulse.d.ts +9 -0
  279. package/assets/plugin/tools/get-liquidation-pulse.js +22 -0
  280. package/assets/plugin/tools/get-market-breadth.d.ts +6 -0
  281. package/assets/plugin/tools/get-market-breadth.js +8 -0
  282. package/assets/plugin/tools/get-market-intel.d.ts +19 -0
  283. package/assets/plugin/tools/get-market-intel.js +116 -0
  284. package/assets/plugin/tools/get-market-structure.d.ts +47 -0
  285. package/assets/plugin/tools/get-market-structure.js +198 -0
  286. package/assets/plugin/tools/get-my-mined-patterns.d.ts +20 -0
  287. package/assets/plugin/tools/get-my-mined-patterns.js +61 -0
  288. package/assets/plugin/tools/get-my-proposed-learnings.d.ts +20 -0
  289. package/assets/plugin/tools/get-my-proposed-learnings.js +55 -0
  290. package/assets/plugin/tools/get-my-recent-reviews.d.ts +22 -0
  291. package/assets/plugin/tools/get-my-recent-reviews.js +66 -0
  292. package/assets/plugin/tools/get-orderbook.d.ts +21 -0
  293. package/assets/plugin/tools/get-orderbook.js +32 -0
  294. package/assets/plugin/tools/get-pattern-scan.d.ts +7 -0
  295. package/assets/plugin/tools/get-pattern-scan.js +8 -0
  296. package/assets/plugin/tools/get-regime.d.ts +6 -0
  297. package/assets/plugin/tools/get-regime.js +7 -0
  298. package/assets/plugin/tools/get-relevant-learnings.d.ts +21 -0
  299. package/assets/plugin/tools/get-relevant-learnings.js +65 -0
  300. package/assets/plugin/tools/get-resting-liquidity.d.ts +6 -0
  301. package/assets/plugin/tools/get-resting-liquidity.js +11 -0
  302. package/assets/plugin/tools/get-risk-scenario.d.ts +29 -0
  303. package/assets/plugin/tools/get-risk-scenario.js +47 -0
  304. package/assets/plugin/tools/get-risk-summary.d.ts +51 -0
  305. package/assets/plugin/tools/get-risk-summary.js +118 -0
  306. package/assets/plugin/tools/get-sentiment.d.ts +4 -0
  307. package/assets/plugin/tools/get-sentiment.js +6 -0
  308. package/assets/plugin/tools/get-session-review.d.ts +7 -0
  309. package/assets/plugin/tools/get-session-review.js +8 -0
  310. package/assets/plugin/tools/get-setup-detail.d.ts +7 -0
  311. package/assets/plugin/tools/get-setup-detail.js +303 -0
  312. package/assets/plugin/tools/get-signals.d.ts +15 -0
  313. package/assets/plugin/tools/get-signals.js +54 -0
  314. package/assets/plugin/tools/get-sizing.d.ts +6 -0
  315. package/assets/plugin/tools/get-sizing.js +6 -0
  316. package/assets/plugin/tools/get-trade-feedback.d.ts +7 -0
  317. package/assets/plugin/tools/get-trade-feedback.js +8 -0
  318. package/assets/plugin/tools/get-trade-flow.d.ts +7 -0
  319. package/assets/plugin/tools/get-trade-flow.js +7 -0
  320. package/assets/plugin/tools/get-volume-analysis.d.ts +21 -0
  321. package/assets/plugin/tools/get-volume-analysis.js +74 -0
  322. package/assets/plugin/tools/get-volume-profile.d.ts +7 -0
  323. package/assets/plugin/tools/get-volume-profile.js +7 -0
  324. package/assets/plugin/tools/helpers.d.ts +25 -0
  325. package/assets/plugin/tools/helpers.js +38 -0
  326. package/assets/plugin/tools/intel-api.d.ts +14 -0
  327. package/assets/plugin/tools/intel-api.js +52 -0
  328. package/assets/plugin/tools/intel-cache.d.ts +25 -0
  329. package/assets/plugin/tools/intel-cache.js +133 -0
  330. package/assets/plugin/tools/list-strategies.d.ts +7 -0
  331. package/assets/plugin/tools/list-strategies.js +6 -0
  332. package/assets/plugin/tools/modify-stop.d.ts +17 -0
  333. package/assets/plugin/tools/modify-stop.js +81 -0
  334. package/assets/plugin/tools/modify-target.d.ts +17 -0
  335. package/assets/plugin/tools/modify-target.js +71 -0
  336. package/assets/plugin/tools/propose-learning.d.ts +22 -0
  337. package/assets/plugin/tools/propose-learning.js +65 -0
  338. package/assets/plugin/tools/query-review-outcomes.d.ts +30 -0
  339. package/assets/plugin/tools/query-review-outcomes.js +64 -0
  340. package/assets/plugin/tools/query-trades.d.ts +21 -0
  341. package/assets/plugin/tools/query-trades.js +37 -0
  342. package/assets/plugin/tools/record-position-reviews.d.ts +38 -0
  343. package/assets/plugin/tools/record-position-reviews.js +147 -0
  344. package/assets/plugin/tools/save-strategy.d.ts +16 -0
  345. package/assets/plugin/tools/save-strategy.js +46 -0
  346. package/assets/plugin/tools/scan-pairs.d.ts +18 -0
  347. package/assets/plugin/tools/scan-pairs.js +220 -0
  348. package/assets/plugin/tools/score-setup.d.ts +31 -0
  349. package/assets/plugin/tools/score-setup.js +268 -0
  350. package/assets/plugin/tools/set-bracket-requirement.d.ts +18 -0
  351. package/assets/plugin/tools/set-bracket-requirement.js +81 -0
  352. package/assets/plugin/tools/set-exchange-credentials.d.ts +25 -0
  353. package/assets/plugin/tools/set-exchange-credentials.js +80 -0
  354. package/assets/plugin/tools/set-trading-mode.d.ts +26 -0
  355. package/assets/plugin/tools/set-trading-mode.js +135 -0
  356. package/assets/plugin/tools/test-exchange-credentials.d.ts +16 -0
  357. package/assets/plugin/tools/test-exchange-credentials.js +100 -0
  358. package/assets/plugin/tools/toggle-strategy.d.ts +8 -0
  359. package/assets/plugin/tools/toggle-strategy.js +8 -0
  360. package/assets/plugin/trading-params-cache.d.ts +26 -0
  361. package/assets/plugin/trading-params-cache.js +52 -0
  362. package/assets/plugin/types.d.ts +110 -0
  363. package/assets/plugin/types.js +7 -0
  364. package/assets/plugin/util/plugin-paths.d.ts +3 -0
  365. package/assets/plugin/util/plugin-paths.js +15 -0
  366. package/assets/shared/fills.d.ts +36 -0
  367. package/assets/shared/fills.js +20 -0
  368. package/assets/shared/index.d.ts +10 -0
  369. package/assets/shared/index.js +5 -0
  370. package/assets/shared/logger.d.ts +10 -0
  371. package/assets/shared/logger.js +34 -0
  372. package/assets/shared/protocol.d.ts +29 -0
  373. package/assets/shared/protocol.js +14 -0
  374. package/assets/shared/redact.d.ts +6 -0
  375. package/assets/shared/redact.js +31 -0
  376. package/assets/shared/shared/indicators-extended.d.ts +52 -0
  377. package/assets/shared/shared/indicators-extended.js +284 -0
  378. package/assets/shared/shared/indicators.d.ts +15 -0
  379. package/assets/shared/shared/indicators.js +107 -0
  380. package/assets/shared/signals/conditions/registry.d.ts +16 -0
  381. package/assets/shared/signals/conditions/registry.js +1267 -0
  382. package/assets/shared/signals/conditions/types.d.ts +65 -0
  383. package/assets/shared/signals/conditions/types.js +8 -0
  384. package/assets/shared/signals/direction-rules.d.ts +3 -0
  385. package/assets/shared/signals/direction-rules.js +18 -0
  386. package/assets/shared/signals/entry-rules.d.ts +6 -0
  387. package/assets/shared/signals/entry-rules.js +26 -0
  388. package/assets/shared/signals/indicators-extended.d.ts +52 -0
  389. package/assets/shared/signals/indicators-extended.js +284 -0
  390. package/assets/shared/signals/indicators.d.ts +15 -0
  391. package/assets/shared/signals/indicators.js +107 -0
  392. package/assets/shared/signals/serialize-context.d.ts +4 -0
  393. package/assets/shared/signals/serialize-context.js +41 -0
  394. package/assets/shared/signals/stop-rules.d.ts +3 -0
  395. package/assets/shared/signals/stop-rules.js +41 -0
  396. package/assets/shared/signals/strategy-adapter.d.ts +14 -0
  397. package/assets/shared/signals/strategy-adapter.js +117 -0
  398. package/assets/shared/signals/types.d.ts +187 -0
  399. package/assets/shared/signals/types.js +9 -0
  400. package/assets/shared/trading-mode.d.ts +16 -0
  401. package/assets/shared/trading-mode.js +49 -0
  402. package/assets/skill/SKILL.md +1357 -0
  403. package/dist/bridge.js +25 -0
  404. package/dist/bundle.js +33 -0
  405. package/dist/cli.js +120 -0
  406. package/dist/daemon.js +104 -0
  407. package/dist/deps.js +41 -0
  408. package/dist/exec.js +28 -0
  409. package/dist/openclaw.js +153 -0
  410. package/dist/paths.js +23 -0
  411. package/dist/plugin.js +48 -0
  412. package/dist/skill.js +35 -0
  413. package/dist/ui.js +29 -0
  414. package/dist/validate.js +28 -0
  415. package/package.json +32 -0
@@ -0,0 +1,41 @@
1
+ // Stop level calculation for declarative strategies.
2
+ import { findSwingPoints } from '../shared/indicators.js';
3
+ export function computeStop(rule, ctx, direction, condCtx) {
4
+ switch (rule.type) {
5
+ case 'swing_atr': {
6
+ const swingCount = rule.params.swingCount ?? 3;
7
+ const atrFraction = (rule.params.atrFraction ?? 0.3) * ctx.atr14;
8
+ const fallbackAtrMult = rule.params.fallbackAtrMultiple ?? 2;
9
+ const swings = findSwingPoints(ctx.ohlcv1h.slice(-48), 3);
10
+ if (direction === 'LONG') {
11
+ return swings.lows.length > 0
12
+ ? Math.min(...swings.lows.slice(-swingCount)) - atrFraction
13
+ : ctx.currentPrice - ctx.atr14 * fallbackAtrMult;
14
+ }
15
+ else {
16
+ return swings.highs.length > 0
17
+ ? Math.max(...swings.highs.slice(-swingCount)) + atrFraction
18
+ : ctx.currentPrice + ctx.atr14 * fallbackAtrMult;
19
+ }
20
+ }
21
+ case 'recent_extreme_atr': {
22
+ const atrFraction = (rule.params.atrFraction ?? 0.5) * ctx.atr14;
23
+ if (direction === 'LONG') {
24
+ return (condCtx.recentLow ?? ctx.currentPrice - ctx.atr14) - atrFraction;
25
+ }
26
+ else {
27
+ return (condCtx.recentHigh ?? ctx.currentPrice + ctx.atr14) + atrFraction;
28
+ }
29
+ }
30
+ case 'fixed_atr': {
31
+ const atrMultiple = rule.params.atrMultiple ?? 1.5;
32
+ return direction === 'LONG'
33
+ ? ctx.currentPrice - ctx.atr14 * atrMultiple
34
+ : ctx.currentPrice + ctx.atr14 * atrMultiple;
35
+ }
36
+ default:
37
+ return direction === 'LONG'
38
+ ? ctx.currentPrice - ctx.atr14 * 2
39
+ : ctx.currentPrice + ctx.atr14 * 2;
40
+ }
41
+ }
@@ -0,0 +1,14 @@
1
+ import type { StrategyDefinition } from './types.js';
2
+ import type { StrategyConfig } from './conditions/types.js';
3
+ export declare function clearStrategyGatingState(): void;
4
+ /**
5
+ * Convert a declarative StrategyConfig into a StrategyDefinition
6
+ * that the signal engine and backtest engine can evaluate.
7
+ *
8
+ * `gateNamespace` scopes the module-global HTF tick-gate to the calling tenant
9
+ * (pass the strategy-cache key — `global` for the background pass, the user key
10
+ * for a per-user pass). Omit it for single-tenant callers (backtests, the
11
+ * plugin's local evaluator) — they keep the original un-namespaced gate key,
12
+ * so their behaviour is byte-identical.
13
+ */
14
+ export declare function adaptStrategy(config: StrategyConfig, gateNamespace?: string): StrategyDefinition;
@@ -0,0 +1,117 @@
1
+ // Strategy adapter — converts declarative StrategyConfig (JSON) to StrategyDefinition.
2
+ // This bridges the DB-stored configs to the evaluate() interface the engines use.
3
+ import { evaluateConditions } from './conditions/registry.js';
4
+ import { resolveDirection } from './direction-rules.js';
5
+ import { computeEntry } from './entry-rules.js';
6
+ import { computeStop } from './stop-rules.js';
7
+ /**
8
+ * Higher-timeframe tick gating state, keyed by
9
+ * `${gateNamespace}\x1f${strategyName}:${symbol}`.
10
+ *
11
+ * Lives at module scope because the strategy cache (strategy-store.ts) builds
12
+ * a fresh adapter every 60s, so closure-based state would reset constantly
13
+ * and 1d-primary strategies would re-evaluate on every tick instead of once
14
+ * per closed bar. Module-level state survives the cache refresh.
15
+ *
16
+ * ★ Multi-tenant: because the map is module-global, the key MUST carry a
17
+ * per-tenant `gateNamespace` (the caller's strategy-cache key — `global` vs a
18
+ * per-user key). Without it, two tenants running same-named strategies on the
19
+ * same symbol shared ONE gate: whichever evaluated first marked the bar
20
+ * consumed and the other tenant's strategy was silently SKIPPED until the next
21
+ * primary-timeframe bar closed (a full day for a 1d strategy). A `\x1f` (unit
22
+ * separator, never present in a UUID / strategy name / symbol) joins the
23
+ * namespace so distinct tenants can never collide.
24
+ *
25
+ * Backtests should call `clearStrategyGatingState()` between runs to avoid
26
+ * leakage across tests / sequential backtests.
27
+ */
28
+ const lastEvaluatedBarTime = new Map();
29
+ export function clearStrategyGatingState() {
30
+ lastEvaluatedBarTime.clear();
31
+ }
32
+ function pickTimeframeBars(ctx, tf) {
33
+ if (tf === '1d')
34
+ return ctx.ohlcv1d ?? [];
35
+ if (tf === '4h')
36
+ return ctx.ohlcv4h ?? [];
37
+ return ctx.ohlcv1h;
38
+ }
39
+ /** Empty no-signal evaluation — used when gating skips a strategy. */
40
+ const SKIPPED = { direction: null, conditions: [], trade: undefined };
41
+ /**
42
+ * Convert a declarative StrategyConfig into a StrategyDefinition
43
+ * that the signal engine and backtest engine can evaluate.
44
+ *
45
+ * `gateNamespace` scopes the module-global HTF tick-gate to the calling tenant
46
+ * (pass the strategy-cache key — `global` for the background pass, the user key
47
+ * for a per-user pass). Omit it for single-tenant callers (backtests, the
48
+ * plugin's local evaluator) — they keep the original un-namespaced gate key,
49
+ * so their behaviour is byte-identical.
50
+ */
51
+ export function adaptStrategy(config, gateNamespace) {
52
+ return {
53
+ name: config.name,
54
+ regimeWhitelist: config.regimeWhitelist,
55
+ minConfidence: config.minConfidence,
56
+ async evaluate(ctx) {
57
+ // ─── Tick gating ───────────────────────────────────────────────
58
+ // Only re-evaluate when a new bar of the strategy's primary
59
+ // timeframe has closed since the last evaluation for this
60
+ // strategy/symbol pair. Defaults to '1h' so existing strategies
61
+ // are unaffected.
62
+ const tf = config.primaryTimeframe ?? '1h';
63
+ const tfBars = pickTimeframeBars(ctx, tf);
64
+ if (tfBars.length === 0)
65
+ return SKIPPED;
66
+ const latestBarTime = tfBars[tfBars.length - 1].time.getTime();
67
+ // Namespaced per tenant (see lastEvaluatedBarTime doc) so same-named
68
+ // strategies across tenants never share a gate. Un-namespaced when
69
+ // gateNamespace is omitted — byte-identical to the original key.
70
+ const gateKey = gateNamespace
71
+ ? `${gateNamespace}\x1f${config.name}:${ctx.symbol}`
72
+ : `${config.name}:${ctx.symbol}`;
73
+ const lastBarTime = lastEvaluatedBarTime.get(gateKey);
74
+ if (lastBarTime !== undefined && latestBarTime === lastBarTime) {
75
+ return SKIPPED;
76
+ }
77
+ lastEvaluatedBarTime.set(gateKey, latestBarTime);
78
+ // ─── SkipIf gates ──────────────────────────────────────────────
79
+ // Portfolio-wide / cross-symbol filters. If any are met the
80
+ // strategy is skipped this tick. Evaluated before main conditions
81
+ // so the bulk of the work is short-circuited.
82
+ if (config.skipIf && config.skipIf.length > 0) {
83
+ const { conditions: skipResults } = evaluateConditions(config.skipIf, ctx, null);
84
+ if (skipResults.some(c => c.met))
85
+ return SKIPPED;
86
+ }
87
+ // Pass 1: evaluate conditions with direction = null
88
+ const { conditions: pass1, condCtx } = evaluateConditions(config.conditions, ctx, null);
89
+ // Determine direction
90
+ const direction = resolveDirection(config.directionRule, ctx, condCtx);
91
+ // Pass 2: re-evaluate direction-sensitive conditions now that we know direction
92
+ // (orderbook_imbalance and funding_contrarian behave differently per direction)
93
+ const directionSensitive = new Set(['orderbook_imbalance', 'funding_contrarian', 'funding_extreme_skip', 'funding_position_ok', 'return_momentum']);
94
+ const hasDirSensitive = config.conditions.some(c => directionSensitive.has(c.type));
95
+ let finalConditions = pass1;
96
+ if (direction && hasDirSensitive) {
97
+ const { conditions: pass2 } = evaluateConditions(config.conditions, ctx, direction);
98
+ // Merge: use pass2 results for direction-sensitive, pass1 for others
99
+ finalConditions = pass1.map((c, i) => directionSensitive.has(config.conditions[i].type) ? pass2[i] : c);
100
+ }
101
+ const allMet = finalConditions.every(c => c.met);
102
+ let trade;
103
+ if (allMet && direction) {
104
+ const entryZone = computeEntry(config.entryRule, ctx, direction, condCtx);
105
+ const stopLevel = computeStop(config.stopRule, ctx, direction, condCtx);
106
+ const risk = direction === 'LONG'
107
+ ? ctx.currentPrice - stopLevel
108
+ : stopLevel - ctx.currentPrice;
109
+ const targets = config.targetRMultiples.map(rm => direction === 'LONG'
110
+ ? ctx.currentPrice + risk * rm
111
+ : ctx.currentPrice - risk * rm);
112
+ trade = { entryZone, stopLevel, targets };
113
+ }
114
+ return { direction, conditions: finalConditions, trade };
115
+ },
116
+ };
117
+ }
@@ -0,0 +1,187 @@
1
+ export type Direction = 'LONG' | 'SHORT';
2
+ export interface OhlcvBar {
3
+ time: Date;
4
+ open: number;
5
+ high: number;
6
+ low: number;
7
+ close: number;
8
+ volume: number;
9
+ }
10
+ export interface TradeFlowBucket {
11
+ bucket: Date;
12
+ buyVolume: number;
13
+ sellVolume: number;
14
+ netDelta: number;
15
+ largeBuyVolume: number;
16
+ largeSellVolume: number;
17
+ }
18
+ /** Market-wide signals not tied to a single symbol. Computed once per tick
19
+ * by the engine and attached to every per-symbol MarketContext. */
20
+ export interface GlobalMarketContext {
21
+ /** RSI(14) of BTCUSDT on the 1d timeframe, latest closed bar. */
22
+ btcRsi14_1d?: number;
23
+ /** Timestamp of the latest 1d BTC bar used for the RSI calc. */
24
+ btcRsi14_1d_at?: Date;
25
+ }
26
+ /**
27
+ * Market data context shared across all strategy evaluations in a single tick.
28
+ * Fetched once per tick, shared across strategies to avoid duplicate queries.
29
+ */
30
+ export interface MarketContext {
31
+ symbol: string;
32
+ timestamp: Date;
33
+ /** 1h OHLCV bars, oldest first */
34
+ ohlcv1h: OhlcvBar[];
35
+ /** 5m OHLCV bars, oldest first (for swing detection) */
36
+ ohlcv5m: OhlcvBar[];
37
+ /** Current price */
38
+ currentPrice: number;
39
+ /** Current ATR(14) on 1h */
40
+ atr14: number;
41
+ /** Order book imbalance ratio (0-1, 0.5 = balanced) */
42
+ obImbalance: number;
43
+ /** Funding rates, newest first */
44
+ fundingRates: number[];
45
+ /** Funding rate stats */
46
+ fundingMean: number;
47
+ fundingStd: number;
48
+ /** OI data points, newest first */
49
+ oiValues: number[];
50
+ /** Recent liquidation total USD near current price (within 1%) */
51
+ liqNearEntry: number;
52
+ /** Trade flow: net delta, buy/sell volumes for last 10 minutes */
53
+ tradeFlow: TradeFlowBucket[];
54
+ /** Current regime */
55
+ regime: string;
56
+ regimeConfidence: number;
57
+ /** When true, conditions using unavailable data (orderbook, liquidations) should auto-pass */
58
+ backtestMode?: boolean;
59
+ /** CVD (Cumulative Volume Delta) indicators — Phase 16a */
60
+ cvd?: {
61
+ cvd1h: number;
62
+ cvd4h: number;
63
+ cvdSlope5m: number;
64
+ cvdSlope1h: number;
65
+ cvdDivergence: 'bullish' | 'bearish' | null;
66
+ cvdDivergenceStrength: number;
67
+ cvdZScore: number;
68
+ };
69
+ /** 4h OHLCV bars, oldest first. Optional — populated for strategies that
70
+ * need higher-timeframe confirmation. Live tick fills this; legacy
71
+ * contexts may omit it. */
72
+ ohlcv4h?: OhlcvBar[];
73
+ /** 1d OHLCV bars, oldest first. Optional — populated for daily-primary
74
+ * strategies. */
75
+ ohlcv1d?: OhlcvBar[];
76
+ /** Cross-symbol / portfolio-wide signals fetched once per tick and shared
77
+ * across all per-symbol contexts. Used by skipIf clauses (e.g. block
78
+ * longs when BTC daily RSI is overbought). */
79
+ globalContext?: GlobalMarketContext;
80
+ /** Cross-market liquidation pulse — sub-second classification over a
81
+ * rolling 60 s window. Only present when microstructure.liquidationPulse
82
+ * is on. See docs/MARKET_MICROSTRUCTURE_STREAMS.md §3. */
83
+ cascadePulse60s?: 'quiet' | 'elevated' | 'active_cascade';
84
+ /** Same classification but aggregated across the majors group
85
+ * (BTC + ETH + BNB + SOL + XRP). Captures market-wide stress that
86
+ * precedes correlated alt moves. */
87
+ majorsCascadePulse60s?: 'quiet' | 'elevated' | 'active_cascade';
88
+ /** Which side is being flushed in the 60 s window: 'long' = longs
89
+ * liquidated (force SELL), 'short' = shorts liquidated (force BUY),
90
+ * 'balanced' = neither side dominant. */
91
+ cascadeDominantSide?: 'long' | 'short' | 'balanced';
92
+ /** Bid share of resting USD notional in the (mid, mid + 0.5%] band, 0-1.
93
+ * 0.5 = balanced. > 0.5 = more bids than asks (support). < 0.5 = more
94
+ * asks than bids (overhead). Only present when bandedLiquidity flag on. */
95
+ restingBandImbalance05?: number;
96
+ /** Same ratio for the 1.0% band — wider lens, smoother signal. */
97
+ restingBandImbalance10?: number;
98
+ /** Symmetric depth coverage in % units. Mirrors order_book_snapshots
99
+ * column. Below the band you're querying = data unreliable. */
100
+ restingCoveragePct?: number;
101
+ /** 30-day funding-rate percentile context. Only present when
102
+ * FUNDING_OVERLAY=on (the helper returns enabled:false otherwise and
103
+ * fetchMarketContext leaves this undefined). Used by the
104
+ * funding_extreme_skip condition. */
105
+ fundingPercentile?: {
106
+ current: number;
107
+ p10: number;
108
+ p50: number;
109
+ p90: number;
110
+ /** 0..1 rank of `current` in the 30d distribution. */
111
+ percentileRank: number;
112
+ sampleSize: number;
113
+ };
114
+ }
115
+ /** One evaluated condition's result, as surfaced on a signal. */
116
+ export interface SignalCondition {
117
+ name: string;
118
+ met: boolean;
119
+ currentValue: number;
120
+ threshold: number;
121
+ description: string;
122
+ }
123
+ /** Output of evaluating one strategy against a MarketContext. */
124
+ export interface StrategyEvaluation {
125
+ direction: Direction | null;
126
+ conditions: SignalCondition[];
127
+ /** Provided when all conditions are met */
128
+ trade?: {
129
+ entryZone: {
130
+ low: number;
131
+ high: number;
132
+ };
133
+ stopLevel: number;
134
+ targets: number[];
135
+ };
136
+ }
137
+ /**
138
+ * A strategy definition: which regimes it operates in, minimum regime
139
+ * confidence, and a function that evaluates conditions against a MarketContext.
140
+ * Produced from a declarative StrategyConfig by adaptStrategy().
141
+ */
142
+ export interface StrategyDefinition {
143
+ name: string;
144
+ regimeWhitelist: string[];
145
+ minConfidence: number;
146
+ evaluate: (ctx: MarketContext) => Promise<StrategyEvaluation>;
147
+ }
148
+ /** A fired signal — produced by the central engine and (facts-out) the plugin
149
+ * evaluator. This is the shape the agent's get_signals tool surfaces. */
150
+ export interface SignalEvent {
151
+ signalId: string;
152
+ timestamp: string;
153
+ symbol: string;
154
+ strategy: string;
155
+ direction: 'LONG' | 'SHORT';
156
+ regime: string;
157
+ regimeConfidence: number;
158
+ confluenceScore: number;
159
+ conditions: SignalCondition[];
160
+ entryZone: {
161
+ low: number;
162
+ high: number;
163
+ };
164
+ stopLevel: number;
165
+ targets: number[];
166
+ }
167
+ /** Per-strategy state for a symbol (whether or not it fired) — the
168
+ * `activeStrategies` view in a SignalSnapshot. */
169
+ export interface StrategyState {
170
+ strategy: string;
171
+ direction: 'LONG' | 'SHORT' | null;
172
+ regimeGated: boolean;
173
+ regimeRequired: string[];
174
+ currentRegime: string;
175
+ regimeConfidence: number;
176
+ confluenceScore: number;
177
+ conditions: SignalCondition[];
178
+ }
179
+ /** The full per-symbol signal snapshot the get_signals tool returns. */
180
+ export interface SignalSnapshot {
181
+ activeStrategies: StrategyState[];
182
+ recentSignals: SignalEvent[];
183
+ timestamp: string;
184
+ /** 'user' = derived from per-user state, 'global' = system background tick,
185
+ * 'local' = produced by the plugin-side evaluator (facts-out). */
186
+ scope?: 'user' | 'global' | 'local';
187
+ }
@@ -0,0 +1,9 @@
1
+ // Market-snapshot + direction types — the canonical source of truth shared by
2
+ // the intel signal engine (intelligence/src/signals/types.ts re-exports these)
3
+ // and, via facts-out, the plugin-side strategy evaluator.
4
+ //
5
+ // PURE TYPES ONLY — no runtime code. Type imports erase at compile time, so a
6
+ // consumer importing these from @reefclaw/shared keeps a self-contained runtime
7
+ // (no @reefclaw/shared dependency in the emitted JS). The condition registry
8
+ // (runtime code) is shared separately via scripts/sync-shared-code.mjs.
9
+ export {};
@@ -0,0 +1,16 @@
1
+ export type TradingMode = 'PAPER' | 'SHADOW' | 'MICRO_LIVE' | 'LIVE';
2
+ export declare const VALID_TRADING_MODES: readonly TradingMode[];
3
+ /** Type guard — narrows `unknown` to `TradingMode`. */
4
+ export declare function isTradingMode(value: unknown): value is TradingMode;
5
+ export interface ModeTransitionResult {
6
+ allowed: boolean;
7
+ reason?: string;
8
+ }
9
+ /** Check whether transitioning from `from` to `to` is allowed by the ladder.
10
+ * - Same mode is a no-op (allowed).
11
+ * - De-escalation (lower rank) is always allowed.
12
+ * - Escalation must be exactly one rung.
13
+ */
14
+ export declare function validateModeTransition(from: TradingMode, to: TradingMode): ModeTransitionResult;
15
+ /** Whether the target mode requires real exchange credentials. */
16
+ export declare function modeRequiresCredentials(mode: TradingMode): boolean;
@@ -0,0 +1,49 @@
1
+ // Canonical TradingMode + mode-ladder rules, shared by plugin, skill, and webapp.
2
+ //
3
+ // Ladder: PAPER → MICRO_LIVE → LIVE. Escalation must be one rung at a time.
4
+ // De-escalating (e.g. LIVE → PAPER) is always allowed.
5
+ // SHADOW was removed (Apr 16 2026) — redundant with MICRO_LIVE, confused the agent.
6
+ export const VALID_TRADING_MODES = ['PAPER', 'MICRO_LIVE', 'LIVE'];
7
+ const VALID_SET = new Set([...VALID_TRADING_MODES, 'SHADOW']);
8
+ /** Type guard — narrows `unknown` to `TradingMode`. */
9
+ export function isTradingMode(value) {
10
+ return typeof value === 'string' && VALID_SET.has(value);
11
+ }
12
+ const RANK = {
13
+ PAPER: 0,
14
+ SHADOW: 0, // Treated as PAPER if somehow encountered
15
+ MICRO_LIVE: 1,
16
+ LIVE: 2,
17
+ };
18
+ /** Check whether transitioning from `from` to `to` is allowed by the ladder.
19
+ * - Same mode is a no-op (allowed).
20
+ * - De-escalation (lower rank) is always allowed.
21
+ * - Escalation must be exactly one rung.
22
+ */
23
+ export function validateModeTransition(from, to) {
24
+ if (from === to)
25
+ return { allowed: true };
26
+ const fromRank = RANK[from];
27
+ const toRank = RANK[to];
28
+ if (toRank < fromRank)
29
+ return { allowed: true };
30
+ if (toRank - fromRank === 1)
31
+ return { allowed: true };
32
+ return {
33
+ allowed: false,
34
+ reason: `Cannot jump ${from} → ${to}: must progress one rung at a time (PAPER → MICRO_LIVE → LIVE). Transition to ${adjacentUp(from)} first.`,
35
+ };
36
+ }
37
+ /** The next rung up from `mode`, used in error messages. */
38
+ function adjacentUp(mode) {
39
+ switch (mode) {
40
+ case 'PAPER': return 'MICRO_LIVE';
41
+ case 'SHADOW': return 'MICRO_LIVE'; // legacy — treated as PAPER
42
+ case 'MICRO_LIVE': return 'LIVE';
43
+ case 'LIVE': return 'LIVE';
44
+ }
45
+ }
46
+ /** Whether the target mode requires real exchange credentials. */
47
+ export function modeRequiresCredentials(mode) {
48
+ return mode !== 'PAPER';
49
+ }