MyNixOS website logo
Description

Access to the 'twelvedata' Financial Data API.

The 'twelvedata' REST service offers access to current and historical data on stocks, standard as well as digital 'crypto' currencies, and other financial assets covering a wide variety of course and time spans. See <https://twelvedata.com/> for details, to create an account, and to request an API key for free-but-capped access to the data.

td: R Access to twelvedata

CI License CRAN Dependencies Downloads Last Commit

Motivation

twelvedata provides a very rich REST API, see the documentation. While a (free) login and a (free, permitting limited but possibly sufficient use) API key are required, the provided access is rich to set up simple R routines. This package does that.

Example

Here we are running (some) code from shown in example(time_series)

> library(td)
> data <- time_series("SPY", "5min", outputsize=500, as="xts")
> if (requireNamespace("quantmod", quietly=TRUE)) {
>     suppressMessages(library(quantmod))   # suppress some noise
>     chartSeries(data, name=attr(data, "symbol"), theme="white")  # convenient plot for OHLCV
> }

retrieves an xts object (provided xts is installed) and produces a chart like this:

The package can also be used without attaching it. The next example retrieves twenty years of weekly CAD/USD foreign exchange data using a direct td::time_series() call with having the package loaded. The API key is automagically set (if it is in fact provided either in the user config file or as an environment variable). Also shown by calling str() on the return object is the metadata attach after each request:

> cadusd <- td::time_series(sym="CAD/USD", interval="1week", outputsize=52.25*20, as="xts")
> str(cadusd)
An ‘xts’ object on 2001-02-27/2021-02-01 containing:
  Data: num [1:1045, 1:4] 0.651 0.646 0.644 0.638 0.642 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "open" "high" "low" "close"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 6
 $ symbol        : chr "CAD/USD"
 $ interval      : chr "1week"
 $ currency_base : chr "Canadian Dollar"
 $ currency_quote: chr "US Dollar"
 $ type          : chr "Physical Currency"
 $ accessed      : chr "2021-02-06 15:16:29.209635"
> 

As before, it can be plotted using a function from package quantmod; this time we use the newer chart_Series():

> quantmod::chart_Series(cadusd, name=attr(data, "symbol"))

As the returned is a the very common and well-understood [xts] format, many other plotting functions can be used as-is. Here is an example also showing how historical data can be accessed. We retrieve minute-resolution data for GME during the late January / early February period:

> gme <- time_series("GME", "1min",
+                    start_date="2021-01-25 09:30:00",
+                    end_date="2021-02-04 16:00:00", as="xts")

Note the use of exchange timestamps (NYSE is open from 9:30 to 16:00 local time).

We can plot this again using quantmod::chart_Series() showing how to display ticker symbol and exchange as a header:

> quantmod::chart_Series(gme, name=paste0(attr(gme, "symbol"), "/", attr(gme, "exchange")))

Naturally, other plotting functions and packages can be used. Here we use the same dataset but efficiently subset using a key xts feature and fed into CRAN package rtsplot and requesting OHLC instead of line plot.

> rtsplot::rtsplot(gme["20210128"], main="GME on 2021-Jan-28", type="ohlc")

If a vector of symbols is used in the query, a list of results is returned:

> res <- time_series(c("SPY", "QQQ", "IWM", "EEM"), outputsize=300, as="xts")
> op <- par(mfrow=c(2,2))
> sapply(res, function(x) quantmod::chart_Series(x, name=attr(x, "symbol")))
> par(op)

As of version 0.0.2, additional get_quote() and get_price() accessors are available.

As of version 0.0.5, support for reference data has been added.

Status

Still relatively, but already fairly feature-complete. The package is also officially recommended and approved by Twelve Data, but is developed independently. For an officially supported package, see their twelvedata-python package.

Contributing

Any problem, bug report, or feature request for the package can be submitted and handled most conveniently as a Github issue in the repository.

Before submitting pull requests, it is frequently preferable to first discuss need and scope in such an issue ticket. See the file Contributing.md (in the Rcpp repo) for a brief discussion.

Author

Dirk Eddelbuettel and Kenneth Rose

License

GPL (>= 2)

Metadata

Version

0.0.6

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows