Calculations

EndureTrade session performance metrics are JavaScript functions stored in the database and executed only in your browser. Manage them at Calculations.

Function contract

Source must define function calculate(params) and return a non-empty array of number or string values. Single-value table cells show the last array element.

function calculate(params) {
  // params: CalculationParams
  console.log('trade count', params.trades.length);
  return [params.trades.length];
}

Params object

calculate receives a CalculationParams object. There is no side field — the performance tab filters long/short/all trades and passes the subset as params.trades.

interface CalculationParams {
  capital: number;
  trades: Trade[];
}

interface Trade {
  id: string; // uuid
  userId: string;
  sessionId: string;
  tradeNumber: number;
  symbol: string;
  direction: 'long' | 'short';
  positionSize: number;
  entryPrice: number;
  openedAt: string; // ISO 8601 UTC
  /** Omit while the position is still open. */
  exitPrice?: number;
  closedAt?: string;
  profitLoss?: number;
  note?: string;
  tags?: string[];
  entryMethod?: string;
  exitMethod?: string;
  stopLoss?: number;
  takeProfit?: number;
  favorableExcursion?: number;
  adverseExcursion?: number;
  timeframe?: string;
  createdAt: string;
  updatedAt: string;
  messages?: Array<{ id: string; content?: string; createdAt: string }>;
}

Debugging with logs

In the calculation form tester, use console.log, console.warn, or console.error. Captured lines appear in a Logs section under the result (they are not shown on the session performance table).

Prefer console for debug output. You can also put intermediate strings in the return array — the performance table still displays only the last element:

function calculate(params) {
  var closed = (params.trades || []).filter(function (t) {
    return typeof t.profitLoss === 'number';
  });
  // last value is the cell display; earlier strings are visible in the tester Result JSON
  return ['closed=' + closed.length, closed.length];
}

Errors and timeout

Thrown errors become { ok: false, error: "…" }. Invalid returns use Invalid return type. Runs that exceed your timeout (Settings → Preferences, default 5000 ms) are killed and return Calculation timed out.

Import / export JSON shape