Skip to content

AccountSummary HOW

Example from file: examples/demos/lowlevel/01_general_operations.goΒΆ

This example demonstrates how a low-level gRPC call to a MetaTrader server is executed through a gateway.
The code shows the process of retrieving summary information about a trading account using the AccountSummary() method.
Below is a detailed breakdown of each line and an explanation of how such a call is constructed at the client-server interaction level.

🧩 Code Example¢

fmt.Println("\n3.1. AccountSummary() - Get all account data in one call")

    summaryReq := &pb.AccountSummaryRequest{}
    // Use timeout context to prevent hanging
    summaryCtx, summaryCancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer summaryCancel()

    summaryData, err := account.AccountSummary(summaryCtx, summaryReq)
    helpers.Fatal(err, "AccountSummary failed")

    fmt.Println("\nAccount Summary (direct protobuf field access):")
    fmt.Println("─────────────────────────────────────────────────")
    fmt.Printf("  Login:               %d\n", summaryData.AccountLogin)
    fmt.Printf("  UserName:            %s\n", summaryData.AccountUserName)
    fmt.Printf("  Company:             %s\n", summaryData.AccountCompanyName)
    fmt.Printf("  Currency:            %s\n", summaryData.AccountCurrency)
    fmt.Printf("  Balance:             %.2f\n", summaryData.AccountBalance)
    fmt.Printf("  Equity:              %.2f\n", summaryData.AccountEquity)
    fmt.Printf("  Credit:              %.2f\n", summaryData.AccountCredit)
    fmt.Printf("  Leverage:            1:%d\n", summaryData.AccountLeverage)
    fmt.Printf("  Trade Mode:          %v\n", summaryData.AccountTradeMode)

    // ServerTime is a protobuf Timestamp - need to convert
    if summaryData.ServerTime != nil {
        serverTime := summaryData.ServerTime.AsTime()
        fmt.Printf("  Server Time:         %s\n", serverTime.Format("2006-01-02 15:04:05"))
    }

// UTC Timezone Shift: server time offset from UTC in minutes
// For example: 120 minutes = UTC+2 (the server is 2 hours ahead of UTC)
    fmt.Printf("  UTC Timezone Shift:  %d minutes (UTC%+.1f)\n",
        summaryData.UtcTimezoneServerTimeShiftMinutes,
        float64(summaryData.UtcTimezoneServerTimeShiftMinutes)/60.0)

🟒 Detailed Code Explanation for AccountSummary() (Go)¢

fmt.Println("\n3.1. AccountSummary() - Get all account data in one call")

Prints a header to the console. \n β€” a newline character. Used to visually separate the output.


summaryReq := &pb.AccountSummaryRequest{}

Creates an empty AccountSummaryRequest request. pb is a package generated from a .proto file (gRPC). The request is passed to AccountSummary() to retrieve the account summary.


// Use timeout context to prevent hanging

Comment: we create a context with a timeout so the program doesn't hang if the server doesn't respond.


summaryCtx, summaryCancel := context.WithTimeout(context.Background(), 10*time.Second)

Creates a context with an execution time limit β€” 10 seconds. If the response doesn't arrive in time, the request will be interrupted.

  • context.Background() β€” base context.
  • context.WithTimeout() β€” adds a time limit.
  • summaryCancel β€” function to cancel the context.

defer summaryCancel()

Defers the call to summaryCancel() until the end of the function. This ensures resource cleanup.


summaryData, err := account.AccountSummary(summaryCtx, summaryReq)

Calls the AccountSummary() method on the account object.

  • summaryCtx β€” context with timeout.
  • summaryReq β€” request. Returns:
  • summaryData β€” structure with the result (account information).
  • err β€” error if something went wrong.

helpers.Fatal(err, "AccountSummary failed")

Checks if an error occurred. If err != nil, then helpers.Fatal() will print a message and terminate the program.


fmt.Println("\nAccount Summary (direct protobuf field access):")
fmt.Println("─────────────────────────────────────────────────")

Prints a header and separator before the data to make the output readable.


fmt.Printf("  Login:               %d\n", summaryData.AccountLogin)

Prints the account login. %d β€” format for integers.


fmt.Printf("  UserName:            %s\n", summaryData.AccountUserName)
fmt.Printf("  Company:             %s\n", summaryData.AccountCompanyName)
fmt.Printf("  Currency:            %s\n", summaryData.AccountCurrency)

Prints string fields (username, company, account currency). %s β€” format for strings.


fmt.Printf("  Balance:             %.2f\n", summaryData.AccountBalance)
fmt.Printf("  Equity:              %.2f\n", summaryData.AccountEquity)
fmt.Printf("  Credit:              %.2f\n", summaryData.AccountCredit)

Prints numeric data (balance, equity, credit). %.2f β€” number with two decimal places.


fmt.Printf("  Leverage:            1:%d\n", summaryData.AccountLeverage)

Prints the account leverage in the format 1:500.


fmt.Printf("  Trade Mode:          %v\n", summaryData.AccountTradeMode)

Prints the trading mode (for example, real or demo account). %v β€” universal format.


⏱️ Server Time Output Block¢

In Go, the time format is not specified using symbols like YYYY or HH, but rather by an example of a specific date β€” 2006-01-02 15:04:05. These numbers are not chosen randomly: they represent the order of date and time elements, where:

  • 2006 β†’ year,
  • 01 β†’ month,
  • 02 β†’ day,
  • 15 β†’ hour (in 24-hour format),
  • 04 β†’ minutes,
  • 05 β†’ seconds.

Therefore, to output date and time in the desired format, you always specify exactly this template with actual numbers.

// ServerTime is a protobuf Timestamp - need to convert
if summaryData.ServerTime != nil {
    serverTime := summaryData.ServerTime.AsTime()
    fmt.Printf("  Server Time:         %s\n", serverTime.Format("2006-01-02 15:04:05"))
}

// UTC Timezone Shift: server time offset from UTC in minutes
// For example: 120 minutes = UTC+2 (the server is 2 hours ahead of UTC)
fmt.Printf("  UTC Timezone Shift:  %d minutes (UTC%+.1f)\n",
    summaryData.UtcTimezoneServerTimeShiftMinutes,
    float64(summaryData.UtcTimezoneServerTimeShiftMinutes)/60.0)

What this block does:

  1. Checks that the time from the server (ServerTime) is present.
  2. Converts protobuf time to the time.Time type via the .AsTime() method.
  3. Formats the date and time in a human-readable form: YYYY-MM-DD HH:MM:SS.
  4. Prints the server time offset from UTC both in minutes and hours (for example, 120 minutes (UTC+2.0)).

Why this is important:

  • In trading systems, it's crucial to know the exact server time for synchronizing quotes, trades, and reports.
  • The MetaTrader server may be located in a different timezone, and this affects candlestick calculations, order opening times, and reporting.

Summary:ΒΆ

  1. A request and context with timeout are created.
  2. The AccountSummary() method is called via RPC.
  3. Errors are checked.
  4. If successful β€” account data is printed.
  5. At the bottom, server time and UTC offset are additionally displayed.

Applications:ΒΆ

  • Testing the MetaTrader gateway API.
  • Retrieving real-time account information.
  • Use within algorithmic systems for account state monitoring and time synchronization.