About Tronix

Tronix (TRX, a.k.a. Tron) is the native currency of the TRON blockchain. The TRX token is based on the ERC-20 Etherium Standard and is fully compatible with it. Although the original purpose of TRX was to enable payments for digital entertainment, nowadays it has gained many other use cases that power transactions on the TRON blockchain and build up its economy (mainly in the gaming and decentralised finance sectors).

One can purchase TRX or swap it for other cryptocurrencies on numerous exchanges (e.g., Binance, Huobi, Bittrex, etc.). In February 2021, TRX’s global rank among cryptocurrencies was around 25, with a total market cap exceeding $4B.

TRX market data offered by tronr

The package offers several functions that one can use to query TRX market data provided by the public CoinGecko API. At a high level, the following data can be retrieved:

  • a set of metrics characterising the current status of TRX on the cryptocurrency market against user-specified base currencies (e.g., USD, EUR, BTC, etc.; a list of base currencies currently supported by the CoinGecko service can be obtained with the get_supported_coingecko_currencies() function);
  • TRX market metrics for an arbitrary date in the past or a range of historical dates;
  • TRX market metrics for a range of recent historical dates (e.g., last 7 days).

All of these functions return tibbles that are directly suitable for any downstream analyses using R. Provided below are a few examples of querying the TRX market data.

Please note that the CoinGecko API has a limit of 100 calls per minute from a given IP address. Keep this in mind when developing your applications based on tronr.

Querying the current market status

The current price of TRX against a set of base currencies can be obtained using the get_current_trx_price() function. The list of base currencies is specified with the vs_currencies argument, e.g.:

library(tronr)
#> R toolbox to explore the TRON blockchain
#> Developed by Next Game Solutions (http://nextgamesolutions.com)
library(tibble)

get_current_trx_price(vs_currencies = c("usd", "eur", "gbp"))
#> # A tibble: 3 x 3
#>   trx_price vs_currency last_updated_at    
#>       <dbl> <chr>       <dttm>             
#> 1    0.0603 usd         2021-07-12 22:04:47
#> 2    0.0509 eur         2021-07-12 22:04:47
#> 3    0.0434 gbp         2021-07-12 22:04:47

The last_updated_at column shows when the price values have been updated last time (using the UTC timezone, here and in all other results returned by the tronr functions).

The get_current_trx_price() function can also be used to retrieve data on the current market cap, trading volume over the last 24 hours, and price percentage change compared to 24 hours ago. This can be done by turning on the include_market_cap, include_24h_vol, and include_24h_change options, respectively:

get_current_trx_price(vs_currencies = c("usd", "eur", "gbp"),
                      include_market_cap = TRUE,
                      include_24h_vol = TRUE,
                      include_24h_change = TRUE)
#> # A tibble: 3 x 6
#>   trx_price vs_currency  market_cap vol_24h price_percent_c… last_updated_at    
#>       <dbl> <chr>             <dbl>   <dbl>            <dbl> <dttm>             
#> 1    0.0603 usd         4323975921.  8.05e8            -4.68 2021-07-12 22:04:47
#> 2    0.0509 eur         3645293309.  6.78e8            -4.56 2021-07-12 22:04:47
#> 3    0.0434 gbp         3113729653.  5.79e8            -4.62 2021-07-12 22:04:47

An even more comprehensive set of metrics characterising the current status of TRX on the cryptocurrency market can be obtained with the get_current_trx_market_data() function:

