MT4Sugar — 6. Order Management¶
Close, partial-close, net, flatten, cancel pendings, and move SL/TP — by price or by pips.
Closing helpers use the configured default slippage. The pip-based modifier handles buy/sell direction for you.
Close(ctx, ticket, lots)¶
func (s *MT4Sugar) Close(ctx context.Context, ticket int32, lots *float64) (*pb.OrderCloseDeleteData, error)
Close a market order fully (lots = nil) or partially (lots = &volume), or delete a pending.
Example — full close / delete:
sugar.Close(ctx, ticket, nil)
Example — partial close (0.05 lot):
lots := 0.05
sugar.Close(ctx, ticket, &lots)
ClosePartial(ctx, ticket, lots)¶
func (s *MT4Sugar) ClosePartial(ctx context.Context, ticket int32, lots float64) (*pb.OrderCloseDeleteData, error)
Convenience for a partial close by volume (Close with &lots).
Example — scale out half a position:
sugar.ClosePartial(ctx, ticket, 0.05)
CloseBy(ctx, ticket, oppositeTicket)¶
func (s *MT4Sugar) CloseBy(ctx context.Context, ticket, oppositeTicket int32) (*pb.OrderCloseByData, error)
Net two opposite orders on the same symbol against each other (saves one spread).
Example — net a hedged pair:
d, _ := sugar.CloseBy(ctx, buyTicket, sellTicket)
fmt.Printf("netted @ %.5f profit %+.2f\n", d.GetClosePrice(), d.GetProfit())
CloseAll(ctx, symbol) · CancelPendings(ctx, symbol)¶
func (s *MT4Sugar) CloseAll(ctx context.Context, symbol string) (int, error)
func (s *MT4Sugar) CancelPendings(ctx context.Context, symbol string) (int, error)
CloseAll closes every open market position; CancelPendings deletes every pending. Pass "" for all symbols, or a symbol to scope it. Best-effort (one failure doesn't abort the rest); both return how many were closed.
Example — flatten everything:
n, _ := sugar.CloseAll(ctx, "")
fmt.Printf("closed %d positions\n", n)
Example — cancel EURUSD pendings only:
n, _ := sugar.CancelPendings(ctx, "EURUSD")
ModifySLTPByPrice(ctx, ticket, sl, tp)¶
func (s *MT4Sugar) ModifySLTPByPrice(ctx context.Context, ticket int32, sl, tp *float64) error
Set SL/TP to absolute prices (nil leaves a side unchanged).
Example — set an absolute stop, leave the target:
sl := 1.0850
err := sugar.ModifySLTPByPrice(ctx, ticket, &sl, nil)
ModifySLTPByPips(ctx, ticket, slPips, tpPips)¶
func (s *MT4Sugar) ModifySLTPByPips(ctx context.Context, ticket int32, slPips, tpPips *float64) error
Set SL/TP as pip distances from the order's open price, handling direction for you (buy → stop below / target above; sell → mirrored). It looks the order up to read its side and open price. nil leaves a side unchanged.
Example — 20-pip stop, 40-pip target:
err := sugar.ModifySLTPByPips(ctx, ticket, ptr(20.0), ptr(40.0))
Example — move only the stop:
err := sugar.ModifySLTPByPips(ctx, ticket, ptr(15.0), nil)
See also: 5. Order Entry · 7. Trailing & Breakeven