Skip to contents

Introduction

The KrakenR package provides a user-friendly interface to the Kraken cryptocurrency exchange’s REST API. It allows R users to access real-time and historical market data, asset information, and exchange metrics, making it a valuable tool for financial analysts, traders, and researchers interested in the cryptocurrency markets.

This vignette will introduce key functionalities of the package, showcasing how to use the API functions to retrieve market data and more.

Installation

You can install KrakenR directly from GitHub:

# Install the development version from GitHub
devtools::install_github("nathanael-g-durst/KrakenR")
# Load the package
library(KrakenR)

Overview of Available Functions

The package includes several key functions that allow users to fetch various types of data from the Kraken exchange:

  • getAssets() - Retrieve detailed asset information.
  • getOB() - Fetch order book data for a specific trading pair.
  • getOHLC() - Access OHLC (Open, High, Low, Close) data for a trading pair.
  • getPairs() - Retrieve tradable asset pair information.
  • getSpreads() - Fetch recent spread data.
  • getStatus() - Get the current system status of the Kraken exchange.
  • getTickers() - Retrieve detailed ticker information for trading pairs.
  • getTime() - Fetch the current Kraken server time.
  • getTrades() - Retrieve recent trade data for a trading pair.

Example Workflow

This section provides a detailed walkthrough using KrakenR to access Kraken market data.

1. Fetching Asset Information

To retrieve detailed information about all available assets or specific ones:

# Fetch all available assets
assets_all <- getAssets()

# Fetch data for specific assets
assets_specific <- getAssets(c("BTC", "ETH", "ADA"))

The getAssets() function returns a data frame containing asset information, such as the asset class, decimals, and status.

AssetID aclass altname decimals display_decimals status Asset collateral_value margin_rate
1INCH currency 1INCH 10 5 enabled 1INCH NA NULL
AAVE currency AAVE 10 5 enabled AAVE 0.8 0.02
ACA currency ACA 10 5 enabled ACA NA NULL
ACH currency ACH 10 5 enabled ACH NA NULL
ACT currency ACT 5 3 enabled ACT NA 0
ACX currency ACX 5 3 enabled ACX NA 0

2. Retrieving Order Book Data

You can use getOB() to fetch order book data for a specific trading pair:

# Fetch order book data for ADAEUR pair
order_book <- getOB("ADAEUR")

# Fetch order book data with a limit on the number of orders
order_book_limited <- getOB("ADAEUR", count = 3)

The output includes bid and ask orders, sorted by price, and can be used for market analysis.

Bid_Price Bid_Volume Bid_Timestamp Order_Type Ask_Price Ask_Volume Ask_Timestamp
0.649820 3160.675 2025-03-13 16:11:01 Bid NA NA NA
0.649895 1417.114 2025-03-13 16:11:19 Bid NA NA NA
0.649947 2125.502 2025-03-13 16:11:19 Bid NA NA NA
NA NA NA Ask 0.650524 11137.58 2025-03-13 16:11:19
NA NA NA Ask 0.650525 7108.34 2025-03-13 16:11:19
NA NA NA Ask 0.650529 19234.95 2025-03-13 16:11:19

3. Fetching OHLC Data

The getOHLC() function allows you to retrieve OHLC (Open, High, Low, Close) data for a given trading pair at various time intervals:

# Fetch 1-minute interval OHLC data for ADAEUR
ohlc_data <- getOHLC("ADAEUR", interval = 1)

# Fetch 4-hour interval data
ohlc_data_4h <- getOHLC("ADAEUR", interval = 240)

This function is useful for technical analysis and charting.

Time Open High Low Close VWAP Volume Count
2024-11-13 20:00:00 0.537894 0.549370 0.524987 0.547344 0.542094 1327741.5 1157
2024-11-14 00:00:00 0.547027 0.550286 0.534000 0.542110 0.541763 681605.0 476
2024-11-14 04:00:00 0.539549 0.564900 0.536665 0.550754 0.547604 998557.9 1025
2024-11-14 08:00:00 0.550864 0.552999 0.530000 0.539740 0.544928 894591.4 941
2024-11-14 12:00:00 0.539685 0.545000 0.513476 0.519291 0.531911 1762476.3 1378
2024-11-14 16:00:00 0.519166 0.535814 0.517227 0.535724 0.527327 821872.1 824

4. Getting Asset Pair Information

To retrieve tradable asset pairs and their details:

