# KrakenR

## 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
--- | --- | --- | --- | --- | --- | --- | ---
1INCH | currency | 1INCH | 10 | 5 | enabled | 1INCH | NA
AAVE | currency | AAVE | 10 | 5 | enabled | AAVE | NA
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.334025 | 11048.20 | 2024-10-21 23:06:16 | Bid | NA | NA | NA
0.334033 | 13820.01 | 2024-10-21 23:06:42 | Bid | NA | NA | NA
0.334050 | 50554.76 | 2024-10-21 23:06:46 | Bid | NA | NA | NA
NA | NA | NA | Ask | 0.334051 | 83313.603 | 2024-10-21 23:05:49
NA | NA | NA | Ask | 0.334321 | 9679.675 | 2024-10-21 23:06:48
NA | NA | NA | Ask | 0.334338 | 362.802 | 2024-10-21 23:06:31

### 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-06-24 02:00:00 | 0.356047 | 0.356286 | 0.351564 | 0.355843 | 0.354318 | 104656.8 | 83
2024-06-24 06:00:00 | 0.354037 | 0.354386 | 0.345861 | 0.348543 | 0.348949 | 370810.1 | 1082
2024-06-24 10:00:00 | 0.348606 | 0.352655 | 0.337538 | 0.346832 | 0.345331 | 918418.7 | 777
2024-06-24 14:00:00 | 0.346943 | 0.353498 | 0.345000 | 0.345000 | 0.348788 | 248185.1 | 287
2024-06-24 18:00:00 | 0.344991 | 0.347858 | 0.341000 | 0.342916 | 0.343552 | 846399.9 | 413
2024-06-24 22:00:00 | 0.343516 | 0.352510 | 0.338250 | 0.352000 | 0.347171 | 650738.7 | 375

### 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-10-21 23:00:44 | 0.333928 | 0.334044
2024-10-21 23:00:44 | 0.333930 | 0.334044
2024-10-21 23:00:44 | 0.333929 | 0.334044
2024-10-21 23:00:44 | 0.333929 | 0.334044
2024-10-21 23:00:44 | 0.333929 | 0.334044
2024-10-21 23:00:45 | 0.333932 | 0.334044

### 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.259
Ask_WholeLotVolume | 50975
Ask_LotVolume | 50975
Bid_Price | 0.258
Bid_WholeLotVolume | 9720
Bid_LotVolume | 9720
LastTrade_Price | 0.258
LastTrade_LotVolume | 30.05704304
Volume_Today | 47390.92125152
Volume_24h | 47622.86870632
VWAP_Today | 0.2574
VWAP_24h | 0.25745
Trades_Today | 64
Trades_24h | 70
Low_Today | 0.254
Low_24h | 0.254
High_Today | 0.266
High_24h | 0.268
Open_Price | 0.265

### 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.334051 | 1423.900 | 2024-10-21 23:05:49 | buy | limit |  | 11285860
0.334051 | 3110.000 | 2024-10-21 23:05:44 | buy | limit |  | 11285859
0.334051 | 6110.000 | 2024-10-21 23:05:44 | buy | limit |  | 11285858
0.334051 | 1820.000 | 2024-10-21 23:05:44 | buy | limit |  | 11285857
0.334051 | 1880.000 | 2024-10-21 23:05:44 | buy | limit |  | 11285856
0.334051 | 1763.697 | 2024-10-21 23:05:44 | buy | limit |  | 11285855

### 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: 2024-10-21T21:06:52Z"

### 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: 2024-10-21 21:06:53"