dexbot 1.6.12 → 1.6.14
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.
- package/CHANGELOG.md +65 -1
- package/README.md +2 -2
- package/analysis/README.md +9 -11
- package/analysis/ama_fitting/README.md +5 -3
- package/claw/README.md +1 -0
- package/claw/docs/AI_BOT_LIBRARY_API.md +3 -1
- package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +4 -4
- package/claw/modules/claw_bridge.ts +0 -3
- package/claw/modules/claw_catalog.ts +8 -4
- package/claw/modules/claw_skill_md.ts +1 -0
- package/claw/modules/dexbot_profiles.ts +59 -15
- package/claw/modules/types.ts +2 -1
- package/claw/package.json +3 -3
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/claw/skills/margin-trading/SKILL.md +1 -1
- package/claw/skills/margin-trading/references/position-management.md +1 -1
- package/claw/tsconfig.json +2 -2
- package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +1 -14
- package/dist/analysis/ama_fitting/fetch_lp_candles.js +51 -41
- package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -24
- package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +36 -18
- package/dist/analysis/ama_fitting/optimizer_high_resolution.js +18 -66
- package/dist/analysis/analyze_args.d.ts +12 -0
- package/dist/analysis/analyze_args.js +20 -0
- package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +1 -16
- package/dist/analysis/bot_fitting/backtest_ama_sweep.js +20 -101
- package/dist/analysis/bot_fitting/backtest_bot_fitting.js +26 -92
- package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
- package/dist/analysis/bot_fitting/shared_utils.d.ts +71 -3
- package/dist/analysis/bot_fitting/shared_utils.js +124 -8
- package/dist/analysis/chart_ui.d.ts +30 -1
- package/dist/analysis/chart_ui.js +37 -1
- package/dist/analysis/fills_cache.d.ts +3 -23
- package/dist/analysis/fills_cache.js +3 -84
- package/dist/analysis/fills_source.d.ts +31 -1
- package/dist/analysis/fills_source.js +69 -1
- package/dist/analysis/grid_correction_check.d.ts +1 -23
- package/dist/analysis/grid_correction_check.js +15 -58
- package/dist/analysis/math_utils.d.ts +85 -1
- package/dist/analysis/math_utils.js +116 -1
- package/dist/analysis/price_sources.d.ts +22 -1
- package/dist/analysis/price_sources.js +23 -0
- package/dist/analysis/resolve_source.js +31 -10
- package/dist/analysis/trade_profitability.d.ts +6 -0
- package/dist/analysis/trade_profitability.js +23 -97
- package/dist/analysis/tradingview/analyze_tradingview.d.ts +1 -17
- package/dist/analysis/tradingview/analyze_tradingview.js +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +27 -40
- package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
- package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +22 -80
- package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +11 -37
- package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
- package/dist/analysis/worker_pool.d.ts +38 -0
- package/dist/analysis/worker_pool.js +67 -0
- package/dist/dexbot.js +67 -48
- package/dist/market_adapter/ama_signal_runner.js +21 -54
- package/dist/market_adapter/candle_utils.d.ts +7 -1
- package/dist/market_adapter/candle_utils.js +11 -1
- package/dist/market_adapter/cli_args.d.ts +22 -0
- package/dist/market_adapter/cli_args.js +30 -0
- package/dist/market_adapter/core/config_normalizers.js +11 -6
- package/dist/market_adapter/core/kibana_candles.d.ts +3 -1
- package/dist/market_adapter/core/kibana_candles.js +3 -2
- package/dist/market_adapter/core/market_adapter_service.d.ts +85 -85
- package/dist/market_adapter/core/market_adapter_service.js +93 -158
- package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
- package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
- package/dist/market_adapter/inputs/fetch_book_data.js +0 -1
- package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +63 -107
- package/dist/market_adapter/inputs/fetch_lp_data.d.ts +8 -1
- package/dist/market_adapter/inputs/fetch_lp_data.js +25 -7
- package/dist/market_adapter/inputs/kibana_feed_source.js +4 -26
- package/dist/market_adapter/inputs/window_cache.d.ts +4 -72
- package/dist/market_adapter/inputs/window_cache.js +52 -177
- package/dist/market_adapter/interval_utils.d.ts +11 -1
- package/dist/market_adapter/interval_utils.js +16 -1
- package/dist/market_adapter/lp_chart_runner.d.ts +3 -0
- package/dist/market_adapter/lp_chart_runner.js +37 -19
- package/dist/market_adapter/market_adapter.js +27 -72
- package/dist/market_adapter/utils/data_discovery.d.ts +45 -1
- package/dist/market_adapter/utils/data_discovery.js +220 -27
- package/dist/market_adapter/utils/month_shards.d.ts +84 -0
- package/dist/market_adapter/utils/month_shards.js +237 -0
- package/dist/modules/account_bots.d.ts +8 -6
- package/dist/modules/account_bots.js +112 -131
- package/dist/modules/account_orders.d.ts +49 -4
- package/dist/modules/account_orders.js +114 -49
- package/dist/modules/bitshares-native/chain_client.d.ts +1 -1
- package/dist/modules/bitshares-native/chain_client.js +14 -13
- package/dist/modules/bitshares-native/crypto/ecc_core.d.ts +1 -2
- package/dist/modules/bitshares-native/crypto/ecc_core.js +1 -1
- package/dist/modules/bitshares-native/resolvers.js +22 -25
- package/dist/modules/bitshares-native/serial/chain_constants.d.ts +1 -2
- package/dist/modules/bitshares-native/serial/chain_constants.js +1 -2
- package/dist/modules/bitshares-native/serial/types.js +22 -50
- package/dist/modules/bitshares-native/signing_client.d.ts +1 -1
- package/dist/modules/bot_defaults.d.ts +18 -3
- package/dist/modules/bot_defaults.js +34 -8
- package/dist/modules/bot_settings.d.ts +5 -1
- package/dist/modules/bot_settings.js +55 -21
- package/dist/modules/broadcast_failure.d.ts +2 -1
- package/dist/modules/chain_orders.d.ts +1 -1
- package/dist/modules/chain_orders.js +51 -96
- package/dist/modules/cli_start_onboarding.d.ts +2 -1
- package/dist/modules/config.js +6 -1
- package/dist/modules/constants.d.ts +14 -6
- package/dist/modules/constants.js +32 -6
- package/dist/modules/cr_planner.d.ts +1 -1
- package/dist/modules/cr_planner.js +1 -5
- package/dist/modules/credit_pricing.d.ts +2 -1
- package/dist/modules/credit_pricing.js +36 -40
- package/dist/modules/credit_runtime.d.ts +7 -0
- package/dist/modules/credit_runtime.js +45 -73
- package/dist/modules/daemon_node_health.d.ts +4 -3
- package/dist/modules/dexbot_class.d.ts +33 -17
- package/dist/modules/dexbot_class.js +113 -61
- package/dist/modules/dexbot_cow_runtime.d.ts +62 -181
- package/dist/modules/dexbot_cow_runtime.js +380 -69
- package/dist/modules/dexbot_credential_client.d.ts +1 -23
- package/dist/modules/dexbot_credential_client.js +2 -3
- package/dist/modules/dexbot_fill_runtime.d.ts +2 -3
- package/dist/modules/dexbot_fill_runtime.js +17 -7
- package/dist/modules/dexbot_maintenance_runtime.d.ts +36 -81
- package/dist/modules/dexbot_maintenance_runtime.js +137 -133
- package/dist/modules/dexbot_startup_runtime.d.ts +0 -1
- package/dist/modules/dexbot_startup_runtime.js +25 -14
- package/dist/modules/dexbot_state_recovery.js +16 -0
- package/dist/modules/general_settings.d.ts +1 -1
- package/dist/modules/graceful_shutdown.js +17 -11
- package/dist/modules/key_store.d.ts +0 -5
- package/dist/modules/key_store.js +1 -2
- package/dist/modules/lazy_shim.d.ts +11 -0
- package/dist/modules/lazy_shim.js +16 -0
- package/dist/modules/node_connect_policy.d.ts +3 -2
- package/dist/modules/node_failure_ledger.d.ts +6 -5
- package/dist/modules/order/accounting.js +11 -11
- package/dist/modules/order/export.js +2 -2
- package/dist/modules/order/format.d.ts +26 -3
- package/dist/modules/order/format.js +33 -3
- package/dist/modules/order/genesis_policy.d.ts +4 -4
- package/dist/modules/order/genesis_policy.js +6 -3
- package/dist/modules/order/grid.d.ts +26 -12
- package/dist/modules/order/grid.js +468 -80
- package/dist/modules/order/grid_reconcile.d.ts +1 -1
- package/dist/modules/order/grid_reconcile.js +5 -5
- package/dist/modules/order/grid_reconcile_internal.js +48 -40
- package/dist/modules/order/index.d.ts +35 -8
- package/dist/modules/order/logger.d.ts +1 -8
- package/dist/modules/order/logger.js +2 -11
- package/dist/modules/order/manager.d.ts +18 -15
- package/dist/modules/order/manager.js +91 -74
- package/dist/modules/order/strategy.d.ts +2 -2
- package/dist/modules/order/strategy.js +51 -21
- package/dist/modules/order/sync_engine.js +4 -0
- package/dist/modules/order/utils/math.d.ts +3 -3
- package/dist/modules/order/utils/math.js +38 -27
- package/dist/modules/order/utils/order.d.ts +58 -58
- package/dist/modules/order/utils/order.js +158 -137
- package/dist/modules/order/utils/system.d.ts +202 -35
- package/dist/modules/order/utils/system.js +409 -31
- package/dist/modules/order/utils/validate.js +28 -6
- package/dist/modules/order/utils/withPoolRef.d.ts +2 -1
- package/dist/modules/paths.js +32 -34
- package/dist/modules/runtime_settings.d.ts +3 -5
- package/dist/modules/runtime_settings.js +3 -3
- package/dist/modules/settings_merge.d.ts +2 -2
- package/dist/modules/socket_json_client.d.ts +2 -1
- package/dist/modules/types.d.ts +89 -30
- package/dist/modules/utils/errors.js +1 -3
- package/dist/modules/version_notice.d.ts +11 -10
- package/dist/pm2.d.ts +0 -1
- package/dist/pm2.js +56 -35
- package/dist/scripts/analyze-git.js +2 -15
- package/dist/scripts/analyze-orders.d.ts +1 -1
- package/dist/scripts/analyze-orders.js +4 -4
- package/dist/scripts/chart_command.js +2 -2
- package/dist/scripts/check_any.d.ts +4 -0
- package/dist/scripts/check_any.js +53 -21
- package/dist/scripts/diagnose-pool-history.js +1 -6
- package/dist/scripts/divergence-calc.js +6 -6
- package/dist/scripts/run-tests.js +0 -1
- package/dist/scripts/sync-version.js +0 -10
- package/dist/scripts/validate_bots.d.ts +1 -1
- package/dist/scripts/validate_bots.js +3 -3
- package/dist/unlock.js +7 -2
- package/docs/BITSHARES_ONBOARDING.md +8 -12
- package/docs/COPY_ON_WRITE_MASTER_PLAN.md +2 -3
- package/docs/COW_INVARIANTS.md +6 -6
- package/docs/DEXBOT_COMPARISON.md +7 -7
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +8 -6
- package/docs/GRID_PRICE_INVARIANT.md +3 -3
- package/docs/GRID_RECALCULATION.md +4 -1
- package/docs/GRID_RECONCILE.md +4 -4
- package/docs/LIFECYCLE.md +11 -11
- package/docs/LOGGING.md +14 -0
- package/docs/MPA_CREDIT_USAGE.md +1 -1
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +12 -4
- package/docs/README.md +3 -3
- package/docs/WORKFLOW.md +1 -0
- package/docs/architecture.md +25 -16
- package/docs/developer_guide.md +7 -2
- package/market_adapter/README.md +6 -4
- package/package.json +6 -9
- package/scripts/README.md +43 -38
- package/tests/README.md +26 -11
- package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
- package/dist/scripts/diagnose-kibana-candles.js +0 -82
- package/scripts/git-viewer.sh +0 -489
- package/scripts/postinstall.js +0 -16
|
@@ -60,13 +60,13 @@
|
|
|
60
60
|
* - getOrderSize(order) - Extract order size with fallback
|
|
61
61
|
*
|
|
62
62
|
* SECTION 10: STRATEGY CALCULATIONS (7 functions)
|
|
63
|
-
* -
|
|
64
|
-
* -
|
|
65
|
-
* -
|
|
66
|
-
* -
|
|
67
|
-
* -
|
|
68
|
-
* -
|
|
69
|
-
* -
|
|
63
|
+
* - resolveEdgeCount(config, side) - Clamped per-side edge count (>=0 int, 0 disables)
|
|
64
|
+
* - resolveEdgeOrders(config) - Total edge orders buy+sell (fee/count totals)
|
|
65
|
+
* - resolveLiveEdgeAnchorPrice(manager, side) - Live-grid edge anchor (genesis ladder extreme; null when no usable ladder)
|
|
66
|
+
* - resolveEdgeAnchorPrice(config, side) - Config-bound anchor fallback (buy→minPrice, sell→maxPrice; null when unresolvable)
|
|
67
|
+
* - compareEdge(a, b, edge, anchorPrice) - Shared anchored edge comparator (single ordering source)
|
|
68
|
+
* - edgeOrderIdSet(allSlots, config, orderType, anchorPrice?, excludeIds?) - Edge order id set (config count; shares the picker ordering; excludeIds carries the window exclusion)
|
|
69
|
+
* - selectEdgeSlots(sortedAsc, count, excludeIds, edge, anchorPrice?) - Shared position picker (both edges anchor toward their bound)
|
|
70
70
|
*
|
|
71
71
|
* ===============================================================================
|
|
72
72
|
*/
|
|
@@ -846,6 +846,32 @@ async function _batchCancelCorrections(manager, entries, accountName, privateKey
|
|
|
846
846
|
}
|
|
847
847
|
return { corrected, failed, unresolved };
|
|
848
848
|
}
|
|
849
|
+
/**
|
|
850
|
+
* Read a numeric fillProcessing setting: bot config override first, then the
|
|
851
|
+
* frozen default, then an optional extra default. Returns null when the raw
|
|
852
|
+
* value is absent/non-finite or fails the minimum bound, so callers keep their
|
|
853
|
+
* own fallback and rounding policy. Shared by the correction backlog resolvers
|
|
854
|
+
* below (see resolveCorrectionHoldBudget et al.).
|
|
855
|
+
* @param {unknown} manager
|
|
856
|
+
* @param {string} key - fillProcessing key
|
|
857
|
+
* @param {Object} [options]
|
|
858
|
+
* @param {unknown} [options.extraDefault] - Third-level default (rare)
|
|
859
|
+
* @param {number} [options.min=0] - Minimum accepted value
|
|
860
|
+
* @param {boolean} [options.exclusive=false] - Require value strictly > min
|
|
861
|
+
* @returns {number|null}
|
|
862
|
+
*/
|
|
863
|
+
function readFillProcessingNumber(manager, key, options = {}) {
|
|
864
|
+
const { extraDefault, min = 0, exclusive = false } = options;
|
|
865
|
+
const raw = manager?.config?.fillProcessing?.[key]
|
|
866
|
+
?? FILL_PROCESSING?.[key]
|
|
867
|
+
?? extraDefault;
|
|
868
|
+
if (raw == null)
|
|
869
|
+
return null;
|
|
870
|
+
const n = Number(raw);
|
|
871
|
+
if (!Number.isFinite(n))
|
|
872
|
+
return null;
|
|
873
|
+
return exclusive ? (n > min ? n : null) : (n >= min ? n : null);
|
|
874
|
+
}
|
|
849
875
|
/**
|
|
850
876
|
* Resolve the wall-clock budget for the sequential price-update drain in
|
|
851
877
|
* correctAllPriceMismatches. Cancel-class entries are never budgeted; only
|
|
@@ -857,12 +883,8 @@ async function _batchCancelCorrections(manager, entries, accountName, privateKey
|
|
|
857
883
|
* @returns {number} Positive hold budget in milliseconds
|
|
858
884
|
*/
|
|
859
885
|
function resolveCorrectionHoldBudget(manager) {
|
|
860
|
-
const
|
|
861
|
-
|
|
862
|
-
const n = Number(raw);
|
|
863
|
-
if (Number.isFinite(n) && n > 0)
|
|
864
|
-
return Math.floor(n);
|
|
865
|
-
return 4000;
|
|
886
|
+
const n = readFillProcessingNumber(manager, 'CORRECTION_LOCK_HOLD_BUDGET_MS', { exclusive: true });
|
|
887
|
+
return n === null ? 4000 : Math.floor(n);
|
|
866
888
|
}
|
|
867
889
|
/**
|
|
868
890
|
* Resolve the optional hard cap on price updates drained per cycle. Layered
|
|
@@ -873,14 +895,8 @@ function resolveCorrectionHoldBudget(manager) {
|
|
|
873
895
|
* @returns {number} Finite cap, or Infinity when uncapped
|
|
874
896
|
*/
|
|
875
897
|
function resolveCorrectionMaxUpdates(manager) {
|
|
876
|
-
const
|
|
877
|
-
|
|
878
|
-
if (raw == null)
|
|
879
|
-
return Infinity;
|
|
880
|
-
const n = Number(raw);
|
|
881
|
-
if (Number.isFinite(n) && n >= 0)
|
|
882
|
-
return Math.floor(n);
|
|
883
|
-
return Infinity;
|
|
898
|
+
const n = readFillProcessingNumber(manager, 'CORRECTION_MAX_UPDATES_PER_CYCLE');
|
|
899
|
+
return n === null ? Infinity : Math.floor(n);
|
|
884
900
|
}
|
|
885
901
|
/**
|
|
886
902
|
* Resolve the inter-op delay between successive correction broadcasts.
|
|
@@ -891,14 +907,8 @@ function resolveCorrectionMaxUpdates(manager) {
|
|
|
891
907
|
* @returns {number} Non-negative delay in milliseconds
|
|
892
908
|
*/
|
|
893
909
|
function resolveCorrectionInterOpDelayMs(manager) {
|
|
894
|
-
const
|
|
895
|
-
|
|
896
|
-
if (raw != null) {
|
|
897
|
-
const n = Number(raw);
|
|
898
|
-
if (Number.isFinite(n) && n >= 0)
|
|
899
|
-
return Math.floor(n);
|
|
900
|
-
}
|
|
901
|
-
return TIMING.SYNC_DELAY_MS;
|
|
910
|
+
const n = readFillProcessingNumber(manager, 'CORRECTION_INTER_OP_DELAY_MS');
|
|
911
|
+
return n === null ? TIMING.SYNC_DELAY_MS : Math.floor(n);
|
|
902
912
|
}
|
|
903
913
|
/**
|
|
904
914
|
* Resolve the correction-backlog warn threshold (see
|
|
@@ -907,12 +917,8 @@ function resolveCorrectionInterOpDelayMs(manager) {
|
|
|
907
917
|
* @returns {number}
|
|
908
918
|
*/
|
|
909
919
|
function resolveCorrectionWarnThreshold(manager) {
|
|
910
|
-
const
|
|
911
|
-
|
|
912
|
-
const n = Number(raw);
|
|
913
|
-
if (Number.isFinite(n) && n > 0)
|
|
914
|
-
return Math.floor(n);
|
|
915
|
-
return 0;
|
|
920
|
+
const n = readFillProcessingNumber(manager, 'CORRECTION_QUEUE_WARN_THRESHOLD', { exclusive: true });
|
|
921
|
+
return n === null ? 0 : Math.floor(n);
|
|
916
922
|
}
|
|
917
923
|
/**
|
|
918
924
|
* Resolve the correction-backlog warn rate-limit window.
|
|
@@ -920,13 +926,11 @@ function resolveCorrectionWarnThreshold(manager) {
|
|
|
920
926
|
* @returns {number}
|
|
921
927
|
*/
|
|
922
928
|
function resolveCorrectionWarnRateLimitMs(manager) {
|
|
923
|
-
const
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
return n;
|
|
929
|
-
return 5 * 60 * 1000;
|
|
929
|
+
const n = readFillProcessingNumber(manager, 'CORRECTION_QUEUE_WARN_RATE_LIMIT_MS', {
|
|
930
|
+
extraDefault: TIMING?.STALE_TOTALS_WARN_RATE_LIMIT_MS,
|
|
931
|
+
exclusive: true,
|
|
932
|
+
});
|
|
933
|
+
return n === null ? 5 * 60 * 1000 : n;
|
|
930
934
|
}
|
|
931
935
|
/**
|
|
932
936
|
* Correct all pending price mismatches atomically.
|
|
@@ -1275,6 +1279,11 @@ function geometryTypeForSlotIndex(idx, boundaryIdx, gapSlots) {
|
|
|
1275
1279
|
return null;
|
|
1276
1280
|
if (boundaryIdx === null || boundaryIdx === undefined || boundaryIdx === '')
|
|
1277
1281
|
return null;
|
|
1282
|
+
// Unknown gap width must also fail closed: Number(null)/Number('') === 0
|
|
1283
|
+
// would collapse the gap band and misclassify an in-band slot as a rail
|
|
1284
|
+
// slot (the same trap guarded in detectGapEvacuationCandidates).
|
|
1285
|
+
if (gapSlots === null || gapSlots === undefined || gapSlots === '')
|
|
1286
|
+
return null;
|
|
1278
1287
|
const n = Number(idx);
|
|
1279
1288
|
const b = Number(boundaryIdx);
|
|
1280
1289
|
const g = Number(gapSlots);
|
|
@@ -1302,6 +1311,14 @@ function detectGapEvacuationCandidates(masterGrid, boundaryIdx, gapSlots) {
|
|
|
1302
1311
|
const out = [];
|
|
1303
1312
|
if (!masterGrid || typeof masterGrid.values !== 'function')
|
|
1304
1313
|
return out;
|
|
1314
|
+
// Explicit null/undefined/'' guard BEFORE Number(): Number(null) === 0 and
|
|
1315
|
+
// Number('') === 0 are finite and would read an unknown boundary as slot 0,
|
|
1316
|
+
// selecting a fake gap band around it (same trap guarded in
|
|
1317
|
+
// geometryTypeForSlotIndex / isSlotIndexInGapBand).
|
|
1318
|
+
if (boundaryIdx === null || boundaryIdx === undefined || boundaryIdx === '')
|
|
1319
|
+
return out;
|
|
1320
|
+
if (gapSlots === null || gapSlots === undefined || gapSlots === '')
|
|
1321
|
+
return out;
|
|
1305
1322
|
const b = Number(boundaryIdx);
|
|
1306
1323
|
const g = Number(gapSlots);
|
|
1307
1324
|
if (!Number.isFinite(b) || !Number.isFinite(g) || g < 0)
|
|
@@ -2396,7 +2413,7 @@ function deriveTargetBoundary(fills, currentBoundaryIdx, allSlots, config, gapSl
|
|
|
2396
2413
|
referencePrice = centerPrice;
|
|
2397
2414
|
}
|
|
2398
2415
|
if (referencePrice === null) {
|
|
2399
|
-
const fallbackCap = Math.max(Math.floor((config?.
|
|
2416
|
+
const fallbackCap = Math.max(Math.floor((config?.coreOrders?.sell ?? 1) / 2), Math.floor((config?.coreOrders?.buy ?? 1) / 2), 1);
|
|
2400
2417
|
const effectiveBudget = crossChunkBudget ?? fallbackCap;
|
|
2401
2418
|
return { boundaryIdx: null, remainingBudget: effectiveBudget };
|
|
2402
2419
|
}
|
|
@@ -2405,22 +2422,22 @@ function deriveTargetBoundary(fills, currentBoundaryIdx, allSlots, config, gapSl
|
|
|
2405
2422
|
// Empty slot list with a Tier 1-3 reference: no honest index
|
|
2406
2423
|
// exists (calculateIdealBoundary returns -1). Stay null rather
|
|
2407
2424
|
// than letting the clamp below fabricate a slot-0 boundary.
|
|
2408
|
-
const fallbackCap = Math.max(Math.floor((config?.
|
|
2425
|
+
const fallbackCap = Math.max(Math.floor((config?.coreOrders?.sell ?? 1) / 2), Math.floor((config?.coreOrders?.buy ?? 1) / 2), 1);
|
|
2409
2426
|
return { boundaryIdx: null, remainingBudget: crossChunkBudget ?? fallbackCap };
|
|
2410
2427
|
}
|
|
2411
2428
|
recovered = true;
|
|
2412
2429
|
}
|
|
2413
|
-
// Apply shift from fills with rate-limiting (
|
|
2430
|
+
// Apply shift from fills with rate-limiting (edge fills excluded: static insurance).
|
|
2414
2431
|
let netShift = 0;
|
|
2415
|
-
//
|
|
2432
|
+
// Edge ladder: fills from edge-pinned edge slots never crawl the
|
|
2416
2433
|
// boundary — they are static fat-finger insurance, not market movement.
|
|
2417
|
-
// `edgeAnchors` (
|
|
2434
|
+
// `edgeAnchors` (resolveLiveEdgeAnchorPrice) is the same anchor the
|
|
2418
2435
|
// placement sites use; without it the classification falls back to the
|
|
2419
2436
|
// config-bound anchor, which can disagree with the slots actually placed.
|
|
2420
2437
|
// The window exclusion is the same one every placement picker applies: a
|
|
2421
2438
|
// window that reaches the grid edge (e.g. a keep-low window sitting on
|
|
2422
2439
|
// the floor) must not let the edge pick land on window members — otherwise
|
|
2423
|
-
// a genuine window fill is treated as a static
|
|
2440
|
+
// a genuine window fill is treated as a static edge fill and never
|
|
2424
2441
|
// crawls, and the hole is refilled same-side. Unknown geometry
|
|
2425
2442
|
// (null boundary) fails open to the previous classification.
|
|
2426
2443
|
const windowBoundaryIdx = (currentBoundaryIdx === null || currentBoundaryIdx === undefined)
|
|
@@ -2428,28 +2445,28 @@ function deriveTargetBoundary(fills, currentBoundaryIdx, allSlots, config, gapSl
|
|
|
2428
2445
|
: currentBoundaryIdx;
|
|
2429
2446
|
const windowBuyIds = windowIdSetFromSlots(allSlots, config, ORDER_TYPES.BUY, windowBoundaryIdx, gapSlots);
|
|
2430
2447
|
const windowSellIds = windowIdSetFromSlots(allSlots, config, ORDER_TYPES.SELL, windowBoundaryIdx, gapSlots);
|
|
2431
|
-
const
|
|
2432
|
-
const
|
|
2448
|
+
const edgeBuyIds = edgeOrderIdSet(allSlots, config, ORDER_TYPES.BUY, edgeAnchors?.buy ?? null, windowBuyIds);
|
|
2449
|
+
const edgeSellIds = edgeOrderIdSet(allSlots, config, ORDER_TYPES.SELL, edgeAnchors?.sell ?? null, windowSellIds);
|
|
2433
2450
|
// Pending crawls: fills recorded by earlier batches whose derivation
|
|
2434
2451
|
// never committed (refused broadcast, P4 abort, or pre-restart loss —
|
|
2435
2452
|
// the Sep-10 case: 4 fills consumed under a null boundary, crawl lost,
|
|
2436
2453
|
// restart refilled the holes same-side). Entries for slots in the
|
|
2437
2454
|
// CURRENT batch are excluded — those fills crawl below as usual;
|
|
2438
|
-
// anything older is still owed and shifts here.
|
|
2455
|
+
// anything older is still owed and shifts here. Edge-slot entries
|
|
2439
2456
|
// never crawl (static insurance), same as live fills.
|
|
2440
2457
|
const currentSlotIds = new Set((fills ?? []).map((f) => f?.id).filter((id) => typeof id === 'string' && id.length > 0));
|
|
2441
2458
|
const owedPending = (pendingCrawls ?? []).filter((e) => e
|
|
2442
2459
|
&& typeof e.slotId === 'string' && e.slotId.length > 0
|
|
2443
2460
|
&& !currentSlotIds.has(e.slotId)
|
|
2444
2461
|
&& (e.side === ORDER_TYPES.BUY || e.side === ORDER_TYPES.SELL)
|
|
2445
|
-
&& !(e.side === ORDER_TYPES.BUY &&
|
|
2446
|
-
&& !(e.side === ORDER_TYPES.SELL &&
|
|
2462
|
+
&& !(e.side === ORDER_TYPES.BUY && edgeBuyIds && edgeBuyIds.has(e.slotId))
|
|
2463
|
+
&& !(e.side === ORDER_TYPES.SELL && edgeSellIds && edgeSellIds.has(e.slotId)));
|
|
2447
2464
|
for (const fill of fills ?? []) {
|
|
2448
2465
|
if (!isShiftEligibleFill(fill))
|
|
2449
2466
|
continue;
|
|
2450
|
-
if (fill && fill.type === ORDER_TYPES.BUY &&
|
|
2467
|
+
if (fill && fill.type === ORDER_TYPES.BUY && edgeBuyIds && fill.id != null && edgeBuyIds.has(fill.id))
|
|
2451
2468
|
continue;
|
|
2452
|
-
if (fill && fill.type === ORDER_TYPES.SELL &&
|
|
2469
|
+
if (fill && fill.type === ORDER_TYPES.SELL && edgeSellIds && fill.id != null && edgeSellIds.has(fill.id))
|
|
2453
2470
|
continue;
|
|
2454
2471
|
if (fill.type === ORDER_TYPES.SELL)
|
|
2455
2472
|
netShift++;
|
|
@@ -2473,7 +2490,7 @@ function deriveTargetBoundary(fills, currentBoundaryIdx, allSlots, config, gapSl
|
|
|
2473
2490
|
// consumes from the same pool so the total across all chunks
|
|
2474
2491
|
// never exceeds half the active window.
|
|
2475
2492
|
// Falls back to a per-call cap when no budget is set.
|
|
2476
|
-
const fallbackCap = Math.max(Math.floor((config?.
|
|
2493
|
+
const fallbackCap = Math.max(Math.floor((config?.coreOrders?.sell ?? 1) / 2), Math.floor((config?.coreOrders?.buy ?? 1) / 2), 1);
|
|
2477
2494
|
const effectiveBudget = crossChunkBudget ?? fallbackCap;
|
|
2478
2495
|
const cap = Math.min(Math.abs(effectiveBudget), fallbackCap);
|
|
2479
2496
|
if (recovered && anchoredFromFills) {
|
|
@@ -2555,15 +2572,15 @@ export function consumePendingFillCrawls(manager) {
|
|
|
2555
2572
|
const slots = Array.from(manager?.orders instanceof Map ? manager.orders.values() : []);
|
|
2556
2573
|
// Classify with the SAME live anchors the strategy derivation uses, or the
|
|
2557
2574
|
// two disagree: the config-bound fallback is null for mode-string/relative
|
|
2558
|
-
// bounds, so a restart would rank a stale below-rail slot as
|
|
2575
|
+
// bounds, so a restart would rank a stale below-rail slot as an edge order and
|
|
2559
2576
|
// silently drop a crawl the live run recorded as ordinary market movement.
|
|
2560
2577
|
// The window exclusion must match placement too (same source as the count
|
|
2561
|
-
// fix) — a window over the
|
|
2578
|
+
// fix) — a window over the edge must not be read as an edge order, or
|
|
2562
2579
|
// its owed crawl is dropped instead of applied.
|
|
2563
2580
|
const windowBuyIds = liveWindowIdSet(manager, ORDER_TYPES.BUY);
|
|
2564
2581
|
const windowSellIds = liveWindowIdSet(manager, ORDER_TYPES.SELL);
|
|
2565
|
-
const
|
|
2566
|
-
const
|
|
2582
|
+
const edgeBuyIds = edgeOrderIdSet(slots, config, ORDER_TYPES.BUY, resolveLiveEdgeAnchorPrice(manager, 'buy'), windowBuyIds);
|
|
2583
|
+
const edgeSellIds = edgeOrderIdSet(slots, config, ORDER_TYPES.SELL, resolveLiveEdgeAnchorPrice(manager, 'sell'), windowSellIds);
|
|
2567
2584
|
let netShift = 0;
|
|
2568
2585
|
let count = 0;
|
|
2569
2586
|
for (const e of pending) {
|
|
@@ -2571,9 +2588,9 @@ export function consumePendingFillCrawls(manager) {
|
|
|
2571
2588
|
continue;
|
|
2572
2589
|
if (e.side !== ORDER_TYPES.BUY && e.side !== ORDER_TYPES.SELL)
|
|
2573
2590
|
continue;
|
|
2574
|
-
if (e.side === ORDER_TYPES.BUY &&
|
|
2591
|
+
if (e.side === ORDER_TYPES.BUY && edgeBuyIds && edgeBuyIds.has(e.slotId))
|
|
2575
2592
|
continue;
|
|
2576
|
-
if (e.side === ORDER_TYPES.SELL &&
|
|
2593
|
+
if (e.side === ORDER_TYPES.SELL && edgeSellIds && edgeSellIds.has(e.slotId))
|
|
2577
2594
|
continue;
|
|
2578
2595
|
netShift += e.side === ORDER_TYPES.SELL ? 1 : -1;
|
|
2579
2596
|
count++;
|
|
@@ -2582,12 +2599,15 @@ export function consumePendingFillCrawls(manager) {
|
|
|
2582
2599
|
clear();
|
|
2583
2600
|
return { applied: false, reason: 'nothing-owed' };
|
|
2584
2601
|
}
|
|
2585
|
-
const
|
|
2586
|
-
const
|
|
2587
|
-
const fallbackCap = Math.max(Math.floor(Number(
|
|
2602
|
+
const coreOrdersCfg = config?.coreOrders;
|
|
2603
|
+
const coreOrdersObj = (coreOrdersCfg && typeof coreOrdersCfg === 'object') ? coreOrdersCfg : {};
|
|
2604
|
+
const fallbackCap = Math.max(Math.floor(Number(coreOrdersObj.sell ?? 1) / 2), Math.floor(Number(coreOrdersObj.buy ?? 1) / 2), 1);
|
|
2588
2605
|
if (Math.abs(netShift) > fallbackCap)
|
|
2589
2606
|
netShift = Math.sign(netShift) * fallbackCap;
|
|
2590
|
-
|
|
2607
|
+
// Absent-aware: Number(null) === 0 is finite, so a plain Number() would use
|
|
2608
|
+
// a collapsed (0) gap instead of the config-derived fallback and let the
|
|
2609
|
+
// ceiling + validatePersistedBoundary run on the wrong band.
|
|
2610
|
+
let gapSlots = Format.toKnownNumber(manager?._gapSlots);
|
|
2591
2611
|
if (!Number.isFinite(gapSlots)) {
|
|
2592
2612
|
try {
|
|
2593
2613
|
gapSlots = MathUtils.calculateGapSlots(config?.incrementPercent, config?.targetSpreadPercent, config?.gridLimits);
|
|
@@ -2623,89 +2643,89 @@ export function consumePendingFillCrawls(manager) {
|
|
|
2623
2643
|
return { applied: true, from: boundary, to: candidate, count };
|
|
2624
2644
|
}
|
|
2625
2645
|
/**
|
|
2626
|
-
* Per-side
|
|
2627
|
-
* Buy
|
|
2646
|
+
* Per-side edge count (edge-pinned fat-finger insurance orders).
|
|
2647
|
+
* Buy edges pin at the grid floor, sell edges at the grid ceiling.
|
|
2628
2648
|
* Non-finite/non-integer/negative values disable (0).
|
|
2629
2649
|
*
|
|
2630
|
-
* The legacy numeric form (`
|
|
2650
|
+
* The legacy numeric form (`edgeOrders: n`) is migrated here at read time
|
|
2631
2651
|
* to `{ buy: n, sell: 0 }`: the editor seeder rewrites the persisted JSON, but
|
|
2632
|
-
* a hand-edited bots.json that bypassed the editor must not have its
|
|
2652
|
+
* a hand-edited bots.json that bypassed the editor must not have its edge
|
|
2633
2653
|
* count silently read as 0.
|
|
2634
2654
|
*
|
|
2635
2655
|
* @param {Object} config - Bot configuration
|
|
2636
2656
|
* @param {string} side - 'buy' or 'sell'
|
|
2637
|
-
* @returns {number}
|
|
2657
|
+
* @returns {number} Edge count for the side (>= 0 integer)
|
|
2638
2658
|
*/
|
|
2639
|
-
function
|
|
2659
|
+
function resolveEdgeCount(config, side) {
|
|
2640
2660
|
const key = side === 'sell' ? 'sell' : 'buy';
|
|
2641
|
-
const
|
|
2642
|
-
if (typeof
|
|
2661
|
+
const rawEdge = config?.edgeOrders;
|
|
2662
|
+
if (typeof rawEdge === 'number') {
|
|
2643
2663
|
if (side === 'sell')
|
|
2644
2664
|
return 0;
|
|
2645
|
-
const n = Math.floor(
|
|
2665
|
+
const n = Math.floor(rawEdge);
|
|
2646
2666
|
return Number.isFinite(n) && n >= 0 ? n : 0;
|
|
2647
2667
|
}
|
|
2648
|
-
const raw = Number(
|
|
2668
|
+
const raw = Number(rawEdge?.[key] ?? 0);
|
|
2649
2669
|
if (!Number.isInteger(raw) || raw < 0)
|
|
2650
2670
|
return 0;
|
|
2651
2671
|
return raw;
|
|
2652
2672
|
}
|
|
2653
2673
|
/**
|
|
2654
|
-
* Total
|
|
2674
|
+
* Total edge count across both sides (fee/count totals).
|
|
2655
2675
|
*
|
|
2656
2676
|
* @param {Object} config - Bot configuration
|
|
2657
|
-
* @returns {number} Total
|
|
2677
|
+
* @returns {number} Total edges (buy + sell)
|
|
2658
2678
|
*/
|
|
2659
|
-
function
|
|
2660
|
-
return
|
|
2679
|
+
function resolveEdgeOrders(config) {
|
|
2680
|
+
return resolveEdgeCount(config, 'buy') + resolveEdgeCount(config, 'sell');
|
|
2661
2681
|
}
|
|
2662
2682
|
/**
|
|
2663
|
-
* Edge-pinned
|
|
2683
|
+
* Edge-pinned order id set for one side, or null when disabled.
|
|
2664
2684
|
* Type/price-filtered, then ordered by the SAME edge order the placement
|
|
2665
|
-
* pickers use (
|
|
2685
|
+
* pickers use (compareEdge via selectEdgeSlots) — single source
|
|
2666
2686
|
* of truth, so the no-crawl classification can never drift from placement.
|
|
2667
2687
|
* Anchor: explicit live-grid edge when supplied, otherwise the config-bound
|
|
2668
2688
|
* fallback (unresolved -> plain rank). Shelf/manual ids (non-slot-N, e.g.
|
|
2669
|
-
* fork-kept deep-* orders below the rail) are never
|
|
2689
|
+
* fork-kept deep-* orders below the rail) are never edge orders: they would
|
|
2670
2690
|
* otherwise win the cheapest-first rank and poison the deficit check while
|
|
2671
2691
|
* the shelf is live (issue #27 follow-up). No-op upstream (grids only mint
|
|
2672
2692
|
* slot-N).
|
|
2673
2693
|
*
|
|
2674
2694
|
* @param {Array<Object>} allSlots - All grid slots (need id/price/type)
|
|
2675
|
-
* @param {Object} config - Bot configuration (
|
|
2695
|
+
* @param {Object} config - Bot configuration (edge count source)
|
|
2676
2696
|
* @param {string} orderType - ORDER_TYPES.BUY (floor) or SELL (ceiling)
|
|
2677
2697
|
* @param {number|null} [anchorPrice] - Explicit edge anchor (live grid edge);
|
|
2678
2698
|
* callers that picked slots must pass the SAME anchor so no-crawl
|
|
2679
2699
|
* classification matches placement. NB: null/undefined falls back to the
|
|
2680
2700
|
* config-bound anchor (unresolved -> plain rank) — unlike
|
|
2681
|
-
*
|
|
2701
|
+
* selectEdgeSlots, where null alone means plain rank.
|
|
2682
2702
|
* @param {Set<string>|null} [excludeIds] - Windowed ids to skip (same set the
|
|
2683
2703
|
* placement pickers exclude). Window + edge are additive in every target
|
|
2684
2704
|
* (order counts, fees, hold-back), so a window that reaches the grid edge
|
|
2685
2705
|
* (e.g. a keep-low window sitting on the floor) must not let the edge pick
|
|
2686
2706
|
* land on window members — otherwise counting reads N/N with zero
|
|
2687
|
-
* dedicated
|
|
2707
|
+
* dedicated edge orders and the deficit never fires (issue #27 follow-up).
|
|
2688
2708
|
* @returns {Set<string>|null} Edge slot ids, or null when side disabled
|
|
2689
2709
|
*/
|
|
2690
|
-
function
|
|
2710
|
+
function edgeOrderIdSet(allSlots, config, orderType, anchorPrice = null, excludeIds = null) {
|
|
2691
2711
|
const isSell = orderType === ORDER_TYPES.SELL;
|
|
2692
2712
|
const side = isSell ? 'sell' : 'buy';
|
|
2693
2713
|
// Filter by the canonical side type, not the caller's token: the previous
|
|
2694
2714
|
// per-side resolvers did the same, so a non-canonical token keeps the
|
|
2695
2715
|
// floor behavior instead of silently matching nothing.
|
|
2696
2716
|
const type = isSell ? ORDER_TYPES.SELL : ORDER_TYPES.BUY;
|
|
2697
|
-
const n =
|
|
2717
|
+
const n = resolveEdgeCount(config, side);
|
|
2698
2718
|
if (n <= 0)
|
|
2699
2719
|
return null;
|
|
2700
2720
|
const ids = new Set();
|
|
2701
2721
|
if (!Array.isArray(allSlots))
|
|
2702
2722
|
return ids;
|
|
2703
2723
|
// NB: Number(null) === 0 is finite — null/undefined must mean "no anchor".
|
|
2704
|
-
const anchor = anchorPrice == null ?
|
|
2724
|
+
const anchor = anchorPrice == null ? resolveEdgeAnchorPrice(config, side) : Number(anchorPrice);
|
|
2705
2725
|
const ascending = allSlots
|
|
2706
2726
|
.filter((s) => s && s.id != null && s.price != null && s.type === type && parseSlotIndex(s.id) !== null)
|
|
2707
2727
|
.sort((a, b) => Number(a.price) - Number(b.price));
|
|
2708
|
-
for (const s of
|
|
2728
|
+
for (const s of selectEdgeSlots(ascending, n, excludeIds, isSell ? 'ceiling' : 'floor', anchor)) {
|
|
2709
2729
|
ids.add(String(s.id));
|
|
2710
2730
|
}
|
|
2711
2731
|
return ids;
|
|
@@ -2713,21 +2733,21 @@ function reserveEdgeIdSet(allSlots, config, orderType, anchorPrice = null, exclu
|
|
|
2713
2733
|
/**
|
|
2714
2734
|
* Window member ids for one side from an explicit slot list and geometry.
|
|
2715
2735
|
*
|
|
2716
|
-
* Mirror of the window every placement picker excludes from its
|
|
2736
|
+
* Mirror of the window every placement picker excludes from its edge
|
|
2717
2737
|
* pick: in-rail slots of the side (geometry via isSlotInRail), ordered
|
|
2718
2738
|
* closest to market first (buys: highest price first; sells: lowest first),
|
|
2719
|
-
* sliced to the configured
|
|
2739
|
+
* sliced to the configured coreOrders count. The slice runs over the FULL
|
|
2720
2740
|
* rail, not just live orders — window membership is geometric (a virtual
|
|
2721
|
-
* hole inside the window still blocks the
|
|
2722
|
-
* slices would misclassify live
|
|
2741
|
+
* hole inside the window still blocks the edge pick there), so live-only
|
|
2742
|
+
* slices would misclassify live edge orders as window members whenever the
|
|
2723
2743
|
* window itself is under-filled.
|
|
2724
2744
|
*
|
|
2725
2745
|
* Returns null when the boundary geometry is unknown (null boundary): the
|
|
2726
|
-
* pickers cannot place
|
|
2746
|
+
* pickers cannot place edge orders without it either, so callers fail open
|
|
2727
2747
|
* (no exclusion) and keep their previous classification instead of guessing.
|
|
2728
2748
|
*
|
|
2729
2749
|
* @param {Array<Object>} allSlots - Slots (master grid or clone)
|
|
2730
|
-
* @param {Object} config - Bot configuration (
|
|
2750
|
+
* @param {Object} config - Bot configuration (coreOrders count source)
|
|
2731
2751
|
* @param {string} orderType - ORDER_TYPES.BUY or SELL
|
|
2732
2752
|
* @param {number|null|undefined} boundaryIdx - Boundary the rail is derived from
|
|
2733
2753
|
* @param {number} gapSlots - Gap band size
|
|
@@ -2738,7 +2758,7 @@ function windowIdSetFromSlots(allSlots, config, orderType, boundaryIdx, gapSlots
|
|
|
2738
2758
|
const isSell = orderType === ORDER_TYPES.SELL;
|
|
2739
2759
|
const type = isSell ? ORDER_TYPES.SELL : ORDER_TYPES.BUY;
|
|
2740
2760
|
const side = isSell ? 'sell' : 'buy';
|
|
2741
|
-
const count = Math.max(0, Math.floor(Number(config?.
|
|
2761
|
+
const count = Math.max(0, Math.floor(Number(config?.coreOrders?.[side])) || 0);
|
|
2742
2762
|
if (!(count > 0))
|
|
2743
2763
|
return new Set();
|
|
2744
2764
|
if (!Array.isArray(allSlots))
|
|
@@ -2797,26 +2817,26 @@ function liveWindowIdSet(manager, orderType) {
|
|
|
2797
2817
|
*
|
|
2798
2818
|
* - CREATE ids of the plan (the slots a fold did not convert into an
|
|
2799
2819
|
* UPDATE), minus
|
|
2800
|
-
* -
|
|
2820
|
+
* - edge-ladder ids. Edge orders are static edge insurance; their fills
|
|
2801
2821
|
* never crawl (deriveTargetBoundary filters them), so a guard-skipped
|
|
2802
|
-
*
|
|
2822
|
+
* edge order must not pin geometry either. Without this exclusion an edge order
|
|
2803
2823
|
* CREATE skipped at the wrong moment (e.g. a floor BUY above the last-fill
|
|
2804
2824
|
* pivot while the market dumps below the grid) would hold the boundary for
|
|
2805
2825
|
* a cycle although nothing was stranded.
|
|
2806
2826
|
*
|
|
2807
|
-
* Absent/disabled
|
|
2808
|
-
* so callers that never configured
|
|
2827
|
+
* Absent/disabled edge orders or an empty action list yield the plain CREATE ids,
|
|
2828
|
+
* so callers that never configured edge orders keep the previous behavior.
|
|
2809
2829
|
*
|
|
2810
2830
|
* @param {Array<Object>} actions - Optimized COW actions
|
|
2811
2831
|
* @param {Object} [options]
|
|
2812
|
-
* @param {Object} [options.config] - Bot configuration (
|
|
2813
|
-
* @param {Iterable<Object>} [options.slots] - Master slots (
|
|
2832
|
+
* @param {Object} [options.config] - Bot configuration (edge count source)
|
|
2833
|
+
* @param {Iterable<Object>} [options.slots] - Master slots (edge classification)
|
|
2814
2834
|
* @param {{buy?: number|null, sell?: number|null}} [options.edgeAnchors] - Live
|
|
2815
|
-
* edge anchors (same pair the strategy classifies
|
|
2835
|
+
* edge anchors (same pair the strategy classifies edge fills against)
|
|
2816
2836
|
* @param {unknown} [options.manager] - OrderManager for the window exclusion (same
|
|
2817
2837
|
* set the placement pickers exclude); omit to fall back to the previous
|
|
2818
2838
|
* exclusion-free classification (fail-open)
|
|
2819
|
-
* @returns {string[]} Refill slot ids (CREATE ids minus
|
|
2839
|
+
* @returns {string[]} Refill slot ids (CREATE ids minus edge ids)
|
|
2820
2840
|
*/
|
|
2821
2841
|
function collectRefillSlotIds(actions, options = {}) {
|
|
2822
2842
|
const { config = null, slots = null, edgeAnchors = null, manager = null } = options;
|
|
@@ -2828,12 +2848,12 @@ function collectRefillSlotIds(actions, options = {}) {
|
|
|
2828
2848
|
.map((a) => a.id);
|
|
2829
2849
|
if (createIds.length === 0)
|
|
2830
2850
|
return out;
|
|
2831
|
-
let
|
|
2851
|
+
let edgeIds = null;
|
|
2832
2852
|
if (slots && config) {
|
|
2833
2853
|
try {
|
|
2834
2854
|
// Accept a Map (master grid), an array of slots, or any iterable of
|
|
2835
2855
|
// slot objects. Map entries are [id, slot] pairs, so `.values()` is
|
|
2836
|
-
// required — Array.from(map) would hand
|
|
2856
|
+
// required — Array.from(map) would hand edgeOrderIdSet pairs.
|
|
2837
2857
|
const allSlots = Array.isArray(slots)
|
|
2838
2858
|
? slots
|
|
2839
2859
|
: (slots && typeof slots.values === 'function'
|
|
@@ -2842,30 +2862,30 @@ function collectRefillSlotIds(actions, options = {}) {
|
|
|
2842
2862
|
// Window exclusion keeps classification in agreement with the
|
|
2843
2863
|
// placement pickers: a window reaching the grid edge must not have
|
|
2844
2864
|
// its window hole-CREATEs dropped from the wire as if they were
|
|
2845
|
-
//
|
|
2865
|
+
// edge orders, or a guard-skipped window refill would no longer pin
|
|
2846
2866
|
// the committed boundary (stranding the hole it failed to fill).
|
|
2847
2867
|
const windowBuyIds = manager ? liveWindowIdSet(manager, ORDER_TYPES.BUY) : null;
|
|
2848
2868
|
const windowSellIds = manager ? liveWindowIdSet(manager, ORDER_TYPES.SELL) : null;
|
|
2849
|
-
const buyIds =
|
|
2850
|
-
const sellIds =
|
|
2869
|
+
const buyIds = edgeOrderIdSet(allSlots, config, ORDER_TYPES.BUY, edgeAnchors?.buy ?? null, windowBuyIds);
|
|
2870
|
+
const sellIds = edgeOrderIdSet(allSlots, config, ORDER_TYPES.SELL, edgeAnchors?.sell ?? null, windowSellIds);
|
|
2851
2871
|
if (buyIds || sellIds)
|
|
2852
|
-
|
|
2872
|
+
edgeIds = new Set([...(buyIds ?? []), ...(sellIds ?? [])]);
|
|
2853
2873
|
}
|
|
2854
2874
|
catch {
|
|
2855
|
-
|
|
2875
|
+
edgeIds = null;
|
|
2856
2876
|
}
|
|
2857
2877
|
}
|
|
2858
2878
|
for (const id of createIds) {
|
|
2859
|
-
// Fail-open on classification errors: an id we cannot prove is
|
|
2879
|
+
// Fail-open on classification errors: an id we cannot prove is an edge order
|
|
2860
2880
|
// stays in the wire, so the hold keeps its previous (conservative) reach.
|
|
2861
|
-
if (
|
|
2881
|
+
if (edgeIds && edgeIds.has(id))
|
|
2862
2882
|
continue;
|
|
2863
2883
|
out.push(id);
|
|
2864
2884
|
}
|
|
2865
2885
|
return out;
|
|
2866
2886
|
}
|
|
2867
2887
|
/**
|
|
2868
|
-
* Resolved bound anchor for
|
|
2888
|
+
* Resolved bound anchor for edges from CONFIG alone.
|
|
2869
2889
|
* BUY floor anchors toward minPrice (dip-insurance end), SELL ceiling toward
|
|
2870
2890
|
* maxPrice (spike-insurance end). Resolves numeric and "Nx" relative forms
|
|
2871
2891
|
* via resolveConfiguredPriceBound (startPrice-referenced); falls back to the
|
|
@@ -2873,16 +2893,16 @@ function collectRefillSlotIds(actions, options = {}) {
|
|
|
2873
2893
|
*
|
|
2874
2894
|
* Known limit: this is the statically resolved config bound, not the
|
|
2875
2895
|
* gridPrice/AMA-referenced live rail bound. Placement call sites should use
|
|
2876
|
-
*
|
|
2896
|
+
* resolveLiveEdgeAnchorPrice(manager, side), which prefers the genesis
|
|
2877
2897
|
* ladder extreme and returns null when there is no usable ladder. Callers that
|
|
2878
|
-
* need a fallback (e.g.
|
|
2898
|
+
* need a fallback (e.g. edgeOrderIdSet) still resolve this config bound when
|
|
2879
2899
|
* the live anchor is null; the live resolver itself no longer computes it.
|
|
2880
2900
|
*
|
|
2881
2901
|
* @param {Object} config - Bot configuration
|
|
2882
2902
|
* @param {string} side - 'buy' or 'sell'
|
|
2883
2903
|
* @returns {number|null} Finite anchor price, or null
|
|
2884
2904
|
*/
|
|
2885
|
-
function
|
|
2905
|
+
function resolveEdgeAnchorPrice(config, side) {
|
|
2886
2906
|
const isSell = side === 'sell';
|
|
2887
2907
|
const bound = isSell ? config?.maxPrice : config?.minPrice;
|
|
2888
2908
|
const mode = isSell ? 'max' : 'min';
|
|
@@ -2896,7 +2916,7 @@ function resolveReserveEdgeAnchorPrice(config, side) {
|
|
|
2896
2916
|
return Number.isFinite(raw) ? raw : null;
|
|
2897
2917
|
}
|
|
2898
2918
|
/**
|
|
2899
|
-
* Live-grid
|
|
2919
|
+
* Live-grid edge anchor (single source for edge placement).
|
|
2900
2920
|
*
|
|
2901
2921
|
* The anchor must come from the geometry the bot is actually trading, never
|
|
2902
2922
|
* from a config value that can be a mode string ("pool"/"book"), a relative
|
|
@@ -2914,7 +2934,7 @@ function resolveReserveEdgeAnchorPrice(config, side) {
|
|
|
2914
2934
|
* paper over. The former tiers — the live in-rail slot extreme and the resolved
|
|
2915
2935
|
* config bound — are gone with the legacy matcher they only existed to cover;
|
|
2916
2936
|
* any config-bound fallback is now resolved by the individual consumer that
|
|
2917
|
-
* wants one (see
|
|
2937
|
+
* wants one (see edgeOrderIdSet), not by this live-edge resolver. The fault
|
|
2918
2938
|
* itself is reported
|
|
2919
2939
|
* once per grid generation by the sync-entry gate; this resolver stays silent
|
|
2920
2940
|
* because it is called from per-fill and per-placement paths.
|
|
@@ -2923,7 +2943,7 @@ function resolveReserveEdgeAnchorPrice(config, side) {
|
|
|
2923
2943
|
* @param {string} side - 'buy' or 'sell'
|
|
2924
2944
|
* @returns {number|null} Finite anchor price, or null
|
|
2925
2945
|
*/
|
|
2926
|
-
function
|
|
2946
|
+
function resolveLiveEdgeAnchorPrice(manager, side) {
|
|
2927
2947
|
const isSell = side === 'sell';
|
|
2928
2948
|
const levels = manager?._genesis?.priceLevels;
|
|
2929
2949
|
if (Array.isArray(levels) && levels.length > 0) {
|
|
@@ -2934,7 +2954,7 @@ function resolveLiveReserveEdgeAnchorPrice(manager, side) {
|
|
|
2934
2954
|
return null;
|
|
2935
2955
|
}
|
|
2936
2956
|
/**
|
|
2937
|
-
* Shared anchored edge comparator for
|
|
2957
|
+
* Shared anchored edge comparator for edge selection (single source).
|
|
2938
2958
|
* With a finite anchor: in-bound slots first, nearest the anchor first
|
|
2939
2959
|
* (floor ascending, ceiling descending); stale out-of-bound slots last,
|
|
2940
2960
|
* still nearest the anchor first. Without one: plain rank fallback
|
|
@@ -2946,7 +2966,7 @@ function resolveLiveReserveEdgeAnchorPrice(manager, side) {
|
|
|
2946
2966
|
* @param {number|null} anchorPrice - Resolved bound anchor (null = rank fallback)
|
|
2947
2967
|
* @returns {number} Comparator result for Array.prototype.sort
|
|
2948
2968
|
*/
|
|
2949
|
-
function
|
|
2969
|
+
function compareEdge(a, b, edge, anchorPrice) {
|
|
2950
2970
|
const ceil = edge === 'ceiling';
|
|
2951
2971
|
// NB: Number(null) === 0 is finite — null/undefined must mean "no anchor".
|
|
2952
2972
|
const anchor = anchorPrice == null ? Number.NaN : Number(anchorPrice);
|
|
@@ -2964,25 +2984,25 @@ function compareReserveEdge(a, b, edge, anchorPrice) {
|
|
|
2964
2984
|
return ceil ? pa - pb : pb - pa;
|
|
2965
2985
|
}
|
|
2966
2986
|
/**
|
|
2967
|
-
* Central edge selector: take
|
|
2987
|
+
* Central edge selector: take edge slots from a price-ascending list,
|
|
2968
2988
|
* skipping already-windowed ids. Both edges anchor at the live grid's own
|
|
2969
|
-
* edge (
|
|
2989
|
+
* edge (resolveLiveEdgeAnchorPrice) when finite — floor: nearest
|
|
2970
2990
|
* at/above the live floor first, slots below it rank last; ceiling: nearest
|
|
2971
2991
|
* at/below the live ceiling first, slots above it rank last. Anchoring to the
|
|
2972
|
-
* live edge keeps the
|
|
2992
|
+
* live edge keeps the edge order on genuine live-rail slots when the grid still
|
|
2973
2993
|
* carries leftovers from an older bound or the configured bound disagrees
|
|
2974
2994
|
* with the geometry being traded. Callers pre-filter rail/type and apply
|
|
2975
2995
|
* their own size gates; this only picks positions. A null anchor degrades to
|
|
2976
2996
|
* plain rank (floor: lowest first; ceiling: highest first).
|
|
2977
2997
|
*
|
|
2978
2998
|
* @param {Array<Object>} sortedAsc - Slots sorted by price ascending
|
|
2979
|
-
* @param {number} count -
|
|
2999
|
+
* @param {number} count - Edge count
|
|
2980
3000
|
* @param {Set<string>|null} excludeIds - Windowed ids to skip
|
|
2981
3001
|
* @param {string} edge - 'floor' or 'ceiling'
|
|
2982
3002
|
* @param {number|null} [anchorPrice] - Live edge anchor for the side (null = rank-based)
|
|
2983
|
-
* @returns {Array<Object>}
|
|
3003
|
+
* @returns {Array<Object>} Edge slots (ascending for floor, descending for ceiling)
|
|
2984
3004
|
*/
|
|
2985
|
-
function
|
|
3005
|
+
function selectEdgeSlots(sortedAsc, count, excludeIds, edge, anchorPrice = null) {
|
|
2986
3006
|
const n = Math.max(0, Math.floor(Number(count) || 0));
|
|
2987
3007
|
if (n <= 0 || !Array.isArray(sortedAsc))
|
|
2988
3008
|
return [];
|
|
@@ -2997,21 +3017,22 @@ function selectReserveEdgeSlots(sortedAsc, count, excludeIds, edge, anchorPrice
|
|
|
2997
3017
|
// Anchored: shared comparator — in-bound slots nearest the bound first,
|
|
2998
3018
|
// stale out-of-bound slots last.
|
|
2999
3019
|
return avail
|
|
3000
|
-
.sort((x, y) =>
|
|
3020
|
+
.sort((x, y) => compareEdge(x, y, edge, anchor))
|
|
3001
3021
|
.slice(0, n);
|
|
3002
3022
|
}
|
|
3003
3023
|
/**
|
|
3004
3024
|
* Total target order count across both sides (used for BTS fee calculation).
|
|
3005
3025
|
* Single source of truth so every budget derivation sizes identically.
|
|
3006
|
-
* Includes per-side
|
|
3026
|
+
* Includes per-side edge orders: they rest live on-chain and pay creation fees.
|
|
3007
3027
|
*
|
|
3008
3028
|
* @param {Object} config - Bot configuration
|
|
3009
3029
|
* @returns {number} Total target order count
|
|
3010
3030
|
*/
|
|
3011
|
-
function
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3031
|
+
function getCoreOrdersTotal(config) {
|
|
3032
|
+
const coreOrders = config?.coreOrders;
|
|
3033
|
+
return Math.max(0, coreOrders?.buy ?? 1) +
|
|
3034
|
+
Math.max(0, coreOrders?.sell ?? 1) +
|
|
3035
|
+
resolveEdgeOrders(config);
|
|
3015
3036
|
}
|
|
3016
3037
|
/**
|
|
3017
3038
|
* Calculate side budget after BTS fee deduction.
|
|
@@ -3264,6 +3285,6 @@ function collectKnownOnChainOrderIds(mgr, placedResults, placedContexts, extraCr
|
|
|
3264
3285
|
const all = new Set([...masterIds, ...createIds]);
|
|
3265
3286
|
return { masterIds: [...masterIds], createIds: [...createIds], all: [...all] };
|
|
3266
3287
|
}
|
|
3267
|
-
export { parseChainOrder, applyChainSizeToGridOrder, buildFillKey, correctOrderPriceOnChain, correctAllPriceMismatches, _validatePriceCorrectionEntry, _stampCorrectionProvenance, findLiveOrderOwnerByChainId, buildCreateOrderArgs, getOrderTypeFromUpdatedFlags, resolveConfiguredPriceBound, virtualizeOrder, convertToSpreadPlaceholder, toRailHolePlaceholder, geometryTypeForSlotIndex, detectGapEvacuationCandidates, updateGapEvacuationStreaks, resolveSpreadOrderSide, chainOrderMatchesSlot, chainOrderMatchesSlotWithTolerance, crossingCandidateChainId, isCrossingCheckCandidate, buildCrossingCheckCandidates, parseSlotIndex, filterOrdersByType, buildOutsideInPairGroups, extractBatchOperationResults, formatUnmatchedChainOrder, isNonBlockingUnmatchedOrder, isStrandedHoldOrder, isOrderOnChain, isOrderVirtual, hasOnChainId, isOrderPlaced, isPhantomOrder, isSlotAvailable, isEmptyGridSlot, isOrderHealthy, checkSizeThreshold, checkSizesBeforeMinimum, calculateIdealBoundary, assignGridRoles, resolveOnChainRetypeType, shouldFlagOutOfSpread, buildIndexes, validateIndexes, ordersEqual, buildDelta, deriveTargetBoundary, isShiftEligibleFill,
|
|
3268
|
-
export {
|
|
3288
|
+
export { parseChainOrder, applyChainSizeToGridOrder, buildFillKey, correctOrderPriceOnChain, correctAllPriceMismatches, _validatePriceCorrectionEntry, _stampCorrectionProvenance, findLiveOrderOwnerByChainId, buildCreateOrderArgs, getOrderTypeFromUpdatedFlags, resolveConfiguredPriceBound, virtualizeOrder, convertToSpreadPlaceholder, toRailHolePlaceholder, geometryTypeForSlotIndex, detectGapEvacuationCandidates, updateGapEvacuationStreaks, resolveSpreadOrderSide, chainOrderMatchesSlot, chainOrderMatchesSlotWithTolerance, crossingCandidateChainId, isCrossingCheckCandidate, buildCrossingCheckCandidates, parseSlotIndex, filterOrdersByType, buildOutsideInPairGroups, extractBatchOperationResults, formatUnmatchedChainOrder, isNonBlockingUnmatchedOrder, isStrandedHoldOrder, isOrderOnChain, isOrderVirtual, hasOnChainId, isOrderPlaced, isPhantomOrder, isSlotAvailable, isEmptyGridSlot, isOrderHealthy, checkSizeThreshold, checkSizesBeforeMinimum, calculateIdealBoundary, assignGridRoles, resolveOnChainRetypeType, shouldFlagOutOfSpread, buildIndexes, validateIndexes, ordersEqual, buildDelta, deriveTargetBoundary, isShiftEligibleFill, resolveEdgeCount, resolveEdgeOrders, selectEdgeSlots, getCoreOrdersTotal, getSideBudget, calculateBudgetedSizes, buildCreateOpFingerprint, isOrderGoneErrorMessage, recordDuplicateOrphanDetection, clearDuplicateOrphanDetection, duplicateOrphanLogInfo, chainOrderUnchangedFromCache, detectCrossedBookPlan, collectKnownOnChainOrderIds, edgeOrderIdSet, liveWindowIdSet, checkGridPriceInvariant, reportGridPriceInvariant };
|
|
3289
|
+
export { resolveEdgeAnchorPrice, resolveLiveEdgeAnchorPrice, compareEdge, collectRefillSlotIds, recordOrderPlacement, isFreshlyPlacedOrder, _filterUnmatchedChainOrders };
|
|
3269
3290
|
//# sourceMappingURL=order.js.map
|