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
|
@@ -38,7 +38,7 @@ import * as KC from '../market_adapter/core/kibana_client.js';
|
|
|
38
38
|
import * as C from '../modules/constants.js';
|
|
39
39
|
import { loadBotSettings, computeBotKey } from './bot_key_utils.js';
|
|
40
40
|
import { resolveBotAccount } from './account_resolver.js';
|
|
41
|
-
import {
|
|
41
|
+
import { classifyFill, assetSymbol, fetchAllFills, resolveAssetPrecisions, } from './fills_source.js';
|
|
42
42
|
const { kibanaSearch, DEFAULT_CONFIG: BASE_CONFIG } = KC;
|
|
43
43
|
const OP_LIMIT_ORDER_UPDATE = 77;
|
|
44
44
|
// ─── LAST-FILL-GUARD helper (1:1 with dexbot_cow_runtime.ts) ─────────────────
|
|
@@ -324,70 +324,27 @@ async function fetchAllOrderUpdates(config, accountId, gte, lte) {
|
|
|
324
324
|
return updates;
|
|
325
325
|
}
|
|
326
326
|
// ─── Fill classification ─────────────────────────────────────────────────────
|
|
327
|
+
// Direction/base/quote resolution is shared with trade_profitability via
|
|
328
|
+
// fills_source.classifyFill; this wrapper only owns the skip count and the
|
|
329
|
+
// TradeFill shape (no market-fee fields — grid checks prices, not PnL).
|
|
327
330
|
function classifyFills(fills) {
|
|
328
331
|
const trades = [];
|
|
329
332
|
let skipped = 0;
|
|
330
333
|
for (const f of fills) {
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
let quoteAsset;
|
|
336
|
-
let baseAmount;
|
|
337
|
-
let quoteAmount;
|
|
338
|
-
let price;
|
|
339
|
-
if (pAsset === BTS_ID && rAsset !== BTS_ID) {
|
|
340
|
-
direction = 'buy';
|
|
341
|
-
baseAsset = rAsset;
|
|
342
|
-
quoteAsset = BTS_ID;
|
|
343
|
-
baseAmount = toReal(f.receives.amount, rAsset);
|
|
344
|
-
quoteAmount = toReal(f.pays.amount, BTS_ID);
|
|
345
|
-
if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
|
|
346
|
-
skipped++;
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
price = quoteAmount / baseAmount;
|
|
350
|
-
}
|
|
351
|
-
else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
|
|
352
|
-
direction = 'sell';
|
|
353
|
-
baseAsset = pAsset;
|
|
354
|
-
quoteAsset = BTS_ID;
|
|
355
|
-
baseAmount = toReal(f.pays.amount, pAsset);
|
|
356
|
-
quoteAmount = toReal(f.receives.amount, BTS_ID);
|
|
357
|
-
if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
|
|
358
|
-
skipped++;
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
price = quoteAmount / baseAmount;
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
|
|
365
|
-
const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
|
|
366
|
-
if (assetPrec(baseForCheck) === undefined || assetPrec(quoteForCheck) === undefined) {
|
|
367
|
-
skipped++;
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
370
|
-
const isSell = pAsset < rAsset;
|
|
371
|
-
direction = isSell ? 'sell' : 'buy';
|
|
372
|
-
baseAsset = isSell ? pAsset : rAsset;
|
|
373
|
-
quoteAsset = isSell ? rAsset : pAsset;
|
|
374
|
-
baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
|
|
375
|
-
quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
|
|
376
|
-
if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
|
|
377
|
-
skipped++;
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
price = quoteAmount / baseAmount;
|
|
334
|
+
const c = classifyFill(f);
|
|
335
|
+
if (!c) {
|
|
336
|
+
skipped++;
|
|
337
|
+
continue;
|
|
381
338
|
}
|
|
382
339
|
trades.push({
|
|
383
340
|
time: f.time,
|
|
384
341
|
orderId: f.orderId,
|
|
385
|
-
direction,
|
|
386
|
-
baseAsset,
|
|
387
|
-
quoteAsset,
|
|
388
|
-
baseAmount,
|
|
389
|
-
quoteAmount,
|
|
390
|
-
price,
|
|
342
|
+
direction: c.direction,
|
|
343
|
+
baseAsset: c.baseAsset,
|
|
344
|
+
quoteAsset: c.quoteAsset,
|
|
345
|
+
baseAmount: c.baseAmount,
|
|
346
|
+
quoteAmount: c.quoteAmount,
|
|
347
|
+
price: c.price,
|
|
391
348
|
isMaker: f.isMaker,
|
|
392
349
|
sequence: f.blockNum * 1e6 + f.opNum,
|
|
393
350
|
});
|
|
@@ -729,7 +686,7 @@ async function main() {
|
|
|
729
686
|
exportCsv(opts.csv, violations);
|
|
730
687
|
process.exit(violations.length > 0 ? 2 : 0);
|
|
731
688
|
}
|
|
732
|
-
export { isLastFillGuardBlocked, classifyFills
|
|
689
|
+
export { isLastFillGuardBlocked, classifyFills };
|
|
733
690
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
734
691
|
main().catch(e => {
|
|
735
692
|
console.error('\n[fatal]', getErrorMessage(e));
|
|
@@ -8,6 +8,66 @@ import { getCandleClose, getCandleTimestamp, normalizeCandle } from '../market_a
|
|
|
8
8
|
*/
|
|
9
9
|
declare function range(min: number, max: number, step: number, decimals?: number): number[];
|
|
10
10
|
declare function calcStdDev(arr: number[]): number;
|
|
11
|
+
/**
|
|
12
|
+
* Median of a numeric array (average of the two middle values for even
|
|
13
|
+
* lengths). Returns `null` for an empty array. Shared by the analysis
|
|
14
|
+
* backtests instead of each keeping a local copy.
|
|
15
|
+
*/
|
|
16
|
+
declare function median(values: number[]): number | null;
|
|
17
|
+
interface PercentileOptions {
|
|
18
|
+
/** 'linear' interpolates between the two bracketing ranks (R-7, default);
|
|
19
|
+
* 'nearest' picks a single rank. */
|
|
20
|
+
interpolation?: 'linear' | 'nearest';
|
|
21
|
+
/** For 'nearest': round the rank (default) or floor it. */
|
|
22
|
+
rounding?: 'round' | 'floor';
|
|
23
|
+
/** Skip the internal sort when the input is already ascending. */
|
|
24
|
+
sorted?: boolean;
|
|
25
|
+
/** Value returned for an empty input (default null). */
|
|
26
|
+
empty?: number | null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Percentile of a numeric array.
|
|
30
|
+
*
|
|
31
|
+
* Single home for the four copies previously kept by optimizer_high_resolution
|
|
32
|
+
* (fractional q, linear), backtest_ama_slope_huber (nearest/round),
|
|
33
|
+
* trade_profitability (pre-sorted, linear) and backtest_lookback_drawdown
|
|
34
|
+
* (nearest/floor). The options preserve each caller's exact ranking rule so
|
|
35
|
+
* centralizing does not silently change any reported number.
|
|
36
|
+
*
|
|
37
|
+
* @param values numeric array (copied + sorted unless `sorted` is true)
|
|
38
|
+
* @param p percentile position in [0, 100]
|
|
39
|
+
*/
|
|
40
|
+
declare function percentile(values: number[], p: number, opts: PercentileOptions & {
|
|
41
|
+
empty: number;
|
|
42
|
+
}): number;
|
|
43
|
+
declare function percentile(values: number[], p: number, opts?: PercentileOptions): number | null;
|
|
44
|
+
interface FmtNumOptions {
|
|
45
|
+
/** Text returned for a value that is not finite. */
|
|
46
|
+
fallback?: string;
|
|
47
|
+
/** When true, only a real finite `number` counts (null/undefined fall back);
|
|
48
|
+
* when false (default), values are coerced with `Number()` first, so
|
|
49
|
+
* `null` formats as `0`. */
|
|
50
|
+
strict?: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Fixed-decimal number formatting with a non-finite fallback.
|
|
54
|
+
*
|
|
55
|
+
* Single home for the identical `fmt(x, d)` copies in backtest_ama_slope_huber
|
|
56
|
+
* and backtest_lookback_drawdown, plus the strict/padded variant in
|
|
57
|
+
* bot_fitting/shared_utils (`fmt(x, d, { fallback: ' n/a', strict: true })`).
|
|
58
|
+
*/
|
|
59
|
+
declare function fmtNum(x: number | null | undefined, d?: number, { fallback, strict }?: FmtNumOptions): string;
|
|
60
|
+
/**
|
|
61
|
+
* Evenly spaced geometric progression from `min` to `max` (endpoints forced).
|
|
62
|
+
* Values are optionally snapped to `quantum` and clamped back into the range,
|
|
63
|
+
* rounded to `decimals`, deduped and sorted ascending. Single home for the
|
|
64
|
+
* helper previously copied into analyze_lambda_vs_slow and
|
|
65
|
+
* optimizer_high_resolution (which used different rounding precision).
|
|
66
|
+
*/
|
|
67
|
+
declare function geometricRange(min: number, max: number, count: number, { decimals, quantum }?: {
|
|
68
|
+
decimals?: number | undefined;
|
|
69
|
+
quantum?: number | null | undefined;
|
|
70
|
+
}): number[];
|
|
11
71
|
/**
|
|
12
72
|
* Parse a candle JSON file with format detection:
|
|
13
73
|
* flat array → {candles: [...]} → {data: [...]}
|
|
@@ -17,5 +77,29 @@ export interface CandleFile {
|
|
|
17
77
|
meta: Record<string, unknown> | null;
|
|
18
78
|
}
|
|
19
79
|
declare function loadCandleFile(filePath: string): CandleFile;
|
|
20
|
-
|
|
80
|
+
/** Normalized OHLCV candle (millisecond-free epoch seconds, like the adapter). */
|
|
81
|
+
type Candle = {
|
|
82
|
+
time: number;
|
|
83
|
+
open: number;
|
|
84
|
+
high: number;
|
|
85
|
+
low: number;
|
|
86
|
+
close: number;
|
|
87
|
+
volume: number;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Load candle records from a single JSON file or a directory of monthly JSON
|
|
91
|
+
* shards.
|
|
92
|
+
*
|
|
93
|
+
* Directory entries are filtered to `*.json` (manifests skipped) and sorted;
|
|
94
|
+
* shards are merged and deduped by candle time (a later file overwrites an
|
|
95
|
+
* earlier duplicate). Returns the merged candles, the first non-null `meta` and
|
|
96
|
+
* the file count. Single home for the loader previously copied into
|
|
97
|
+
* backtest_ama_slope_huber.
|
|
98
|
+
*/
|
|
99
|
+
declare function loadCandleSeries(input: string): {
|
|
100
|
+
candles: Candle[];
|
|
101
|
+
meta: Record<string, unknown> | null;
|
|
102
|
+
files: number;
|
|
103
|
+
};
|
|
104
|
+
export { range, calcStdDev, geometricRange, median, percentile, fmtNum, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, loadCandleSeries, };
|
|
21
105
|
//# sourceMappingURL=math_utils.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
import { getStorage } from '../modules/storage/index.js';
|
|
4
5
|
const { readJSON } = getStorage();
|
|
5
6
|
import { getCandleClose, getCandleTimestamp, normalizeCandle, } from '../market_adapter/candle_utils.js';
|
|
@@ -21,6 +22,73 @@ function calcStdDev(arr) {
|
|
|
21
22
|
const sqDiffs = arr.reduce((sum, v) => sum + (v - mean) ** 2, 0);
|
|
22
23
|
return Math.sqrt(sqDiffs / arr.length);
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Median of a numeric array (average of the two middle values for even
|
|
27
|
+
* lengths). Returns `null` for an empty array. Shared by the analysis
|
|
28
|
+
* backtests instead of each keeping a local copy.
|
|
29
|
+
*/
|
|
30
|
+
function median(values) {
|
|
31
|
+
if (values.length === 0)
|
|
32
|
+
return null;
|
|
33
|
+
const s = values.slice().sort((a, b) => a - b);
|
|
34
|
+
const mid = s.length >> 1;
|
|
35
|
+
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
|
|
36
|
+
}
|
|
37
|
+
function percentile(values, p, { interpolation = 'linear', rounding = 'round', sorted = false, empty = null } = {}) {
|
|
38
|
+
if (values.length === 0)
|
|
39
|
+
return empty;
|
|
40
|
+
const s = sorted ? values : values.slice().sort((a, b) => a - b);
|
|
41
|
+
const pct = Math.max(0, Math.min(100, p));
|
|
42
|
+
const idx = (pct / 100) * (s.length - 1);
|
|
43
|
+
if (interpolation === 'linear') {
|
|
44
|
+
const lo = Math.floor(idx);
|
|
45
|
+
const hi = Math.ceil(idx);
|
|
46
|
+
if (lo === hi)
|
|
47
|
+
return s[lo];
|
|
48
|
+
const t = idx - lo;
|
|
49
|
+
return s[lo] * (1 - t) + s[hi] * t;
|
|
50
|
+
}
|
|
51
|
+
const i = rounding === 'floor' ? Math.floor(idx) : Math.round(idx);
|
|
52
|
+
return s[Math.min(s.length - 1, Math.max(0, i))];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Fixed-decimal number formatting with a non-finite fallback.
|
|
56
|
+
*
|
|
57
|
+
* Single home for the identical `fmt(x, d)` copies in backtest_ama_slope_huber
|
|
58
|
+
* and backtest_lookback_drawdown, plus the strict/padded variant in
|
|
59
|
+
* bot_fitting/shared_utils (`fmt(x, d, { fallback: ' n/a', strict: true })`).
|
|
60
|
+
*/
|
|
61
|
+
function fmtNum(x, d = 2, { fallback = 'n/a', strict = false } = {}) {
|
|
62
|
+
const finite = strict ? Number.isFinite(x) : Number.isFinite(Number(x));
|
|
63
|
+
return finite ? Number(x).toFixed(d) : fallback;
|
|
64
|
+
}
|
|
65
|
+
function quantize(value, quantum) {
|
|
66
|
+
if (quantum == null || !Number.isFinite(quantum) || quantum <= 0)
|
|
67
|
+
return value;
|
|
68
|
+
return Math.round(value / quantum) * quantum;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Evenly spaced geometric progression from `min` to `max` (endpoints forced).
|
|
72
|
+
* Values are optionally snapped to `quantum` and clamped back into the range,
|
|
73
|
+
* rounded to `decimals`, deduped and sorted ascending. Single home for the
|
|
74
|
+
* helper previously copied into analyze_lambda_vs_slow and
|
|
75
|
+
* optimizer_high_resolution (which used different rounding precision).
|
|
76
|
+
*/
|
|
77
|
+
function geometricRange(min, max, count, { decimals = 10, quantum = null } = {}) {
|
|
78
|
+
const out = [];
|
|
79
|
+
const ratio = Math.pow(max / min, 1 / (count - 1));
|
|
80
|
+
for (let i = 0; i < count; i++) {
|
|
81
|
+
let v = min * Math.pow(ratio, i);
|
|
82
|
+
if (i === 0)
|
|
83
|
+
v = min;
|
|
84
|
+
if (i === count - 1)
|
|
85
|
+
v = max;
|
|
86
|
+
v = quantize(v, quantum);
|
|
87
|
+
v = Math.max(min, Math.min(max, v));
|
|
88
|
+
out.push(parseFloat(v.toFixed(decimals)));
|
|
89
|
+
}
|
|
90
|
+
return [...new Set(out)].sort((a, b) => a - b);
|
|
91
|
+
}
|
|
24
92
|
function loadCandleFile(filePath) {
|
|
25
93
|
if (!filePath || !fs.existsSync(filePath))
|
|
26
94
|
return { candles: [], meta: null };
|
|
@@ -34,5 +102,52 @@ function loadCandleFile(filePath) {
|
|
|
34
102
|
return { candles: r.data, meta: r };
|
|
35
103
|
return { candles: [], meta: null };
|
|
36
104
|
}
|
|
37
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Load candle records from a single JSON file or a directory of monthly JSON
|
|
107
|
+
* shards.
|
|
108
|
+
*
|
|
109
|
+
* Directory entries are filtered to `*.json` (manifests skipped) and sorted;
|
|
110
|
+
* shards are merged and deduped by candle time (a later file overwrites an
|
|
111
|
+
* earlier duplicate). Returns the merged candles, the first non-null `meta` and
|
|
112
|
+
* the file count. Single home for the loader previously copied into
|
|
113
|
+
* backtest_ama_slope_huber.
|
|
114
|
+
*/
|
|
115
|
+
function loadCandleSeries(input) {
|
|
116
|
+
const resolved = path.resolve(input);
|
|
117
|
+
if (!fs.existsSync(resolved))
|
|
118
|
+
throw new Error(`Data path not found: ${resolved}`);
|
|
119
|
+
const stat = fs.statSync(resolved);
|
|
120
|
+
const files = stat.isDirectory()
|
|
121
|
+
? fs.readdirSync(resolved)
|
|
122
|
+
.filter((name) => /\.json$/i.test(name) && !/manifest/i.test(name))
|
|
123
|
+
.map((name) => path.join(resolved, name))
|
|
124
|
+
.sort()
|
|
125
|
+
: [resolved];
|
|
126
|
+
const byTime = new Map();
|
|
127
|
+
let meta = null;
|
|
128
|
+
for (const file of files) {
|
|
129
|
+
let raw = null;
|
|
130
|
+
try {
|
|
131
|
+
raw = readJSON(file);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const arr = Array.isArray(raw?.candles) ? raw.candles : (Array.isArray(raw) ? raw : null);
|
|
137
|
+
if (!arr || arr.length === 0)
|
|
138
|
+
continue;
|
|
139
|
+
if (!meta && raw?.meta)
|
|
140
|
+
meta = raw.meta;
|
|
141
|
+
for (const c of arr) {
|
|
142
|
+
const n = normalizeCandle(c);
|
|
143
|
+
if (n)
|
|
144
|
+
byTime.set(n.time, { time: n.time, open: n.open, high: n.high, low: n.low, close: n.close, volume: n.volume });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const candles = [...byTime.values()].sort((a, b) => a.time - b.time);
|
|
148
|
+
if (candles.length === 0)
|
|
149
|
+
throw new Error(`No candles found under ${resolved}`);
|
|
150
|
+
return { candles, meta, files: files.length };
|
|
151
|
+
}
|
|
152
|
+
export { range, calcStdDev, geometricRange, median, percentile, fmtNum, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, loadCandleSeries, };
|
|
38
153
|
//# sourceMappingURL=math_utils.js.map
|
|
@@ -20,6 +20,27 @@ declare class JsonFileSource {
|
|
|
20
20
|
timestamp: number;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
interface InlineConfig {
|
|
24
|
+
candles: Record<string, unknown>[];
|
|
25
|
+
meta?: Record<string, unknown> | null;
|
|
26
|
+
name?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* In-memory candle series (no backing file). Used when discovery assembles a
|
|
30
|
+
* series across month shards, where there is no single whole-history file to
|
|
31
|
+
* hand JsonFileSource.
|
|
32
|
+
*/
|
|
33
|
+
declare class InlineCandleSource {
|
|
34
|
+
candles: Record<string, unknown>[];
|
|
35
|
+
meta: Record<string, unknown> | null;
|
|
36
|
+
name: string;
|
|
37
|
+
constructor(config: InlineConfig);
|
|
38
|
+
fetchCandles(): Promise<Record<string, unknown>[]>;
|
|
39
|
+
extractMarketPrice(candle: unknown): {
|
|
40
|
+
marketPrice: number;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
23
44
|
interface MarketAdapterConfig {
|
|
24
45
|
stateDir?: string;
|
|
25
46
|
botKey: string;
|
|
@@ -36,6 +57,6 @@ declare class MarketAdapterSource {
|
|
|
36
57
|
timestamp: number;
|
|
37
58
|
};
|
|
38
59
|
}
|
|
39
|
-
declare function createSource(type: string, config: JsonFileConfig | MarketAdapterConfig): JsonFileSource | MarketAdapterSource;
|
|
60
|
+
declare function createSource(type: string, config: JsonFileConfig | InlineConfig | MarketAdapterConfig): JsonFileSource | InlineCandleSource | MarketAdapterSource;
|
|
40
61
|
export { createSource };
|
|
41
62
|
//# sourceMappingURL=price_sources.d.ts.map
|
|
@@ -40,6 +40,27 @@ class JsonFileSource {
|
|
|
40
40
|
return { marketPrice: getCandleClose(candle), timestamp: getCandleTimestamp(candle) };
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* In-memory candle series (no backing file). Used when discovery assembles a
|
|
45
|
+
* series across month shards, where there is no single whole-history file to
|
|
46
|
+
* hand JsonFileSource.
|
|
47
|
+
*/
|
|
48
|
+
class InlineCandleSource {
|
|
49
|
+
candles;
|
|
50
|
+
meta;
|
|
51
|
+
name;
|
|
52
|
+
constructor(config) {
|
|
53
|
+
this.candles = config.candles || [];
|
|
54
|
+
this.meta = config.meta || null;
|
|
55
|
+
this.name = `inline:${config.name || 'lp-series'}`;
|
|
56
|
+
}
|
|
57
|
+
async fetchCandles() {
|
|
58
|
+
return this.candles;
|
|
59
|
+
}
|
|
60
|
+
extractMarketPrice(candle) {
|
|
61
|
+
return { marketPrice: getCandleClose(candle), timestamp: getCandleTimestamp(candle) };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
43
64
|
class MarketAdapterSource {
|
|
44
65
|
stateDir;
|
|
45
66
|
centersFile;
|
|
@@ -91,6 +112,8 @@ function createSource(type, config) {
|
|
|
91
112
|
switch (type.toLowerCase()) {
|
|
92
113
|
case 'json':
|
|
93
114
|
return new JsonFileSource(config);
|
|
115
|
+
case 'inline':
|
|
116
|
+
return new InlineCandleSource(config);
|
|
94
117
|
case 'market_adapter':
|
|
95
118
|
return new MarketAdapterSource(config);
|
|
96
119
|
default:
|
|
@@ -4,7 +4,7 @@ import { PATHS } from '../modules/paths.js';
|
|
|
4
4
|
import { MARKET_ADAPTER } from '../modules/constants.js';
|
|
5
5
|
import { createSource } from './price_sources.js';
|
|
6
6
|
import { resolveCandleFile, resolveAmaConfig, resolveAmaKey, loadBotSettings, computeBotKey } from './bot_key_utils.js';
|
|
7
|
-
import {
|
|
7
|
+
import { loadLatestLpSeries, loadLpSeriesFromPath } from '../market_adapter/utils/data_discovery.js';
|
|
8
8
|
import { loadCandleFile } from './math_utils.js';
|
|
9
9
|
const INTERVAL_LABEL = MARKET_ADAPTER.RUNTIME_DEFAULTS.intervalLabel;
|
|
10
10
|
function listAvailableBots() {
|
|
@@ -51,19 +51,40 @@ function resolveSource(config, options = {}) {
|
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
if (config.type === 'json') {
|
|
54
|
-
|
|
54
|
+
const filePath = config.filePath;
|
|
55
55
|
if (!filePath) {
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
58
|
-
filePath = autoFile;
|
|
59
|
-
if (!quiet)
|
|
60
|
-
console.log(`[Source] Auto-discovered LP data: ${autoFile}`);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
56
|
+
const series = loadLatestLpSeries();
|
|
57
|
+
if (!series) {
|
|
63
58
|
throw new Error('No --file provided and no LP data auto-discovered in market_adapter/data/lp');
|
|
64
59
|
}
|
|
60
|
+
if (!quiet) {
|
|
61
|
+
console.log(`[Source] Auto-discovered LP data: ${series.path}${series.assembled ? ' (assembled from month shards)' : ''}`);
|
|
62
|
+
}
|
|
63
|
+
const source = createSource('inline', {
|
|
64
|
+
candles: series.candles,
|
|
65
|
+
meta: series.meta,
|
|
66
|
+
name: series.path,
|
|
67
|
+
});
|
|
68
|
+
if (config.botKey) {
|
|
69
|
+
return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta: series.meta };
|
|
70
|
+
}
|
|
71
|
+
return { source, amaConfig: resolveAmaConfig(''), amaKey: 'AMA3', meta: series.meta };
|
|
72
|
+
}
|
|
73
|
+
const series = loadLpSeriesFromPath(path.resolve(filePath));
|
|
74
|
+
if (series && series.assembled) {
|
|
75
|
+
if (!quiet)
|
|
76
|
+
console.log(`[Source] Assembled ${path.basename(filePath)} across month shards`);
|
|
77
|
+
const source = createSource('inline', {
|
|
78
|
+
candles: series.candles,
|
|
79
|
+
meta: series.meta,
|
|
80
|
+
name: series.path,
|
|
81
|
+
});
|
|
82
|
+
if (config.botKey) {
|
|
83
|
+
return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta: series.meta };
|
|
84
|
+
}
|
|
85
|
+
return { source, amaConfig: resolveAmaConfig(''), amaKey: 'AMA3', meta: series.meta };
|
|
65
86
|
}
|
|
66
|
-
const source = createSource('json', { filePath
|
|
87
|
+
const source = createSource('json', { filePath });
|
|
67
88
|
const meta = loadCandleFile(filePath).meta;
|
|
68
89
|
if (config.botKey) {
|
|
69
90
|
return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta };
|
|
@@ -72,6 +72,12 @@ interface TradeProfitabilityOptions {
|
|
|
72
72
|
report: string | null;
|
|
73
73
|
}
|
|
74
74
|
declare function parseArgs(argv?: string[]): TradeProfitabilityOptions;
|
|
75
|
+
/**
|
|
76
|
+
* Classify fills into trade records. Direction/base/quote resolution and the
|
|
77
|
+
* amount sanity check live in fills_source.classifyFill (single home, also used
|
|
78
|
+
* by grid_correction_check); this wrapper owns the PnL-specific market-fee
|
|
79
|
+
* handling, the asset/pair filters and the unknown-precision skip count.
|
|
80
|
+
*/
|
|
75
81
|
declare function classifyFills(fills: FillRecord[], filterAsset: string | null, pairFilter?: {
|
|
76
82
|
base: string;
|
|
77
83
|
quote: string;
|
|
@@ -4,8 +4,9 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { pathToFileURL } from 'node:url';
|
|
6
6
|
import { resolveAccountRef } from './account_resolver.js';
|
|
7
|
-
import { BTS_ID, assetPrec as getPrec, assetSymbol, resolveAssetPrecisions,
|
|
7
|
+
import { BTS_ID, assetPrec as getPrec, assetSymbol, resolveAssetPrecisions, classifyFill, matchesAssetRef, } from './fills_source.js';
|
|
8
8
|
import { writePnlReport } from './pnl_report.js';
|
|
9
|
+
import { percentile } from './math_utils.js';
|
|
9
10
|
import { fetchFillsCached } from './fills_cache.js';
|
|
10
11
|
import { toFileUrl } from './chart_utils.js';
|
|
11
12
|
import { PATHS } from '../modules/paths.js';
|
|
@@ -202,101 +203,39 @@ function parseArgs(argv = process.argv.slice(2)) {
|
|
|
202
203
|
}
|
|
203
204
|
// ─── Fill Classification ─────────────────────────────────────────────────────
|
|
204
205
|
/**
|
|
205
|
-
*
|
|
206
|
-
* amount
|
|
207
|
-
*
|
|
206
|
+
* Classify fills into trade records. Direction/base/quote resolution and the
|
|
207
|
+
* amount sanity check live in fills_source.classifyFill (single home, also used
|
|
208
|
+
* by grid_correction_check); this wrapper owns the PnL-specific market-fee
|
|
209
|
+
* handling, the asset/pair filters and the unknown-precision skip count.
|
|
208
210
|
*/
|
|
209
|
-
function isValidFillAmounts(baseAmount, quoteAmount) {
|
|
210
|
-
return Number.isFinite(baseAmount) && Number.isFinite(quoteAmount)
|
|
211
|
-
&& baseAmount > 0 && quoteAmount > 0;
|
|
212
|
-
}
|
|
213
|
-
/** Match a user-supplied pair leg against a chain asset id or its symbol. */
|
|
214
|
-
function matchesAssetRef(ref, assetId) {
|
|
215
|
-
const want = String(ref).trim().toUpperCase();
|
|
216
|
-
if (!want)
|
|
217
|
-
return false;
|
|
218
|
-
return assetId.toUpperCase() === want || assetSymbol(assetId).toUpperCase() === want;
|
|
219
|
-
}
|
|
220
211
|
function classifyFills(fills, filterAsset, pairFilter = null) {
|
|
221
212
|
const trades = [];
|
|
222
213
|
const pairs = new Set();
|
|
223
214
|
let skipped = 0;
|
|
224
215
|
for (const f of fills) {
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
let price;
|
|
233
|
-
let marketFeeReal;
|
|
234
|
-
let marketFeeAsset;
|
|
216
|
+
const c = classifyFill(f);
|
|
217
|
+
if (!c) {
|
|
218
|
+
skipped++;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
// Market fee is always deducted from receives; an unknown fee asset
|
|
222
|
+
// precision makes the fee (and therefore net PnL) uncomputable.
|
|
235
223
|
const feePrec = getPrec(f.fee.asset_id);
|
|
236
224
|
if (feePrec === undefined || isNaN(f.fee.amount)) {
|
|
237
225
|
skipped++;
|
|
238
226
|
continue;
|
|
239
227
|
}
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
baseAsset = rAsset;
|
|
244
|
-
quoteAsset = BTS_ID;
|
|
245
|
-
baseAmount = toReal(f.receives.amount, rAsset);
|
|
246
|
-
quoteAmount = toReal(f.pays.amount, BTS_ID);
|
|
247
|
-
if (!isValidFillAmounts(baseAmount, quoteAmount)) {
|
|
248
|
-
skipped++;
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
price = quoteAmount / baseAmount;
|
|
252
|
-
marketFeeReal = feeReal;
|
|
253
|
-
marketFeeAsset = f.fee.asset_id;
|
|
254
|
-
}
|
|
255
|
-
else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
|
|
256
|
-
direction = 'sell';
|
|
257
|
-
baseAsset = pAsset;
|
|
258
|
-
quoteAsset = BTS_ID;
|
|
259
|
-
baseAmount = toReal(f.pays.amount, pAsset);
|
|
260
|
-
quoteAmount = toReal(f.receives.amount, BTS_ID);
|
|
261
|
-
if (!isValidFillAmounts(baseAmount, quoteAmount)) {
|
|
262
|
-
skipped++;
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
price = quoteAmount / baseAmount;
|
|
266
|
-
marketFeeReal = feeReal;
|
|
267
|
-
marketFeeAsset = f.fee.asset_id;
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
// Non-BTS cross-pair: use consistent ordering (lower asset ID = base)
|
|
271
|
-
const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
|
|
272
|
-
const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
|
|
273
|
-
// Skip if either asset precision is unknown
|
|
274
|
-
if (getPrec(baseForCheck) === undefined || getPrec(quoteForCheck) === undefined) {
|
|
275
|
-
skipped++;
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
const isSell = pAsset < rAsset;
|
|
279
|
-
direction = isSell ? 'sell' : 'buy';
|
|
280
|
-
baseAsset = isSell ? pAsset : rAsset;
|
|
281
|
-
quoteAsset = isSell ? rAsset : pAsset;
|
|
282
|
-
baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
|
|
283
|
-
quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
|
|
284
|
-
if (!isValidFillAmounts(baseAmount, quoteAmount)) {
|
|
285
|
-
skipped++;
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
price = quoteAmount / baseAmount;
|
|
289
|
-
marketFeeReal = feeReal;
|
|
290
|
-
marketFeeAsset = f.fee.asset_id;
|
|
291
|
-
}
|
|
228
|
+
const marketFeeReal = f.fee.amount / Math.pow(10, feePrec);
|
|
229
|
+
const marketFeeAsset = f.fee.asset_id;
|
|
230
|
+
const { direction, baseAsset, quoteAsset, baseAmount, quoteAmount, price } = c;
|
|
292
231
|
if (filterAsset && baseAsset !== filterAsset)
|
|
293
232
|
continue;
|
|
294
233
|
if (pairFilter && !(matchesAssetRef(pairFilter.base, baseAsset) && matchesAssetRef(pairFilter.quote, quoteAsset)))
|
|
295
234
|
continue;
|
|
296
235
|
// Validate market fee asset: fee is always deducted from receives
|
|
297
236
|
// (base for buys, quote for sells). Warn if unexpected.
|
|
298
|
-
if (marketFeeReal > 0 && marketFeeAsset !== '' && marketFeeAsset !==
|
|
299
|
-
console.warn(` [warn] Fill ${f.orderId}: fee asset ${marketFeeAsset} ≠ receives asset ${
|
|
237
|
+
if (marketFeeReal > 0 && marketFeeAsset !== '' && marketFeeAsset !== f.receives.asset_id) {
|
|
238
|
+
console.warn(` [warn] Fill ${f.orderId}: fee asset ${marketFeeAsset} ≠ receives asset ${f.receives.asset_id}. Market fee PnL may be incorrect.`);
|
|
300
239
|
}
|
|
301
240
|
const pairKey = `${baseAsset}:${quoteAsset}`;
|
|
302
241
|
pairs.add(pairKey);
|
|
@@ -595,19 +534,6 @@ function printPnlDetail(pairs) {
|
|
|
595
534
|
console.log('');
|
|
596
535
|
}
|
|
597
536
|
}
|
|
598
|
-
function percentile(sorted, p) {
|
|
599
|
-
const n = sorted.length;
|
|
600
|
-
if (n === 0)
|
|
601
|
-
return 0;
|
|
602
|
-
if (n === 1)
|
|
603
|
-
return sorted[0];
|
|
604
|
-
const k = (p / 100) * (n - 1);
|
|
605
|
-
const f = Math.floor(k);
|
|
606
|
-
const c = Math.ceil(k);
|
|
607
|
-
if (f === c)
|
|
608
|
-
return sorted[f];
|
|
609
|
-
return sorted[f] * (c - k) + sorted[c] * (k - f);
|
|
610
|
-
}
|
|
611
537
|
function computeMetrics(pair, window) {
|
|
612
538
|
const pnls = pair.realizedPnls;
|
|
613
539
|
const total = pnls.length;
|
|
@@ -757,7 +683,7 @@ function computeMetrics(pair, window) {
|
|
|
757
683
|
? fillCounts.reduce((a, b) => Math.max(a, b), 0)
|
|
758
684
|
: 0;
|
|
759
685
|
const sortedCounts = [...fillCounts].sort((a, b) => a - b);
|
|
760
|
-
const fillsPerOrderMedian = sellOrdersFilled > 0 ? percentile(sortedCounts, 50) : 0;
|
|
686
|
+
const fillsPerOrderMedian = sellOrdersFilled > 0 ? percentile(sortedCounts, 50, { sorted: true, empty: 0 }) : 0;
|
|
761
687
|
const oneShotOrderRatio = sellOrdersFilled > 0
|
|
762
688
|
? fillCounts.filter(c => c === 1).length / sellOrdersFilled
|
|
763
689
|
: 0;
|
|
@@ -890,15 +816,15 @@ function computeMetrics(pair, window) {
|
|
|
890
816
|
}
|
|
891
817
|
// Payoff distribution stats
|
|
892
818
|
const pnlPcts = [...pnls.map(r => r.pnlPct)].sort((a, b) => a - b);
|
|
893
|
-
const medianPnlPct = percentile(pnlPcts, 50);
|
|
894
|
-
const p25PnlPct = percentile(pnlPcts, 25);
|
|
895
|
-
const p75PnlPct = percentile(pnlPcts, 75);
|
|
819
|
+
const medianPnlPct = percentile(pnlPcts, 50, { sorted: true, empty: 0 });
|
|
820
|
+
const p25PnlPct = percentile(pnlPcts, 25, { sorted: true, empty: 0 });
|
|
821
|
+
const p75PnlPct = percentile(pnlPcts, 75, { sorted: true, empty: 0 });
|
|
896
822
|
const absAvgLoss = Math.abs(avgLoss);
|
|
897
823
|
let rValues = [];
|
|
898
824
|
if (absAvgLoss > 0) {
|
|
899
825
|
rValues = pnls.map(r => r.pnl / absAvgLoss).sort((a, b) => a - b);
|
|
900
826
|
}
|
|
901
|
-
const medianR = rValues.length > 0 ? percentile(rValues, 50) : 0;
|
|
827
|
+
const medianR = rValues.length > 0 ? percentile(rValues, 50, { sorted: true, empty: 0 }) : 0;
|
|
902
828
|
const pctRGreater1 = rValues.length > 0 ? rValues.filter(r => r > 1).length / rValues.length : 0;
|
|
903
829
|
const pctRGreater2 = rValues.length > 0 ? rValues.filter(r => r > 2).length / rValues.length : 0;
|
|
904
830
|
const pctRLessNeg1 = rValues.length > 0 ? rValues.filter(r => r < -1).length / rValues.length : 0;
|