Skip to content

OrdersTotal

How many orders are currently open โ€” a single integer count.

API Information:

  • Low-level API: MT4Account.OrdersTotal(ctx) (defined in mt4/MT4Account.go)
  • Built on: OpenedOrders โ€” it is a convenience, not a separate RPC: it returns len(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 0 and a non-nil error (text "API error (code=โ€ฆ): โ€ฆ") โ€” see the Return Codes Reference.


๐Ÿงฉ Notes & Tips

  • Positions + pendings are both counted โ€” it doesn't distinguish them (use OpenedOrders for 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 OpenedOrders when 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 returns 1.
  • Cap. Open N orders and assert underCap flips at the threshold.
  • Consistency. It equals len(OpenedOrders().GetOrderInfos()).

๐Ÿ“š See also