Alpaca API PATCH /v2/orders/{order_id}
Replaces an open order with new quantity, prices, time in force or trail. The reply is a new Order with a new id.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
Read on the OpenAPI document and the Placing Orders page. The document's required list for the body is empty. Not allowed while the status is accepted, pending_new, pending_cancel or pending_replace; notional orders other than IPO ones cannot be replaced, OTO orders cannot be replaced, and a bracket or OCO replacement updates limit_price and stop_price.
Parameters
| Name | In | Type | Description |
|---|---|---|---|
| Content-Typerequired | header | string | Send application/json: the document marks the body required and gives it as JSON. |
| order_idrequired | path | string | The id of the order to replace. |
| qty | body | string | The new quantity, whole shares only. Not with notional. |
| notional | body | string | The new dollar amount. Only for IPO indications of interest. |
| time_in_force | body | string | The new time in force. |
| limit_price | body | string | The new limit price. The document says it is required where the original type was limit or stop_limit. |
| stop_price | body | string | The new stop price. The document says it is required where the original type was limit or stop_limit. |
| trail | body | string | The new trail_price or trail_percent of a trailing stop. A dollar trail cannot become a percentage trail or the reverse. |
| client_order_id | body | string | Your own id for the replacement, at most 128 characters. |
Authentication headers
| Name | Type | Description |
|---|---|---|
| APCA-API-KEY-IDrequired | string | Your API key id. Placeholder only: never paste a real key into this site. |
| APCA-API-SECRET-KEYrequired | string | The secret key that goes with the key id. Placeholder only: never paste a real secret into this site. |
Request
Written from the method, path and parameters recorded on this page, against the demo host. Not executed against a live or demo account. Credentials are read from environment variables on your own machine and are never entered on this site. The request builder writes these same snippets, from the same record.
This call takes a request body. The record marks no body field as required, so the samples send an empty JSON object with a Content-Type of application/json (in Python, passing json= sets that header). Add the fields the call needs from the parameters above before the request can succeed.
Python
# Set ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY as environment variables first. Never paste real values into code.
# Replace each {placeholder}, "..." and 0 with a real value before running.
# Optional and not shown: body fields qty, notional, time_in_force, limit_price, stop_price, trail, client_order_id.
import os
import requests
BASE_URL = "https://paper-api.alpaca.markets" # the demo host
headers = {
"APCA-API-KEY-ID": os.environ["ALPACA_API_KEY_ID"],
"APCA-API-SECRET-KEY": os.environ["ALPACA_API_SECRET_KEY"],
"Content-Type": "application/json",
}
body = {}
resp = requests.patch(BASE_URL + "/v2/orders/{order_id}", headers=headers, json=body, timeout=10)
resp.raise_for_status()
print(resp.json())JavaScript
// Set ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY as environment variables first. Never paste real values into code.
// Replace each {placeholder}, "..." and 0 with a real value before running.
// Optional and not shown: body fields qty, notional, time_in_force, limit_price, stop_price, trail, client_order_id.
// Run as a module, for example a .mjs file, on a Node version that has fetch built in.
const BASE_URL = "https://paper-api.alpaca.markets"; // the demo host
const res = await fetch(BASE_URL + "/v2/orders/{order_id}", {
method: "PATCH",
headers: {
"APCA-API-KEY-ID": process.env.ALPACA_API_KEY_ID,
"APCA-API-SECRET-KEY": process.env.ALPACA_API_SECRET_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
console.log(await res.json());curl
# Set ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY as environment variables first. Never paste real values into code.
# Replace each {placeholder}, "..." and 0 with a real value before running.
# Optional and not shown: body fields qty, notional, time_in_force, limit_price, stop_price, trail, client_order_id.
curl -X PATCH "https://paper-api.alpaca.markets/v2/orders/{order_id}" \
-H "APCA-API-KEY-ID: $ALPACA_API_KEY_ID" \
-H "APCA-API-SECRET-KEY: $ALPACA_API_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{}'Response
A mock response. The values are invented, so check field names against the official reference.
{
"_mock": true,
"id": "00000000-0000-4000-8000-000000mock03",
"replaces": "00000000-0000-4000-8000-000000mock01",
"symbol": "MOCK",
"qty": "2",
"type": "limit",
"limit_price": "96.00",
"status": "pending_new"
}Limits that apply at Alpaca
| Limit | Value | Scope | Source |
|---|---|---|---|
| client_order_id length | 128 | POST /v2/orders and PATCH /v2/orders/{order_id} | docs.alpaca.marketschecked |
| Crypto order size | 200,000 | Crypto spot orders, buy or sell | docs.alpaca.marketschecked |
| Market data historical API, Basic plan | 200 | Market Data API historical calls, Basic (free) plan | docs.alpaca.marketschecked |
| Market data points per page | 10,000 | Market Data API historical bars, trades and quotes, counted across all symbols | docs.alpaca.marketschecked |
| Market data historical API, Algo Trader Plus | 10,000 | Market Data API historical calls, Algo Trader Plus plan (99 US dollars a month) | docs.alpaca.marketschecked |
| GTC order age | 90 | Orders with time in force gtc | docs.alpaca.marketschecked |
| Trading API request rate | HTTP 429 above the limit, with X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset headers on replies; no figure on the pages opened | Trading API, per account | docs.alpaca.marketschecked |
| Market data WebSocket authentication timeout | 10 | Market data WebSocket, from connecting to the auth message | docs.alpaca.marketschecked |
| Market data WebSocket connections | Depends on the subscription; 1 for many subscriptions and with none. A second connection is answered with error 406 connection limit exceeded | Market data WebSocket, per account | docs.alpaca.marketschecked |
| Market data WebSocket symbols, Basic plan | 30 | Market data WebSocket, US equities, Basic (free) plan; Algo Trader Plus is unlimited | docs.alpaca.marketschecked |
Alpaca API errors you may meet
- HTTP 422HTTP 422. Unprocessable.
- HTTP 400HTTP 400. One of the request's parameters is invalid.
- HTTP 401 (market data)HTTP 401. The Market Data API's shared reply where the authentication headers are missing or invalid.
- HTTP 403HTTP 403. Forbidden.
- HTTP 404HTTP 404. Not found: an asset by symbol or id, a watchlist by id, or on the market data host a feed that does not exist.
- 42210000The code in the body of a 422 reply.
All Alpaca API error codes, including 20 more not listed here.