David's Blog

Interfacing with the Kraken API in Go

By David Li on 2023-10-11T12:10:48.000Z

Interfacing with the Kraken API in Go

In this article, we’ll explore how to use the Kraken cryptocurrency exchange API with Go (Golang). We will create a simple application that retrieves account balance, places an order, and checks the status of an open order.

Prerequisites

Before getting started, ensure you have the following:

  1. Go installed (version 1.16 or later)
  2. A Kraken account with API keys generated (API Key and Private Key)

Kraken API Overview

The Kraken API allows you to access various features of the platform, including public market data, user-specific data, and trading operations. The API is built on the HTTP protocol and supports RESTful requests.

API documentation can be found here.

Setting Up the Project

Create a new directory for the project, and initialize it as a Go module:

$ mkdir kraken-go
$ cd kraken-go
$ go mod init github.com/yourusername/kraken-go

Installing Required Libraries

We will use the go-kraken library to connect to the Kraken API. This library provides a convenient wrapper around the API and simplifies request and response handling. To install it, run:

$ go get -u github.com/aopoltorzhicky/go_kraken

Getting Account Balance

First, let’s create a function to retrieve the account balance using the Kraken API. Create a new file called `main. and add the following code:

package main

import (
	"fmt"
	"log"

	krakenapi "github.com/aopoltorzhicky/go_kraken"
)

func main() {
	apiKey := "YOUR_API_KEY"
	apiSecret := "YOUR_API_SECRET"

	client := krakenapi.New(apiKey, apiSecret)

	balance, err := client.Balance()
	if err != nil {
		log.Fatalf("Error retrieving balance: %v", err)
	}

	fmt.Println("Account balance:")
	for currency, amount := range balance {
		fmt.Printf("%s: %s\n", currency, amount)
	}
}

Replace YOUR_API_KEY and YOUR_API_SECRET with your Kraken API key and secret, respectively.

Run the code with `go run main.. The output should display your account balance.

Placing an Order

Let’s extend the code to place an order. In this example, we will place a limit order to buy 0.01 BTC using USD at a specified price. Add the following function to `main.:

func placeOrder(client *krakenapi.KrakenApi, pair string, price, volume float64) (string, error) {
	orderInfo, err := client.AddOrder(pair, "buy", "limit", fmt.Sprintf("%.8f", price), map[string]string{
		"volume": fmt.Sprintf("%.8f", volume),
	})
	if err != nil {
		return "", err
	}

	return orderInfo.TransactionIds[0], nil
}

Next, modify the main function to place an order:

func main() {
	// ... (previous code)

	orderID, err := placeOrder(client, "XBTUSD", 30000, 0.01)
	if err != nil {
		log.Fatalf("Error placing order: %v", err)
	}

	fmt.Printf("Order placed successfully: %s\n", orderID)
}

Run the code again with `go run main.. The output should display your account balance and the placed order’s transaction ID.

Note: Ensure that the specified price and volume are appropriate for your account balance and the current market conditions.

Checking Order Status

Finally, let’s add a function to check the status of an open order. Add the following function to `main.:

func getOrderStatus(client *krakenapi.KrakenApi, orderID string) (string, error) {
	orderInfo, err := client.QueryOrdersInfo(orderID)
	if err != nil {
		return "", err
	}

	order := orderInfo[orderID]
	return order.Status, nil
}

Modify the main function to check the order status:

func main() {
	// ... (previous code)

	status, err := getOrderStatus(client, orderID)
	if err != nil {
		log.Fatalf("Error retrieving order status: %v", err)
	}

	fmt.Printf("Order %s status: %s\n", orderID, status)
}

Run the code again with `go run main.. The output should display your account balance, the placed order’s transaction ID, and its current status.

Conclusion

In this article, we explored how to use the Kraken API with Go toretrieve account balance, place an order, and check the status of an open order. By leveraging the go-kraken library, we were able to simplify the process of interacting with the Kraken API. This example can be extended further to include more advanced trading strategies, portfolio management, and automated trading bots.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-15.