项目作者: shill1729

项目描述 :
R wrapper for Alpha Vantage API and various quantitative finance methods/functions/algorithms
高级语言: R
项目地址: git://github.com/shill1729/trader.git
创建时间: 2020-06-12T21:54:19Z
项目社区:https://github.com/shill1729/trader

开源协议:Other

下载


trader

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.

Installation

You can install the package via devtools from github

  1. devtools::install_github("shill1729/trader")

Setting up your API key in .Renviron

Register with Alpha Vantage to obtain a free API key or subscribe for a premium API key. Save these in your .Renviron file with

  1. usethis::edit_r_environ()

and simply set in the .Renviron file the following variables:

  1. free_api_key = "YOUR_KEY_CODE1"
  2. 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.

Examples

Here are various examples on how to use the package:

Downloading a single stock’s price history

  1. library(trader)
  2. symbol <- "NTDOY"
  3. s <- getPriceTimeSeries(symbol, key = "free")
  4. # Plot the price chart
  5. print(plot(s$adj_close, main = symbol))
  6. # Print latest adjust close prices
  7. print(tail(s$adj_close))

Download multiple stocks with common history

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.

  1. library(trader)
  2. symbols <- c("NTDOY", "ROKU", "TSLA")
  3. s <- getStocks(symbols, key = "free")
  4. print(plot(s))
  5. print(tail(s))

Optimal log-utility investment under discrete-time stable vs Gaussian mixture returns

This function performs four tasks:

  1. Downloads a stock’s prices from AV
  2. Fits a Gaussian mixture distribution and stable distribution to
    daily arithmetic returns.
  3. Computes a likelihood ratio test to reject one of the two fits, if possible.
  4. 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).

  1. library(trader)
  2. symbol <- "NTDOY"
  3. # Default call uses entire price history, can be changed with rollingWindow
  4. z <- dtfm_strategy(symbol)
  5. print(z)