OrdersTotal¶
How many orders are currently open โ a single integer count.
API Information:
- Low-level API:
MT4Account.OrdersTotal(ctx)(defined inmt4/MT4Account.go) - Built on:
OpenedOrdersโ it is a convenience, not a separate RPC: it returnslen(OpenedOrders().GetOrderInfos()). - Returns:
int32
๐ฌ Just the essentials¶
- What it is. MQL
OrdersTotal()โ the count of currently open orders (positions + pendings). - Why you need it. A quick "do I have anything open?" check without pulling the list.
- When to use. Gates and caps: skip work when flat, refuse new entries past a limit.
๐ฏ Purpose¶
- Count open orders in one tiny call
- Gate logic on "flat vs has-positions"
- Enforce a max-open-orders cap
Method Signature¶
func (a *MT4Account) OrdersTotal(ctx context.Context) (int32, error)
It takes no request of its own โ it counts the rows that OpenedOrders returns.
Returns: int32 โ the number of open orders (and an error).
๐ฝ Input¶
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
yes | Cancellation / timeout. The call takes no other arguments. |
โฌ๏ธ Output¶
| Return | Type | Description |
|---|---|---|
| count | int32 |
Number of currently open orders (positions + pendings) |
On failure the call returns
0and a non-nilerror(text"API error (code=โฆ): โฆ") โ see the Return Codes Reference.
๐งฉ Notes & Tips¶
- Positions + pendings are both counted โ it doesn't distinguish them (use
OpenedOrdersfor that). - Cheaper than the list when you only need the number.
- 0 means flat โ a clean signal to start fresh.
๐ Usage Examples¶
1) Skip work when flat¶
func hasOpen(ctx context.Context, acc *mt4.MT4Account) (bool, error) {
n, err := acc.OrdersTotal(ctx)
if err != nil { return false, err }
return n > 0, nil
}
2) Enforce a max-open cap before entering¶
func underCap(ctx context.Context, acc *mt4.MT4Account, max int32) (bool, error) {
n, err := acc.OrdersTotal(ctx)
if err != nil { return false, err }
if n >= max {
fmt.Printf("at cap: %d/%d open โ not entering\n", n, max)
return false, nil
}
return true, nil
}
3) Wait until flat¶
func waitFlat(ctx context.Context, acc *mt4.MT4Account, timeout time.Duration) (bool, error) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
n, err := acc.OrdersTotal(ctx)
if err != nil { return false, err }
if n == 0 { return true, nil }
time.Sleep(time.Second)
}
return false, nil
}
โ ๏ธ Pitfalls¶
- Count only. It won't tell you which orders โ pair with
OpenedOrderswhen you need detail. - Snapshot. The number can change immediately after the read.
๐งช Testing Suggestions¶
- Happy path (demo). On a flat account it returns
0; after opening one order it returns1. - Cap. Open N orders and assert
underCapflips at the threshold. - Consistency. It equals
len(OpenedOrders().GetOrderInfos()).