cur_market <- get_current_trx_market_data(vs_currencies = c("usd", "eur"))
glimpse(cur_market)
#> Rows: 2
#> Columns: 34
#> $ last_updated_at                  <dttm> 2021-07-12 22:04:05, 2021-07-12 22:0…
#> $ total_supply                     <dbl> 100850743812, 100850743812
#> $ circulating_supply               <dbl> 71660220128, 71660220128
#> $ vs_currency                      <chr> "usd", "eur"
#> $ market_cap                       <dbl> 4323975921, 3645293309
#> $ market_cap_rank                  <int> 25, 25
#> $ market_cap_change_24h            <dbl> -183088612, -150209398
#> $ market_cap_percentage_change_24h <dbl> -4.06226, -3.95756
#> $ total_trading_vol_24h            <dbl> 804691900, 678359295
#> $ current_price                    <dbl> 0.060334, 0.050862
#> $ price_high_24h                   <dbl> 0.063296, 0.053296
#> $ price_low_24h                    <dbl> 0.059608, 0.050262
#> $ price_change_24h                 <dbl> -0.002861234, -0.002350672
#> $ price_percentage_change_24h      <dbl> -4.52762, -4.41752
#> $ price_percentage_change_7d       <dbl> -10.57446, -10.60331
#> $ price_percentage_change_14d      <dbl> -7.10935, -6.48760
#> $ price_percentage_change_30d      <dbl> -12.91022, -11.10287
#> $ price_percentage_change_60d      <dbl> -51.43301, -50.55253
#> $ price_percentage_change_200d     <dbl> 141.3054, 147.9403
#> $ price_percentage_change_1y       <dbl> 228.6859, 213.1047
#> $ ath                              <dbl> 0.231673, 0.192595
#> $ ath_change_percentage            <dbl> -73.96018, -73.59314
#> $ ath_date                         <dttm> 2018-01-05, 2018-01-05
#> $ atl                              <dbl> 0.00180434, 0.00154713
#> $ atl_change_percentage            <dbl> 3243.445, 3187.279
#> $ atl_date                         <dttm> 2017-11-12, 2017-11-12
#> $ coingecko_rank                   <int> 6, 6
#> $ coingecko_score                  <dbl> 64.699, 64.699
#> $ developer_score                  <dbl> 89.218, 89.218
#> $ community_score                  <dbl> 48.559, 48.559
#> $ liquidity_score                  <dbl> 76.67, 76.67
#> $ public_interest_score            <dbl> 0.091, 0.091
#> $ sentiment_votes_up_percentage    <dbl> 74.59, 74.59
#> $ sentiment_votes_down_percentage  <dbl> 25.41, 25.41

Most of the variables in the above table should be self-explanatory. Additional details can be found at the “Methodology” page on the CoinGecko website.

Querying a specific historical date

TRX market data for a specific historical date can be obtained with the get_trx_market_data_for_date() function. The date argumet of this function accepts character, Date, or POSIXct values in the %Y-%m-%d format, e.g.:

# `date` as a character value:
df1 <- get_trx_market_data_for_date(date = "2021-01-01")

# `date` as a Date value:
df2 <- get_trx_market_data_for_date(date = as.Date("2021-01-01"))

identical(df1, df2)
#> [1] TRUE

df1
#> # A tibble: 2 x 5
#>   date       vs_currency  market_cap total_trading_vol  price
#>   <date>     <chr>             <dbl>             <dbl>  <dbl>
#> 1 2021-01-01 usd         1918734259.       1231160613. 0.0268
#> 2 2021-01-01 eur         1570737264.       1007867475. 0.0220

The history of TRX market data offered by CoinGecko starts on 2017-11-09 00:00:00. Attempts to retrieve data for earlier dates will fail with the corresponding console message:

get_trx_market_data_for_date(date = "2017-11-08")
#> Error: No data are available for dates before 2017-11-09. Check the `date` argument

Attempts to request a future date, for which no history exists yet, will fail as well:

get_trx_market_data_for_date(date = "2090-01-01")
#> Error: Cannot retrieve data for a future `date`. Check the `date` argument

Querying a range of historical dates

One can also retrieve TRX market data for a range of historical dates. This can be done using the get_trx_market_data_for_time_range() function. In contrast to get_trx_market_data_for_date(), this function only accepts a single base currency per query (argument vs_currency). In addition, timestamps that define the start (min_timestamp) and the end (max_timestamp) of historical range are to be character values in the Unix time format.

Granularity of the data returned by get_trx_market_data_for_time_range() depends on the requested range. Hourly data will be retrieved for periods of up to 90 days, and daily data for periods above 90 days:

# Range of less than 1 day:
(min_timestamp <- as.POSIXct("2020-01-01 10:00:10") %>% to_unix_timestamp())
#> [1] "1577872810000"
(max_timestamp = as.POSIXct("2020-01-01 20:45:10") %>% to_unix_timestamp())
#> [1] "1577911510000"

