Learn R Programming

FinancialInstrument (version 1.3.1)

fn_SpreadBuilder: Calculate prices of a spread from 2 instruments.

Description

Given 2 products, calculate spread values for as many columns as practicable.

Usage

fn_SpreadBuilder(prod1, prod2, ratio = 1, currency = "USD", from = NULL,
  to = NULL, session_times = NULL, notional = TRUE,
  unique_method = c("make.index.unique", "duplicated", "least.liq",
  "price.change"), silent = FALSE, auto.assign = TRUE, env = .GlobalEnv,
  ...)

Arguments

prod1

chr name of instrument that will be the 1st leg of a 2 leg spread (Can also be xts data for first product)

prod2

chr name of instrument that will be the 2nd leg of a 2 leg spread (Can also be xts data for second product)

ratio

Hedge ratio. Can be a single number, or a vector of same length as data.

currency

chr name of currency denomination of the spread

from

from Date to pass through to getSymbols if needed.

to

to Date to pass through to getSymbols if needed.

session_times

ISO-8601 time subset for the session time, in GMT, in the format 'T08:00/T14:59'

notional

TRUE/FALSE. Should the prices be multiplied by contract multipliers before calculating the spread?

unique_method

method for making the time series unique

silent

silence warnings? (FALSE by default)

auto.assign

If TRUE (the default) the constructed spread will be stored in symbol created with make_spread_id. instrument metadata will also be created and stored with the same primary_id.

env

If prod1 and prod1 are character, this is where to get the data. Also, if auto.assign is TRUE this is the environment in which to store the data (.GlobalEnv by default)

other arguments to pass to getSymbols and/or make_spread_id

Value

an xts object with Bid, Ask, Mid columns, or Open, Close, Adjusted columns, or Open, Close columns. or Price column.

Details

prod1 and prod2 can be the names of instruments, or the xts objects themselves. Alternatively, prod2 can be omitted, and a vector of 2 instrument names can be given to prod1. See the last example for this usage.

If prod1 and prod2 are names (not xts data), it will try to get data for prod1 and prod2 from env (.GlobalEnv by default). If it cannot find the data, it will get it with a call to getSymbols. Prices are multiplied by multipliers and exchange rates to get notional values in the currency specified. The second leg's notional values are multiplied by ratio. Then the difference is taken between the notionals of leg1 and the new values for leg2.

‘make.index.unique’ uses the xts function make.index.unique ‘least.liq’ subsets the spread time series, by using the timestamps of the leg that has the fewest rows. ‘duplicated’ removes any duplicate indexes. ‘price.change’ only return rows where there was a price change in the Bid, Mid or Ask Price of the spread.

See Also

buildSpread synthetic.instrument formatSpreadPrice buildRatio

Examples

Run this code
# NOT RUN {
currency("USD")
stock("SPY", "USD")
stock("DIA", "USD")
getSymbols(c("SPY","DIA"))

#can call with names of instrument/xts ojects
fSB <- fn_SpreadBuilder("SPY","DIA") 
fSB2 <- fn_SpreadBuilder(SPY,DIA) # or you can pass xts objects

#assuming you first somehow calculated the ratio to be a constant 1.1
fSB3 <- fn_SpreadBuilder("SPY","DIA",1.1) 
head(fSB)

# Call fn_SpreadBuilder with vector of 2 instrument names
# in 1 arg instead of using both prod1 and prod2.
fSB4 <- fn_SpreadBuilder(c("SPY","DIA"))
#download data and plot the closing values of a spread in one line
chartSeries(Cl(fn_SpreadBuilder(getSymbols(c("SPY","DIA")),auto.assign=FALSE)))
# }

Run the code above in your browser using DataLab