mt5.orders_total() in MetaTrader5 Python: count pending orders
Returns how many pending orders, such as limits and stops, are open on the account right now. Returns an integer.
Signature
mt5.orders_total() What it does
orders_total() counts pending orders only. Open positions are counted by positions_total().
What it returns
- An integer: the number of pending orders.
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
print(mt5.orders_total(), "pending orders")
print(mt5.positions_total(), "open positions")
mt5.shutdown() Common mistakes
- An order that has filled is no longer pending. It moves to history and its position shows up in
positions_get(). - A 0 can also mean the terminal has not finished loading after a restart. Check
account_info()first.
The same with Tickwire on Linux
The package needs a Windows terminal on the same machine. Tickwire runs the terminal for you and gives you a REST API, so the code below runs on Linux, macOS or a server.
Tickwire's orders route returns the pending orders as a JSON array. Its length is the count.
import os, requests
API = "https://api.tickwire.io"
h = {"auth-token": os.environ["TW_KEY"]} # your Tickwire key
base = f"{API}/users/current/accounts/{os.environ['TW_ACCOUNT']}"
orders = requests.get(f"{base}/orders", headers=h).json()
print(len(orders), "pending orders") Related functions
- mt5.orders_get() in MetaTrader5 Python: read pending orders
- mt5.positions_total() in MetaTrader5 Python: count open positions
- mt5.history_orders_total() in MetaTrader5 Python: count orders in history
Related error codes
MT5 from Python, on any machine
One REST API for MT4 and MT5, with idempotent orders and an audit log. Read the MT5 Python API page or the quickstart.