OpenedOrders (RECOMMENDED)¶
Every open order โ market positions and resting pendings โ in one call.
API Information:
- Low-level API:
MT4Account.OpenedOrders(ctx)(defined inmt4/MT4Account.go) - gRPC service:
mt4_term_api.AccountHelper - RPC:
OpenedOrders(OpenedOrdersRequest) โ OpenedOrdersReply - Payload:
OpenedOrdersDataโGetOrderInfos()โOpenedOrderInfo
๐ฌ Just the essentials¶
- What it is. A snapshot of all currently open orders with full detail (price, lots, SL/TP, profit, magic).
- Why you need it. The source of truth for "what do I have open right now?" โ sizing, reconciliation, management.
- When to use. Before/after trading, in dashboards, to reconcile after a timed-out send.
๐ฏ Purpose¶
- List open positions (BUY/SELL) and pendings (LIMIT/STOP)
- Read each order's lots, open price, SL/TP, floating profit
- Filter by symbol or magic for "my" orders
- Reconcile after a failed/timed-out
OrderSend
Method Signature¶
func (a *MT4Account) OpenedOrders(ctx context.Context) (*pb.OpenedOrdersData, error)
Request message:
message OpenedOrdersRequest {} // no arguments
Payload message:
message OpenedOrdersData { repeated OpenedOrderInfo order_infos = 1; }
message OpenedOrderInfo {
int32 ticket = ...;
string symbol = ...;
OpenedOrderType order_type = ...;
double lots = ...;
double open_price = ...;
double stop_loss = ...;
double take_profit = ...;
double profit = ...;
double swap = ...;
double commision = ...;
int32 magic_number = ...;
string comment = ...;
Timestamp open_time = ...;
Timestamp expiration_time = ...;
}
๐ฝ Input¶
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
yes | Cancellation / timeout. The call takes no other arguments. |
โฌ๏ธ Output โ *pb.OpenedOrdersData¶
GetOrderInfos() returns []*pb.OpenedOrderInfo:
| Field | Proto Type | Go getter | Description |
|---|---|---|---|
ticket |
int32 | GetTicket() int32 |
Order ticket |
symbol |
string | GetSymbol() string |
Instrument |
order_type |
OpenedOrderType | GetOrderType() |
Buy/sell, market or pending |
lots |
double | GetLots() float64 |
Volume |
open_price |
double | GetOpenPrice() float64 |
Entry / pending price |
stop_loss / take_profit |
double | GetStopLoss() / GetTakeProfit() |
Protective levels (0 = none) |
profit |
double | GetProfit() float64 |
Floating P/L (money) |
swap / commision |
double | GetSwap() / GetCommision() |
Costs |
magic_number |
int32 | GetMagicNumber() int32 |
EA / strategy tag |
comment |
string | GetComment() string |
Order comment |
open_time / expiration_time |
Timestamp | GetOpenTime() / GetExpirationTime() |
Open / pending expiry |
On failure the call returns a non-nil
error(text"API error (code=โฆ): โฆ") โ see the Return Codes Reference.
๐งฑ Related enums (from proto)¶
OpenedOrderType โ field order_type¶
| Constant | Value | Meaning |
|---|---|---|
OO_OP_BUY |
0 | Market buy (position) |
OO_OP_SELL |
1 | Market sell (position) |
OO_OP_BUYLIMIT |
2 | Buy limit (pending) |
OO_OP_SELLLIMIT |
3 | Sell limit (pending) |
OO_OP_BUYSTOP |
4 | Buy stop (pending) |
OO_OP_SELLSTOP |
5 | Sell stop (pending) |
OO_OP_BALANCE |
6 | Balance entry (non-trading) |
OO_OP_CREDIT |
7 | Credit entry (non-trading) |
OO_OP_BUY/OO_OP_SELLare open positions;*LIMIT/*STOPare pendings. Compare viao.GetOrderType() == pb.OpenedOrderType_OO_OP_BUY. See the ENUMs Reference.
๐งฉ Notes & Tips¶
- Positions vs pendings. Filter on
GetOrderType()โ the two market types are positions, the four*LIMIT/*STOPare pendings. - Floating profit is
GetProfit(); addGetSwap()+GetCommision()for the net. - Reconcile after errors. If an
OrderSendtimes out, call this to see whether it actually filled before retrying. - For just the tickets (lighter), use
OpenedOrdersTickets.
๐ Usage Examples¶
1) Print all open orders¶
func listOpen(ctx context.Context, acc *mt4.MT4Account) error {
d, err := acc.OpenedOrders(ctx)
if err != nil { return err }
for _, o := range d.GetOrderInfos() {
fmt.Printf("#%d %s %s %.2f @ %.5f P/L %+.2f\n",
o.GetTicket(), o.GetSymbol(), o.GetOrderType(),
o.GetLots(), o.GetOpenPrice(), o.GetProfit())
}
return nil
}
2) Split positions from pendings¶
import pb "github.com/MetaRPC/GoMT4/package"
func split(ctx context.Context, acc *mt4.MT4Account) (positions, pendings []*pb.OpenedOrderInfo, err error) {
d, err := acc.OpenedOrders(ctx)
if err != nil { return nil, nil, err }
for _, o := range d.GetOrderInfos() {
switch o.GetOrderType() {
case pb.OpenedOrderType_OO_OP_BUY, pb.OpenedOrderType_OO_OP_SELL:
positions = append(positions, o)
case pb.OpenedOrderType_OO_OP_BUYLIMIT, pb.OpenedOrderType_OO_OP_SELLLIMIT,
pb.OpenedOrderType_OO_OP_BUYSTOP, pb.OpenedOrderType_OO_OP_SELLSTOP:
pendings = append(pendings, o)
}
}
return positions, pendings, nil
}
3) Total floating P/L for "my" magic¶
func myFloatingPL(ctx context.Context, acc *mt4.MT4Account, magic int32) (float64, error) {
d, err := acc.OpenedOrders(ctx)
if err != nil { return 0, err }
var pl float64
for _, o := range d.GetOrderInfos() {
if o.GetMagicNumber() == magic {
pl += o.GetProfit() + o.GetSwap() + o.GetCommision()
}
}
return pl, nil
}
โ ๏ธ Pitfalls¶
- Snapshot, not a stream. Re-query for fresh values, or use
OnOpenedOrdersProfit. - Balance/credit rows.
OO_OP_BALANCE/OO_OP_CREDITaren't trades โ skip them when summing exposure. - Empty is normal. A flat account returns zero
OrderInfosโ guardlen(...).
๐งช Testing Suggestions¶
- Happy path (demo). Open a 0.01 order; it appears here with the right
GetOrderType()andGetLots(). - Round-trip. After
OrderCloseit no longer appears. - Magic filter. Tag an order with a magic and assert the filtered sum includes only it.