GitHubBlog

Search Documentation

Search for a page in the docs

Orders & Execution

All order execution follows the Trading-as-Git workflow: stage → commit → push. This page covers the order types, parameters, and reading tools available.

Order Types

TypeRequired ParametersDescription
MKTtotalQuantity or cashQtyMarket order — execute immediately at best available price
LMTtotalQuantity + lmtPriceLimit order — execute at specified price or better
STPtotalQuantity + auxPriceStop order — becomes market order when stop price is hit
STP LMTtotalQuantity + auxPrice + lmtPriceStop-limit — becomes limit order when stop price is hit
TRAILtotalQuantity + auxPrice or trailingPercentTrailing stop — stop price trails the market by a fixed offset
TRAIL LIMITtotalQuantity + auxPrice + lmtPriceTrailing stop-limit — trailing stop that becomes a limit order
MOCtotalQuantityMarket-on-close — execute at the closing price

Quantity options:

  • totalQuantity — Number of shares/contracts
  • cashQty — Dollar amount (mutually exclusive with totalQuantity, broker support varies)

Time in Force

TIFDescription
DAYExpires at end of trading day (default)
GTCGood till cancelled
IOCImmediate or cancel — fill what you can, cancel the rest
FOKFill or kill — fill entirely or cancel
OPGOpening — execute at the market open
GTDGood till date — requires goodTillDate parameter

Take Profit & Stop Loss (TPSL)

Attach automatic exit orders when placing an entry order:

Buy 100 shares of AAPL at market with a take profit at $200
and a stop loss at $175.

Alice stages this as a single placeOrder with TPSL parameters:

{
  "action": "BUY",
  "orderType": "MKT",
  "totalQuantity": 100,
  "takeProfit": { "price": "200" },
  "stopLoss": { "price": "175" }
}

The broker creates the bracket automatically — the take profit and stop loss are linked to the parent order with One-Cancels-All (OCA) logic.

For a stop-limit stop loss, add limitPrice:

{
  "stopLoss": { "price": "175", "limitPrice": "174.50" }
}

Full Execution Flow

1. placeOrder          → Stage the order
2. tradingCommit       → Bundle with a message, get commit hash
3. tradingPush         → Request execution (requires your approval)
4. [User approves]     → Guards run, order sent to broker
5. tradingSync         → Check fill status from broker

Staging

The placeOrder tool stages an operation without executing it. Multiple orders can be staged before committing:

Stage: BUY 10 AAPL @ MKT
Stage: SELL 5 NVDA @ LMT 135.00

Committing

tradingCommit bundles all staged operations with a descriptive message:

Commit a3f8c1d2: "Rotate portfolio — reduce NVDA, add AAPL"
  2 operations staged

Pushing

tradingPush signals that the commit is ready for execution. The push requires manual approval in the Web UI — Alice cannot bypass this step.

When approved:

  1. The guard pipeline evaluates each operation
  2. Approved operations are dispatched to the broker
  3. Results are recorded (submitted/filled/rejected)
  4. A snapshot captures the account state

Syncing

After pushing, orders may not fill immediately. tradingSync checks the broker for updates:

tradingSync(delayMs: 3000)  // Wait 3s for exchange to settle, then check

Reading Tools

ToolDescription
getAccountAccount info: net liquidation, cash, buying power, P&L
getPortfolioCurrent positions with unrealized P&L and portfolio weight
getOrdersPending orders (or specific orders by ID)
getQuoteLatest price for a contract
getMarketClockMarket open/close status and schedule
tradingLogCommit history (filterable by symbol)
tradingShowDetails of a specific commit by hash
tradingStatusCurrent staging area contents
simulatePriceChangeDry-run portfolio impact of price movements

Multi-Account Queries

Most reading tools accept an optional source parameter:

  • Omit — Query all enabled accounts
  • Account ID — Target a specific account (e.g. "alpaca-paper")
  • Broker type — Target all accounts of a type (e.g. "ccxt")

Price Simulation

The simulatePriceChange tool lets you model portfolio impact without placing orders:

What would happen if AAPL dropped 10% and NVDA went to $150?

Alice calls simulatePriceChange with:

{
  "priceChanges": [
    { "symbol": "AAPL", "change": "-10%" },
    { "symbol": "NVDA", "change": "@150" }
  ]
}

Returns current vs simulated equity, P&L changes per position, and worst-case analysis.

Financial Precision

All monetary calculations use decimal.js for arbitrary-precision decimal arithmetic. This prevents floating-point errors that could affect order quantities and P&L calculations.