Getting Started with GoMT4¶
Quick Setup & Overview - Start automating your trading with GoMT4 (Go for MetaTrader 4) in minutes.
π― What is GoMT4?¶
GoMT4 is a Go client for MetaTrader 4 accounts through the MetaRPC cloud API. Your program talks gRPC over TLS to mt4.mrpc.pro:443; MetaRPC runs and manages an MT4 terminal logged in to your account. You do not install MetaTrader or run any server yourself.
Three-layer architecture¶
The SDK (github.com/MetaRPC/GoMT4/mt4) is built in three stacked layers β enter at whichever level you need:
| Layer | Type | What you get |
|---|---|---|
MT4Account |
low | Raw gRPC β build β¦Request, get (*β¦Data, error). Full control. |
MT4Service |
mid | Clean returns (unwrapped slices/scalars), simpler args. |
MT4Sugar |
high | One-liners: buy/sell by side, pip math, risk sizing, trailing, watchers. |
Also in the box: the generated API package github.com/MetaRPC/GoMT4/package (pb) with every message, enum and gRPC client; built-in retries (transient errors retry with backoff + jitter); and runnable examples under examples/, grouped by layer and launched from an interactive menu.
π¦ Installation¶
The generated API package can be fetched with Go:
go get github.com/MetaRPC/GoMT4/package
The three-layer SDK lives in the mt4 module; the quickest way to see it work is to run the examples from a clone:
git clone https://github.com/MetaRPC/GoMT4.git
cd GoMT4/examples
go run . # interactive menu of grouped examples
π API Key & Authentication¶
Connecting to MetaRPC production endpoints (mt4.mrpc.pro:443) uses an API key:
- Sign Up: Create an account for free at https://mrpc.pro/signup.
- Generate API Key: In your MetaRPC Portal dashboard at https://mrpc.pro/my, go to API Keys to generate and copy your personal API token.
- Configure Connection: Pass the key to
NewMT4Account(last argument), setMRPC_API_KEY, or put it intoexamples/config/config.jsonβApiKey.
Without a key, calls run as a trial (TRIAL); an unknown key is refused with API_KEY_INVALID.
π Account ID & Authentication¶
Every call carries two gRPC metadata headers:
1. apikey: your API key (TRIAL when none is given).
2. id: the id of the terminal that serves your account.
π‘ How the client handles the id:
NewMT4Account(..., uuid.Nil, ...)starts with a deterministic id computed from login + password (ComputeDeterministicTerminalId, the same value the server'sGetIdreturns).ConnectByServerName/ConnectByHostPortconnect the terminal, and the client then uses theTerminalInstanceGuidthe server returns. A call only reaches a terminal that is connected under that id, so connect first.
π Minimal Connection Example¶
Initialize MT4Account, connect to your MetaTrader account, and read the balance (run inside a clone, e.g. as a file in examples/):
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/MetaRPC/GoMT4/mt4"
"github.com/google/uuid"
)
func main() {
user := uint64(12345678)
password := "your-password"
serverName := "MetaQuotes-Demo" // broker server name as shown in MetaTrader
apiKey := "your-api-key"
// "" = default endpoint mt4.mrpc.pro:443; uuid.Nil = deterministic id from login+password
client, err := mt4.NewMT4Account(user, password, "", uuid.Nil, apiKey)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(true)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := client.ConnectByServerName(ctx, serverName, "EURUSD", true, 30); err != nil {
log.Fatal(err)
}
summary, err := client.AccountSummary(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Balance: %.2f, Equity: %.2f\n", summary.GetAccountBalance(), summary.GetAccountEquity())
}
πΊοΈ Documentation Road Map¶
To get the most out of GoMT4, follow this suggested reading order:
- Your First Project - Build and run a working project in 10 minutes.
- Project Map - How the client, the MetaRPC API and the terminal fit together.
- Glossary - Essential MetaTrader 4 terminology.
- API Reference - MT4Account Β· MT4Service Β· MT4Sugar.
- Protobuf Inspector - Explore every type, field and enum offline.