π PlacePendingOrder (GoMT4)¶
Goal: open a pending order (Buy Limit, Sell Limit, Buy Stop, Sell Stop) with proper rounding and expiration.
Uses real code from this repo:
- Account:
examples/mt4/MT4Account.go(OrderSend)- Example:
examples/mt4/MT4_service.go(ShowOrderSendExample)
β 1) Preconditions¶
- MT4 terminal is running & connected to broker.
config.jsonfilled with valid login/server/symbol.- Symbol visible in MT4 Market Watch.
π 2) Read symbol parameters¶
info, err := account.SymbolParams(ctx, symbol)
if err != nil { return err }
digits := int(info.GetDigits())
volStep := info.GetVolumeStep()
volMin := info.GetVolumeMin()
volMax := info.GetVolumeMax()
pointSize := math.Pow10(-digits)
βοΈ 3) Align helpers (same as market order)¶
volume := alignVolume(rawVolume, volStep, volMin, volMax)
price := roundPrice(desiredPrice, digits)
π 4) Build inputs (example: Buy Limit)¶
side := pb.OrderSendOperationType_OC_OP_BUYLIMIT
volume := alignVolume(0.10, volStep, volMin, volMax)
price := roundPrice(1.09500, digits) // target entry price
slippage := int32(5) // still required but ignored for pending
// Optional SL/TP
offset := 20 * pointSize
stop := roundPrice(price-offset, digits)
take := roundPrice(price+2*offset, digits)
// Expiration (good for 1 day)
expiry := timestamppb.New(time.Now().Add(24 * time.Hour))
comment := "Go pending order"
magicNumber := int32(123456)
resp, err := account.OrderSend(
ctx,
symbol,
side,
volume,
&price, // required for pending
&slippage,
&stop, &take, // can be nil
&comment,
&magicNumber,
expiry, // β¬
οΈ required for pending expiration
)
π 5) Result¶
fmt.Printf("β
Pending order placed! Ticket=%d Type=%s Price=%.5f Expires=%s\n",
resp.GetTicket(), resp.GetType().String(), resp.GetPrice(), resp.GetExpiration().AsTime())
β οΈ Common pitfalls¶
- No expiration β broker may reject if you omit expiry for pending.
- Invalid price β must be correctly rounded and on the correct side (e.g., Buy Limit < current Ask).
- Suffix mismatch β always check actual symbol name in MT4.
π Variations¶
OC_OP_SELLLIMIT,OC_OP_BUYSTOP,OC_OP_SELLSTOPβ changeside.expiry=nilβ pending order is goodβtillβcancelled (if broker allows).- Place multiple pendings in loop (just vary
priceandcomment).