Skip to content

MT4Sugar — 2. Prices & Quotes

Current prices, spread, a symbol-readiness gate, and OHLC history by bar count.

Thin reads over MT4Service that return the familiar *pb.QuoteData (or plain numbers).


LastQuote(ctx, symbol)

func (s *MT4Sugar) LastQuote(ctx context.Context, symbol string) (*pb.QuoteData, error)

The latest quote for one symbol. *pb.QuoteData → GetBid(), GetAsk(), GetSymbol(), GetDateTime(), GetHigh(), GetLow().

Example — print bid/ask:

q, _ := sugar.LastQuote(ctx, "EURUSD")
fmt.Printf("%s %.5f / %.5f\n", q.GetSymbol(), q.GetBid(), q.GetAsk())

Example — day range:

q, _ := sugar.LastQuote(ctx, "EURUSD")
fmt.Printf("H %.5f  L %.5f  (as of %s)\n",
    q.GetHigh(), q.GetLow(), q.GetDateTime().AsTime().Format("15:04:05"))

Quotes(ctx, symbols)

func (s *MT4Sugar) Quotes(ctx context.Context, symbols []string) ([]*pb.QuoteData, error)

Latest quotes for several symbols (unwraps QuoteMany's GetQuotes()).

Example — price a watchlist:

qs, _ := sugar.Quotes(ctx, []string{"EURUSD", "GBPUSD", "USDJPY"})
for _, q := range qs {
    fmt.Printf("%-7s %.5f / %.5f\n", q.GetSymbol(), q.GetBid(), q.GetAsk())
}

MidPrice(ctx, symbol)

func (s *MT4Sugar) MidPrice(ctx context.Context, symbol string) (float64, error)

(bid + ask) / 2 — a single mid price.

Example:

mid, _ := sugar.MidPrice(ctx, "EURUSD")
fmt.Printf("mid = %.5f\n", mid)

SpreadPips(ctx, symbol)

func (s *MT4Sugar) SpreadPips(ctx context.Context, symbol string) (float64, error)

Current spread (ask − bid) expressed in pips. A common use is to skip trading when it's wide.

Example — gate entry on spread:

if sp, _ := sugar.SpreadPips(ctx, "EURUSD"); sp > 2.0 {
    log.Println("spread too wide, skipping")
    return
}

Example — pick the tightest of a basket:

best, bestSym := math.MaxFloat64, ""
for _, sym := range []string{"EURUSD", "GBPUSD", "USDJPY"} {
    if sp, err := sugar.SpreadPips(ctx, sym); err == nil && sp < best {
        best, bestSym = sp, sym
    }
}
fmt.Printf("tightest: %s (%.1f pips)\n", bestSym, best)

EnsureSymbol(ctx, symbol, wait)

func (s *MT4Sugar) EnsureSymbol(ctx context.Context, symbol string, wait time.Duration) (*pb.QuoteData, error)

Adds the symbol to Market Watch, then polls until it publishes a usable two-sided quote (bid > 0 && ask > bid) or wait elapses. Call it once before trading a symbol you haven't touched — fresh/demo feeds often send bid == ask or zeros at first.

Example — make a symbol ready before trading:

q, err := sugar.EnsureSymbol(ctx, "EURUSD", 10*time.Second)
if err != nil { return err } // never produced a usable quote
fmt.Println("ready:", q.GetBid(), q.GetAsk())

Example — prepare a whole watchlist:

for _, sym := range []string{"EURUSD", "GBPUSD", "USDJPY"} {
    if _, err := sugar.EnsureSymbol(ctx, sym, 10*time.Second); err != nil {
        log.Printf("%s not ready: %v", sym, err)
    }
}

Bars(ctx, symbol, timeframe, count)

func (s *MT4Sugar) Bars(ctx context.Context, symbol string, timeframe pb.ENUM_QUOTE_HISTORY_TIMEFRAME, count int) (*pb.QuoteHistoryData, error)

OHLC history covering roughly the newest count bars — it computes the time window for you. *pb.QuoteHistoryData → GetHistoricalQuotes().

Example — last 100 H1 candles:

h, _ := sugar.Bars(ctx, "EURUSD", pb.ENUM_QUOTE_HISTORY_TIMEFRAME_QH_PERIOD_H1, 100)
fmt.Println("bars:", len(h.GetHistoricalQuotes()))

Example — close prices for an indicator:

h, _ := sugar.Bars(ctx, "EURUSD", pb.ENUM_QUOTE_HISTORY_TIMEFRAME_QH_PERIOD_H1, 50)
closes := make([]float64, 0, len(h.GetHistoricalQuotes()))
for _, b := range h.GetHistoricalQuotes() {
    closes = append(closes, b.GetClose())
}

See also: 1. Connection & Defaults · 3. Symbol Math