Skip to content

βœ… Check Connection Status (IsConnected)ΒΆ

Sugar method: Verifies if you're currently connected to MT5 Terminal.

API Information:

  • Method: sugar.IsConnected()
  • Package: mt5 (MT5Sugar)
  • Underlying calls: account.Ping() with health check
  • Timeout: 3 seconds

πŸ“‹ Method SignatureΒΆ

func (s *MT5Sugar) IsConnected() bool

πŸ”½ InputΒΆ

No parameters


⬆️ OutputΒΆ

Return Type Description
connected bool true if connected and healthy, false otherwise

πŸ’¬ Just the EssentialsΒΆ

  • What it is: Quick check if MT5 connection is alive and working.
  • Why you need it: Verify connection before trading operations, detect disconnects.
  • Sanity check: After QuickConnect() succeeds, this should return true.

🎯 Purpose¢

Use it to monitor connection health:

  • Pre-flight check - Verify connection before trading
  • Reconnect logic - Detect when connection is lost
  • Health monitoring - Periodic connection checks
  • Error recovery - Know when to retry operations

🧩 Notes & Tips¢

  • Fast check - 3-second timeout
  • True = ready - Safe to call trading methods
  • False = disconnected - Need to reconnect
  • Non-blocking - Quick operation
  • Use before trading - Good practice to verify connection
  • Periodic checks - Monitor in long-running bots

πŸ”§ Under the HoodΒΆ

func (s *MT5Sugar) IsConnected() bool {
    ctx, cancel := context.WithTimeout(s.ctx, 3*time.Second)
    defer cancel()

    // Ping server to check if connection is alive
    err := s.account.Ping(ctx)
    if err != nil {
        return false // Connection lost
    }

    return true // Connected and healthy
}

What it does:

  • βœ… Pings the server - Verifies communication
  • βœ… Quick timeout - 3 seconds max
  • βœ… Clean result - True/false, easy to use

πŸ“Š Low-Level AlternativeΒΆ

WITHOUT sugar:

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

err := account.Ping(ctx)
connected := (err == nil)

WITH sugar:

connected := sugar.IsConnected()

Benefits:

  • βœ… One line vs three
  • βœ… Clear intent
  • βœ… Built-in timeout

πŸ”— Usage ExamplesΒΆ

1) Basic connection checkΒΆ

connected := sugar.IsConnected()

if connected {
    fmt.Println("βœ… Connected to MT5")
} else {
    fmt.Println("❌ Not connected")
}

2) Verify before tradingΒΆ

// Check connection before placing order
connected := sugar.IsConnected()
if !connected {
    fmt.Println("❌ Not connected - cannot place order")
    return
}

// Connection OK - safe to trade
ticket, err := sugar.BuyMarket("EURUSD", 0.1)
if err != nil {
    fmt.Printf("Order failed: %v\n", err)
    return
}

fmt.Printf("βœ… Order #%d placed\n", ticket)

3) Automatic reconnectionΒΆ

func EnsureConnected(sugar *mt5.MT5Sugar, clusterName string) error {
    connected := sugar.IsConnected()

    if connected {
        return nil // Already connected
    }

    // Connection lost - reconnect
    fmt.Println("⚠️  Connection lost - reconnecting...")
    err := sugar.QuickConnect(clusterName)
    if err != nil {
        return fmt.Errorf("reconnection failed: %w", err)
    }

    fmt.Println("βœ… Reconnected successfully")
    return nil
}

// Usage:
err := EnsureConnected(sugar, "FxPro-MT5 Demo")
if err != nil {
    fmt.Printf("Cannot connect: %v\n", err)
    return
}

// Now safe to trade
sugar.BuyMarket("EURUSD", 0.1)

4) Connection monitoring loopΒΆ

func MonitorConnection(sugar *mt5.MT5Sugar, interval time.Duration) {
    ticker := time.NewTicker(interval)
    defer ticker.Stop()

    for range ticker.C {
        connected := sugar.IsConnected()

        if connected {
            fmt.Println("πŸ“‘ Connection: OK")
        } else {
            fmt.Println("❌ Connection: LOST")
            // Trigger reconnect logic here
        }
    }
}

