DXtrade platform for Tradeify247: percent fees, hard daily-loss stops, volatility sizing, every-day lanes

- DXtrade client (lib/dxtrade-class.ts): REST session login with lockout-safe
  backoff, 60s sync of accounts/metrics/positions/orders/transfers, history and
  fund transactions from cash transactions, day-start balance at the 22:00 UTC
  reset, CLOSE-by-positionCode exits (native OCO group, else a stop-first
  emulated pair), a cross-account hedge guard, stray-order reaper.
- Instrument selector: the firm's symbol map lists candidate symbols per CME
  root; the cheapest tradable one per unit of recent hourly volatility wins
  (spread + 2x commission [+ financing] / sigma).
- Fees: a firm's commission % of notional prices every fee off the fill; the
  take-profit is solved in closed form so the leg nets its target.
- Hard daily loss limit per account type: every leg carries a stop keeping the
  firm's day inside the limit less a 10% reserve and both fees (rounded in);
  a green day never widens it; no room or a refused bracket flattens.
- Notional sizing where the platform prices positions: target-hold volatility
  sizing or the symbol map's fixed notional, under a per-type notional cap.
- Every-day firms trade weekends/holidays; the scheduler runs lanes (futures,
  one per DXtrade firm) with their own hours, flat gate, mutex and batches.
- The catalog sync skips platforms this build doesn't know instead of treating
  them as Tradovate. One shared fireLeg replaces the duplicated batch/copy code.
- npm run check: fee/stop/sizing/hours/selector checks; scripts/dxtrade-smoke.mjs
  confirms the API against a real login before trading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-09-26 16:04:15 -05:00
