OrderDelete¶
Delete a pending order by ticket.
API Information:
- Low-level API:
MT4Account.OrderDelete(ctx, ticket)(defined inmt4/MT4Account.go) - gRPC service:
mt4_term_api.TradingHelper - RPC:
OrderCloseDelete(OrderCloseDeleteRequest) โ OrderCloseDeleteReply - Payload:
OrderCloseDeleteData
๐ฌ Just the essentials¶
- What it is. A convenience for cancelling a resting pending order (an
OrderClosewith no lots/price). - Why you need it. Cancel a limit/stop you no longer want.
- When to use. Removing pendings. For market orders use
OrderClose.
๐ฏ Purpose¶
- Cancel a resting pending order
- Confirm the deletion via the returned mode
- Clean up unfilled pendings (e.g. after a timeout)
Method Signature¶
func (a *MT4Account) OrderDelete(ctx context.Context, ticket int32) (*pb.OrderCloseDeleteData, error)
Request message:
message OrderCloseDeleteRequest {
int32 order_ticket = 1; // the pending to delete (no lots/price)
}
Payload message: OrderCloseDeleteData โ same as OrderClose.
๐ฝ Input¶
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
yes | Cancellation / timeout. |
ticket |
int32 |
yes | Pending order to delete. |
โฌ๏ธ Output โ *pb.OrderCloseDeleteData¶
| Field | Proto Type | Go getter | Description |
|---|---|---|---|
mode |
OrderCloseDeleteMode | GetMode() |
Should be OCD_PENDING_ORDER for a pending |
history_order_comment |
string | GetHistoryOrderComment() string |
Broker's comment |
On failure the call returns a non-nil
error(text"API error (code=โฆ): โฆ") โ see the Return Codes Reference. Not retried.
๐งฑ Related enums (from proto)¶
OrderCloseDeleteMode โ field mode¶
| Constant | Value | Meaning |
|---|---|---|
OCD_MARKET_ORDER |
0 | A market order was closed |
OCD_PENDING_ORDER |
1 | A pending order was deleted |
See the ENUMs Reference.
๐งฉ Notes & Tips¶
- Pendings only. Calling it on a filled market order is not its purpose โ use
OrderClose. - Same RPC as
OrderClose.OrderDeletejust omits lots/price; the server treats the ticket as a pending. - Verify the mode is
OCD_PENDING_ORDERto be sure you cancelled a pending.
๐ Usage Examples¶
1) Delete one pending¶
import pb "github.com/MetaRPC/GoMT4/package"
func deletePending(ctx context.Context, acc *mt4.MT4Account, ticket int32) error {
d, err := acc.OrderDelete(ctx, ticket)
if err != nil { return err }
if d.GetMode() == pb.OrderCloseDeleteMode_OCD_PENDING_ORDER {
fmt.Println("pending deleted")
}
return nil
}
2) Cancel all pendings on a symbol¶
func cancelPendings(ctx context.Context, acc *mt4.MT4Account, symbol string) (int, error) {
d, err := acc.OpenedOrders(ctx)
if err != nil { return 0, err }
n := 0
for _, o := range d.GetOrderInfos() {
switch o.GetOrderType() {
case pb.OpenedOrderType_OO_OP_BUYLIMIT, pb.OpenedOrderType_OO_OP_SELLLIMIT,
pb.OpenedOrderType_OO_OP_BUYSTOP, pb.OpenedOrderType_OO_OP_SELLSTOP:
if symbol == "" || o.GetSymbol() == symbol {
if _, err := acc.OrderDelete(ctx, o.GetTicket()); err == nil { n++ }
}
}
}
return n, nil
}
3) Delete after a fill timeout¶
func cancelIfUnfilled(ctx context.Context, acc *mt4.MT4Account, ticket int32) error {
_, err := acc.OrderDelete(ctx, ticket)
return err
}
โ ๏ธ Pitfalls¶
- Filled already? If the pending filled before you delete it, expect an error โ reconcile with
OpenedOrders. - Not retried. Handle the error path.
๐งช Testing Suggestions¶
- Happy path (demo). Place a far pending, delete it, assert
OCD_PENDING_ORDERand that it leavesOpenedOrders. - Already filled. Deleting a filled ticket errors.
- Bulk.
cancelPendingsremoves only pendings, leaving positions untouched.
๐ See also¶
- OrderClose ยท OrderSend ยท OpenedOrders
- Back to: 4. Trading Operations โ Overview ยท MT4Account โ Overview