Skip to content

MT4Service — 4. Trading Operations

Send / modify / close — passed straight through to the terminal. Side-based one-liners with defaults (BuyMarket, BuyLimit, …) live one layer up in MT4Sugar.

⚠️ Trade calls are not retried — a timed-out send/close may already have executed. On error, reconcile with OpenedOrders. See Return Codes.


OrderSend(ctx, symbol, operationType, volume, …)

func (s *MT4Service) OrderSend(
    ctx context.Context,
    symbol string,
    operationType pb.OrderSendOperationType,
    volume float64,
    price *float64,
    slippage *int32,
    stoploss *float64,
    takeprofit *float64,
    comment *string,
    magicNumber *int32,
    expiration *timestamppb.Timestamp,
) (*pb.OrderSendData, error)

Open a market or pending order. The optional fields are pointers — pass nil to omit; price is required for pendings only.

Arg — operationType: pb.OrderSendOperationType (OC_OP_BUY … OC_OP_SELLSTOP). Returns: *pb.OrderSendData → GetTicket(), GetPrice(), GetVolume(), GetOpenTime().

import pb "github.com/MetaRPC/GoMT4/package"

r, err := svc.OrderSend(ctx, "EURUSD",
    pb.OrderSendOperationType_OC_OP_BUY, 0.10,
    nil,          // price (market)
    nil,          // slippage
    ptr(1.0800),  // stoploss
    ptr(1.0900),  // takeprofit
    nil, nil, nil)
if err != nil { return err }
fmt.Println("opened", r.GetTicket(), "@", r.GetPrice())

OrderModify(ctx, ticket, price, stoploss, takeprofit, expiration)

func (s *MT4Service) OrderModify(ctx context.Context, ticket int32, price, stoploss, takeprofit *float64, expiration *timestamppb.Timestamp) (bool, error)

Change an order's price / SL / TP / expiry. nil leaves a field unchanged. Returns whether the terminal accepted the change.

ok, _ := svc.OrderModify(ctx, ticket, nil, ptr(1.0850), nil, nil) // move SL only

OrderClose(ctx, ticket, lots, price, slippage)

func (s *MT4Service) OrderClose(ctx context.Context, ticket int32, lots, price *float64, slippage *int32) (*pb.OrderCloseDeleteData, error)

Close a market order (lots=nil full, &x partial) or delete a pending. price=nil uses the market.

Returns: *pb.OrderCloseDeleteData → GetMode() (market closed / pending deleted).

svc.OrderClose(ctx, ticket, nil, nil, nil)        // full close
svc.OrderClose(ctx, ticket, ptr(0.05), nil, nil)  // partial

OrderCloseBy(ctx, ticket, oppositeTicket)

func (s *MT4Service) OrderCloseBy(ctx context.Context, ticket, oppositeTicket int32) (*pb.OrderCloseByData, error)

Net two opposite orders on the same symbol against each other (saves a spread). Returns GetClosePrice(), GetProfit(), GetCloseTime().

d, _ := svc.OrderCloseBy(ctx, buyTicket, sellTicket)
fmt.Printf("netted, profit %+.2f\n", d.GetProfit())

ptr(x) is just func ptr[T any](v T) *T { return &v } — a helper for the optional pointer args.

See also: MT4Service — Overview · 5. Streaming Methods · MT4Sugar overview