co-authored by Claude Opus 5.5
parent 49c4725e0c
commit 28f465508d
18 changed files with 3962 additions and 534 deletions
+483
View File
@@ -0,0 +1,483 @@
// Checks for the pure trading math in lib/trading-logic.ts.
//
// npm run check
//
// .mjs on purpose: Next's tsconfig includes every .ts file, and a .ts test
// importing another .ts by extension would trip the build's type check. Node
// strips the types from lib/trading-logic.ts at import time (it has no
// imports of its own).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
percentFee, takeProfitPlan, obfuscationStopDollars, dllRoom, dllStopDollars, dllViable, stopTicks,
botStopsActive, sizeNotional, isNoTradeTime, isPastSessionClose, nextTradingDate, effectiveCombine,
projectPerfectPath, cycleCompletion, DLL_RESERVE_FRACTION, DLL_MIN_STOP,
} from '../lib/trading-logic.ts';
// Deterministic PRNG so a failure reproduces.
function rng(seed) {
let s = seed >>> 0;
return () => {
s = (s + 0x6d2b79f5) >>> 0;
let t = s;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
// ── parity with the pre-refactor inline code ────────────────────────────────
/** The take-profit exactly as auto-trade.ts computed it before takeProfitPlan. */
function oldTakeProfit(side, entry, leg, pv, contracts, tickSize, entryCommission) {
const totalCommission = entryCommission * 2;
const grossTarget = leg + totalCommission;
const targetPoints = grossTarget / (pv * contracts);
const ticks = Math.ceil(targetPoints / tickSize);
const exitPrice = side === 'Buy' ? entry + ticks * tickSize : entry - ticks * tickSize;
return { exitPrice, ticks, totalCommission, grossTarget };
}
test('takeProfitPlan reproduces the old formula when fees are measured', () => {
const r = rng(1);
for (let i = 0; i < 2000; i++) {
const side = r() < 0.5 ? 'Buy' : 'Sell';
const [pv, tick] = [[20, 0.25], [2, 0.25], [100, 0.1], [1000, 0.01], [50, 0.25]][Math.floor(r() * 5)];
const entry = Math.round((1000 + r() * 20000) / tick) * tick;
const leg = Math.round(5 + r() * 2500);
const contracts = 1 + Math.floor(r() * 4);
const fee = Math.round(r() * 1000) / 100;
const got = takeProfitPlan({ side, entry, leg, pointValue: pv, contracts, tickSize: tick, commissionPct: 0, entryCommission: fee });
const want = oldTakeProfit(side, entry, leg, pv, contracts, tick, fee);
assert.equal(got.exitPrice, want.exitPrice);
assert.equal(got.ticks, want.ticks);
assert.equal(got.totalCommission, want.totalCommission);
assert.equal(got.grossTarget, want.grossTarget);
}
});
/** obfuscationStopDollars exactly as it lived in auto-trade.ts. */
function oldObfuscation(cfg, dailyPnL, plan, tradeDate, legAmount) {
if (!cfg.firstDayStop && !cfg.minDayStop) return Infinity;
const prior = dailyPnL.filter((d) => d.date < tradeDate && d.pnl !== 0).sort((a, b) => a.date.localeCompare(b.date));
if (prior.length > 0 && prior[prior.length - 1].pnl < 0) return Infinity;
let stop = Infinity;
const realizedToday = Math.round((plan.dayTarget - plan.remainingToday) * 100) / 100;
if (cfg.firstDayStop && prior.length === 0) stop = Math.max(0, 1000 + realizedToday);
if (cfg.minDayStop && plan.dayTarget <= 1700) stop = Math.min(stop, 2 * legAmount);
return stop;
}
test('obfuscationStopDollars is unchanged by the move', () => {
const r = rng(2);
for (let i = 0; i < 1000; i++) {
const cfg = { firstDayStop: r() < 0.5, minDayStop: r() < 0.5 };
const days = Math.floor(r() * 4);
const dailyPnL = Array.from({ length: days }, (_, d) => ({ date: `2026-09-0${d + 1}`, pnl: Math.round((r() - 0.4) * 2000) }));
const dayTarget = Math.round(r() * 3000);
const plan = { dayTarget, remainingToday: Math.round(dayTarget * r()), path: 'normal_day', splitAllowed: true };
const leg = Math.round(100 + r() * 1500);
assert.equal(obfuscationStopDollars(cfg, dailyPnL, plan, '2026-09-08', leg), oldObfuscation(cfg, dailyPnL, plan, '2026-09-08', leg));
}
});
/** isInNoTradeWindow as it was, with the clock and settings passed in. */
function oldNoTrade(now, mode, jitter) {
const centralParts = new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: 'numeric', hour12: false, timeZone: 'America/Chicago' }).formatToParts(now);
const hour = parseInt(centralParts.find((p) => p.type === 'hour').value, 10);
const minute = parseInt(centralParts.find((p) => p.type === 'minute').value, 10);
const day = new Intl.DateTimeFormat('en-US', { weekday: 'short', timeZone: 'America/Chicago' }).format(now);
const timeMinutes = hour * 60 + minute;
if (mode === 'equity_hours') {
if (day === 'Sat' || day === 'Sun') return true;
const open = 8 * 60 + 35 + jitter;
const close = 14 * 60 + 55;
return timeMinutes < open || timeMinutes >= close;
}
if (day === 'Sat') return true;
const cmeOpen = 17 * 60 + 5 + jitter;
if (day === 'Sun') return timeMinutes < cmeOpen;
const cmeClose = 14 * 60 + 55;
if (day === 'Fri' && timeMinutes >= cmeClose) return true;
if (timeMinutes >= cmeClose && timeMinutes < cmeOpen) return true;
return false;
}
function oldAfterClose(now) {
const parts = new Intl.DateTimeFormat('en-US', { weekday: 'short', hour: 'numeric', minute: 'numeric', hour12: false, timeZone: 'America/Chicago' }).formatToParts(now);
const get = (type) => parts.find((p) => p.type === type)?.value ?? '';
if (get('weekday') === 'Sat' || get('weekday') === 'Sun') return false;
const minutes = (parseInt(get('hour'), 10) % 24) * 60 + parseInt(get('minute'), 10);
return minutes >= 14 * 60 + 55 && minutes < 17 * 60;
}
test('CME trading hours are unchanged, across both DST regimes', () => {
for (const start of [Date.UTC(2026, 8, 20), Date.UTC(2026, 0, 11)]) { // a September and a January week
for (let m = 0; m < 7 * 24 * 60; m += 7) {
const now = new Date(start + m * 60_000);
for (const mode of ['full_cme', 'equity_hours']) {
for (const jitter of [1, 13, 20]) {
assert.equal(isNoTradeTime(now, { everyDay: false, mode, jitterMin: jitter }), oldNoTrade(now, mode, jitter), `${now.toISOString()} ${mode} ${jitter}`);
}
}
assert.equal(isPastSessionClose(now, false), oldAfterClose(now), now.toISOString());
}
}
});
// ── every-day hours ──────────────────────────────────────────────────────────
const ct = (iso, offsetHours) => new Date(Date.parse(`${iso}Z`) + offsetHours * 3600_000);
const cdt = (iso) => ct(iso, 5); // Central daylight: UTC−5
const cst = (iso) => ct(iso, 6); // Central standard: UTC−6
test('every-day firms close only around the roll (CDT)', () => {
const o = { everyDay: true, mode: 'full_cme', jitterMin: 7 };
assert.equal(isNoTradeTime(cdt('2026-09-26T12:00:00'), o), false); // Saturday noon
assert.equal(isNoTradeTime(cdt('2026-09-27T09:00:00'), o), false); // Sunday morning
assert.equal(isNoTradeTime(cdt('2026-09-28T16:54:00'), o), false);
assert.equal(isNoTradeTime(cdt('2026-09-28T16:55:00'), o), true);
assert.equal(isNoTradeTime(cdt('2026-09-28T17:11:00'), o), true);
assert.equal(isNoTradeTime(cdt('2026-09-28T17:12:00'), o), false);
assert.equal(isPastSessionClose(cdt('2026-09-26T16:56:00'), true), true);
assert.equal(isPastSessionClose(cdt('2026-09-26T17:01:00'), true), false);
});
test('every-day firms close from 3:55 PM in CST, covering the 4 PM firm reset', () => {
const o = { everyDay: true, mode: 'equity_hours', jitterMin: 1 };
assert.equal(isNoTradeTime(cst('2027-01-12T15:54:00'), o), false);
assert.equal(isNoTradeTime(cst('2027-01-12T15:55:00'), o), true);
assert.equal(isNoTradeTime(cst('2027-01-12T16:30:00'), o), true);
assert.equal(isNoTradeTime(cst('2027-01-12T17:05:00'), o), true);
assert.equal(isNoTradeTime(cst('2027-01-12T17:06:00'), o), false);
assert.equal(isNoTradeTime(cst('2027-01-16T12:00:00'), o), false); // Saturday
assert.equal(isPastSessionClose(cst('2027-01-12T16:10:00'), true), true);
});
// ── fees ─────────────────────────────────────────────────────────────────────
test('percentFee rounds each side up to the cent', () => {
assert.equal(percentFee(0.04, 60000, 1, 1), 24);
assert.equal(percentFee(0.04, 100000, 0.2, 1), 8);
assert.equal(percentFee(0.04, 123.45, 0.001, 7), 0.01); // 0.00035 → a cent
assert.equal(percentFee(0, 60000, 1, 1), 0);
});
test('percent-fee take-profits net at least the leg, and one tick less does not', () => {
const r = rng(3);
for (let i = 0; i < 3000; i++) {
const side = r() < 0.5 ? 'Buy' : 'Sell';
const tick = [0.01, 0.1, 0.5, 1][Math.floor(r() * 4)];
const entry = Math.round((50 + r() * 100000) / tick) * tick;
const step = [0.001, 0.01, 1][Math.floor(r() * 3)];
const contracts = 1 + Math.floor(r() * 400);
const pct = [0.04, 0.02, 0.1][Math.floor(r() * 3)];
const leg = Math.round(5 + r() * 2000);
const entryFee = percentFee(pct, entry, step, contracts);
const tp = takeProfitPlan({ side, entry, leg, pointValue: step, contracts, tickSize: tick, commissionPct: pct, entryCommission: entryFee });
const k = step * contracts;
const dir = side === 'Buy' ? 1 : -1;
const net = (ticks) => {
const x = entry + dir * ticks * tick;
return ticks * tick * k - entryFee - percentFee(pct, x, step, contracts);
};
assert.ok(net(tp.ticks) >= leg - 1e-6, `short of the leg: ${JSON.stringify({ side, entry, tick, step, contracts, pct, leg, tp })}`);
if (tp.ticks > 1) assert.ok(net(tp.ticks - 1) < leg + 0.02, `a tick further than needed: ${JSON.stringify(tp)}`);
assert.equal(tp.totalCommission, Math.round((entryFee + tp.exitFee) * 100) / 100);
}
});
// ── daily loss limit ─────────────────────────────────────────────────────────
test('dllRoom: snapshot, fallback, green-day cap, no limit', () => {
assert.equal(dllRoom({ dailyLossLimit: 0, closedBalance: 50000, realizedToday: -900 }), Infinity);
assert.equal(dllRoom({ dailyLossLimit: 1500, closedBalance: 49600, dayStartBalance: 50000, realizedToday: 0 }), 1100);
assert.equal(dllRoom({ dailyLossLimit: 1500, closedBalance: 50600, dayStartBalance: 50000, realizedToday: 0 }), 1500);
assert.equal(dllRoom({ dailyLossLimit: 1000, closedBalance: 50000, dayStartBalance: null, realizedToday: -200 }), 800);
assert.equal(dllRoom({ dailyLossLimit: 1000, closedBalance: 50000, realizedToday: 350 }), 1000);
});
test('the worked futures example: $1,000 limit, down $200, NQ at $2.14/side', () => {
const room = dllRoom({ dailyLossLimit: 1000, closedBalance: 49800, realizedToday: -200 });
const d = dllStopDollars({ room, dailyLossLimit: 1000, entryFee: 2.14, commissionPct: 0 });
const ticks = stopTicks({ obfuscationDollars: Infinity, dllDollars: d, pointValue: 20, contracts: 1, tickSize: 0.25 });
assert.equal(ticks, 139);
const loss = ticks * 0.25 * 20 + 2 * 2.14;
assert.ok(-200 - loss > -1000 + 100 - 1e-9);
});
test('stopTicks: obfuscation rounds out, the daily-loss stop rounds in, the tighter wins', () => {
const base = { pointValue: 20, contracts: 1, tickSize: 0.25 };
assert.equal(stopTicks({ ...base, obfuscationDollars: Infinity, dllDollars: Infinity }), null);
assert.equal(stopTicks({ ...base, obfuscationDollars: 0, dllDollars: Infinity }), null);
assert.equal(stopTicks({ ...base, obfuscationDollars: 1001, dllDollars: Infinity }), 201);
assert.equal(stopTicks({ ...base, obfuscationDollars: Infinity, dllDollars: 1000 }), 200);
assert.equal(stopTicks({ ...base, obfuscationDollars: Infinity, dllDollars: 999.99 }), 199);
assert.equal(stopTicks({ ...base, obfuscationDollars: 600, dllDollars: 1000 }), 120);
assert.equal(stopTicks({ ...base, obfuscationDollars: 1600, dllDollars: 1000 }), 200);
assert.equal(stopTicks({ ...base, obfuscationDollars: Infinity, dllDollars: 3 }), 0);
assert.equal(stopTicks({ ...base, obfuscationDollars: Infinity, dllDollars: -40 }), 0);
});
test('worst case at the stop, both fees paid, never spends past room − reserve', () => {
const r = rng(4);
for (let i = 0; i < 20000; i++) {
const side = r() < 0.5 ? 'Buy' : 'Sell';
const pctFirm = r() < 0.6;
const pct = pctFirm ? [0.04, 0.1][Math.floor(r() * 2)] : 0;
const tick = pctFirm ? [0.01, 0.1, 1][Math.floor(r() * 3)] : 0.25;
const pv = pctFirm ? [0.001, 0.01, 1][Math.floor(r() * 3)] : [2, 20, 50][Math.floor(r() * 3)];
const contracts = pctFirm ? 1 + Math.floor(r() * 500) : 1 + Math.floor(r() * 3);
const entry = Math.round((100 + r() * 90000) / tick) * tick;
const dll = [300, 750, 1500, 3000][Math.floor(r() * 4)];
const room = dllRoom({ dailyLossLimit: dll, closedBalance: 50000, dayStartBalance: 50000 + Math.round((r() - 0.3) * dll), realizedToday: 0 });
const entryFee = pctFirm ? percentFee(pct, entry, pv, contracts) : Math.round(r() * 500) / 100 * contracts;
const d = dllStopDollars({ room, dailyLossLimit: dll, entryFee, commissionPct: pct });
const ticks = stopTicks({ obfuscationDollars: Infinity, dllDollars: d, pointValue: pv, contracts, tickSize: tick });
if (ticks === 0) continue; // flattened instead
const stopPrice = side === 'Buy' ? entry - ticks * tick : entry + ticks * tick;
const exitFee = pctFirm ? percentFee(pct, stopPrice, pv, contracts) : entryFee;
const loss = ticks * tick * pv * contracts + entryFee + exitFee;
assert.ok(loss <= room - DLL_RESERVE_FRACTION * dll + 1e-6, JSON.stringify({ side, pct, tick, pv, contracts, entry, dll, room, ticks, loss }));
}
});
test('dllViable and botStopsActive', () => {
assert.equal(dllViable(Infinity, 0, 0), true);
assert.equal(dllViable(1500, 1500, 20), true);
assert.equal(dllViable(150 + DLL_MIN_STOP + 20, 1500, 20), true);
assert.equal(dllViable(150 + DLL_MIN_STOP + 19.99, 1500, 20), false);
assert.equal(botStopsActive({ firstDayStop: false, minDayStop: false }), false);
assert.equal(botStopsActive({ firstDayStop: false, minDayStop: false, dailyLossLimit: 1500 }), true);
assert.equal(botStopsActive({ firstDayStop: true, minDayStop: false, dailyLossLimit: 0 }), true);
});
// ── sizing ───────────────────────────────────────────────────────────────────
const btcSigma = 0.03 / Math.sqrt(24); // 3%/day
test('volatility sizing hits the target hold, and the example table', () => {
const stop = () => 1350;
const at = (hours) => sizeNotional({ leg: 600, stopDollarsAt: stop, volatilityPerHour: btcSigma, targetHoldHours: hours, commissionPct: 0, fixedNotional: 0, maxNotional: 0 });
for (const [hours, notional] of [[54, 20000], [24, 30000], [6, 60000]]) {
const s = at(hours);
assert.equal(s.mode, 'volatility');
assert.ok(Math.abs(s.notional - notional) / notional < 0.001, `${hours}h → ${s.notional}`);
assert.ok(Math.abs(s.predictedHoldHours - hours) / hours < 0.001);
assert.ok(Math.abs(s.winProbability - 1350 / 1950) < 1e-9);
}
});
test('sizing: fees feed back, the cap binds, financing only raises N, fixed fallback', () => {
const base = { leg: 600, volatilityPerHour: btcSigma, targetHoldHours: 6, commissionPct: 0.04, spreadFraction: 0.0002, fixedNotional: 20000, maxNotional: 0 };
// Daily-loss stop shrinks as fees grow with N.
const stopDollarsAt = (n) => dllStopDollars({ room: 1500, dailyLossLimit: 1500, entryFee: 0.0004 * n, commissionPct: 0.04 });
const s = sizeNotional({ ...base, stopDollarsAt });
const a = 600 + 2 * 0.0004 * s.notional;
const b = stopDollarsAt(s.notional);
assert.ok(Math.abs(s.predictedHoldHours - 6) < 0.05, `hold ${s.predictedHoldHours}`);
assert.ok(Math.abs(s.winProbability - b / (a + b)) < 1e-9);
const capped = sizeNotional({ ...base, stopDollarsAt, maxNotional: 25000 });
assert.equal(capped.notional, 25000);
assert.ok(capped.predictedHoldHours > 6);
const financed = sizeNotional({ ...base, stopDollarsAt, targetHoldHours: 200, financingPctPerDay: 0.033 });
const unfinanced = sizeNotional({ ...base, stopDollarsAt, targetHoldHours: 200 });
assert.ok(financed.notional > unfinanced.notional);
const fixed = sizeNotional({ ...base, stopDollarsAt: () => Infinity, leg: 1700 });
assert.equal(fixed.mode, 'fixed');
assert.equal(fixed.notional, 40000);
assert.equal(sizeNotional({ ...base, stopDollarsAt: () => Infinity, fixedNotional: 0 }), null);
assert.equal(sizeNotional({ ...base, targetHoldHours: 0, stopDollarsAt }).mode, 'fixed');
});
test('random-walk simulation agrees with the predicted hold and win rate', () => {
const r = rng(5);
const notional = 60000;
const A = 600;
const B = 1350;
const sigma = btcSigma;
const dtHours = 1 / 60; // one-minute steps
const stepDollars = notional * sigma * Math.sqrt(dtHours);
const predicted = (A / notional) * (B / notional) / (sigma * sigma);
let wins = 0;
let totalHours = 0;
const paths = 3000;
for (let p = 0; p < paths; p++) {
let x = 0;
let steps = 0;
while (x < A && x > -B) {
x += r() < 0.5 ? stepDollars : -stepDollars;
steps++;
}
if (x >= A) wins++;
totalHours += steps * dtHours;
}
const meanHold = totalHours / paths;
assert.ok(Math.abs(meanHold - predicted) / predicted < 0.1, `mean hold ${meanHold} vs predicted ${predicted}`);
assert.ok(Math.abs(wins / paths - B / (A + B)) < 0.03, `win rate ${wins / paths}`);
});
// ── every-day calendar ───────────────────────────────────────────────────────
test('nextTradingDate and effectiveCombine', () => {
assert.equal(nextTradingDate('2026-09-25'), '2026-09-28');
assert.equal(nextTradingDate('2026-09-25', true), '2026-09-26');
assert.equal(nextTradingDate('2026-12-24', true), '2026-12-25');
assert.equal(effectiveCombine({ combineCmeHolidays: true }), true);
assert.equal(effectiveCombine({ combineCmeHolidays: true, tradesEveryDay: true }), false);
});
const projCfg = {
profitTarget: 3000, consistency: 0, minDayPnL: 0, minTradingDays: 5, accountSize: 50000,
targetSameEquity: false, withdrawalStages: [], splitEnabled: true,
};
test('every-day projections use consecutive dates; CME ones still skip weekends', () => {
const now = cdt('2026-09-26T12:00:00'); // Saturday noon → trade date Saturday
const common = { accountId: 7, cycleDailyPnL: [], fundTransactions: [], balance: 50000, realizedPnL: 0, now };
const every = projectPerfectPath({ ...common, cfg: { ...projCfg, tradesEveryDay: true } });
const cme = projectPerfectPath({ ...common, cfg: projCfg });
assert.ok(every.length > 1);
for (let i = 1; i < every.length; i++) assert.equal(every[i].date, nextTradingDate(every[i - 1].date, true));
for (const d of cme) {
const dow = new Date(`${d.date}T00:00:00Z`).getUTCDay();
assert.ok(dow !== 0 && dow !== 6, `CME projection landed on ${d.date}`);
}
// Saturday itself is booked (silently) for the every-day firm: its first
// projected day is Sunday, the CME one's is Monday.
assert.equal(every[0].date, '2026-09-27');
assert.equal(cme[0].date, '2026-09-28');
});
test('an every-day firm can finish its cycle on a Saturday', () => {
const now = cdt('2026-09-26T12:00:00');
const cycleDailyPnL = [
{ date: '2026-09-21', pnl: 700 }, { date: '2026-09-22', pnl: 700 },
{ date: '2026-09-23', pnl: 700 }, { date: '2026-09-24', pnl: 700 },
];
const opts = {
accountId: 7, rawTradeDate: '2026-09-26', cycleDailyPnL, rawRealizedPnL: 400, combineCmeHolidays: false,
effective: { profitTarget: 3000, consistency: 0, minTradingDays: 5, minDayPnL: 0, drain: false },
equityProfit: 3200, splitEnabled: true, now,
};
assert.equal(cycleCompletion({ ...opts, tradesEveryDay: true }), 'today');
assert.equal(cycleCompletion(opts), 'none');
});
// ── DXTrade helpers (lib/dxtrade-helpers.ts) ─────────────────────────────────
import {
dxAccountId, accountDisplayName, accountCodeOf, encodeContractName, decodeContractName, roundToStep,
lastUtcReset, ctTradeDayStart, parseDurationMs, ciNum, signedFundValue, inTradingHours,
realizedVolPerHour, scoreCandidate, candidateRejection, MIN_VOL_CANDLES,
} from '../lib/dxtrade-helpers.ts';
test('dxAccountId is stable, safe, far above broker ids, and host-scoped', () => {
const a = dxAccountId('dx.tradeify247.co', 'default:TFY-1');
assert.equal(a, dxAccountId('DX.Tradeify247.co', 'default:TFY-1'));
assert.ok(Number.isSafeInteger(a) && a >= 2 ** 50 && a < 2 ** 53);
assert.notEqual(a, dxAccountId('dx.other.co', 'default:TFY-1'));
const seen = new Set();
for (let i = 0; i < 20000; i++) seen.add(dxAccountId('dx.tradeify247.co', `default:${i}`));
assert.equal(seen.size, 20000);
});
test('account codes and contract names', () => {
assert.equal(accountDisplayName('default:TFY-50K-12'), 'TFY-50K-12');
assert.equal(accountDisplayName('plain'), 'plain');
assert.equal(accountCodeOf({ clearing: 'default', account: 'x1' }), 'default:x1');
assert.equal(accountCodeOf('default:x1'), 'default:x1');
assert.deepEqual(decodeContractName(encodeContractName('BTC/USD', 0.001)), { symbol: 'BTC/USD', step: 0.001 });
assert.equal(decodeContractName('NQZ5'), null);
assert.equal(roundToStep(0.1 + 0.2, 0.1), 0.3);
assert.equal(roundToStep(12 * 0.001, 0.001), 0.012);
assert.equal(roundToStep(101.234, 0.5, 'up'), 101.5);
assert.equal(roundToStep(101.234, 0.5, 'down'), 101);
});
test('the firm reset and the CT trade-date start', () => {
assert.equal(lastUtcReset(new Date('2026-09-26T21:59:00Z')).toISOString(), '2026-09-25T22:00:00.000Z');
assert.equal(lastUtcReset(new Date('2026-09-26T22:00:00Z')).toISOString(), '2026-09-26T22:00:00.000Z');
assert.equal(ctTradeDayStart(cdt('2026-09-26T18:30:00')).toISOString(), '2026-09-26T22:00:00.000Z');
assert.equal(ctTradeDayStart(cdt('2026-09-27T09:00:00')).toISOString(), '2026-09-26T22:00:00.000Z');
assert.equal(ctTradeDayStart(cst('2027-01-12T10:00:00')).toISOString(), '2027-01-11T23:00:00.000Z');
});
test('loose parsing', () => {
assert.equal(parseDurationMs('PT30M'), 30 * 60_000);
assert.equal(parseDurationMs('01:05:08'), (3600 + 300 + 8) * 1000);
assert.equal(parseDurationMs({ seconds: 900 }), 900_000);
assert.equal(parseDurationMs(undefined), 30 * 60_000);
assert.equal(ciNum({ openPL: 12.5 }, 'openPl'), 12.5);
assert.equal(ciNum({ openPL: '12' }, 'openPl'), undefined);
assert.equal(signedFundValue('WITHDRAWAL', 500), -500);
assert.equal(signedFundValue('WITHDRAWAL', -500), -500);
assert.equal(signedFundValue('DEPOSIT', 50000), 50000);
});
test('trading hours from UTC week sessions', () => {
const at = (dayOfWeek, hour, eventType) => ({ eventType, weekDay: { dayOfWeek, time: { hour, minute: 30 } } });
const weekdays = [at('MONDAY', 13, 'SESSION_OPEN'), at('MONDAY', 20, 'SESSION_CLOSE'), at('TUESDAY', 13, 'SESSION_OPEN'), at('TUESDAY', 20, 'SESSION_CLOSE')];
assert.equal(inTradingHours(weekdays, new Date('2026-09-28T15:00:00Z')), true); // Monday
assert.equal(inTradingHours(weekdays, new Date('2026-09-28T21:00:00Z')), false);
assert.equal(inTradingHours(weekdays, new Date('2026-09-27T15:00:00Z')), false); // Sunday wraps to Tuesday's close
assert.equal(inTradingHours([], new Date()), true);
assert.equal(inTradingHours([{ garbage: true }], new Date()), true);
});
test('realized volatility of hourly closes', () => {
assert.equal(realizedVolPerHour([100, 101, 102]), null);
const r = rng(6);
const sigma = 0.004;
const closes = [30000];
for (let i = 0; i < 2000; i++) {
// Box–Muller for a normal step
const z = Math.sqrt(-2 * Math.log(1 - r())) * Math.cos(2 * Math.PI * r());
closes.push(closes[closes.length - 1] * Math.exp(sigma * z));
}
const v = realizedVolPerHour(closes);
assert.ok(Math.abs(v - sigma) / sigma < 0.08, `σ ${v}`);
const flat = Array.from({ length: MIN_VOL_CANDLES + 1 }, () => 200);
assert.equal(realizedVolPerHour(flat), 0);
});
test('the selector ranks the plan example: ETH, BTC, XYZ100, AAPL', () => {
const perHour = (daily) => daily / Math.sqrt(24);
const score = (spread, daily) => scoreCandidate({
spreadFraction: spread, commissionPct: 0.04, financingPctPerDay: 0, targetHoldHours: 6, volatilityPerHour: perHour(daily),
});
const board = [
['BTC/USD', score(0.0002, 0.03)],
['XYZ100', score(0.0001, 0.012)],
['AAPL/USD (Sat)', score(0.0005, 0.004)],
['ETH/USD', score(0.0003, 0.04)],
['AAPL/USD (Tue)', score(0.0005, 0.015)],
].sort((a, b) => a[1] - b[1]).map((b) => b[0]);
assert.deepEqual(board, ['ETH/USD', 'BTC/USD', 'XYZ100', 'AAPL/USD (Tue)', 'AAPL/USD (Sat)']);
// Cost per leg = score × √(A·B/T): the example's $50 and $60.
const k = Math.sqrt((600 * 1350) / 6);
assert.ok(Math.abs(score(0.0003, 0.04) * k - 49.6) < 1);
assert.ok(Math.abs(score(0.0002, 0.03) * k - 60) < 1);
// Fixed sizing ignores volatility; volatility sizing needs it.
assert.equal(scoreCandidate({ spreadFraction: 0.0002, commissionPct: 0.04, financingPctPerDay: 0, targetHoldHours: 0, volatilityPerHour: null }), 0.001);
assert.equal(scoreCandidate({ spreadFraction: 0.0002, commissionPct: 0.04, financingPctPerDay: 0, targetHoldHours: 6, volatilityPerHour: null }), null);
});
test('the selector drops what cannot trade right now', () => {
const ok = {
tradingStatus: 'FULL', bid: 100, ask: 100.02, quoteAgeMs: 1000, inHours: true, candles: 49,
needVolatility: true, minContractsOk: true, currency: 'USD', accountCurrency: 'USD',
};
assert.equal(candidateRejection(ok), null);
assert.match(candidateRejection({ ...ok, tradingStatus: 'CLOSE_ONLY' }), /CLOSE_ONLY/);
assert.match(candidateRejection({ ...ok, inHours: false }), /hours/);
assert.match(candidateRejection({ ...ok, bid: undefined }), /quote/);
assert.match(candidateRejection({ ...ok, quoteAgeMs: 10 * 60_000 }), /old/);
assert.match(candidateRejection({ ...ok, ask: 101 }), /spread/);
assert.match(candidateRejection({ ...ok, candles: 10 }), /candles/);
assert.equal(candidateRejection({ ...ok, candles: 10, needVolatility: false }), null);
assert.match(candidateRejection({ ...ok, currency: 'EUR' }), /EUR/);
});
+217
View File
@@ -0,0 +1,217 @@
#!/usr/bin/env node
// DXtrade smoke test — run against a real login before the scheduler trades a
// DXtrade firm. Confirms the API details lib/dxtrade-class.ts assumes.
//
// DX_BASE=https://dx.tradeify247.co/dxsca-web DX_USER=... DX_PASS=... \
// DX_DOMAIN=default DX_SYMBOLS=BTC/USD,ETH/USD,XYZ100 node scripts/dxtrade-smoke.mjs
//
// Read-only unless DX_CONFIRM=yes AND DX_ACCOUNT=<account code> are set, which
// adds a minimum-size round trip on that account (a real order: it pays two
// commissions). Credentials come from the environment only; the password and
// session token are never printed.
//
// Run it while the instance's client for the firm is disconnected (remove the
// credentials there, or stop the instance): two sessions on one login can
// collide, and logins are rate-limited per IP.
const BASE = (process.env.DX_BASE ?? '').replace(/\/+$/, '');
const USER = process.env.DX_USER ?? '';
const PASS = process.env.DX_PASS ?? '';
const DOMAIN = process.env.DX_DOMAIN || 'default';
const SYMBOLS = (process.env.DX_SYMBOLS ?? 'BTC/USD').split(',').map((s) => s.trim()).filter(Boolean);
const ORDER_ACCOUNT = process.env.DX_ACCOUNT ?? '';
const CONFIRM = process.env.DX_CONFIRM === 'yes';
if (!BASE.startsWith('https://') || !USER || !PASS) {
console.error('Set DX_BASE (https://…/dxsca-web), DX_USER and DX_PASS.');
process.exit(1);
}
let token = null;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const section = (title) => console.log(`\n── ${title} ${'─'.repeat(Math.max(0, 70 - title.length))}`);
const show = (label, value) => console.log(`${label}:`, typeof value === 'string' ? value : JSON.stringify(value, null, 2)?.slice(0, 4000));
async function call(method, path, { params, body, headers } = {}) {
const url = new URL(`${BASE}${path}`);
for (const [k, v] of Object.entries(params ?? {})) url.searchParams.set(k, String(v));
const res = await fetch(url, {
method,
headers: {
Accept: 'application/json',
...(token ? { Authorization: `DXAPI ${token}` } : {}),
...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),
...headers,
},
body: body !== undefined ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(30_000),
});
const text = await res.text();
let data = null;
try { data = text ? JSON.parse(text) : null; } catch { data = text.slice(0, 500); }
return { status: res.status, headers: Object.fromEntries(res.headers), data };
}
async function step(label, fn) {
try {
await fn();
} catch (err) {
console.log(`${label} FAILED: ${err instanceof Error ? err.message : String(err)}`);
}
}
const codeOf = (a) => (typeof a === 'string' ? a : a && typeof a === 'object' ? `${a.clearing}:${a.account}` : String(a));
section('login');
{
const res = await call('POST', '/login', { body: { username: USER, domain: DOMAIN, password: PASS } });
if (res.status !== 200 || !res.data?.sessionToken) {
show(`login refused (HTTP ${res.status})`, res.data);
console.log('Check DX_DOMAIN (try "default", then the broker name) — and stop after a couple of tries: repeated failures lock the account.');
process.exit(1);
}
token = res.data.sessionToken;
show('session timeout field (format check)', res.data.timeout);
}
let accounts = [];
section('accounts (/users)');
for (const path of [`${USER}@${DOMAIN}`, USER]) {
const res = await call('GET', `/users/${encodeURIComponent(path)}`);
console.log(`/users/${path} → HTTP ${res.status}`);
if (res.status === 200) {
accounts = (res.data?.userDetails ?? []).flatMap((u) => u.accounts ?? []);
for (const a of accounts) {
console.log(` ${codeOf(a.account)} status=${a.accountStatus} positionBased=${a.positionBased} currency=${a.baseCurrency} registered=${a.registrationTime}`);
}
break;
}
}
const codes = accounts.map((a) => codeOf(a.account));
const ref = codes[0];
if (!ref) {
console.log('No accounts found — nothing more to check.');
process.exit(0);
}
section('metrics (key casing: openPL vs openPl)');
await step('metrics', async () => {
const res = await call('GET', '/accounts/metrics', { params: { accounts: codes.join(',') } });
show(`HTTP ${res.status}`, res.data?.metrics?.[0] ?? res.data);
});
section('positions / open orders');
await step('positions', async () => show('positions', (await call('GET', '/accounts/positions', { params: { accounts: codes.join(',') } })).data));
await step('orders', async () => show('open orders', (await call('GET', '/accounts/orders', { params: { accounts: codes.join(',') } })).data));
section('transfers — types, signs, paging, the opening DEPOSIT, FINANCING');
await step('transfers', async () => {
const from = new Date(Date.now() - 180 * 86_400_000).toISOString();
const res = await call('GET', '/accounts/transfers', { params: { accounts: codes.join(','), 'time-from': from, limit: 50 } });
const txns = (res.data?.cashTransfers ?? []).flatMap((t) => (t.cashTransactions ?? []).map((x) => ({ ...x, transferTime: t.transactionTime })));
const byType = {};
for (const t of txns) {
const e = (byType[t.type] ??= { count: 0, min: Infinity, max: -Infinity });
e.count++; e.min = Math.min(e.min, t.value); e.max = Math.max(e.max, t.value);
}
show('by type (value range → sign convention)', byType);
show('first / last transaction (ordering)', [txns[0], txns[txns.length - 1]]);
show('nextPageTransactionTime', res.data?.nextPageTransactionTime ?? '(none)');
});
section('instruments');
await step('instruments', async () => {
const res = await call('GET', '/instruments/query', { params: { symbols: SYMBOLS.join(',') } });
for (const i of res.data?.instruments ?? []) {
console.log(` ${i.symbol}: type=${i.type} priceIncrement=${i.priceIncrement} multiplier=${i.multiplier} lotSize=${i.lotSize} quantityIncrement=${i.quantityIncrement} currency=${i.currency} sessions=${(i.tradingHours ?? []).length}`);
}
show('first instrument, raw', res.data?.instruments?.[0]);
});
await step('account instrument details', async () => {
const res = await call('GET', `/accounts/${encodeURIComponent(ref)}/instruments/query`, { params: { symbols: SYMBOLS.join(',') } });
show('details', res.data?.instrumentDetails ?? res.data);
});
section('quotes and hourly candles (σ)');
await step('quotes', async () => {
const res = await call('POST', '/marketdata', { body: { account: ref, symbols: SYMBOLS, eventTypes: [{ type: 'Quote', format: 'COMPACT' }] } });
show(`Quote events (HTTP ${res.status})`, res.data?.events ?? res.data);
});
await step('candles', async () => {
const now = Date.now();
const res = await call('POST', '/marketdata', {
body: {
account: ref, symbols: SYMBOLS,
eventTypes: [{ type: 'Candle', format: 'COMPACT', candleType: '1h', fromTime: new Date(now - 49 * 3_600_000).toISOString(), toTime: new Date(now).toISOString() }],
},
});
const events = res.data?.events ?? [];
console.log(`Candle events: HTTP ${res.status}, ${events.length} event(s); first: ${JSON.stringify(events[0])}`);
for (const symbol of SYMBOLS) {
const closes = events.filter((e) => e.symbol === symbol && typeof e.close === 'number').map((e) => e.close);
const rets = closes.slice(1).map((c, i) => Math.log(c / closes[i]));
const mean = rets.reduce((s, r) => s + r, 0) / Math.max(1, rets.length);
const sd = Math.sqrt(rets.reduce((s, r) => s + (r - mean) ** 2, 0) / Math.max(1, rets.length - 1));
console.log(` ${symbol}: ${closes.length} closes, σ ≈ ${(sd * 100).toFixed(3)}%/h ≈ ${(sd * Math.sqrt(24) * 100).toFixed(2)}%/day`);
}
});
section('end-of-day metrics vs the 22:00 UTC reset');
await step('eodmetrics', async () => {
const d = new Date(Date.now() - 86_400_000).toISOString().slice(0, 10);
const res = await call('GET', `/accounts/eodmetrics/${d}`, { params: { accounts: ref } });
show(`eodmetrics ${d} (HTTP ${res.status}) — compare balance with the instance's dayStart debug value`, res.data);
});
section('rate limits (two history calls back to back)');
await step('rate', async () => {
const a = await call('GET', '/accounts/transfers', { params: { accounts: ref, limit: 1 } });
const b = await call('GET', '/accounts/transfers', { params: { accounts: ref, limit: 1 } });
console.log(`HTTP ${a.status}, ${b.status}${b.status === 429 ? ' — history calls are limited to 1/s' : ''}`);
});
section('ping');
console.log(`POST /ping → HTTP ${(await call('POST', '/ping')).status}`);
if (CONFIRM && ORDER_ACCOUNT) {
section(`LIVE ORDERS on ${ORDER_ACCOUNT}`);
const acct = encodeURIComponent(ORDER_ACCOUNT);
const symbol = SYMBOLS[0];
const inst = (await call('GET', '/instruments/query', { params: { symbols: symbol } })).data?.instruments?.[0] ?? {};
const det = (await call('GET', `/accounts/${acct}/instruments/query`, { params: { symbols: symbol } })).data?.instrumentDetails?.[0] ?? {};
const qty = Math.max(det.minOrderSize ?? 0, inst.quantityIncrement ?? 0, det.minOrderSizeIncrement ?? 0) || 1;
const code = `af-smoke-${Date.now().toString(36)}`;
const open = { account: ORDER_ACCOUNT, orderCode: code, type: 'MARKET', instrument: symbol, quantity: qty, positionEffect: 'OPEN', side: 'BUY', tif: 'GTC' };
const placed = await call('POST', `/accounts/${acct}/orders`, { body: open });
show(`MARKET BUY ${qty} ${symbol} (HTTP ${placed.status})`, placed.data);
show('duplicate orderCode (expect 409 / code 100)', (await call('POST', `/accounts/${acct}/orders`, { body: open })));
await sleep(3_000);
const pos = ((await call('GET', `/accounts/${acct}/positions`)).data?.positions ?? []).find((p) => p.symbol === symbol);
show('position (positionCode should equal the orderId on position-based accounts)', pos);
show('order history row (commission, averagePrice)', (await call('GET', `/accounts/${acct}/orders/history`, { params: { 'with-client-id': code, limit: 5 } })).data);
if (pos) {
const px = pos.openPrice;
const tick = inst.priceIncrement || 0.01;
const far = (p) => Math.round(p / tick) * tick;
const leg = (c, type, price) => ({
account: ORDER_ACCOUNT, orderCode: c, type, instrument: symbol, quantity: pos.quantity, positionEffect: 'CLOSE',
positionCode: pos.positionCode, side: 'SELL', tif: 'GTC', ...(type === 'LIMIT' ? { limitPrice: price } : { stopPrice: price }),
});
const group = await call('POST', `/accounts/${acct}/orders`, {
body: { orders: [leg(`${code}-tp`, 'LIMIT', far(px * 1.05)), leg(`${code}-sl`, 'STOP', far(px * 0.95))], contingencyType: 'OCO' },
});
show(`native OCO group of CLOSE legs (HTTP ${group.status})`, group.data);
show('open orders now', (await call('GET', `/accounts/${acct}/orders`)).data);
const del = await call('DELETE', `/accounts/${acct}/orders/${encodeURIComponent(`${code}-tp`)}`);
show(`cancel without If-Match (HTTP ${del.status}; 403/412/428 means it wants one)`, del.data);
const flatten = await call('POST', `/accounts/${acct}/close`, { body: { account: ORDER_ACCOUNT, closePositions: true, cancelOrders: true } });
show(`flatten via /close (HTTP ${flatten.status})`, flatten.data);
}
await sleep(3_000);
const recent = await call('GET', `/accounts/${acct}/transfers`, { params: { 'time-from': new Date(Date.now() - 10 * 60_000).toISOString() } });
show('transactions of the last 10 minutes (commission = 0.04% × notional per side?)', recent.data);
} else {
console.log('\n(read-only: set DX_CONFIRM=yes and DX_ACCOUNT=<code> to also test a live minimum-size round trip)');
}
await call('POST', '/logout');