Skip to content

Go Examples

Examples for interacting with the XBTFX Trading API using Go’s standard net/http library.

All examples use this helper for making authenticated requests:

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
baseURL = "https://interface.xbtfx.com"
apiKey = "your_api_key_here"
)
func doRequest(method, path string, body any) ([]byte, error) {
var reqBody io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
reqBody = bytes.NewReader(data)
}
req, err := http.NewRequest(method, baseURL+path, reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func checkAuth() {
data, err := doRequest("GET", "/v1/auth/status", nil)
if err != nil {
fmt.Println("Error:", err)
return
}
var auth struct {
Login int `json:"login"`
Tier string `json:"tier"`
Status string `json:"status"`
Permissions []string `json:"permissions"`
MarginMode string `json:"margin_mode"`
}
json.Unmarshal(data, &auth)
fmt.Printf("Login: %d, Status: %s, Mode: %s\n",
auth.Login, auth.Status, auth.MarginMode)
}
func getAccount() {
data, err := doRequest("GET", "/v1/account", nil)
if err != nil {
fmt.Println("Error:", err)
return
}
var account struct {
Login int `json:"login"`
Balance float64 `json:"balance"`
Equity float64 `json:"equity"`
Margin float64 `json:"margin"`
MarginFree float64 `json:"margin_free"`
Profit float64 `json:"profit"`
Leverage int `json:"leverage"`
MarginMode string `json:"margin_mode"`
}
json.Unmarshal(data, &account)
fmt.Printf("Balance: %.2f, Equity: %.2f, Free Margin: %.2f\n",
account.Balance, account.Equity, account.MarginFree)
}
func openTrade() {
trade := map[string]any{
"symbol": "EURUSD",
"side": "buy",
"volume": 0.1,
"sl": 1.08000,
"tp": 1.09500,
}
data, err := doRequest("POST", "/v1/trade", trade)
if err != nil {
fmt.Println("Error:", err)
return
}
var result struct {
Status string `json:"status"`
Retcode int `json:"retcode"`
Deal int64 `json:"deal"`
Order int64 `json:"order"`
Price float64 `json:"price"`
Volume float64 `json:"volume"`
}
json.Unmarshal(data, &result)
if result.Retcode == 10008 || result.Retcode == 10009 {
fmt.Printf("Trade opened: deal=%d, price=%.5f\n", result.Deal, result.Price)
} else {
fmt.Printf("Trade failed: retcode=%d\n", result.Retcode)
}
}
func getPositions() {
data, err := doRequest("GET", "/v1/positions", nil)
if err != nil {
fmt.Println("Error:", err)
return
}
var positions []struct {
Ticket int64 `json:"ticket"`
Symbol string `json:"symbol"`
Side string `json:"side"`
Volume float64 `json:"volume"`
PriceOpen float64 `json:"price_open"`
PriceCurrent float64 `json:"price_current"`
Profit float64 `json:"profit"`
}
json.Unmarshal(data, &positions)
for _, pos := range positions {
fmt.Printf("#%d %s %s %.2f lots P&L: %.2f\n",
pos.Ticket, pos.Symbol, pos.Side, pos.Volume, pos.Profit)
}
}
func closePosition(ticket int64) {
body := map[string]any{
"ticket": ticket,
}
data, err := doRequest("POST", "/v1/close", body)
if err != nil {
fmt.Println("Error:", err)
return
}
var result struct {
Status string `json:"status"`
Retcode int `json:"retcode"`
Deal int64 `json:"deal"`
Price float64 `json:"price"`
}
json.Unmarshal(data, &result)
if result.Retcode == 10008 || result.Retcode == 10009 {
fmt.Printf("Closed at %.5f, deal=%d\n", result.Price, result.Deal)
} else {
fmt.Printf("Close failed: retcode=%d\n", result.Retcode)
}
}
func main() {
checkAuth()
getAccount()
getPositions()
// Open a trade
openTrade()
// Close position by ticket
// closePosition(12345678)
}