š Get Spread (GetSpread)¶
Sugar method: Returns current spread in points (difference between ASK and BID).
API Information:
- Method:
sugar.GetSpread(symbol) - Timeout: 3 seconds
- Returns: Spread in points as
float64
š Method Signature¶
š¬ Just the Essentials¶
- What it is: Spread = ASK - BID in points (broker's fee for each trade).
- Why you need it: Check trading costs before opening positions. High spread = expensive trade.
- Sanity check: Lower spread = better. Typical EURUSD spread: 5-20 points.
𧮠Formula¶
Spread = ASK - BID (in points)
For 5-digit broker (EURUSD):
ASK: 1.08460, BID: 1.08450
Spread = (1.08460 - 1.08450) / 0.00001 = 10 points
Trading cost = Spread Ć Point Ć ContractSize Ć Volume
š Usage Examples¶
1) Basic usage¶
spread, err := sugar.GetSpread("EURUSD")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("EURUSD spread: %.0f points\n", spread)
// Output: EURUSD spread: 10 points
2) Check spread before trading¶
symbol := "EURUSD"
maxAcceptableSpread := 15.0
spread, _ := sugar.GetSpread(symbol)
fmt.Printf("%s current spread: %.0f points\n", symbol, spread)
if spread > maxAcceptableSpread {
fmt.Printf("ā Spread too high (%.0f > %.0f)\n", spread, maxAcceptableSpread)
fmt.Println(" Waiting for better conditions...")
} else {
fmt.Printf("ā
Spread acceptable (%.0f ⤠%.0f)\n", spread, maxAcceptableSpread)
sugar.BuyMarket(symbol, 0.1)
}
3) Compare spreads across symbols¶
symbols := []string{"EURUSD", "GBPUSD", "USDJPY", "XAUUSD", "BTCUSD"}
fmt.Println("Current spreads:")
fmt.Println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
for _, symbol := range symbols {
spread, err := sugar.GetSpread(symbol)
if err != nil {
continue
}
// Categorize spread
status := ""
if spread < 10 {
status = "š¢ Excellent"
} else if spread < 20 {
status = "š” Good"
} else if spread < 50 {
status = "š High"
} else {
status = "š“ Very High"
}
fmt.Printf("%-8s %3.0f points %s\n", symbol, spread, status)
}
// Output:
// Current spreads:
// āāāāāāāāāāāāāāāāāāāāāāāāāāāāā
// EURUSD 10 points š¢ Excellent
// GBPUSD 15 points š” Good
// USDJPY 12 points š” Good
// XAUUSD 45 points š High
// BTCUSD 120 points š“ Very High
4) Calculate trading cost¶
symbol := "EURUSD"
volume := 1.0 // 1 lot
spread, _ := sugar.GetSpread(symbol)
info, _ := sugar.GetSymbolInfo(symbol)
// Calculate cost in account currency
spreadCost := spread * info.Point * info.ContractSize * volume
fmt.Printf("Symbol: %s\n", symbol)
fmt.Printf("Volume: %.2f lots\n", volume)
fmt.Printf("Spread: %.0f points\n", spread)
fmt.Printf("Trading cost: $%.2f\n", spreadCost)
// Output:
// Symbol: EURUSD
// Volume: 1.00 lots
// Spread: 10 points
// Trading cost: $10.00
5) Monitor spread changes¶
symbol := "EURUSD"
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
fmt.Printf("Monitoring %s spread:\n\n", symbol)
previousSpread := 0.0
for i := 0; i < 20; i++ {
<-ticker.C
spread, _ := sugar.GetSpread(symbol)
if previousSpread > 0 {
change := spread - previousSpread
if change > 0 {
fmt.Printf("%.0f points (ā widened by %.0f)\n", spread, change)
} else if change < 0 {
fmt.Printf("%.0f points (ā narrowed by %.0f)\n", spread, -change)
} else {
fmt.Printf("%.0f points (ā no change)\n", spread)
}
} else {
fmt.Printf("%.0f points (initial)\n", spread)
}
previousSpread = spread
}
6) Find best time to trade (lowest spread)¶
symbol := "EURUSD"
checkDuration := 5 * time.Minute
checkInterval := 10 * time.Second
minSpread := 99999.0
maxSpread := 0.0
totalSpread := 0.0
samples := 0
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()
timeout := time.After(checkDuration)
for {
select {
case <-timeout:
avgSpread := totalSpread / float64(samples)
fmt.Println("\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
fmt.Printf("Spread analysis for %s:\n", symbol)
fmt.Printf("Duration: %v\n", checkDuration)
fmt.Printf("Minimum: %.0f points\n", minSpread)
fmt.Printf("Maximum: %.0f points\n", maxSpread)
fmt.Printf("Average: %.0f points\n", avgSpread)
fmt.Println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
return
case <-ticker.C:
spread, _ := sugar.GetSpread(symbol)
if spread < minSpread {
minSpread = spread
}
if spread > maxSpread {
maxSpread = spread
}
totalSpread += spread
samples++
fmt.Printf("Spread: %.0f points (min: %.0f, max: %.0f)\n",
spread, minSpread, maxSpread)
}
}
7) Spread warning system¶
func MonitorSpreadWarnings(sugar *mt5.MT5Sugar, symbol string, warningLevel float64) {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for range ticker.C {
spread, _ := sugar.GetSpread(symbol)
if spread > warningLevel {
fmt.Printf("ā ļø WARNING: %s spread is %.0f points (threshold: %.0f)\n",
symbol, spread, warningLevel)
} else {
fmt.Printf("ā
%s spread: %.0f points (OK)\n", symbol, spread)
}
}
}
// Run in background
go MonitorSpreadWarnings(sugar, "EURUSD", 20.0)
8) Validate spread before scalping¶
// Scalping requires tight spreads
func CanScalp(sugar *mt5.MT5Sugar, symbol string) bool {
spread, err := sugar.GetSpread(symbol)
if err != nil {
return false
}
maxScalpingSpread := 10.0 // Scalpers need very tight spreads
if spread > maxScalpingSpread {
fmt.Printf("ā Spread %.0f too high for scalping (max: %.0f)\n",
spread, maxScalpingSpread)
return false
}
fmt.Printf("ā
Spread %.0f acceptable for scalping\n", spread)
return true
}
if CanScalp(sugar, "EURUSD") {
// Open quick in/out trades
sugar.BuyMarket("EURUSD", 0.1)
}
9) Spread dashboard for multiple symbols¶
symbols := []string{"EURUSD", "GBPUSD", "USDJPY", "AUDUSD", "USDCAD"}
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for range ticker.C {
fmt.Print("\033[H\033[2J") // Clear screen
fmt.Println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
fmt.Println("ā SPREAD MONITOR ā")
fmt.Println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
fmt.Printf("Time: %s\n\n", time.Now().Format("15:04:05"))
fmt.Printf("%-10s %-10s %-10s\n", "Symbol", "Spread", "Status")
fmt.Println("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā")
for _, symbol := range symbols {
spread, _ := sugar.GetSpread(symbol)
status := ""
if spread < 10 {
status = "š¢ Tight"
} else if spread < 20 {
status = "š” Normal"
} else {
status = "š“ Wide"
}
fmt.Printf("%-10s %-10.0f %-10s\n", symbol, spread, status)
}
}
10) Historical spread tracking¶
type SpreadRecord struct {
Time time.Time
Spread float64
}
func TrackSpreads(sugar *mt5.MT5Sugar, symbol string, duration time.Duration) {
records := []SpreadRecord{}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
timeout := time.After(duration)
for {
select {
case <-timeout:
// Analyze collected data
if len(records) == 0 {
return
}
var total float64
min := records[0].Spread
max := records[0].Spread
for _, r := range records {
total += r.Spread
if r.Spread < min {
min = r.Spread
}
if r.Spread > max {
max = r.Spread
}
}
avg := total / float64(len(records))
fmt.Println("\nāāāāāāā SPREAD ANALYSIS āāāāāāā")
fmt.Printf("Symbol: %s\n", symbol)
fmt.Printf("Samples: %d\n", len(records))
fmt.Printf("Min: %.0f points\n", min)
fmt.Printf("Max: %.0f points\n", max)
fmt.Printf("Average: %.0f points\n", avg)
return
case t := <-ticker.C:
spread, _ := sugar.GetSpread(symbol)
records = append(records, SpreadRecord{
Time: t,
Spread: spread,
})
fmt.Printf("%s: %.0f points\n", t.Format("15:04:05"), spread)
}
}
}
// Track for 1 hour
TrackSpreads(sugar, "EURUSD", 1*time.Hour)
š Related Methods¶
GetBid()- BID priceGetAsk()- ASK priceGetPriceInfo()- Get BID, ASK, and spread at once ā
ā ļø Common Pitfalls¶
1) Ignoring spread when calculating profit¶
// ā WRONG - forgetting spread cost
entry := 1.08460
target := 1.08560
profit := target - entry // 100 pips
// Real profit is less due to spread!
// ā
CORRECT - account for spread
spread, _ := sugar.GetSpread("EURUSD")
info, _ := sugar.GetSymbolInfo("EURUSD")
spreadInPrice := spread * info.Point
realProfit := (target - entry) - spreadInPrice
š Pro Tips¶
- Lower is better - Always prefer low spread symbols/times
- Volatile times = wide spreads - News events widen spreads
- Major pairs = tight spreads - EUR/USD, GBP/USD have lowest
- Check before scalping - Scalpers need spreads < 10 points
- Use GetPriceInfo() - More efficient than calculating manually
See also: GetBid.md, GetAsk.md, GetPriceInfo.md