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)ΒΆ
Prints a header to the console. \n β a newline character. Used to visually separate the output.
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.
Comment: we create a context with a timeout so the program doesn't hang if the server doesn't respond.
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.
Defers the call to summaryCancel() until the end of the function. This ensures resource cleanup.
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.
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.
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.
Prints the account leverage in the format 1:500.
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:
- Checks that the time from the server (
ServerTime) is present. - Converts protobuf time to the
time.Timetype via the.AsTime()method. - Formats the date and time in a human-readable form:
YYYY-MM-DD HH:MM:SS. - 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:ΒΆ
- A request and context with timeout are created.
- The
AccountSummary()method is called via RPC. - Errors are checked.
- If successful β account data is printed.
- 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.