get_trx_market_data_for_time_range(
  vs_currency = "usd",
  min_timestamp = min_timestamp,
  max_timestamp = max_timestamp
) 
#> # A tibble: 11 x 5
#>    timestamp           vs_currency  price total_trading_vol market_cap
#>    <dttm>              <chr>        <dbl>             <dbl>      <dbl>
#>  1 2020-01-01 10:09:54 usd         0.0132       1102088570. 874524530.
#>  2 2020-01-01 11:05:39 usd         0.0133       1099017083. 879244117.
#>  3 2020-01-01 12:09:46 usd         0.0132       1087184132. 872880577.
#>  4 2020-01-01 13:09:29 usd         0.0133       1096215627. 878736567.
#>  5 2020-01-01 14:09:44 usd         0.0132       1074667283. 876375501.
#>  6 2020-01-01 15:09:52 usd         0.0133       1049571116. 879150841.
#>  7 2020-01-01 16:09:50 usd         0.0132       1031092825. 874377735.
#>  8 2020-01-01 17:09:34 usd         0.0133       1044756293. 878762444.
#>  9 2020-01-01 18:09:53 usd         0.0133       1040631289. 880559705.
#> 10 2020-01-01 19:09:42 usd         0.0133       1030222943. 881087716.
#> 11 2020-01-01 20:12:32 usd         0.0133       1022783881. 879145359.

# Range of >90 days:
(min_timestamp <- as.POSIXct("2020-01-01 00:00:00") %>% to_unix_timestamp())
#> [1] "1577836800000"
(max_timestamp = as.POSIXct("2020-05-01 00:00:00") %>% to_unix_timestamp())
#> [1] "1588291200000"

get_trx_market_data_for_time_range(
  vs_currency = "usd",
  min_timestamp = min_timestamp,
  max_timestamp = max_timestamp
)
#> # A tibble: 122 x 5
#>    timestamp           vs_currency  price total_trading_vol market_cap
#>    <dttm>              <chr>        <dbl>             <dbl>      <dbl>
#>  1 2020-01-01 00:00:00 usd         0.0133       1134528759. 877119319.
#>  2 2020-01-02 00:00:00 usd         0.0132       1032624901. 872350811.
#>  3 2020-01-03 00:00:00 usd         0.0128       1056549454. 848482045.
#>  4 2020-01-04 00:00:00 usd         0.0134       1168793811. 885589788.
#>  5 2020-01-05 00:00:00 usd         0.0134       1033957595. 888326322.
#>  6 2020-01-06 00:00:00 usd         0.0135       1138776203. 893369823.
#>  7 2020-01-07 00:00:00 usd         0.0145       1286051294. 955193949.
#>  8 2020-01-08 00:00:00 usd         0.0144       1232245488. 950130927.
#>  9 2020-01-09 00:00:00 usd         0.0140       1149165537. 923243641.
#> 10 2020-01-10 00:00:00 usd         0.0140       1029207693. 924240156.
#> # … with 112 more rows

Similar to get_trx_market_data_for_date(), get_trx_market_data_for_time_range() will fail if min_timestamp and/or max_timestamp define a time range for which no data exist:

# Requesting non-existing data from the past:
min_timestamp <- as.POSIXct("2017-11-08 00:00:00") %>% to_unix_timestamp()
max_timestamp = as.POSIXct("2020-01-01 00:00:00") %>% to_unix_timestamp()

get_trx_market_data_for_time_range(
  vs_currency = "usd",
  min_timestamp = min_timestamp,
  max_timestamp = max_timestamp
)
#> Error: No data are available for dates before 2017-11-09. Check the `min_timestamp` argument

# Requesting non-existing data from the future:
min_timestamp <- as.POSIXct("2021-01-01 00:00:00") %>% to_unix_timestamp()
max_timestamp = as.POSIXct("2022-01-01 00:00:00") %>% to_unix_timestamp()

get_trx_market_data_for_time_range(
  vs_currency = "usd",
  min_timestamp = min_timestamp,
  max_timestamp = max_timestamp
)
#> Error: Cannot retrieve data for future dates. Check the `max_timestamp` argument

Querying the last n days

As a variant of querying a range of historical dates, one can use the get_trx_market_data_for_last_n_days() function to retrieve TRX market data for a number of recent dates (as of, and including, the current date):

