NexusTrade guides

Backtest a portfolio in TypeScript

Author a typed NexusTrade portfolio in Node, submit a backtest, and await the historical result.

Install and authenticate

Install nexustrade from npm and configure NEXUSTRADE_API_KEY in the server runtime. Never embed a private key in browser bundles. Registered keys require write for submission and read for result retrieval.

shell
npm install nexustrade

Run the documented example

Place the complete example in a Node TypeScript project that supports top-level await. Install nexustrade in that project and configure the API key in the same process that runs the script. The strategy builders serialize the same indicator specification used by the trading engine. Local construction creates configuration; createBacktest submits an operation. This example is historical research, not a performance claim.

typescript
import {
  NexusTradeClient,
  always,
  backtest,
  buy,
  portfolio,
  stockAsset,
  strategy,
} from "nexustrade";

const client = new NexusTradeClient();

const book = portfolio("Example", [
  strategy("Buy SPY", always(), buy(stockAsset("SPY"), 100)),
]);

const operation = await client.createBacktest(
  backtest(book, { startDate: "2024-01-01", endDate: "2024-12-31" }),
  { idempotencyKey: "example-v1" }
);
const result = await client.waitForBacktest(operation.id as string);
console.log(result.result);

Wait for completion and retry the same request

On success, result.result contains the historical result. The wait helper throws on failed or cancelled operations by default. If it times out, call client.getBacktest with the saved ID or resume client.waitForBacktest on that ID. A timeout leaves the operation running. Keep the original operation ID and await completion. Reuse the same idempotency key only for the same request; changing a payload under that key returns a conflict. Inspect terminal warnings as well as success.

The SDK uses HTTPS except loopback and rejects unsafe redirects. A transport error does not establish whether a job was accepted; recover the idempotent operation rather than submitting unrelated duplicate work.

Continue exploring