NexusTrade TypeScript SDK
Author trading strategies in typed TypeScript. Backtest them on the engine that runs them live.
Quickstart · Authoring · Polling · Agents · Lake SQL · Auth · Errors
npm install nexustrade
Zero runtime dependencies. ESM and CommonJS builds ship together, with types.
MCP server
NexusTrade also exposes the platform as a hosted, remote Model Context Protocol server. Modern MCP clients connect directly to the production Streamable HTTP endpoint and discover NexusTrade OAuth automatically:
claude mcp add --transport http nexustrade https://nexustrade.io/api/mcp
Cursor and other remote-capable clients use:
{
"mcpServers": {
"nexustrade": {
"url": "https://nexustrade.io/api/mcp"
}
}
}
For Claude Desktop and other stdio-only clients, use the established
mcp-remote bridge—no clone or local NexusTrade server is required:
{
"mcpServers": {
"nexustrade": {
"command": "npx",
"args": [
"-y",
"mcp-remote@latest",
"https://nexustrade.io/api/mcp",
"--transport",
"http-only"
]
}
}
}
The live server exposes more than 120 tools across market research, portfolio construction, backtesting, optimization, walk-forward validation, managed compute, Aurora agents, paper trading, and controlled brokerage operations. Its creator-marketplace tools cover the full strategy adoption path:
search_creatorsdiscovers public creators and their marketplace portfolios.subscribe_portfoliovalidates a monetized listing and returns a safe checkout preview; the user completes payment in NexusTrade, never through the MCP tool.fork_shared_portfoliocreates a one-time editable copy of a marketplace strategy in a new or existing portfolio.copy_trade_sharedcontinuously mirrors an accessible strategy into a paper or live portfolio at an explicit allocation.
See the developer guide, the utility tool reference, and the Aurora tool reference.
Research and historical results are not investment advice and do not guarantee future performance. Keep paper and live modes explicit. Tools that can affect portfolios, schedules, or brokerage orders remain subject to the authenticated account's NexusTrade permissions and approval controls.
Quickstart
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);
Backtest operations may include warnings: string[] immediately after
submission and again in the terminal result. Treat them as material caveats;
they do not change a successful operation into a failure.
Collateral at risk
A terminal operation's result.statistics answers how much capital the run had
on the line, not only what it returned. Two fields carry it, typed as
BacktestCollateralStatistics:
| Field | Meaning |
|---|---|
peakReservedCollateral |
Largest collateral locked at any tick, in account currency |
medianReservedCollateral |
Median across the ticks that held at least one position |
Both are optional and may be null, and that absence is a real answer: a
backtest run before the engine reported collateral has no value, which is not
the same as a book that locked nothing. Display "not recorded" rather than $0
— a zero here reads as "this strategy risks nothing", the opposite of what an
unpopulated field means. Do not substitute the portfolio's value either: a book
risking a few thousand dollars would be reported as risking all of it.
Never reconstruct either number from cash - buyingPower. Buying power is
clamped at both ends and carries an open credit-spread premium term, so the
inversion breaks precisely on the heavily collateralised books this measures.
Authoring strategies
Every builder is generated from the same indicator specification the NexusTrade engine runs, so a book is valid by construction rather than by convention.
TypeScript cannot overload comparison operators, so indicators compose through
gt / gte / lt / lte / eq / neq and and / or:
import * as nt from "nexustrade";
const book = nt.portfolio(
"Momentum",
[
nt.strategy(
"Rotate into strength",
nt.always(),
nt.dynamicRebalance({
universe: nt.universe("SP500"),
pipeline: [
nt.filter(nt.gt(nt.Price(nt.CANDIDATE), nt.SMA(nt.CANDIDATE, 200))),
nt.selectTop(nt.RSI(nt.CANDIDATE, 14), 10),
],
weightIndicator: nt.RSI(nt.CANDIDATE, 14),
limit: 10,
deploymentPercent: nt.Value(80),
})
),
],
{ initialValue: 100_000 }
);
Congressional-disclosure signals are native indicators too. House and Senate are combined unless you explicitly choose a chamber; amount metrics default to the disclosed lower bound and confirmed option disclosures are excluded by default:
const pelosiPurchases = nt.PoliticalTrades(
nt.CANDIDATE,
"Nancy Pelosi",
"BuyAmount",
90,
"LowerBound",
"Equity",
);
Use "" as the filer to include all members. BuyCount counts canonical
purchase events, while BuyAmount measures disclosed magnitude. Select
"Option" only when you want option-disclosure activity associated with the
underlying ticker.
Order execution belongs to the strategy. Omit it for the backward-compatible Market default, use a fixed unit price for Buy/Sell, or set an option strategy's maximum net debit / minimum net credit:
nt.strategy("Buy SPY at my price", nt.always(), nt.buy(nt.stockAsset("SPY"), 10), {
orderExecution: nt.limitOrder({
price: nt.unitPriceLimit(500),
workingTime: nt.goodForDay(),
}),
});
nt.strategy("Sell the spread for $1.50 or better", nt.always(), optionAction, {
orderExecution: nt.limitOrder({
price: nt.minimumNetCredit(1.5),
workingTime: nt.goodForMinutes(30),
}),
});
An option net limit can also follow a position the strategy holds. Pass an indicator instead of a number and the engine evaluates it when the strategy fires. If it has no positive value at that moment, no order is placed:
nt.strategy("Sell the credit spread for the debit fill plus $0.50", whenDebitSpreadHeld, creditSpreadAction, {
orderExecution: nt.limitOrder({
price: nt.minimumNetCredit(
nt.Plus(nt.OptionSpreadEntryPrice("SPX", "call", "long", "vertical"), nt.Value(0.5)),
),
workingTime: nt.goodForDay(),
}),
});
currentLimit() creates a quote-relative Limit for dynamic rebalance strategies.
It keeps the no-worse-than-current-quote protection, but it is not a resting
price target. Live option strategies must choose an explicit Limit policy.
Option selection and execution limits
strategy(..., { pendingOrderPolicy: "CancelConflictingBuys" }) enables backtest-only
exit priority over conflicting pending buys. Omission preserves waiting. Paper
and live deployment reject this opt-in because broker cancellation acknowledgements
are not implemented. The setting also accepts explicit "Wait".
Builders describe drafts. Typechecking does not prove that the engine can execute every requested rule; the API validates complete portfolios and their conditions.
- Expiration selectors support a DTE window and
nearest,middle, orfurthest.middleselects the middle eligible expiration by index, not the expiration nearest a target such as 180 days. There is no monthly-only or quarterly-only expiration filter. rebalanceOptionacceptsrenewBeforeDays(integer 1–90) andrevisitDisclosedTargets. These maintain modeled political-disclosure option targets. The strategy must contain aNewPoliticalDisclosurecondition for instrumentOptionwith exactly one member ID. These fields do not implement a general close-and-replace roll for a trend strategy.- For backtests, the job's
feeConfig.OptionSlippageFractionsets quote-side fills:0uses the midpoint,1uses ask for buys and bid for sells, and the default is0.5. This job setting does not add a last-trade fallback or define live order execution. - Evaluation timing and order execution are separate.
OpenCloseschedules evaluations at session open and close; it does not itself carry a signal observed at close into an order at the next open. Explicit zero timing offsets and agt(MinutesAfterOpen(), Value(0))condition can restrict evaluations to the calendar session close.MinutesUntilClosecounts down to 16:00 Eastern, so it cannot identify an early session close.
When authoring an approximation, describe the actual expiration policy, signal timing, fills, and replacement behavior. Backtest results describe that implementation, not the exact method it approximates.
What you can build — 170+ generated builders
| Group | Examples |
|---|---|
| Price & volume | Price OpeningPrice HighOfDay MinuteBarHigh MinuteBarLow HeikinAshi VWAP Volume GapPercentage |
| Technicals | SMA EMA RSI BollingerBand AverageTrueRange CrossAbove |
| Position state | PositionValue PositionPercentChange PositionMaxDrawdown |
| Portfolio state | PortfolioValue BuyingPower MaxDrawdown InitialValue |
| Fundamentals | Fundamental Economic DaysUntilEarnings IsIndexMember IsIndustry |
| Options | OptionDaysToExpiration OptionCollateral OptionSpreadEntryPrice OptionUnrealizedPnL openOption closeOption |
| Actions | buy sell alert dynamicRebalance rebalanceOption |
| Selection | filter selectTop selectPercentile universe |
| Logic | always atLeast atMost exactly fewerThan multi and or sequence |
| Anchored levels | IndicatorAtEntry LastOrderPrice IndicatorAtMinutesAfterOpen IndicatorWindowAgo |
Every builder is fully typed — your editor completes the whole surface.
Jobs run on the engine — you poll
create* enqueues work and returns immediately. It does not resolve when
results exist. There are no webhooks today.
sequenceDiagram
participant You
participant SDK
participant Engine
You->>SDK: createBacktest(book)
SDK->>Engine: POST (enqueue)
Engine-->>SDK: id, status=queued
SDK-->>You: operation (returns immediately)
loop waitForBacktest — backoff 2s→15s
SDK->>Engine: GET /operations/{id}
Engine-->>SDK: status update
end
SDK-->>You: result (when completed)
Note over You,Engine: Poll timeout throws operation_timeout.<br/>The job keeps running — call wait again with the same id.
Every job kind reports the same envelope, so one poller serves all of them:
{
id: "op_...",
kind: "backtest", // backtest | optimization | walk_forward
status: "queued", // queued | running | completed | failed | cancelled
result: {...}, // present only once terminal
error: { code, message, retryable },
}
const finished = await client.waitForBacktest(operation.id as string);
| Option | Default | Meaning |
|---|---|---|
timeoutSeconds |
900 |
Give up waiting (the job keeps running) |
pollIntervalSeconds |
2 |
First interval; backs off 1.5× |
maxPollIntervalSeconds |
15 |
Interval ceiling |
throwOnFailure |
true |
Throw on failed/cancelled instead of returning |
A timeout throws operation_timeout and does not cancel the job — call the
waiter again with the same id rather than resubmitting.
Transient network and HTTP 408/429/5xx polling failures retry the same operation at most twice consecutively, within the wait deadline. Persistent failures keep their original error code and operation ID; no new job is submitted.
Durable lake exports use a 900-second server budget by default (registered API
keys may request up to 1800 seconds). waitForLakeQuery defaults to 1200 seconds
to include queue time. Compute token budgets remain 90 seconds by default and
180 seconds maximum. A client wait timeout does not extend the server budget.
Batches. createBacktests submits many in one request and returns one
operation each; waitForBacktests(operations) waits on all of them. Prefer it
over a loop: one request, one idempotency key, one rate-limit slot.
Optimization and walk-forward follow the identical shape:
const study = await client.createWalkForward(
nt.walkForward(book, {
globalStartDate: "2022-01-01",
globalEndDate: "2024-12-31",
foldCount: 4,
}),
{ idempotencyKey: "wf-v1" }
);
await client.waitForWalkForward(study.id as string);
Deploying a portfolio
Authoring and backtesting a book does not persist it. save writes it to your
account; deploy starts running it.
const book = nt.portfolio("Momentum", [
/* … */
]);
await book.save({ idempotencyKey: "momentum-v1" }); // persists; sets book.id
const deployment = await book.deploy(); // starts paper trading
await book.undeploy(); // stops it
save and deploy produce different ids, and the distinction matters.
save persists a draft and sets book.id to it. deploy mints the real
paper portfolio and returns its own portfolioId — deploying creates a
portfolio rather than converting the draft into one, so the two ids coexist.
Hold on to deployment.portfolioId for anything that reads live state;
book.id addresses the draft.
deployment.portfolioId; // the running portfolio
deployment.deploymentType; // paper, unless you deployed an existing live one
deployment.outcome; // created | reactivated
Handle methods accept an optional transport; omitted, they resolve one from
the environment. The same operations exist on the client — client.deploy(id),
client.undeploy(id) — when you have an id rather than a handle.
await client.listPortfolios({ includePaper: true, includePositions: true });
await client.getPortfolio(portfolioId);
Editing a saved portfolio
updatePortfolio applies deterministic edits with no LLM in the path. The
operations array is a typed union, so the compiler knows which fields each
edit needs.
import type { PortfolioEditOperation } from "nexustrade";
const operations: PortfolioEditOperation[] = [
{ type: "rename", name: "AAPL Income" },
{
type: "replaceStrategy",
targetStrategyId: strategyId, // or targetStrategyName
strategyObject: nt.strategy(
"Buy AAPL",
nt.always(),
nt.buy(nt.stockAsset("AAPL"), 25, "percent of portfolio"),
{
orderExecution: nt.limitOrder({
price: nt.unitPriceLimit(150),
workingTime: nt.goodForDay(),
}),
},
),
},
];
await client.updatePortfolio(portfolioId, operations, {
idempotencyKey: "aapl-limit-v1",
});
Prefer one updatePortfolio operation with a finished portfolio patch.
It can combine strategies, deployment (frequency and timing), name,
policy.stockEligibility, dividendPolicy, and alerts. Omitted fields retain
their saved values. Draft starting capital and capabilities can change only
before deployment. The existing strategy and rename operations remain supported.
Every authored operation for the target in one call commits together; validation
failure saves none of them. Reusing the same idempotency key replays the saved
result. Deploy, undeploy, delete and automated approval remain separate account
operations.
replaceStrategies replaces the whole set, so a strategy left out of the
array is deleted. Carry unchanged strategies through verbatim, including the
orderExecution each already has, or a working Limit silently reverts to
Market. removeStrategies takes strategy ids from a fetched portfolio; removal
by name is rejected.
Stock eligibility is part of what you author. Pass
policy: { stockEligibility } to portfolio(...), or call
setStockEligibility(...) on a handle, to set market-cap bounds, an industry
filter, missingMarketCapBehavior, or shareClassBehavior. Omitted fields take
the defaults. A GOOG/GOOGL pairs book needs ALL_CLASSES, because the default
keeps one share class per company:
const pair = portfolio("GOOG/GOOGL pair", strategies, {
policy: { stockEligibility: { shareClassBehavior: "ALL_CLASSES" } },
});
Automated trading is never authored. Only the owner enables it, in Portfolio
Settings, and a policy that names automatedApproval is refused before it is
sent. Fetched handles include a typed, read-only policy snapshot; saving or
deploying a copy of one carries its stock eligibility and nothing else.
listPortfolios filters with includePaper, includeLive, includeInactive,
includeChatPortfolios, search, limit, and page. includePositions
defaults off when search is set.
A portfolio you create here is always paper, and minting a live one still happens in the web app. Orders and brokerage status are reachable from here; see Live trading.
But deploy can start live trading. Given the id of a portfolio that is
already deployed, it reactivates that portfolio as whatever it already is — so
client.deploy(id) on a paused live portfolio resumes live trading against the connected
brokerage, and includeLive: true above will hand you such an id. Check deployment.deploymentType before
treating a deploy as simulated.
Live trading
Live trading needs a brokerage linked to your account. Linking is an OAuth redirect, so an API key cannot complete it — a human opens the URL.
await client.listBrokerages();
// [{ brokerage: "Alpaca", connected: false,
// connectUrl: "https://nexustrade.io/live-trading" }, ...]
await client.connectBrokerage("Alpaca"); // logs the URL, waits until connected
connectBrokerage waits by default only when stdout is a TTY. In CI, cron,
or run_compute it rejects with brokerage_not_connected immediately, with the
URL in the message, rather than stalling for five minutes in front of nobody.
Pass { wait: true } or { wait: false } to force either.
A live-only listing that comes back empty rejects with the same error rather than an empty array, since an empty array says nothing about why:
await client.listPortfolios({ includeLive: true, includePaper: false });
// NexusTradeApiError: brok