The package trader provides a library/wrapper to the Alpha Vantage API provided a free or premium API key (see below). Additionally, various trading models based on historical data are provided, none of which (disclaimer) is investment advice.
You can install the package via devtools from github
devtools::install_github("shill1729/trader")
Register with Alpha Vantage to obtain a free API key or subscribe for a premium API key. Save these in your .Renviron file with
usethis::edit_r_environ()
and simply set in the .Renviron file the following variables:
free_api_key = "YOUR_KEY_CODE1"
premium_api_key = "YOUR_KEY_CODE2"
where the RHS is the actual key obtained from AV. The second line is not required obviously if you only plan on registering a free API key.
By default a premium key is assumed in every function that calls the AV API. In the future we will add an option to set defaults.
Here are various examples on how to use the package:
library(trader)
symbol <- "NTDOY"
s <- getPriceTimeSeries(symbol, key = "free")
# Plot the price chart
print(plot(s$adj_close, main = symbol))
# Print latest adjust close prices
print(tail(s$adj_close))
When downloading multiple stocks the data-set will go back as far as the latest common date between all assets. As of now, only adjusted close prices are available. In the future options to return any of the OHLC prices will be available.
library(trader)
symbols <- c("NTDOY", "ROKU", "TSLA")
s <- getStocks(symbols, key = "free")
print(plot(s))
print(tail(s))
This function performs four tasks:
- Downloads a stock's prices from AV
- Fits a Gaussian mixture distribution and stable distribution to daily arithmetic returns.
- Computes a likelihood ratio test to reject one of the two fits, if possible.
- Computes the optimal log-utility allocation in the stock.
Returned is the optimal fraction, the long-term optimal growth rate, and the reduction fraction together with a plot of the empirical density function compared to the model densities (Gaussian mixture and stable).
library(trader)
symbol <- "NTDOY"
# Default call uses entire price history, can be changed with rollingWindow
z <- dtfm_strategy(symbol)
print(z)