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")
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 |
---|---|---|---|---|---|---|---|
1INCH | currency | 1INCH | 10 | 5 | enabled | 1INCH | NA |
AAVE | currency | AAVE | 10 | 5 | enabled | AAVE | 0.8 |
ACA | currency | ACA | 10 | 5 | enabled | ACA | NA |
ACH | currency | ACH | 10 | 5 | enabled | ACH | NA |
ADA | currency | ADA | 8 | 6 | enabled | ADA | 0.9 |
ADA.S | currency | ADA.S | 8 | 6 | enabled | ADA.S | NA |
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.697173 | 12.251 | 2024-11-18 23:23:52 | Bid | NA | NA | NA |
0.697184 | 970.000 | 2024-11-18 23:24:14 | Bid | NA | NA | NA |
0.697185 | 49.347 | 2024-11-18 23:24:14 | Bid | NA | NA | NA |
NA | NA | NA | Ask | 0.697566 | 544.812 | 2024-11-18 23:24:14 |
NA | NA | NA | Ask | 0.697567 | 300.735 | 2024-11-18 23:23:44 |
NA | NA | NA | Ask | 0.697597 | 970.000 | 2024-11-18 23:24:14 |
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-07-22 00:00:00 | 0.410000 | 0.412912 | 0.402658 | 0.402658 | 0.407833 | 89500.19 | 96 |
2024-07-22 04:00:00 | 0.403218 | 0.403802 | 0.394673 | 0.398754 | 0.398090 | 145027.95 | 365 |
2024-07-22 08:00:00 | 0.398749 | 0.402853 | 0.396017 | 0.402433 | 0.400360 | 180837.85 | 245 |
2024-07-22 12:00:00 | 0.401985 | 0.403048 | 0.394219 | 0.395215 | 0.398420 | 181719.59 | 289 |
2024-07-22 16:00:00 | 0.394415 | 0.397723 | 0.393181 | 0.397581 | 0.394209 | 226646.02 | 307 |
2024-07-22 20:00:00 | 0.398169 | 0.398169 | 0.389424 | 0.392028 | 0.395443 | 153254.60 | 179 |
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 |
---|---|---|
2024-11-18 23:23:43 | 0.697193 | 0.697540 |
2024-11-18 23:23:43 | 0.697194 | 0.697540 |
2024-11-18 23:23:43 | 0.697194 | 0.697586 |
2024-11-18 23:23:43 | 0.697194 | 0.697568 |
2024-11-18 23:23:43 | 0.697195 | 0.697568 |
2024-11-18 23:23:43 | 0.697196 | 0.697568 |
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.313 |
Ask_WholeLotVolume | 41388 |
Ask_LotVolume | 41388 |
Bid_Price | 0.311 |
Bid_WholeLotVolume | 42711 |
Bid_LotVolume | 42711 |
LastTrade_Price | 0.315 |
LastTrade_LotVolume | 84.68257688 |
Volume_Today | 42295.00571728 |
Volume_24h | 42295.00571728 |
VWAP_Today | 0.30272 |
VWAP_24h | 0.30272 |
Trades_Today | 84 |
Trades_24h | 84 |
Low_Today | 0.283 |
Low_24h | 0.283 |
High_Today | 0.316 |
High_24h | 0.316 |
Open_Price | 0.283 |
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.698385 | 78.50937 | 2024-11-18 23:23:34 | sell | limit | 11449370 | |
0.698386 | 2215.55598 | 2024-11-18 23:23:16 | buy | market | 11449369 | |
0.699150 | 27.50460 | 2024-11-18 23:22:52 | buy | limit | 11449368 | |
0.698790 | 45.07924 | 2024-11-18 23:22:51 | sell | market | 11449365 | |
0.698789 | 33.79806 | 2024-11-18 23:22:51 | sell | market | 11449366 | |
0.698789 | 4308.86496 | 2024-11-18 23:22:51 | sell | market | 11449367 |