// Run in background
go MonitorConnection(sugar, 30*time.Second)

5) Pre-trading validationΒΆ

func ValidateTrading(sugar *mt5.MT5Sugar) error {
    // Check connection
    connected := sugar.IsConnected()
    if !connected {
        return fmt.Errorf("not connected to MT5")
    }

    // Check balance
    balance, err := sugar.GetBalance()
    if err != nil {
        return fmt.Errorf("cannot get balance: %w", err)
    }

    if balance <= 0 {
        return fmt.Errorf("insufficient balance: %.2f", balance)
    }

    return nil
}

// Before trading
err := ValidateTrading(sugar)
if err != nil {
    fmt.Printf("❌ Cannot trade: %v\n", err)
    return
}

fmt.Println("βœ… All checks passed - ready to trade")

6) Retry with connection checkΒΆ

func PlaceOrderWithRetry(sugar *mt5.MT5Sugar, symbol string, volume float64, maxRetries int) (uint64, error) {
    for i := 0; i < maxRetries; i++ {
        // Check connection before each attempt
        connected := sugar.IsConnected()
        if !connected {
            fmt.Printf("Attempt %d: Not connected - reconnecting...\n", i+1)
            sugar.QuickConnect("FxPro-MT5 Demo")
            time.Sleep(2 * time.Second)
            continue
        }

        // Try to place order
        ticket, err := sugar.BuyMarket(symbol, volume)
        if err == nil {
            return ticket, nil // Success
        }

        fmt.Printf("Attempt %d failed: %v\n", i+1, err)
        time.Sleep(time.Second)
    }

    return 0, fmt.Errorf("all %d attempts failed", maxRetries)
}

// Usage:
ticket, err := PlaceOrderWithRetry(sugar, "EURUSD", 0.1, 3)
if err != nil {
    fmt.Printf("Order failed: %v\n", err)
} else {
    fmt.Printf("Order #%d placed\n", ticket)
}

7) Status dashboardΒΆ

func PrintStatus(sugar *mt5.MT5Sugar) {
    connected := sugar.IsConnected()

    fmt.Println("╔═══════════════════════════════════════╗")
    fmt.Println("β•‘          MT5 STATUS DASHBOARD         β•‘")
    fmt.Println("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")

    if connected {
        fmt.Println("Connection:  βœ… CONNECTED")

        balance, _ := sugar.GetBalance()
        equity, _ := sugar.GetEquity()
        positions, _ := sugar.CountOpenPositions()

        fmt.Printf("Balance:     %.2f\n", balance)
        fmt.Printf("Equity:      %.2f\n", equity)
        fmt.Printf("Positions:   %d\n", positions)
    } else {
        fmt.Println("Connection:  ❌ DISCONNECTED")
        fmt.Println("Balance:     N/A")
        fmt.Println("Equity:      N/A")
        fmt.Println("Positions:   N/A")
    }
}

// Usage:
PrintStatus(sugar)

8) Connection timeout handlingΒΆ

func CheckConnectionWithTimeout(sugar *mt5.MT5Sugar, timeout time.Duration) (bool, error) {
    done := make(chan bool, 1)

    go func() {
        connected := sugar.IsConnected()
        done <- connected
    }()

    select {
    case connected := <-done:
        return connected, nil
    case <-time.After(timeout):
        return false, fmt.Errorf("connection check timeout after %v", timeout)
    }
}

// Usage with custom timeout
connected, err := CheckConnectionWithTimeout(sugar, 5*time.Second)
if err != nil {
    fmt.Printf("Check failed: %v\n", err)
}

9) Bot startup sequenceΒΆ

