MT4Sugar — 8. Watchers & Waiters¶
Live streams (channels) and blocking polls — react to ticks, profit and ticket changes, or wait for a price/fill/close.
The watchers return the stream pair (<-chan *pb.…Data, <-chan error) — read frames, cancel the context to stop. The waiters poll every 500 ms until a condition holds or a timeout elapses.
SubscribeQuotes(ctx, symbols)¶
func (s *MT4Sugar) SubscribeQuotes(ctx context.Context, symbols []string) (<-chan *pb.OnSymbolTickData, <-chan error)
Live ticks for the given symbols. Cancel the context (or let it time out) to unsubscribe.
Example — stream ticks for 30s:
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
data, errs := sugar.SubscribeQuotes(ctx, []string{"EURUSD"})
for {
select {
case d := <-data:
t := d.GetSymbolTick(); fmt.Println(t.GetSymbol(), t.GetBid(), t.GetAsk())
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
WatchProfit(ctx, intervalMs) · WatchTickets(ctx, intervalMs)¶
func (s *MT4Sugar) WatchProfit(ctx context.Context, intervalMs int32) (<-chan *pb.OnOpenedOrdersProfitData, <-chan error)
func (s *MT4Sugar) WatchTickets(ctx context.Context, intervalMs int32) (<-chan *pb.OnOpenedOrdersTicketsData, <-chan error)
Periodic pushes: per-order floating profit, and the open-ticket list, every intervalMs.
Example — live equity heartbeat:
data, errs := sugar.WatchProfit(ctx, 1000)
for {
select {
case d := <-data:
fmt.Printf("equity %.2f\n", d.GetAccountInfo().GetEquity())
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
WaitPrice(ctx, symbol, target, above, timeout)¶
func (s *MT4Sugar) WaitPrice(ctx context.Context, symbol string, target float64, above bool, timeout time.Duration) (*pb.QuoteData, error)
Block until the price reaches target — above = true waits for bid >= target (price rose to it), above = false for ask <= target (fell to it). Returns the triggering quote, or an error on timeout.
Example — wait for a rise to 1.0950:
q, err := sugar.WaitPrice(ctx, "EURUSD", 1.0950, true, time.Minute)
if err != nil { return err } // not reached in time
fmt.Println("reached, bid", q.GetBid())
WaitFilled(ctx, ticket, timeout) · WaitClosed(ctx, ticket, timeout)¶
func (s *MT4Sugar) WaitFilled(ctx context.Context, ticket int32, timeout time.Duration) (bool, error)
func (s *MT4Sugar) WaitClosed(ctx context.Context, ticket int32, timeout time.Duration) (bool, error)
WaitFilled returns true once a pending becomes a market order; WaitClosed once the ticket is no longer open. Both return false on timeout (not an error).
Example — place a pending, cancel if it doesn't fill:
r, _ := sugar.BuyLimit(ctx, "EURUSD", 0.10, price, nil, nil)
if filled, _ := sugar.WaitFilled(ctx, r.GetTicket(), time.Minute); !filled {
sugar.Close(ctx, r.GetTicket(), nil) // delete the resting pending
}
Example — block until a position closes:
if closed, _ := sugar.WaitClosed(ctx, ticket, 5*time.Minute); closed {
fmt.Println("position closed")
}
See also: 7. Trailing & Breakeven · 9. Order Info · MT4Account streaming