Tradovate API

Flatten & Liquidate Positions via Tradovate API

There's a moment every automated trader knows: your strategy is holding a position and you need out of everything now. Here's how the Tradovate API's liquidatePosition call zeros out an open contract, from grabbing your account ID to flattening the whole account.

Geprüft vom PickMyTrade Trading Systems Team Zuletzt aktualisiert
· 6 min read
POST request to the Tradovate accessTokenRequest endpoint returning an access token in an API client

There's a moment every automated trader knows: your strategy is holding a position, the market's turning against you, and you need out of everything now. Hunting for the flatten button on a web page is too slow. That's exactly what the Tradovate API's liquidatePosition call is for. One request zeros out an open contract, no manual DOM clicks required. This guide walks the whole path, from grabbing your account ID to looping over every open position so the account ends up completely flat.

A quick word on what this endpoint actually does. It doesn't cancel a resting order or "sell some contracts." It looks up the net position you're holding on a contract and submits a market order in the opposite direction to bring that net to zero. Because it's a live order, it only fills while the contract's session is open.

Before You Start

  • API access and a live token. You need API Access enabled on the account and a valid access token. If you haven't set that up, start with getting Tradovate API access and a key.
  • The right environment. Demo and live are separate hosts: demo.tradovateapi.com/v1 for simulation, live.tradovateapi.com/v1 for real money. Point a demo token at the live host (or vice versa) and you'll get "Access is denied."
  • Order permission on that account. The token has to belong to a user who can place orders on the account you're flattening.
  • Prop-account rules. Some prop firms disable direct API automation on their evaluation accounts. Check your firm's current rules before you build against a funded account.

Flatten & Liquidate Positions via Tradovate API: Step-by-Step

1

Authenticate and Hold Onto Your Token

Every call needs a bearer token. Send your credentials to /auth/accessTokenRequest and pull the accessToken out of the response. Tokens don't live forever, so if your process runs for a while, call /auth/renewAccessToken before the current one lapses rather than waiting for a 401. On every request after this, attach the header Authorization: Bearer YOUR_ACCESS_TOKEN.

2

Get Your Numeric Account ID

Liquidation is keyed to a numeric account ID, not the account nickname you see in the app. Send a GET to /account/list and you'll get back an array of the accounts your user can see. Grab the id field (a number like 2100) for the account you want to flatten. If you have several accounts, match on the name field, then use that row's id.

3

Find Your Open Positions and Their Contract IDs

You can't liquidate a contract you can't name. Call /position/list and look at two fields on each row: contractId and netPos. netPos is the signed number of contracts you're holding. Positive means long, negative means short, and zero means you're already flat on that contract. Any row where netPos isn't zero is an open position you may want to close. Note the contractId. If you want a human-readable symbol to log, pass that ID to /contract/item?id={contractId}.

4

Fire the Liquidation

Now the actual call. POST to /order/liquidatePosition with the account ID and contract ID you just collected, sending accountId, contractId, and an admin boolean in the body. A few things that trip people up here.

5

Flatten Everything, Then Confirm

There's no single "close all" button in the REST API. To flatten the whole account, you loop. Pull /position/list, keep the rows where netPos isn't zero, and call /order/liquidatePosition once per contractId. Two contracts open? Two calls. Then verify. Re-pull /position/list and every netPos should read 0. It's also worth hitting /order/list to make sure any bracket or OCO orders tied to those positions actually cleared, so a leftover stop doesn't reopen you later.

Tradovate account list API response showing the numeric account id fieldTradovate position list API response with contractId and netPos fields for an open positionPOST body for the Tradovate liquidatePosition endpoint with accountId, contractId and admin fieldsLoop closing every open Tradovate position with position list confirming netPos is now zero
Field What it is
accountIdRequired. The numeric id from Step 2.
contractIdRequired, and it has to be greater than 0. Passing 0 throws "contractId must be > 0". Use the real ID from /position/list.
adminA boolean the body expects. Regular accounts pass false. Leaving it out is the classic cause of a 400.
customTag50Optional. A short string tag you can attach for your own tracking.

The server works out your net position itself and submits the market order to close it. If the position already went flat between your position check and this call, you get a harmless no-op instead of an accidental opposite trade, so you don't have to race it perfectly.

When It Doesn't Work

Most failures come down to three things. A 401 "Access is denied" almost always means the wrong environment host, the wrong account ID, or a user without order permission on that account. contractId must be > 0 means you sent a placeholder instead of a real ID. And a plain 401 with an expired token just needs a renew. If the liquidation order itself gets rejected, the reason is usually the same one that blocks any order, so it's worth checking the general access-denied on place order guidance and confirming the market is open.

Automate This with PickMyTrade

Don't want to babysit tokens, position loops, and liquidation logic yourself? PickMyTrade connects your TradingView alerts straight to Tradovate, so entries, exits, and flatten commands fire automatically without you writing a line of API code.

Skip the Liquidation Loops

Let PickMyTrade connect your TradingView alerts straight to Tradovate, entries, exits, and flatten commands fire automatically.

Start Your Free 5-Day Trial

Frequently Asked Questions

Usually the wrong environment host, the wrong account ID, or a user without order permission on that account. Demo tokens only work against demo.tradovateapi.com/v1, and live tokens only against live.tradovateapi.com/v1.

You sent a placeholder instead of a real contract ID. Pull the actual contractId from /position/list before calling liquidatePosition.

No. It only looks up your net position on a contract and submits an opposite-side market order to zero it. After liquidating, check /order/list separately to confirm any bracket or OCO legs tied to that position actually cleared.

This guide is for educational and informational purposes only and is not financial, investment, or trading advice. Trading futures and other leveraged products carries a substantial risk of loss and is not suitable for every investor. PickMyTrade is an independent third-party automation platform and is not affiliated with, endorsed by, or sponsored by Tradovate, Inc. "Tradovate" and all related names, logos, and trademarks are the property of their respective owners. Platform features and steps change over time, so always confirm the current process in the official Tradovate platform and documentation before acting.