# Fetch all available asset pairs
pairs_all <- getPairs()

# Fetch information for a specific pair
pair_info <- getPairs(c("ADAEUR", "BTCUSD"))

You can also filter by specific details such as leverage, fees, or margin.

Column Example
PairID 1INCHEUR
altname 1INCHEUR
wsname 1INCH/EUR
aclass_base currency
base 1INCH
aclass_quote currency
quote ZEUR
lot unit
cost_decimals 5
pair_decimals 3
lot_decimals 8
lot_multiplier 1
leverage_buy list(leverage_buy = list())
leverage_sell list(leverage_sell = list())
fees list(fees = c(0, 10000, 50000, 1e+05, 250000, 5e+05, 1e+06, 2500000, 5e+06, 1e+07, 0.4, 0.35, 0.24, 0.22, 0.2, 0.18, 0.16, 0.14, 0.12, 0.1))
fees_maker list(fees_maker = c(0, 10000, 50000, 1e+05, 250000, 5e+05, 1e+06, 2500000, 5e+06, 1e+07, 0.25, 0.2, 0.14, 0.12, 0.1, 0.08, 0.06, 0.04, 0.02, 0))
fee_volume_currency ZUSD
margin_call 80
margin_stop 40
ordermin 11
costmin 0.45
tick_size 0.001
status online
Pair 1INCHEUR
long_position_limit list(NA)
short_position_limit list(NA)

5. Fetching Spread Data

To get recent spread data for a trading pair:

# Fetch spread data for ADAEUR
spread_data <- getSpreads("ADAEUR")

The spread data provides insight into the bid-ask spread over time, which is useful for liquidity analysis.

Time Bid Ask
2025-03-13 16:11:01 0.650253 0.650704
2025-03-13 16:11:01 0.650253 0.650703
2025-03-13 16:11:01 0.650235 0.650703
2025-03-13 16:11:01 0.650235 0.650702
2025-03-13 16:11:02 0.650230 0.650702
2025-03-13 16:11:02 0.650230 0.650701

6. Getting Ticker Information

To fetch real-time ticker information for trading pairs:

# Fetch ticker information for all pairs
tickers_all <- getTickers()

# Fetch ticker information for specific pairs
tickers_specific <- getTickers(c("ADAEUR", "BTCUSD"))

This function provides real-time price, volume, and trading information.

Column Example
PairID 1INCHEUR
Pair 1INCHEUR
Ask_Price 0.18
Ask_WholeLotVolume 11789
Ask_LotVolume 11789
Bid_Price 0.179
Bid_WholeLotVolume 31302
Bid_LotVolume 31302
LastTrade_Price 0.181
LastTrade_LotVolume 27.87267981
Volume_Today 10337.14032738
Volume_24h 11809.71223667
VWAP_Today 0.17995
VWAP_24h 0.17996
Trades_Today 12
Trades_24h 15
Low_Today 0.178
Low_24h 0.178
High_Today 0.184
High_24h 0.184
Open_Price 0.18

7. Fetching Recent Trade Data

To retrieve recent trade data for a trading pair:

# Fetch recent trades for ADAEUR
recent_trades <- getTrades("ADAEUR")

# Fetch trades since a specific timestamp
recent_trades_since <- getTrades("ADAEUR", since = "2024-10-01 12:00:00")
Price Volume Time Order_Type Execution_Type Miscellaneous Trade_ID
0.651849 61.02000 2025-03-13 16:09:50 buy limit 12274996
0.651866 61.02000 2025-03-13 16:09:48 buy limit 12274995
0.651834 84.46776 2025-03-13 16:08:51 buy limit 12274994
0.651531 30.69796 2025-03-13 16:07:51 buy market 12274993
0.651509 20.06968 2025-03-13 16:07:31 buy market 12274992
0.650714 661.71748 2025-03-13 16:07:07 sell limit 12274978

8. Retrieving System Status

To check the operational status of the Kraken exchange:

# Fetch both system status and timestamp
status_info <- getStatus()

# Fetch only system status
system_status <- getStatus("status")
#> [1] "System Status: online"               "System Status: 2025-03-13T16:11:26Z"

9. Retrieving Server Time

You can check the current server time in UNIX or RFC formats:

# Fetch UNIX time
server_time_unix <- getTime("unix")

# Fetch RFC 1123 time
server_time_rfc <- getTime("rfc")
#> [1] "UNIX Time: 2025-03-13 16:11:27"