func StartTradingBot(login uint64, password, server, cluster string) error {
    // Create Sugar
    sugar, err := mt5.NewMT5Sugar(login, password, server)
    if err != nil {
        return fmt.Errorf("failed to create Sugar: %w", err)
    }

    // Connect
    fmt.Println("πŸ”Œ Connecting to MT5...")
    err = sugar.QuickConnect(cluster)
    if err != nil {
        return fmt.Errorf("connection failed: %w", err)
    }

    // Verify connection
    fmt.Println("πŸ” Verifying connection...")
    connected := sugar.IsConnected()
    if !connected {
        return fmt.Errorf("connection verification failed")
    }

    // Get account info
    fmt.Println("πŸ“Š Loading account info...")
    info, err := sugar.GetAccountInfo()
    if err != nil {
        return fmt.Errorf("failed to get account info: %w", err)
    }

    fmt.Println("╔═══════════════════════════════════════╗")
    fmt.Println("β•‘       BOT STARTED SUCCESSFULLY        β•‘")
    fmt.Println("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
    fmt.Printf("Account:  %d\n", info.Login)
    fmt.Printf("Balance:  %.2f %s\n", info.Balance, info.Currency)
    fmt.Printf("Company:  %s\n", info.Company)

    return nil
}

// Usage:
err := StartTradingBot(591129415, "password", "mt5.mrpc.pro:443", "FxPro-MT5 Demo")
if err != nil {
    fmt.Printf("❌ Startup failed: %v\n", err)
}

10) Connection status with fallbackΒΆ

func GetBalanceOrDefault(sugar *mt5.MT5Sugar) float64 {
    // Check connection first
    connected := sugar.IsConnected()
    if !connected {
        fmt.Println("⚠️  Not connected - returning 0")
        return 0.0
    }

    // Connection OK - get balance
    balance, err := sugar.GetBalance()
    if err != nil {
        fmt.Printf("⚠️  Balance error: %v - returning 0\n", err)
        return 0.0
    }

    return balance
}

// Safe to call even when disconnected
balance := GetBalanceOrDefault(sugar)
fmt.Printf("Balance: %.2f\n", balance)

🍬 Connection methods:

  • QuickConnect() - Connect to MT5 Terminal
  • Ping() - Detailed connection health check

πŸ“– Typical usage pattern:

// 1. Connect
sugar.QuickConnect("FxPro-MT5 Demo")

// 2. Verify
connected := sugar.IsConnected()
if !connected {
    return
}

// 3. Trade
sugar.BuyMarket("EURUSD", 0.1)

⚠️ Common Pitfalls¢

1) Ignoring the resultΒΆ

// ❌ WRONG - ignoring connection status
sugar.IsConnected()
sugar.BuyMarket("EURUSD", 0.1) // Might fail!

// βœ… CORRECT - check before trading
connected := sugar.IsConnected()
if !connected {
    return
}
sugar.BuyMarket("EURUSD", 0.1)

2) Not checking after QuickConnectΒΆ

// ❌ WRONG - assuming connection worked
sugar.QuickConnect("FxPro-MT5 Demo")
// Start trading immediately...

// βœ… CORRECT - verify connection
err := sugar.QuickConnect("FxPro-MT5 Demo")
if err != nil {
    return
}
connected := sugar.IsConnected()
if !connected {
    return
}
// Now safe to trade

3) Checking too frequentlyΒΆ

// ❌ WRONG - checking before every single operation
for i := 0; i < 1000; i++ {
    connected := sugar.IsConnected()
    if !connected { return }
    sugar.GetBalance() // Too many checks!
}

// βœ… CORRECT - check once before loop
connected := sugar.IsConnected()
if !connected { return }
for i := 0; i < 1000; i++ {
    sugar.GetBalance()
}

πŸ’Ž Pro TipsΒΆ

  1. Check once, trade many - Don't check before every operation
  2. Periodic monitoring - Check every 30-60 seconds in bots
  3. Before critical operations - Always check before trading
  4. Reconnect on false - Auto-reconnect when connection lost
  5. Combine with Ping() - Use Ping() for detailed health checks

See also: QuickConnect.md, Ping.md