get_trx_market_data_for_last_n_days(vs_currency = "usd", days = 7)
#> # A tibble: 170 x 5
#>    timestamp           vs_currency  price total_trading_vol  market_cap
#>    <dttm>              <chr>        <dbl>             <dbl>       <dbl>
#>  1 2021-07-05 22:01:00 usd         0.0654       1125285199. 4680797409.
#>  2 2021-07-05 23:02:15 usd         0.0654       1104212328. 4676236818.
#>  3 2021-07-06 00:02:32 usd         0.0649       1066816968. 4646707803.
#>  4 2021-07-06 01:01:55 usd         0.0656       1057772816. 4704812107.
#>  5 2021-07-06 02:01:13 usd         0.0650       1026094705. 4667097163.
#>  6 2021-07-06 03:00:57 usd         0.0650        997388918. 4660854675.
#>  7 2021-07-06 04:02:30 usd         0.0651        990944229. 4667215286.
#>  8 2021-07-06 05:03:17 usd         0.0663       1010848448. 4750421838.
#>  9 2021-07-06 06:01:30 usd         0.0662       1010462956. 4756660866.
#> 10 2021-07-06 07:00:34 usd         0.0662        995403489. 4746487508.
#> # … with 160 more rows

Besides accepting numeric values, the days argument can also accept a character value "max", which will result in retrieving the entire existing history of TRX market data:

hist_max <- get_trx_market_data_for_last_n_days(vs_currency = "usd", days = "max")

hist_max
#> # A tibble: 1,342 x 5
#>    timestamp           vs_currency   price total_trading_vol market_cap
#>    <dttm>              <chr>         <dbl>             <dbl>      <dbl>
#>  1 2017-11-09 00:00:00 usd         0.00239          1224287. 156404162.
#>  2 2017-11-10 00:00:00 usd         0.00204           990423. 133968506.
#>  3 2017-11-11 00:00:00 usd         0.00191           707643. 125470649.
#>  4 2017-11-12 00:00:00 usd         0.00180           814789. 118235246.
#>  5 2017-11-13 00:00:00 usd         0.00202           894986. 132386427.
#>  6 2017-11-14 00:00:00 usd         0.00242          1073924. 158479487.
#>  7 2017-11-15 00:00:00 usd         0.00232          1179822. 152242301.
#>  8 2017-11-16 00:00:00 usd         0.00225          1324833. 147470996.
#>  9 2017-11-17 00:00:00 usd         0.00202          1905625. 132729589.
#> 10 2017-11-18 00:00:00 usd         0.00202          2184964. 132693857.
#> # … with 1,332 more rows

Notice how the data granularity changed in the last two examples. Generally, if days = 1 the data will be presented for approximately every 3-8 minutes. If days is between 2 and 90 (inclusive), an hourly time step will be used. Daily data are used for days above 90. One can use the interval argument to control this granularity (by default, interval = NULL). At the moment the only value it accepts is "daily":

# Within-day data, with `interval = "daily"`:
get_trx_market_data_for_last_n_days(
  vs_currency = "usd",
  days = 1,
  interval = "daily"
)
#> # A tibble: 2 x 5
#>   timestamp           vs_currency  price total_trading_vol  market_cap
#>   <dttm>              <chr>        <dbl>             <dbl>       <dbl>
#> 1 2021-07-12 00:00:00 usd         0.0621        736852906. 4451204211.
#> 2 2021-07-12 22:04:47 usd         0.0603        804691779. 4323975921.

# Less than 90 days, with `interval = "daily"`:
get_trx_market_data_for_last_n_days(
  vs_currency = "usd",
  days = 10,
  interval = "daily"
)
#> # A tibble: 11 x 5
#>    timestamp           vs_currency  price total_trading_vol  market_cap
#>    <dttm>              <chr>        <dbl>             <dbl>       <dbl>
#>  1 2021-07-03 00:00:00 usd         0.0666       1141455725. 4758781824.
#>  2 2021-07-04 00:00:00 usd         0.0669        940066906. 4791306600.
#>  3 2021-07-05 00:00:00 usd         0.0675       1072230609. 4846858583.
#>  4 2021-07-06 00:00:00 usd         0.0649       1066816968. 4646707803.
#>  5 2021-07-07 00:00:00 usd         0.0650        905777528. 4648866288.
#>  6 2021-07-08 00:00:00 usd         0.0644        920669464. 4618022352.
#>  7 2021-07-09 00:00:00 usd         0.0614       1115885436. 4397188049.
#>  8 2021-07-10 00:00:00 usd         0.0626        888427762. 4480289556.
#>  9 2021-07-11 00:00:00 usd         0.0616        794567260. 4416356903.
#> 10 2021-07-12 00:00:00 usd         0.0621        736852906. 4451204211.
#> 11 2021-07-12 21:47:14 usd         0.0602        803682206. 4304384209.

