Learn R Programming

FinancialInstrument (version 1.4.1)

saveInstruments: Save and Load all instrument definitions

Description

Saves (loads) the .instrument environment to (from) disk.

Usage

saveInstruments(file_name = "MyInstruments", dir = "", compress = "gzip")

loadInstruments(file_name = "MyInstruments", dir = "")

reloadInstruments(file_name = "MyInstruments", dir = "")

Value

Called for side-effect

Arguments

file_name

name of file. e.g. “MyInstruments.RData”. As an experimental feature, a list or environment can be passed to file_name.

dir

Directory of file (defaults to current working directory. ie. "")

compress

argument passed to save, default is "gzip"

Author

Garrett See

Details

After you have defined some instruments, you can use saveInstruments to save the entire .instrument environment to disk.

loadInstruments will read a file that contains instruments and add those instrument definitions to your .instrument environment. reloadInstruments will remove all instruments in the current .instrument environment before loading instruments from disk.

The file_name should have a file extension of “RData”, “rda”, “R”, or “txt”. If the file_name does not end with one of those, “.RData” will be appended to the file_name

If the file extension is “R” or “txt”, saveInstruments will create a text file of R code that can be sourced to load instruments back into the .instrument environment.

See Also

save, load load.instrument define_stocks, define_futures, define_options (option_series.yahoo)

Examples

Run this code

backup_file <- tempfile(fileext = ".RData")
output_dir <- tempfile("fi-save-test-")
dir.create(output_dir)

saveInstruments(backup_file)

tryCatch(
  {
    rm_instruments(keep.currencies = FALSE)

    currency("USD")
    stock("SPY", currency = "USD")

    saveInstruments(
      "test-instruments.R",
      dir = output_dir
    )

    rm_instruments(keep.currencies = FALSE)

    source(
      file.path(output_dir, "test-instruments.R")
    )

    stopifnot(is.instrument.name("SPY"))
    stopifnot(getInstrument("SPY")$currency == "USD")
  },
  finally = {
    reloadInstruments(backup_file)
    unlink(backup_file)
    unlink(output_dir, recursive = TRUE)
  }
)

Run the code above in your browser using DataLab