[R] Interpret List Label as Date from Quantmod getOptionChain

Dirk Eddelbuettel edd at debian.org
Mon Mar 5 04:41:44 CET 2018


On 5 March 2018 at 03:13, Sparks, John wrote:
| library(quantmod)
| #in fairness, I did not include this last time and my example was therefore not reproducible.  Apologies to Bert and everyone else #for not following the posting guidelines.
| aapl_total<-getOptionChain("AAPL", NULL)>
| 
| How could I then get the subset of the entire list which only has expiry dates in 2019, or more specifically, in Mar or Apr or May of 2019?

Here you go:

R> aapl_total<-getOptionChain("AAPL", NULL)     # your query
R> names(aapl_total)                            # the list element names
 [1] "Mar.09.2018" "Mar.23.2018" "Mar.29.2018" "Apr.06.2018" "Apr.13.2018" "Apr.20.2018" "May.18.2018"
 [8] "Jun.15.2018" "Aug.17.2018" "Sep.21.2018" "Oct.19.2018" "Nov.16.2018" "Jan.18.2019" "Jun.21.2019"
[15] "Jan.17.2020" "Jun.19.2020"
R> library(anytime)    # one of many tools to parse dates, this one is easiest in my biased view
R> anydate(names(aapl_total))
 [1] "2018-03-09" "2018-03-23" "2018-03-29" "2018-04-06" "2018-04-13" "2018-04-20" "2018-05-18"
 [8] "2018-06-15" "2018-08-17" "2018-09-21" "2018-10-19" "2018-11-16" "2019-01-18" "2019-06-21"
[15] "2020-01-17" "2020-06-19"
R> dvec <- anydate(names(aapl_total))           # helper variable
R> ind <- dvec >= anydate("2018-04-01") & dvec <= anydate("2018-04-30")     # one example
R> ind
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
R> str(aapl_total[ind])                         # index return object by desired dates
List of 3
 $ Apr.06.2018:List of 2
  ..$ calls:'data.frame':	24 obs. of  7 variables:
  .. ..$ Strike: num [1:24] 145 150 152 155 158 ...
  .. ..$ Last  : num [1:24] 30.4 24 27.1 20.9 22.1 ...
  .. ..$ Chg   : num [1:24] -3.12 0 0 -2.87 0 ...
  .. ..$ Bid   : num [1:24] 31.4 26.6 24.1 21.8 21.9 ...
  .. ..$ Ask   : num [1:24] 31.9 27.1 24.7 22.3 22.5 ...
  .. ..$ Vol   : int [1:24] 2 1 50 2 216 51 50 55 33 109 ...
  .. ..$ OI    : int [1:24] 30 1 50 2 0 32 57 101 136 297 ...
  ..$ puts :'data.frame':	21 obs. of  7 variables:
  .. ..$ Strike: num [1:21] 145 147 148 149 150 ...
  .. ..$ Last  : num [1:21] 0.27 0.14 0.17 0.18 0.36 0.49 0.68 0.81 0.96 1.17 ...
  .. ..$ Chg   : num [1:21] 0.13 0 -0.08 0 -0.07 ...
  .. ..$ Bid   : num [1:21] 0.14 0.18 0.13 0.23 0.27 0.35 0.46 0.61 0.81 1.06 ...
  .. ..$ Ask   : num [1:21] 0.24 0.28 0.21 0.35 0.36 0.45 0.56 0.71 0.9 1.17 ...
  .. ..$ Vol   : int [1:21] 12 1 1 3 86 15 31 74 39 213 ...
  .. ..$ OI    : int [1:21] 97 1 2 14 177 395 160 368 271 211 ...
 $ Apr.13.2018:List of 2
  ..$ calls:'data.frame':	19 obs. of  7 variables:
  .. ..$ Strike: num [1:19] 155 160 162 165 168 ...
  .. ..$ Last  : num [1:19] 21.2 16.9 14.9 13.1 10.7 ...
  .. ..$ Chg   : int [1:19] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ Bid   : num [1:19] 21.6 17.3 15.2 13.3 11.3 ...
  .. ..$ Ask   : num [1:19] 22.9 17.9 15.7 13.6 11.5 ...
  .. ..$ Vol   : int [1:19] 20 1 7 44 46 22 62 313 71 327 ...
  .. ..$ OI    : int [1:19] 20 20 6 11 11 0 153 156 19 54 ...
  ..$ puts :'data.frame':	18 obs. of  7 variables:
  .. ..$ Strike: num [1:18] 145 148 149 150 152 ...
  .. ..$ Last  : num [1:18] 0.38 0.48 0.56 0.56 0.69 0.87 1.04 1.05 1.34 1.75 ...
  .. ..$ Chg   : int [1:18] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ Bid   : num [1:18] 0.06 0.19 0.22 0.28 0.42 0.54 0.73 0.98 1.28 1.64 ...
  .. ..$ Ask   : num [1:18] 0.6 0.42 0.45 0.47 0.58 0.72 0.9 1.13 1.44 1.8 ...
  .. ..$ Vol   : int [1:18] 25 103 1 50 64 74 24 47 100 324 ...
  .. ..$ OI    : int [1:18] 0 6 0 0 7 10 11 19 56 19 ...
 $ Apr.20.2018:List of 2
  ..$ calls:'data.frame':	60 obs. of  7 variables:
  .. ..$ Strike: num [1:60] 2.5 5 7.5 10 12.5 17.5 22.5 40 50 55 ...
  .. ..$ Last  : num [1:60] 171 173 171 158 163 ...
  .. ..$ Chg   : num [1:60] -1 0 0 0 -3.6 ...
  .. ..$ Bid   : num [1:60] 166 166 163 148 164 ...
  .. ..$ Ask   : num [1:60] 168 167 165 152 164 ...
  .. ..$ Vol   : int [1:60] 5 10 10 10 1 10 5 4 300 10 ...
  .. ..$ OI    : int [1:60] 5 15 10 5 1 10 5 0 0 0 ...
  ..$ puts :'data.frame':	48 obs. of  7 variables:
  .. ..$ Strike: num [1:48] 2.5 50 55 60 65 70 75 80 85 90 ...
  .. ..$ Last  : num [1:48] 0.01 0.01 0.01 0.02 0.02 0.01 0.01 0.02 0.07 0.02 ...
  .. ..$ Chg   : num [1:48] 0 0 -0.01 0 -0.01 -0.02 0 0 0 0 ...
  .. ..$ Bid   : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ Ask   : num [1:48] 0.02 0.02 0.02 0.03 0.07 0.02 0.02 0.03 0.04 0.04 ...
  .. ..$ Vol   : int [1:48] 1 2 1 212 2 5 3 32 385 11 ...
  .. ..$ OI    : int [1:48] 1 0 1 212 621 473 3529 362 1823 1164 ...
R> 

You probably want such questions on the r-sig-finance list.

Cheers from across town,  Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org



More information about the R-help mailing list