An R interface to the Alpaca API
'alpacar' provides an R interface to the Alpaca API's Python SDK. It uses Miniconda and 'reticulate' to automatically configure your python environment for exposure to R.
- Install 'alpacar'
remotes::install_github("https://github.com/joshwlivingston/alpacar/")
usethis::edit_r_profile()
- Set your API key ID and secret key in your .Rprofile
Sys.setenv("APCA_API_KEY_ID" = <ALPACA_API_KEY_ID>)
Sys.setenv("APCA_API_SECRET_KEY" = <ALPACA_API_SECRET_KEY>)
- Set the correct url for your paper or live trading account
Sys.setenv("APCA_API_BASE_URL" = "https://paper-api.alpaca.markets/")
or
Sys.setenv("APCA_API_BASE_URL" = "https://api.alpaca.markets/")
- Set additional parameters as required. E.g., for market data:
Sys.setenv("APCA_API_DATA_URL" = "https://data.alpaca.markets/")
To get started, simply load the python module as an R object:
library(alpacar)
alpaca <- alpaca_trade_api()
By default, a conda environment named "r-alpacar" will be created to host the python executable containing the alpaca-trade-api module.
If you want to use your own conda environment, you can specify with the env
argument.
You can configure an existing conda environment:
configure_env(conda_env = "myenv")
Once the module is loaded, the submodules are accessed with $
REST <- alpaca$rest$REST
TimeFrame <- alpaca$rest$TimeFrame
Module methods are accessible as callable R functions
r <- api$REST()
r$get_account()
Module attributes are accessible as R objects
api <- REST()
data_res <- api$get_bars(
"AAPL",
TimeFrame$Hour,
"2021-06-08",
"2021-06-08",
adjustment = "raw"
)
head(data_res$df)