# More than 90 days, with `interval = "daily"`:
get_trx_market_data_for_last_n_days(
  vs_currency = "usd",
  days = 100,
  interval = "daily"
)
#> # A tibble: 101 x 5
#>    timestamp           vs_currency price total_trading_vol  market_cap
#>    <dttm>              <chr>       <dbl>             <dbl>       <dbl>
#>  1 2021-04-04 00:00:00 usd         0.102       8364754397. 7321302884.
#>  2 2021-04-05 00:00:00 usd         0.128      10746448327. 9144431642.
#>  3 2021-04-06 00:00:00 usd         0.138      13018731030. 9985858391.
#>  4 2021-04-07 00:00:00 usd         0.125       9499933826. 8954301783.
#>  5 2021-04-08 00:00:00 usd         0.111       8323619561. 8072075620.
#>  6 2021-04-09 00:00:00 usd         0.124       6225961259. 8877512598.
#>  7 2021-04-10 00:00:00 usd         0.116       3766873571. 8323623723.
#>  8 2021-04-11 00:00:00 usd         0.126       4610974849. 8984059740.
#>  9 2021-04-12 00:00:00 usd         0.122       4159674472. 8760333396.
#> 10 2021-04-13 00:00:00 usd         0.130       6655337315. 9304010446.
#> # … with 91 more rows

If the requested number of days covers dates before 2017-11-09, the retrived data will be clipped at 2017-11-09 (the beginning of history for TRX). Compare the following result to the previously created tibble hist_max:

hist_clipped <- get_trx_market_data_for_last_n_days(
  vs_currency = "usd",
  days = 100000,
  interval = "daily"
)

min(hist_max$timestamp)
#> [1] "2017-11-09 UTC"
min(hist_clipped$timestamp)
#> [1] "2017-11-09 UTC"

Querying the OHLC data for the last n days

The open-high-low-close (OHLC) data characterise within-date and between-date price movements of a financial asset. This type of data for TRX can be retrieved using the get_trx_ohlc_data_for_last_n_days() function, which has the same arguments as get_trx_market_data_for_last_n_days(), except for not having the interval argument. Granularity of the retrieved data (i.e. candle’s body) depends on the value of days as follows:

  • 1 day: 30 minutes
  • 7 - 30 days: 4 hours
  • 31 and above: 4 days

The only values currently accepted by the days argument are 1, 7, 14, 30, 90, 180, 365 and "max". Here are some examples:

# 30-min granularity:
get_trx_ohlc_data_for_last_n_days(vs_currency = "usd", days = 1)
#> # A tibble: 49 x 6
#>    timestamp           vs_currency price_open price_high price_low price_close
#>    <dttm>              <chr>            <dbl>      <dbl>     <dbl>       <dbl>
#>  1 2021-07-11 22:30:00 usd             0.0633     0.0633    0.0631      0.0631
#>  2 2021-07-11 23:00:00 usd             0.0630     0.0630    0.0621      0.0621
#>  3 2021-07-11 23:30:00 usd             0.0622     0.0622    0.0621      0.0621
#>  4 2021-07-12 00:00:00 usd             0.0620     0.0621    0.0620      0.0621
#>  5 2021-07-12 00:30:00 usd             0.0621     0.0621    0.0618      0.0619
#>  6 2021-07-12 01:00:00 usd             0.0620     0.0622    0.0620      0.0621
#>  7 2021-07-12 01:30:00 usd             0.0620     0.0620    0.0619      0.0619
#>  8 2021-07-12 02:00:00 usd             0.0620     0.0621    0.0620      0.0621
#>  9 2021-07-12 02:30:00 usd             0.0622     0.0622    0.0621      0.0622
#> 10 2021-07-12 03:00:00 usd             0.0625     0.0625    0.0624      0.0624
#> # … with 39 more rows

