Skip to content

MT4Sugar — 5. Order Entry

Open a position or place a pending by side — defaults (magic / slippage / comment) stamped for you.

Every helper returns *pb.OrderSendData → GetTicket() (int32), GetVolume(), GetPrice(), GetOpenTime(). sl / tp are absolute prices (nil to skip, or set later by pips with ModifySLTPByPips). All route through the configured defaults.


BuyMarket(ctx, symbol, lots, sl, tp)

func (s *MT4Sugar) BuyMarket(ctx context.Context, symbol string, lots float64, sl, tp *float64) (*pb.OrderSendData, error)

Open a market buy at the current ask.

Example — plain market buy:

r, err := sugar.BuyMarket(ctx, "EURUSD", 0.10, nil, nil)
if err != nil { return err }
fmt.Printf("opened #%d @ %.5f\n", r.GetTicket(), r.GetPrice())

Example — buy then attach a pip bracket:

r, _ := sugar.BuyMarket(ctx, "EURUSD", 0.10, nil, nil)
_ = sugar.ModifySLTPByPips(ctx, r.GetTicket(), ptr(20.0), ptr(40.0)) // 20/40 pips

SellMarket(ctx, symbol, lots, sl, tp)

func (s *MT4Sugar) SellMarket(ctx context.Context, symbol string, lots float64, sl, tp *float64) (*pb.OrderSendData, error)

Open a market sell at the current bid.

Example — sell with absolute SL/TP:

sl, tp := 1.1050, 1.0950
r, _ := sugar.SellMarket(ctx, "EURUSD", 0.10, &sl, &tp)
fmt.Println("short ticket:", r.GetTicket())

BuyLimit · SellLimit · BuyStop · SellStop

func (s *MT4Sugar) BuyLimit(ctx context.Context, symbol string, lots, price float64, sl, tp *float64) (*pb.OrderSendData, error)
func (s *MT4Sugar) SellLimit(ctx context.Context, symbol string, lots, price float64, sl, tp *float64) (*pb.OrderSendData, error)
func (s *MT4Sugar) BuyStop(ctx context.Context, symbol string, lots, price float64, sl, tp *float64) (*pb.OrderSendData, error)
func (s *MT4Sugar) SellStop(ctx context.Context, symbol string, lots, price float64, sl, tp *float64) (*pb.OrderSendData, error)

Place a pending at price. Limits fill when price returns to them (buy-limit below ask, sell-limit above bid); stops fill on breakout (buy-stop above ask, sell-stop below bid).

Example — buy the dip 15 pips below the ask:

ask := must(sugar.LastQuote(ctx, "EURUSD")).GetAsk()
dist, _ := sugar.PipsToPrice(ctx, "EURUSD", 15)
r, _ := sugar.BuyLimit(ctx, "EURUSD", 0.10, ask-dist, nil, nil)

Example — breakout buy-stop above the ask:

ask := must(sugar.LastQuote(ctx, "EURUSD")).GetAsk()
dist, _ := sugar.PipsToPrice(ctx, "EURUSD", 15)
r, _ := sugar.BuyStop(ctx, "EURUSD", 0.10, ask+dist, nil, nil)

⚠️ Not retried. Like the raw layer, trade calls aren't auto-retried — a timed-out send may have filled. Reconcile with Orders on error. See the low-level OrderSend.


See also: 4. Risk Sizing · 6. Order Management