Search code examples
rquantmodchain

getOptionChain with multiple expiries


I use R to download option chains, via the quantmod package. My goal is to download and export option chains in order to be used on other software.

If I download only the front month expiry, I am able to correctly export to a .txt file, using these lines:

library(quantmod)
aapl_front <- getOptionChain ('AAPL')
front <- do.call('rbind', aapl_front)
write.table (front, 'data_front.txt')

Problems appear when I download all the expiries. Here the rbind function fails to work as I think it should, and I export a table that is useless; these are the lines:

aapl_total <- getOptionChain('AAPL', NULL)
total <- do.call('rbind', aapl_total)
write.table(total, 'data_total.txt')

I guess that in the second case aapl_total is a list of lists, that contains all the expiries, and I'm not able to correctly split them.

Any suggestions?


Solution

  • You could loop through each expiration and rbind the calls and puts.

    lapply(aapl_total, function(x) do.call(rbind, x))
    

    Then, you'd have a list that you could do.call(rbind()).

    In one step:

    do.call(rbind, lapply(aapl_total, function(x) do.call(rbind, x)))