MT4Sugar — 1. Connection & Defaults¶
Connect, disconnect, liveness, and the magic / slippage / comment applied to every order-entry helper.
NewMT4Sugar(svc) starts with magic = 0, slippage = 20, empty comment. Chain SetDefaults(...) to change them.
sugar := mt4.NewMT4Sugar(mt4.NewMT4Service(acc)).SetDefaults(777, 20, "my-bot")
SetDefaults(magic, slippage, comment)¶
func (s *MT4Sugar) SetDefaults(magic, slippage int32, comment string) *MT4Sugar
Builder — sets the defaults the Order Entry helpers stamp onto each order. slippage is in points. Returns the receiver, so chain it after NewMT4Sugar.
Example — configure defaults inline:
sugar := mt4.NewMT4Sugar(svc).SetDefaults(1001, 20, "scalper")
Example — re-stamp mid-session:
sugar.SetDefaults(2002, 30, "news-bot") // later orders use the new magic/slippage/comment
magic = 0means "don't stamp a magic"; an empty comment means "don't stamp a comment".
ConnectByServerName(ctx, server, defaultSymbol, autoSelect, timeoutSeconds)¶
func (s *MT4Sugar) ConnectByServerName(ctx context.Context, server, defaultSymbol string, autoSelect bool, timeoutSeconds int) error
Resolve the broker by server name and log in. defaultSymbol is selected on connect; autoSelect adds it to Market Watch; timeoutSeconds bounds the connect. Delegates to MT4Account.ConnectByServerName.
Example — connect and defer teardown:
if err := sugar.ConnectByServerName(ctx, "MetaQuotes-Demo", "EURUSD", true, 30); err != nil {
log.Fatal(err)
}
defer sugar.Disconnect(true)
Disconnect(delete)¶
func (s *MT4Sugar) Disconnect(delete bool) error
Close the session. delete = true also removes the terminal instance server-side; false leaves it for a quick reconnect.
Example — close for good:
sugar.Disconnect(true)
Example — keep the instance for a fast reconnect:
sugar.Disconnect(false)
Ping(ctx)¶
func (s *MT4Sugar) Ping(ctx context.Context) bool
Liveness probe — true if an AccountSummary round-trip succeeds. Never errors (any failure → false); it is a real counted read, so don't hammer it.
Example — gate work on a live link:
if !sugar.Ping(ctx) {
log.Println("terminal not responding")
return
}
See also: MT4Sugar — Overview · 2. Prices & Quotes