- SQLite DB (better-sqlite3) with firms, account_configs, firm_fees, instruments tables - REST API routes: firms CRUD, account configs CRUD, state, accounts, instruments - Live Tradovate WebSocket client: login, sync, positions, auto-liq thresholds - Dashboard (app/page.tsx): per-firm account list with balance, day P&L, days traded, target progress, and Dead/Inactive/Flat status based on Tradovate auto-liq floors - Account detail page: objectives progress, daily P&L chart, consistency tracking - Per-firm settings page: account configs and instrument fee management - Dead detection uses trailingMaxDrawdownLimit - trailingMaxDrawdown from userAccountAutoLiqs; filters Tradovate sentinel value (999999999 = no limit) - FIFO P&L engine with commission accounting for daily P&L history - Removed manual maxLoss fallback in favour of live Tradovate auto-liq data Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
555 lines
23 KiB
TypeScript
555 lines
23 KiB
TypeScript
// Create a class with a constructor that takes a username and password and sets the credentials
|
|
|
|
import axios from 'axios';
|
|
import type { AccountItem, AuthLoginResponse, Contract } from './tradovate-helpers';
|
|
import { computeSec, randomUUIDV4 } from './tradovate-helpers';
|
|
|
|
export class TradovateClient {
|
|
private name: string;
|
|
private password: string;
|
|
private accessInfo: AuthLoginResponse;
|
|
private deviceId = randomUUIDV4();
|
|
|
|
public accountList: AccountItem[] = [];
|
|
public accountCashBalances: {
|
|
[accountId: number]: {
|
|
amount: number;
|
|
realizedPnL: number;
|
|
};
|
|
} = {};
|
|
public positions: {
|
|
[accountId: number]: {
|
|
contractId: number;
|
|
netPos: number;
|
|
netPrice: number;
|
|
timestamp: Date;
|
|
};
|
|
} = {};
|
|
|
|
public daysTraded: { [accountId: number]: number } = {};
|
|
public dailyPnL: { [accountId: number]: { date: string; pnl: number }[] } = {};
|
|
/** Balance floor set by the prop firm — account is blown when amount <= this value (0 = not set) */
|
|
public autoLiqThresholds: { [accountId: number]: number } = {};
|
|
|
|
public products: { id: number; name: string }[] = [];
|
|
|
|
private ws: WebSocket;
|
|
private callbackOnSyncRequest: () => Promise<void>;
|
|
|
|
// Events that we sent out, and tradovate gives us a response for the id we sent out
|
|
private directEventCallbacks: {
|
|
[id: number]: (response: any) => void;
|
|
} = {};
|
|
private indirectEventCallbacks: {
|
|
entityType:
|
|
| 'order'
|
|
| 'orderVersion'
|
|
| 'auditUserAction'
|
|
| 'command'
|
|
| 'commandReport'
|
|
| 'fill'
|
|
| 'fillFee'
|
|
| 'executionReport'
|
|
| 'cashBalance';
|
|
eventType: 'Created' | 'Updated';
|
|
// Since the entity is not always the same, we need a validator to check if the response is the one we are looking for
|
|
validator: (response: any) => boolean;
|
|
callback: (response: any) => void;
|
|
}[] = [];
|
|
|
|
|
|
constructor(
|
|
name: string,
|
|
password: string,
|
|
callbackOnSyncRequest: () => Promise<void>
|
|
) {
|
|
this.name = name;
|
|
this.password = password;
|
|
this.callbackOnSyncRequest = callbackOnSyncRequest;
|
|
|
|
this.login().then((res) => {
|
|
if (!res?.accessToken) {
|
|
console.log('Failed to login', res);
|
|
return;
|
|
}
|
|
|
|
console.log('Logged in', res);
|
|
|
|
// check every 2 minutes if the access token is expired
|
|
setInterval(() => {
|
|
// If we are within 15 minutes of the expiration time, renew the access token
|
|
if (
|
|
new Date(res.expirationTime).getTime() <
|
|
new Date().getTime() + 15 * 60 * 1000
|
|
) {
|
|
this.renewAccessToken();
|
|
}
|
|
}, 2 * 60 * 1000);
|
|
|
|
this.accessInfo = res;
|
|
|
|
const randomnumber = Math.random().toString(36).substring(2, 15);
|
|
this.ws = new WebSocket(`wss://demo.tradovateapi.com/v1/websocket?r=${randomnumber}`);
|
|
|
|
this.ws.onopen = () => {
|
|
console.log('Connected to websocket');
|
|
this.ws.send('authorize\n2\n\n' + this.accessInfo.accessToken);
|
|
this.directEventCallbacks[2] = (response: any) => {
|
|
this.requestSync();
|
|
setInterval(() => this.requestSync(), 60000);
|
|
|
|
// Every 2.5 seconds send a heartbeat
|
|
setInterval(() => {
|
|
this.ws.send('[]');
|
|
}, 2500);
|
|
};
|
|
};
|
|
|
|
this.ws.onmessage = (event) => {
|
|
if (event.data[0] === 'a') {
|
|
const dataString = event.data.slice(1);
|
|
|
|
const data: (
|
|
| {
|
|
i?: number; // Id
|
|
s?: number; // Status
|
|
d?: any; // Data
|
|
}
|
|
| {
|
|
e?: string;
|
|
d?: {
|
|
entityType:
|
|
| 'order'
|
|
| 'orderVersion'
|
|
| 'auditUserAction'
|
|
| 'command'
|
|
| 'commandReport';
|
|
eventType: 'Created' | 'Updated';
|
|
entity: any;
|
|
};
|
|
}
|
|
)[] = JSON.parse(dataString);
|
|
|
|
for (const response of data) {
|
|
if (
|
|
'i' in response &&
|
|
response.i &&
|
|
this.directEventCallbacks[response.i]
|
|
) {
|
|
this.directEventCallbacks[response.i](response.d);
|
|
}
|
|
|
|
if ('e' in response && response.e) {
|
|
// Handle callbacks
|
|
for (const info of this.indirectEventCallbacks) {
|
|
if (
|
|
response.d?.entityType === info?.entityType &&
|
|
response.d?.eventType === info?.eventType &&
|
|
info?.validator(response.d.entity)
|
|
) {
|
|
info?.callback(response.d.entity);
|
|
}
|
|
}
|
|
|
|
// console.log('No callback found', response.d);
|
|
}
|
|
}
|
|
} else if (event.data[0] === 'h') {
|
|
// Heartbeat response
|
|
}
|
|
};
|
|
|
|
this.ws.onerror = (event) => {
|
|
console.error('Error on websocket', event);
|
|
};
|
|
});
|
|
}
|
|
|
|
private requestSync(): void {
|
|
this.directEventCallbacks[3] = (response: any) => {
|
|
try {
|
|
if (!response) {
|
|
console.error('[requestSync] Received null/undefined response — auth may have failed');
|
|
return;
|
|
}
|
|
|
|
// liquidateOnly flag lives in accountRiskStatuses
|
|
const riskStatusById: { [accountId: number]: { liquidateOnly?: string } } = (
|
|
response.accountRiskStatuses || []
|
|
).reduce((acc: any, item: any) => {
|
|
const key = item.accountId ?? item.id;
|
|
acc[key] = item;
|
|
return acc;
|
|
}, {});
|
|
|
|
// Auto-liquidation balance floor lives in userAccountAutoLiqs.
|
|
// item.id IS the account ID. The floor is: trailingMaxDrawdownLimit - trailingMaxDrawdown.
|
|
// Tradovate uses 999999999 as a sentinel for "no limit" — skip those.
|
|
for (const item of (response.userAccountAutoLiqs ?? [])) {
|
|
const accountId: number = item.id;
|
|
const limit: number = item.trailingMaxDrawdownLimit ?? 0;
|
|
const drawdown: number = item.trailingMaxDrawdown ?? 0;
|
|
const isSentinel = limit >= 999999999;
|
|
const floor = (!isSentinel && limit > 0 && drawdown > 0) ? limit - drawdown : 0;
|
|
this.autoLiqThresholds[accountId] = floor;
|
|
if (floor > 0) {
|
|
console.log(`[autoLiq] account ${accountId} → floor $${floor} (hwm=$${limit} drawdown=$${drawdown})`);
|
|
}
|
|
}
|
|
|
|
this.accountList = ((response.accounts ?? []) as AccountItem[]).map((account) => {
|
|
if (riskStatusById[account.id]?.liquidateOnly) {
|
|
return { ...account, active: false };
|
|
}
|
|
return account;
|
|
});
|
|
|
|
this.accountCashBalances = (response.cashBalances ?? []).reduce(
|
|
(
|
|
acc: { [accountId: number]: { amount: number; realizedPnL: number } },
|
|
item: { accountId: number; amount: number; realizedPnL: number }
|
|
) => {
|
|
acc[item.accountId] = { amount: item.amount, realizedPnL: item.realizedPnL };
|
|
return acc;
|
|
},
|
|
{} as { [accountId: number]: { amount: number; realizedPnL: number } }
|
|
);
|
|
|
|
this.positions = (response.positions ?? [])
|
|
.filter((item: any) => item.netPos !== 0)
|
|
.reduce(
|
|
(
|
|
acc: { [accountId: number]: { contractId: number; netPos: number; netPrice: number; timestamp: Date } },
|
|
item: { accountId: number; contractId: number; netPos: number; netPrice: number; timestamp: Date }
|
|
) => {
|
|
acc[item.accountId] = {
|
|
contractId: item.contractId,
|
|
netPos: item.netPos,
|
|
netPrice: item.netPrice,
|
|
timestamp: new Date(item.timestamp),
|
|
};
|
|
return acc;
|
|
},
|
|
{} as { [accountId: number]: { contractId: number; netPos: number; netPrice: number; timestamp: Date } }
|
|
);
|
|
|
|
console.log(`[requestSync] ${this.accountList.length} account(s), ${Object.keys(this.accountCashBalances).length} balance(s)`);
|
|
|
|
this.fetchDaysTraded();
|
|
|
|
if (this.products.length > 0) {
|
|
this.callbackOnSyncRequest();
|
|
return;
|
|
}
|
|
|
|
this.directEventCallbacks[30] = (products: any) => {
|
|
if (Array.isArray(products) && products.length > 0) {
|
|
this.products = products.map((p: any) => ({ id: p.id, name: p.name }));
|
|
console.log(`Loaded ${this.products.length} products`);
|
|
}
|
|
this.callbackOnSyncRequest();
|
|
};
|
|
this.ws.send('product/list\n30\n\n');
|
|
} catch (err) {
|
|
console.error('[requestSync] Error processing sync response:', err);
|
|
}
|
|
};
|
|
|
|
this.ws.send('user/syncrequest\n3\n\n{"splitResponses":false}');
|
|
}
|
|
|
|
private async fetchDaysTraded(): Promise<void> {
|
|
if (!this.accessInfo?.accessToken) return;
|
|
|
|
const now = new Date();
|
|
const start = new Date();
|
|
start.setDate(start.getDate() - 28);
|
|
|
|
const fmtDate = (d: Date) => {
|
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
return `${m}/${day}/${d.getFullYear()}`;
|
|
};
|
|
|
|
for (const account of this.accountList) {
|
|
try {
|
|
const res = await axios.post(
|
|
'https://rpt-demo.tradovateapi.com/v1/reports/requestreport',
|
|
{
|
|
name: 'Fills',
|
|
params: [
|
|
{ name: 'startDate', value: fmtDate(start) },
|
|
{ name: 'endDate', value: fmtDate(now) },
|
|
{ name: 'startTime', value: '00:00:00' },
|
|
{ name: 'endTime', value: '00:00:00' },
|
|
{ name: 'account', value: account.name },
|
|
],
|
|
representationType: 'json',
|
|
timezone: 0,
|
|
},
|
|
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
|
|
);
|
|
// _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 ?? '[]')
|
|
.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"');
|
|
type Fill = {
|
|
_tradeDate: string;
|
|
_timestamp: string;
|
|
_action: number; // 0 = Buy, 1 = Sell
|
|
_qty: number;
|
|
_price: number;
|
|
Product: string;
|
|
commission: number;
|
|
};
|
|
const fills: Fill[] = JSON.parse(raw);
|
|
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,
|
|
};
|
|
|
|
// FIFO P&L computation: match buy/sell fills into round-trips
|
|
// Both the opening and closing commissions are deducted on close.
|
|
const sorted = [...fills].sort((a, b) => a._timestamp.localeCompare(b._timestamp));
|
|
interface Lot { price: number; qty: number; commPerUnit: number }
|
|
const longBook: Lot[] = [];
|
|
const shortBook: Lot[] = [];
|
|
const dailyMap: { [date: string]: number } = {};
|
|
|
|
for (const fill of sorted) {
|
|
const pointValue = POINT_VALUES[fill.Product] ?? 1;
|
|
const isBuy = fill._action === 0;
|
|
let remaining = fill._qty;
|
|
const commPerUnit = fill._qty > 0 ? fill.commission / fill._qty : 0;
|
|
|
|
if (isBuy) {
|
|
// Close any short lots first (FIFO), then open long
|
|
while (remaining > 0 && shortBook.length > 0) {
|
|
const lot = shortBook[0];
|
|
const closed = Math.min(lot.qty, remaining);
|
|
const pnl = (lot.price - fill._price) * closed * pointValue
|
|
- (commPerUnit * closed) // closing fill commission
|
|
- (lot.commPerUnit * closed); // opening fill commission
|
|
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
|
lot.qty -= closed;
|
|
remaining -= closed;
|
|
if (lot.qty === 0) shortBook.shift();
|
|
}
|
|
if (remaining > 0) longBook.push({ price: fill._price, qty: remaining, commPerUnit });
|
|
} else {
|
|
// Close any long lots first (FIFO), then open short
|
|
while (remaining > 0 && longBook.length > 0) {
|
|
const lot = longBook[0];
|
|
const closed = Math.min(lot.qty, remaining);
|
|
const pnl = (fill._price - lot.price) * closed * pointValue
|
|
- (commPerUnit * closed) // closing fill commission
|
|
- (lot.commPerUnit * closed); // opening fill commission
|
|
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
|
lot.qty -= closed;
|
|
remaining -= closed;
|
|
if (lot.qty === 0) longBook.shift();
|
|
}
|
|
if (remaining > 0) shortBook.push({ price: fill._price, qty: remaining, commPerUnit });
|
|
}
|
|
}
|
|
|
|
this.dailyPnL[account.id] = Object.entries(dailyMap)
|
|
.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);
|
|
this.daysTraded[account.id] ??= 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async login(): Promise<AuthLoginResponse> {
|
|
const chl = '' + (Date.now() - 1581e9);
|
|
const sec = computeSec(this.name, this.password, this.deviceId, chl);
|
|
|
|
const generateEncodedPassword = (e) => {
|
|
const { name: t, password: n } = e,
|
|
o = t.length % n.length,
|
|
r = (n.slice(o) + n.slice(0, o)).split('').reverse().join('');
|
|
return btoa(r);
|
|
};
|
|
|
|
const res = await axios.post('https://live.tradovateapi.com/v1/auth/accesstokenrequest', {
|
|
name: this.name,
|
|
password: generateEncodedPassword({ name: this.name, password: this.password }),
|
|
environment: 'demo',
|
|
appId: 'tradovate_trader(web)',
|
|
appVersion: '3.251205.0',
|
|
deviceId: this.deviceId,
|
|
cid: '1',
|
|
chl: chl,
|
|
sec: sec,
|
|
enc: true,
|
|
});
|
|
|
|
if (res.status !== 200) {
|
|
console.error('Failed to login', res.data);
|
|
}
|
|
|
|
return res.data;
|
|
}
|
|
|
|
private async renewAccessToken(): Promise<void> {
|
|
const res = await axios.get('https://live.tradovateapi.com/v1/auth/renewAccessToken', {
|
|
headers: {
|
|
Authorization: `Bearer ${this.accessInfo.accessToken}`,
|
|
},
|
|
});
|
|
|
|
if (res.status !== 200) {
|
|
console.error('Failed to renew access token', res.data);
|
|
}
|
|
|
|
console.log('Renewed access token');
|
|
this.accessInfo = res.data;
|
|
}
|
|
|
|
async getAccountList(): Promise<AccountItem[]> {
|
|
if (!this.accessInfo?.accessToken) {
|
|
console.log('Not authenticated');
|
|
return [];
|
|
}
|
|
|
|
const res = await axios.get('https://demo.tradovateapi.com/v1/account/list', {
|
|
headers: {
|
|
Authorization: `Bearer ${this.accessInfo.accessToken}`,
|
|
},
|
|
});
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async fetchInstrumentFees(symbols: string[]): Promise<{ [symbol: string]: number }> {
|
|
if (!this.accessInfo?.accessToken || this.products.length === 0) return {};
|
|
|
|
const productIds = symbols
|
|
.map((sym) => this.products.find((p) => p.name === sym)?.id)
|
|
.filter((id): id is number => id !== undefined);
|
|
|
|
const res = await axios.post(
|
|
'https://demo.tradovateapi.com/v1/contract/getproductfeeparams',
|
|
{ productIds },
|
|
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
|
|
);
|
|
|
|
const result: { [symbol: string]: number } = {};
|
|
for (const param of (res.data?.params ?? [])) {
|
|
const product = this.products.find((p) => p.id === param.productId);
|
|
if (product && symbols.includes(product.name)) {
|
|
const raw =
|
|
(param.clearingFee ?? 0) +
|
|
(param.exchangeFee ?? 0) +
|
|
(param.nfaFee ?? 0) +
|
|
(param.brokerageFee ?? 0) +
|
|
(param.ipFee ?? 0) +
|
|
(param.commission ?? 0) +
|
|
(param.orderRoutingFee ?? 0);
|
|
result[product.name] = parseFloat(raw.toFixed(4));
|
|
console.log(
|
|
`[fees] ${product.name}: clearing=${param.clearingFee ?? 0}` +
|
|
` exchange=${param.exchangeFee ?? 0}` +
|
|
` nfa=${param.nfaFee ?? 0}` +
|
|
` brokerage=${param.brokerageFee ?? 0}` +
|
|
` ip=${param.ipFee ?? 0}` +
|
|
` commission=${param.commission ?? 0}` +
|
|
` routing=${param.orderRoutingFee ?? 0}` +
|
|
` → total=${result[product.name]}`
|
|
);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async requestContractsFromSocket(names: string[]): Promise<{
|
|
[name: string]: Contract;
|
|
}> {
|
|
if (!this.ws) {
|
|
console.log('Websocket not connected');
|
|
return {};
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
this.directEventCallbacks[10] = (response: any) => {
|
|
resolve(
|
|
(response || []).reduce((acc: { [name: string]: Contract }, item: Contract) => {
|
|
return { ...acc, [item.name]: item };
|
|
}, {} as { [name: string]: Contract })
|
|
);
|
|
};
|
|
this.ws.send(`contract/finds\n10\nnames=${names.join(',')}`);
|
|
});
|
|
}
|
|
|
|
async sendOrder(
|
|
accountId: number,
|
|
contractId: number,
|
|
quantity: number,
|
|
action: 'Buy' | 'Sell',
|
|
orderType: 'Market' | 'Limit',
|
|
price?: number
|
|
): Promise<any> {
|
|
if (!this.ws) {
|
|
console.log('Websocket not connected');
|
|
return;
|
|
}
|
|
|
|
this.ws.send(
|
|
`user/registeraudituseraction\n25\n\n${JSON.stringify({
|
|
accountId: accountId,
|
|
actionType: action + orderType,
|
|
details: `DOM MESZ5: Buy ${orderType}, Buy ${quantity} ${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);
|
|
|
|
// 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);
|
|
|
|
this.indirectEventCallbacks.push({
|
|
entityType: 'fill',
|
|
eventType: 'Created',
|
|
validator: (item: any) => item?.orderId === response?.orderId,
|
|
callback: (response: any) => {
|
|
resolve(response);
|
|
},
|
|
});
|
|
};
|
|
|
|
this.ws.send(
|
|
`order/placeorder\n26\n\n${JSON.stringify({
|
|
accountId: accountId,
|
|
action: action,
|
|
symbol: contractId,
|
|
orderQty: quantity,
|
|
orderType: orderType,
|
|
price: price,
|
|
timeInForce: 'Day',
|
|
text: 'DOM',
|
|
})}`
|
|
);
|
|
});
|
|
}
|
|
}
|