π Get Complete Price Information (GetPriceInfo)ΒΆ
Sugar method: Returns BID, ASK, spread, and timestamp in one method call.
API Information:
- Method:
sugar.GetPriceInfo(symbol) - Timeout: 3 seconds (shared by both requests)
- Returns:
*PriceInfostructure - Underlying calls:
service.GetSymbolTick(ctx, symbol)+service.GetSymbolDouble(ctx, symbol, pb.SymbolInfoDoubleProperty_SYMBOL_POINT)- two requests
π Method SignatureΒΆ
// PriceInfo structure
type PriceInfo struct {
Symbol string // Symbol name
Bid float64 // Current BID price
Ask float64 // Current ASK price
SpreadPips float64 // Spread in POINTS (despite the name)
Time time.Time // Time of the last tick in broker server time
TimeUtc time.Time // The same moment in UTC
}
How the fields are filled:
Bid,Ask- from the same tick (SymbolInfoTick).SpreadPips- despite the name, the spread in points:(Ask - Bid) / point, wherepointis the symbol'sSYMBOL_POINT(EURUSD with 5 digits:0.00010 / 0.00001 = 10points = 1 pip). It is computed from the tick, not read fromSYMBOL_SPREADlikeGetSpread(), so the two can differ slightly. Rounded to 0.1 point (float noise: 3.9999999 β 4). A symbol whoseSYMBOL_POINTis <= 0 gives an error.Time- the tick time in broker server time: MT5 writes the broker's wall clock as a Unix timestamp, andGetSymbolTickkeeps it as a UTC-locatedtime.Time(time.Unix(tick.Time, 0).UTC()), sopriceInfo.Time.Format(...)prints the broker's clock. Do not use it for age checks -time.Since(priceInfo.Time)is off by the broker's UTC offset.TimeUtc- the same moment in real UTC (time.Unix(tick.TimeUtc, 0).UTC()). Use it fortime.Since(...).
π¬ Just the EssentialsΒΆ
- What it is: Get BID, ASK, spread, and time from ONE method instead of three separate methods.
- Why you need it: 2 requests instead of 3 (GetBid + GetAsk + GetSpread), and BID/ASK come from the same tick.
- Sanity check: priceInfo.Ask β₯ priceInfo.Bid, SpreadPips = (ask - bid) / point.
π― When to UseΒΆ
β Price snapshots - Need complete price picture at once
β Performance - Fewer API calls (2 instead of 3)
β Logging/reporting - Capture full price state with timestamp
β Display dashboards - Show all price data together
π Usage ExamplesΒΆ
1) Basic usageΒΆ
priceInfo, err := sugar.GetPriceInfo("EURUSD")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Symbol: %s\n", priceInfo.Symbol)
fmt.Printf("BID: %.5f\n", priceInfo.Bid)
fmt.Printf("ASK: %.5f\n", priceInfo.Ask)
fmt.Printf("Spread: %.0f points\n", priceInfo.SpreadPips)
fmt.Printf("Time: %s\n", priceInfo.Time.Format("15:04:05"))
// Output:
// Symbol: EURUSD
// BID: 1.08450
// ASK: 1.08460
// Spread: 10 points
// Time: 14:23:15
2) Price snapshot for loggingΒΆ
func LogPriceSnapshot(sugar *mt5.MT5Sugar, symbol string) {
priceInfo, err := sugar.GetPriceInfo(symbol)
if err != nil {
fmt.Printf("Error getting price: %v\n", err)
return
}
// Log to file/database
logEntry := fmt.Sprintf("[%s] %s: BID=%.5f ASK=%.5f SPREAD=%.0f",
priceInfo.Time.Format("2006-01-02 15:04:05"),
priceInfo.Symbol,
priceInfo.Bid,
priceInfo.Ask,
priceInfo.SpreadPips)
fmt.Println(logEntry)
// In production: write to file or database
}
// Usage:
LogPriceSnapshot(sugar, "EURUSD")
3) Comparison: GetPriceInfo vs individual callsΒΆ
symbol := "EURUSD"
// β LESS EFFICIENT - 3 separate API calls (and BID/ASK may come from different ticks)
bid, _ := sugar.GetBid(symbol)
ask, _ := sugar.GetAsk(symbol)
spread, _ := sugar.GetSpread(symbol)
fmt.Printf("BID: %.5f, ASK: %.5f, Spread: %.0f\n", bid, ask, spread)
// β
MORE EFFICIENT - 2 API calls (tick + point size)
priceInfo, _ := sugar.GetPriceInfo(symbol)
fmt.Printf("BID: %.5f, ASK: %.5f, Spread: %.0f\n",
priceInfo.Bid, priceInfo.Ask, priceInfo.SpreadPips)
4) Real-time price dashboardΒΆ
symbols := []string{"EURUSD", "GBPUSD", "USDJPY"}
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for range ticker.C {
fmt.Print("\033[H\033[2J") // Clear screen
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
fmt.Println("β REAL-TIME PRICE DASHBOARD β")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
fmt.Printf("Updated: %s\n\n", time.Now().Format("15:04:05"))
fmt.Printf("%-10s %-12s %-12s %-10s\n",
"Symbol", "BID", "ASK", "Spread")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
for _, symbol := range symbols {
priceInfo, err := sugar.GetPriceInfo(symbol)
if err != nil {
continue
}
fmt.Printf("%-10s %-12.5f %-12.5f %-10.0f\n",
priceInfo.Symbol,
priceInfo.Bid,
priceInfo.Ask,
priceInfo.SpreadPips)
}
}
5) Trade signal with price snapshotΒΆ
func CheckTradingSignal(sugar *mt5.MT5Sugar, symbol string) {
priceInfo, _ := sugar.GetPriceInfo(symbol)
fmt.Println("βββββββββββββββββββββββββββββββββββββββββ")
fmt.Println("β TRADE SIGNAL CHECK β")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββ")
fmt.Printf("Symbol: %s\n", priceInfo.Symbol)
fmt.Printf("Time: %s\n", priceInfo.Time.Format("15:04:05"))
fmt.Printf("BID: %.5f\n", priceInfo.Bid)
fmt.Printf("ASK: %.5f\n", priceInfo.Ask)
fmt.Printf("Spread: %.0f points\n", priceInfo.SpreadPips)
fmt.Println()
// Check conditions
if priceInfo.SpreadPips > 15 {
fmt.Println("β Spread too high - NO TRADE")
return
}
if priceInfo.Bid < 1.08000 {
fmt.Println("β
BUY SIGNAL - Price at support")
// Place buy order
} else if priceInfo.Bid > 1.09000 {
fmt.Println("β
SELL SIGNAL - Price at resistance")
// Place sell order
} else {
fmt.Println("βΈοΈ NO SIGNAL - Price in range")
}
}
6) Price history recorderΒΆ
type PriceSnapshot struct {
Time time.Time
Bid float64
Ask float64
Spread float64
}
func RecordPriceHistory(sugar *mt5.MT5Sugar, symbol string, duration time.Duration) {
history := []PriceSnapshot{}
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
timeout := time.After(duration)
for {
select {
case <-timeout:
// Save history
fmt.Printf("\nRecorded %d price snapshots for %s\n",
len(history), symbol)
// Show summary
if len(history) > 0 {
first := history[0]
last := history[len(history)-1]
fmt.Printf("First: %.5f (BID) at %s\n",
first.Bid, first.Time.Format("15:04:05"))
fmt.Printf("Last: %.5f (BID) at %s\n",
last.Bid, last.Time.Format("15:04:05"))
fmt.Printf("Change: %.5f (%.2f%%)\n",
last.Bid-first.Bid,
((last.Bid-first.Bid)/first.Bid)*100)
}
return
case <-ticker.C:
priceInfo, _ := sugar.GetPriceInfo(symbol)
snapshot := PriceSnapshot{
Time: priceInfo.Time,
Bid: priceInfo.Bid,
Ask: priceInfo.Ask,
Spread: priceInfo.SpreadPips,
}
history = append(history, snapshot)
fmt.Printf("%s: BID=%.5f SPREAD=%.0f\n",
priceInfo.Time.Format("15:04:05"),
priceInfo.Bid,
priceInfo.SpreadPips)
}
}
}
// Record for 5 minutes
RecordPriceHistory(sugar, "EURUSD", 5*time.Minute)
7) Multi-symbol price comparisonΒΆ
symbols := []string{"EURUSD", "GBPUSD", "AUDUSD", "NZDUSD"}
fmt.Println("USD Pairs Price Comparison:")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββ")
fmt.Printf("%-10s %-12s %-12s %-10s %s\n",
"Pair", "BID", "ASK", "Spread", "Time")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββ")
for _, symbol := range symbols {
priceInfo, err := sugar.GetPriceInfo(symbol)
if err != nil {
continue
}
fmt.Printf("%-10s %-12.5f %-12.5f %-10.0f %s\n",
priceInfo.Symbol,
priceInfo.Bid,
priceInfo.Ask,
priceInfo.SpreadPips,
priceInfo.Time.Format("15:04:05"))
}
8) Price alert with full infoΒΆ
func PriceAlertWithInfo(sugar *mt5.MT5Sugar, symbol string, targetBid float64) {
fmt.Printf("π Alert set: %s BID reaches %.5f\n", symbol, targetBid)
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for range ticker.C {
priceInfo, _ := sugar.GetPriceInfo(symbol)
if priceInfo.Bid <= targetBid {
fmt.Println("\nπ¨ PRICE ALERT TRIGGERED!")
fmt.Println("βββββββββββββββββββββββββββββββββββββββ")
fmt.Printf("Symbol: %s\n", priceInfo.Symbol)
fmt.Printf("Target: %.5f\n", targetBid)
fmt.Printf("Current: %.5f (BID)\n", priceInfo.Bid)
fmt.Printf("ASK: %.5f\n", priceInfo.Ask)
fmt.Printf("Spread: %.0f points\n", priceInfo.SpreadPips)
fmt.Printf("Time: %s\n", priceInfo.Time.Format("15:04:05"))
return
}
fmt.Printf("%s BID: %.5f (target: %.5f, %.5f away)\n",
symbol, priceInfo.Bid, targetBid, priceInfo.Bid-targetBid)
}
}
9) Check if price is freshΒΆ
// PriceInfo.Time is broker server time, so time.Since(priceInfo.Time) is off by the
// broker's UTC offset. PriceInfo.TimeUtc is the same moment in UTC - use that for an age check.
func IsPriceFresh(sugar *mt5.MT5Sugar, symbol string, maxAge time.Duration) bool {
priceInfo, err := sugar.GetPriceInfo(symbol)
if err != nil {
fmt.Printf("β οΈ Cannot read price: %v\n", err)
return false
}
age := time.Since(priceInfo.TimeUtc)
if age > maxAge {
fmt.Printf("β οΈ WARNING: Price data is %v old (max: %v)\n", age, maxAge)
return false
}
fmt.Printf("β
Price data is fresh (%v old)\n", age)
return true
}
// Usage:
if IsPriceFresh(sugar, "EURUSD", 5*time.Second) {
// Use price for trading
sugar.BuyMarket("EURUSD", 0.1)
} else {
// Price too old, wait for update
fmt.Println("Waiting for fresh price...")
}
10) Complete trading decisionΒΆ
func MakeTradingDecision(sugar *mt5.MT5Sugar, symbol string) {
priceInfo, err := sugar.GetPriceInfo(symbol)
if err != nil {
fmt.Printf("β Error: %v\n", err)
return
}
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββ")
fmt.Println("β TRADING DECISION ANALYSIS β")
fmt.Println("βββββββββββββββββββββββββββββββββββββββββββββββββ")
fmt.Printf("Symbol: %s\n", priceInfo.Symbol)
fmt.Printf("Server Time: %s\n", priceInfo.Time.Format("2006-01-02 15:04:05"))
fmt.Printf("BID: %.5f\n", priceInfo.Bid)
fmt.Printf("ASK: %.5f\n", priceInfo.Ask)
fmt.Printf("Spread: %.0f points\n", priceInfo.SpreadPips)
fmt.Println()
// Check 1: Spread
if priceInfo.SpreadPips > 15 {
fmt.Println("β REJECT: Spread too high")
return
}
fmt.Printf("β
Spread OK (%.0f < 15 points)\n", priceInfo.SpreadPips)
// Check 2: Price freshness (uses TimeUtc - see example 9)
age := time.Since(priceInfo.TimeUtc)
if age > 10*time.Second {
fmt.Printf("β REJECT: Price too old (%v)\n", age)
return
}
fmt.Printf("β
Price fresh (%.1f seconds old)\n", age.Seconds())
// Check 3: Market hours (broker server hour: Time holds server wall-clock time)
hour := priceInfo.Time.Hour()
if hour < 8 || hour > 17 {
fmt.Printf("β REJECT: Outside trading hours (%d:00)\n", hour)
return
}
fmt.Printf("β
Market hours OK (%d:00)\n", hour)
// All checks passed
fmt.Println("\nπ’ ALL CHECKS PASSED - READY TO TRADE")
// Calculate position size
lotSize, _ := sugar.CalculatePositionSize(symbol, 2.0, 50)
fmt.Printf("Recommended lot size: %.2f (2%% risk, 50-point SL)\n", lotSize)
}
π Related MethodsΒΆ
GetBid()- Get only BID priceGetAsk()- Get only ASK priceGetSpread()- Get only spreadWaitForPrice()- Wait for valid price with timeout
π Pro TipsΒΆ
- Use this instead of individual calls - Fewer requests (2 vs 3), one consistent tick
- Check timestamp - Verify price is fresh before trading with
TimeUtc(Timeis broker server time) - Store snapshots - Keep price history for analysis
- Spread validation - Always check SpreadPips before trading
- Perfect for dashboards - Get all price data at once
See also: GetBid.md, GetAsk.md, WaitForPrice.md