# 4-hours granularity:
get_trx_ohlc_data_for_last_n_days(vs_currency = "usd", days = 7)
#> # A tibble: 43 x 6
#>    timestamp           vs_currency price_open price_high price_low price_close
#>    <dttm>              <chr>            <dbl>      <dbl>     <dbl>       <dbl>
#>  1 2021-07-06 00:00:00 usd             0.0654     0.0654    0.0654      0.0654
#>  2 2021-07-06 04:00:00 usd             0.0649     0.0656    0.0649      0.065 
#>  3 2021-07-06 08:00:00 usd             0.0651     0.0663    0.0651      0.0662
#>  4 2021-07-06 12:00:00 usd             0.0664     0.0664    0.0649      0.0649
#>  5 2021-07-06 16:00:00 usd             0.0656     0.0658    0.0648      0.0648
#>  6 2021-07-06 20:00:00 usd             0.0649     0.0649    0.0646      0.0648
#>  7 2021-07-07 00:00:00 usd             0.0648     0.0648    0.0642      0.0647
#>  8 2021-07-07 04:00:00 usd             0.0650     0.0651    0.0648      0.0650
#>  9 2021-07-07 08:00:00 usd             0.0654     0.0657    0.0654      0.0656
#> 10 2021-07-07 12:00:00 usd             0.0656     0.0658    0.0656      0.0657
#> # … with 33 more rows

# 4-days granularity:
get_trx_ohlc_data_for_last_n_days(vs_currency = "usd", days = 90)
#> # A tibble: 25 x 6
#>    timestamp           vs_currency price_open price_high price_low price_close
#>    <dttm>              <chr>            <dbl>      <dbl>     <dbl>       <dbl>
#>  1 2021-04-15 00:00:00 usd              0.147      0.147     0.141       0.141
#>  2 2021-04-19 00:00:00 usd              0.166      0.166     0.144       0.144
#>  3 2021-04-23 00:00:00 usd              0.132      0.134     0.111       0.111
#>  4 2021-04-27 00:00:00 usd              0.109      0.117     0.102       0.117
#>  5 2021-04-30 00:00:00 usd              0.125      0.125     0.121       0.121
#>  6 2021-05-03 00:00:00 usd              0.133      0.133     0.128       0.128
#>  7 2021-05-07 00:00:00 usd              0.132      0.153     0.120       0.153
#>  8 2021-05-11 00:00:00 usd              0.149      0.149     0.129       0.129
#>  9 2021-05-15 00:00:00 usd              0.139      0.139     0.122       0.125
#> 10 2021-05-19 00:00:00 usd              0.121      0.121     0.113       0.117
#> # … with 15 more rows

# 4-days granularity:
get_trx_ohlc_data_for_last_n_days(vs_currency = "usd", days = "max")
#> # A tibble: 351 x 6
#>    timestamp           vs_currency price_open price_high price_low price_close
#>    <dttm>              <chr>            <dbl>      <dbl>     <dbl>       <dbl>
#>  1 2017-11-11 00:00:00 usd            0.00239    0.00239   0.00192     0.00192
#>  2 2017-11-15 00:00:00 usd            0.00180    0.00242   0.00180     0.00232
#>  3 2017-11-19 00:00:00 usd            0.00225    0.00225   0.00198     0.00198
#>  4 2017-11-23 00:00:00 usd            0.00213    0.00233   0.00213     0.00214
#>  5 2017-11-27 00:00:00 usd            0.00207    0.00211   0.00202     0.00209
#>  6 2017-11-30 00:00:00 usd            0.00239    0.00239   0.00205     0.00231
#>  7 2017-12-03 00:00:00 usd            0.00214    0.00214   0.0021      0.0021 
#>  8 2017-12-07 00:00:00 usd            0.00215    0.00393   0.00214     0.00393
#>  9 2017-12-11 00:00:00 usd            0.00424    0.00514   0.00424     0.00514
#> 10 2017-12-15 00:00:00 usd            0.00754    0.0176    0.00754     0.0176 
#> # … with 341 more rows