Add auto-trade scheduler with batch locking, commission gross-up, and sync gate
- Auto-trade scheduler fires every 60s; uses Promise.allSettled batch so no new trades fire while any position from the current batch is open
- Commission gross-up: read entryCommission from cash.realizedPnL after fill (fallback 2.5×contracts), grossTarget = target + 2×entryCommission
- Sync gate: TradovateClient.syncComplete flag; scheduler skips tick until every client finishes initial position/balance sync
- Contracts formula changed to Math.ceil so $1500 target = 2 contracts
- Removed all fee caching (perContractFees, recentFills, fillFee handler) from tradovate-class.ts
- Removed firm_fees table, getFirmFees, upsertFirmFee from db.ts
- Deleted instrument-configs API routes; removed Fees UI from firm settings page
- /api/instruments returns full {symbol, enabled}[] objects; dashboard filters to enabled-only for trade selector
- Added auto-trade, debug, orders, settings, and trade API routes
- Instrument selector on dashboard now driven by enabled instruments from DB
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
532c2e2279
commit
b2a1bdd1c3
+181
-43
@@ -3,6 +3,7 @@
|
||||
import axios from 'axios';
|
||||
import type { AccountItem, AuthLoginResponse, Contract } from './tradovate-helpers';
|
||||
import { computeSec, randomUUIDV4 } from './tradovate-helpers';
|
||||
import { POINT_VALUES } from './trading-logic';
|
||||
|
||||
export class TradovateClient {
|
||||
private name: string;
|
||||
@@ -31,11 +32,30 @@ export class TradovateClient {
|
||||
/** Balance floor set by the prop firm — account is blown when amount <= this value (0 = not set) */
|
||||
public autoLiqThresholds: { [accountId: number]: number } = {};
|
||||
|
||||
/** True once fetchDaysTraded() has finished its last full run */
|
||||
public fetchDaysComplete = false;
|
||||
/** Last error per account name from fetchDaysTraded() */
|
||||
public lastFetchErrors: Record<string, string> = {};
|
||||
/** Raw reports API response data per account (first 200 chars) for debugging */
|
||||
public lastFetchRaw: Record<string, string> = {};
|
||||
|
||||
public products: { id: number; name: string }[] = [];
|
||||
|
||||
|
||||
/** Rolling buffer of the last 50 raw entity events — useful for debugging */
|
||||
public recentEntityEvents: { entityType: string; eventType: string; entity: any; ts: number }[] = [];
|
||||
|
||||
/** True once the first requestSync has completed and positions/balances are populated. */
|
||||
public syncComplete = false;
|
||||
|
||||
|
||||
private ws: WebSocket;
|
||||
private callbackOnSyncRequest: () => Promise<void>;
|
||||
|
||||
/** Incrementing ID for outgoing WebSocket messages — ensures concurrent orders don't clobber each other's callbacks. */
|
||||
private nextMsgId = 100;
|
||||
private getMsgId(): number { return this.nextMsgId++; }
|
||||
|
||||
// Events that we sent out, and tradovate gives us a response for the id we sent out
|
||||
private directEventCallbacks: {
|
||||
[id: number]: (response: any) => void;
|
||||
@@ -48,7 +68,6 @@ export class TradovateClient {
|
||||
| 'command'
|
||||
| 'commandReport'
|
||||
| 'fill'
|
||||
| 'fillFee'
|
||||
| 'executionReport'
|
||||
| 'cashBalance';
|
||||
eventType: 'Created' | 'Updated';
|
||||
@@ -151,7 +170,39 @@ export class TradovateClient {
|
||||
}
|
||||
}
|
||||
|
||||
// console.log('No callback found', response.d);
|
||||
// Buffer recent entity events (last 50)
|
||||
if (response.d?.entityType) {
|
||||
this.recentEntityEvents.push({ entityType: response.d.entityType, eventType: response.d.eventType, entity: response.d.entity, ts: Date.now() });
|
||||
if (this.recentEntityEvents.length > 50) this.recentEntityEvents.shift();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update positions from WebSocket position events
|
||||
if (response.d?.entityType === 'position' && response.d?.entity) {
|
||||
const pos = response.d.entity;
|
||||
if (pos.netPos !== 0) {
|
||||
this.positions[pos.accountId] = {
|
||||
contractId: pos.contractId,
|
||||
netPos: pos.netPos,
|
||||
netPrice: pos.netPrice,
|
||||
timestamp: new Date(pos.timestamp),
|
||||
};
|
||||
} else {
|
||||
delete this.positions[pos.accountId];
|
||||
}
|
||||
}
|
||||
|
||||
// Update cash balances from WebSocket cashBalance events
|
||||
if (response.d?.entityType === 'cashBalance' && response.d?.entity) {
|
||||
const cb = response.d.entity;
|
||||
if (cb.accountId) {
|
||||
this.accountCashBalances[cb.accountId] = {
|
||||
amount: cb.amount,
|
||||
realizedPnL: cb.realizedPnL,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (event.data[0] === 'h') {
|
||||
@@ -238,6 +289,7 @@ export class TradovateClient {
|
||||
this.fetchDaysTraded();
|
||||
|
||||
if (this.products.length > 0) {
|
||||
this.syncComplete = true;
|
||||
this.callbackOnSyncRequest();
|
||||
return;
|
||||
}
|
||||
@@ -247,6 +299,7 @@ export class TradovateClient {
|
||||
this.products = products.map((p: any) => ({ id: p.id, name: p.name }));
|
||||
console.log(`Loaded ${this.products.length} products`);
|
||||
}
|
||||
this.syncComplete = true;
|
||||
this.callbackOnSyncRequest();
|
||||
};
|
||||
this.ws.send('product/list\n30\n\n');
|
||||
@@ -260,6 +313,7 @@ export class TradovateClient {
|
||||
|
||||
private async fetchDaysTraded(): Promise<void> {
|
||||
if (!this.accessInfo?.accessToken) return;
|
||||
this.fetchDaysComplete = false;
|
||||
|
||||
const now = new Date();
|
||||
const start = new Date();
|
||||
@@ -273,7 +327,10 @@ export class TradovateClient {
|
||||
|
||||
for (const account of this.accountList) {
|
||||
try {
|
||||
const res = await axios.post(
|
||||
const authHeaders = { Authorization: `Bearer ${this.accessInfo.accessToken}` };
|
||||
|
||||
// Step 1 — request the report
|
||||
let reportData = (await axios.post(
|
||||
'https://rpt-demo.tradovateapi.com/v1/reports/requestreport',
|
||||
{
|
||||
name: 'Fills',
|
||||
@@ -287,11 +344,29 @@ export class TradovateClient {
|
||||
representationType: 'json',
|
||||
timezone: 0,
|
||||
},
|
||||
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
|
||||
);
|
||||
{ headers: authHeaders }
|
||||
)).data;
|
||||
|
||||
// Step 2 — if the report is queued, poll until it's ready
|
||||
let pollAttempts = 0;
|
||||
while (reportData?.['p-ticket'] && pollAttempts < 30) {
|
||||
const pTicket: string = reportData['p-ticket'];
|
||||
const pTime: number = Math.max(1, reportData['p-time'] ?? 1);
|
||||
await new Promise((r) => setTimeout(r, pTime * 1000));
|
||||
reportData = (await axios.get(
|
||||
'https://rpt-demo.tradovateapi.com/v1/reports/getreport',
|
||||
{ params: { 'p-ticket': pTicket }, headers: authHeaders }
|
||||
)).data;
|
||||
pollAttempts++;
|
||||
}
|
||||
|
||||
if (!this.lastFetchRaw) this.lastFetchRaw = {};
|
||||
this.lastFetchRaw[account.name] = JSON.stringify(reportData).slice(0, 500);
|
||||
|
||||
// _tradeDate is unquoted in the response (invalid JSON), but the "Date" field
|
||||
// ("M/D/YY") is a valid quoted string that already reflects CME trade date.
|
||||
const raw: string = (res.data?.data ?? '[]')
|
||||
const rawResponse = reportData?.data ?? '[]';
|
||||
const raw: string = String(rawResponse)
|
||||
.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"');
|
||||
type Fill = {
|
||||
_tradeDate: string;
|
||||
@@ -306,14 +381,7 @@ export class TradovateClient {
|
||||
const uniqueDays = new Set(fills.map(f => f._tradeDate));
|
||||
this.daysTraded[account.id] = uniqueDays.size;
|
||||
|
||||
// Dollar-per-point map for common futures products
|
||||
const POINT_VALUES: { [product: string]: number } = {
|
||||
NQ: 20, MNQ: 2, ES: 50, MES: 5,
|
||||
YM: 5, MYM: 0.5, RTY: 50, M2K: 10,
|
||||
GC: 100, MGC: 10, SI: 50, CL: 1000,
|
||||
MCL: 100, NG: 10000, ZB: 1000, ZN: 1000,
|
||||
ZF: 1000, '6E': 125000, '6J': 12500000, '6B': 62500,
|
||||
};
|
||||
// POINT_VALUES imported from trading-logic.ts
|
||||
|
||||
// FIFO P&L computation: match buy/sell fills into round-trips
|
||||
// Both the opening and closing commissions are deducted on close.
|
||||
@@ -364,10 +432,14 @@ export class TradovateClient {
|
||||
.map(([date, pnl]) => ({ date, pnl: Math.round(pnl * 100) / 100 }))
|
||||
.sort((a, b) => a.date.localeCompare(b.date));
|
||||
} catch (err) {
|
||||
console.error(`[fetchDaysTraded] ${account.name}`, err);
|
||||
const msg = err instanceof Error ? `${err.message}` : String(err);
|
||||
console.error(`[fetchDaysTraded] ${account.name}:`, msg);
|
||||
if (!this.lastFetchErrors) this.lastFetchErrors = {};
|
||||
this.lastFetchErrors[account.name] = msg;
|
||||
this.daysTraded[account.id] ??= 0;
|
||||
}
|
||||
}
|
||||
this.fetchDaysComplete = true;
|
||||
}
|
||||
|
||||
private async login(): Promise<AuthLoginResponse> {
|
||||
@@ -431,6 +503,19 @@ export class TradovateClient {
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async findFrontMonthContract(productName: string): Promise<{ id: number; name: string; tickSize: number } | null> {
|
||||
if (!this.accessInfo?.accessToken) return null;
|
||||
const res = await axios.get(
|
||||
`https://demo.tradovateapi.com/v1/contract/suggest?t=${encodeURIComponent(productName)}&l=20`,
|
||||
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
|
||||
);
|
||||
const contracts: Array<{ id: number; name: string; status: string; providerTickSize: number }> = res.data ?? [];
|
||||
// Front-month = first contract whose name starts with the product symbol (results are ordered front→back)
|
||||
const match = contracts.find((c) => c.name.startsWith(productName));
|
||||
if (!match) return null;
|
||||
return { id: match.id, name: match.name, tickSize: match.providerTickSize ?? 0.25 };
|
||||
}
|
||||
|
||||
async fetchInstrumentFees(symbols: string[]): Promise<{ [symbol: string]: number }> {
|
||||
if (!this.accessInfo?.accessToken || this.products.length === 0) return {};
|
||||
|
||||
@@ -494,61 +579,114 @@ export class TradovateClient {
|
||||
|
||||
async sendOrder(
|
||||
accountId: number,
|
||||
contractId: number,
|
||||
contractSymbol: string, // e.g. "NQH6" — WebSocket placeorder requires "symbol"
|
||||
quantity: number,
|
||||
action: 'Buy' | 'Sell',
|
||||
orderType: 'Market' | 'Limit',
|
||||
price?: number
|
||||
): Promise<any> {
|
||||
if (!this.ws) {
|
||||
console.log('Websocket not connected');
|
||||
return;
|
||||
}
|
||||
): Promise<Record<string, any>> {
|
||||
if (!this.ws) throw new Error('WebSocket not connected');
|
||||
|
||||
this.ws.send(
|
||||
`user/registeraudituseraction\n25\n\n${JSON.stringify({
|
||||
accountId: accountId,
|
||||
accountId,
|
||||
actionType: action + orderType,
|
||||
details: `DOM MESZ5: Buy ${orderType}, Buy ${quantity} ${orderType}${
|
||||
price ? ` ${price}` : ''
|
||||
}, TIF Day`,
|
||||
details: `${action} ${quantity} ${contractSymbol} ${orderType}${price ? ` @ ${price}` : ''}, TIF Day`,
|
||||
})}`
|
||||
);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.directEventCallbacks[26] = (response: any) => {
|
||||
// console.log('Order id', response?.orderId);
|
||||
console.log('Order placed, order id: ', response);
|
||||
const msgId = this.getMsgId();
|
||||
this.directEventCallbacks[msgId] = (response: any) => {
|
||||
console.log(`[sendOrder] raw ack:`, JSON.stringify(response));
|
||||
if (typeof response === 'string' || !response) {
|
||||
reject(new Error(typeof response === 'string' ? response : 'Empty response from order/placeorder'));
|
||||
return;
|
||||
}
|
||||
// Tradovate returns the command object: { id, commandStatus, orderId, ... }
|
||||
const orderId = response.orderId ?? response.id;
|
||||
console.log(`[sendOrder] orderId=${orderId} status=${response.commandStatus}`);
|
||||
|
||||
// TODO: Implement for limit orders and rejected market orders
|
||||
|
||||
// If we don't get a response within 5 seconds, reject the promise
|
||||
setTimeout(() => {
|
||||
reject(new Error('No response from order placement'));
|
||||
}, 5000);
|
||||
// Wait for the fill event — it carries the real execution price
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error(`Order ${orderId} acknowledged but no fill within 30s (market may be closed)`));
|
||||
}, 30000);
|
||||
|
||||
this.indirectEventCallbacks.push({
|
||||
entityType: 'fill',
|
||||
eventType: 'Created',
|
||||
validator: (item: any) => item?.orderId === response?.orderId,
|
||||
callback: (response: any) => {
|
||||
resolve(response);
|
||||
validator: (item: any) => item?.orderId === orderId,
|
||||
callback: (fill: any) => {
|
||||
clearTimeout(timeout);
|
||||
console.log(`[sendOrder] fill:`, JSON.stringify(fill));
|
||||
resolve(fill);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
this.ws.send(
|
||||
`order/placeorder\n26\n\n${JSON.stringify({
|
||||
accountId: accountId,
|
||||
action: action,
|
||||
symbol: contractId,
|
||||
`order/placeorder\n${msgId}\n\n${JSON.stringify({
|
||||
accountId,
|
||||
action,
|
||||
symbol: contractSymbol,
|
||||
orderQty: quantity,
|
||||
orderType: orderType,
|
||||
price: price,
|
||||
orderType,
|
||||
price,
|
||||
timeInForce: 'Day',
|
||||
text: 'DOM',
|
||||
})}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/** Place an order and resolve as soon as the command is acknowledged (does not wait for fill). */
|
||||
async placeOrderNoWait(
|
||||
accountId: number,
|
||||
contractSymbol: string,
|
||||
quantity: number,
|
||||
action: 'Buy' | 'Sell',
|
||||
orderType: 'Market' | 'Limit',
|
||||
price?: number
|
||||
): Promise<{ orderId?: number }> {
|
||||
if (!this.ws) throw new Error('WebSocket not connected');
|
||||
|
||||
const msgId = this.getMsgId();
|
||||
return new Promise((resolve, reject) => {
|
||||
this.directEventCallbacks[msgId] = (response: any) => {
|
||||
if (typeof response === 'string') {
|
||||
reject(new Error(response));
|
||||
return;
|
||||
}
|
||||
resolve({ orderId: response?.orderId ?? response?.id });
|
||||
};
|
||||
|
||||
this.ws.send(
|
||||
`order/placeorder\n${msgId}\n\n${JSON.stringify({
|
||||
accountId,
|
||||
action,
|
||||
symbol: contractSymbol,
|
||||
orderQty: quantity,
|
||||
orderType,
|
||||
price,
|
||||
timeInForce: 'Day',
|
||||
text: 'DOM',
|
||||
})}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/** Close the WebSocket connection and stop all intervals. Call before discarding the instance. */
|
||||
public disconnect(): void {
|
||||
try { this.ws?.close(); } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
/** Register a one-time callback for when a fill arrives for a given orderId. */
|
||||
onFill(orderId: number, callback: (fill: any) => void): void {
|
||||
this.indirectEventCallbacks.push({
|
||||
entityType: 'fill',
|
||||
eventType: 'Created',
|
||||
validator: (item: any) => item?.orderId === orderId,
|
||||
callback,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user