简体   繁体   中英

R Extracting the High and Low based on purchase and sale dates on a portfolio of stocks

I have been given a csv file which contains 50 different stock symbols that the hedgefund purchased and sold during the month of October. I have to check to make sure the purchase price was between the high and low on the particular days of purchase and sale.

Here's the formated CSV:

head(hilo)

# A tibble: 6 x 9
  Symbol `Date of Purchase` `Date of Sale` `Purchase price` `Sale Price` open  high  low   close
  <chr>  <date>             <date>                    <dbl>        <dbl> <chr> <chr> <chr> <chr>
1 PCH    2018-10-02         2018-10-03                 40.2         38.4 NA    NA    NA    NA   
2 NBHC   2018-10-03         2018-10-11                 37.8         36.2 NA    NA    NA    NA   
3 STWD   2018-10-08         2018-10-10                 21.3         21.0 NA    NA    NA    NA   
4 RWT    2018-10-08         2018-10-11                 16.1         16   NA    NA    NA    NA   
5 NVEE   2018-10-08         2018-10-10                 84.3         83.0 NA    NA    NA    NA   
6 PRIM   2018-10-08         2018-10-10                 23.5         23.1 NA    NA    NA    NA

I then collect the symbols using getSymbols(), format them into a list, and grab the high and low column.

dataEnv <- new.env()

#Get symbol data from Yahoo!
getSymbols(hilo$Symbol, from = min(hilo$`Date of Purchase`, na.rm = TRUE), to = max(hilo$`Date of Sale`, na.rm = TRUE), env = dataEnv)

slist <- as.list(dataEnv)
Yhilo <- xts()
for (i in 1:length(slist)) {
  Yhilo <- cbind(Yhilo, slist[[i]][,2:3])
}

head(Yhilo)
                   CEQP.High CEQP.Low TCP.High TCP.Low CRC.High CRC.Low WPC.High WPC.Low IRTC.High IRTC.Low RWT.High RWT.Low STWD.High STWD.Low NVEE.High NVEE.Low TILE.High TILE.Low
2018-10-01 02:00:00     37.69    36.80    31.17   30.30   49.040  47.850    64.35   63.46     95.50   92.300    16.31   16.08     21.54    21.33    87.300   84.000     23.50    22.28
2018-10-02 02:00:00     37.57    36.80    31.69   30.63   49.417  47.530    64.19   63.26     93.25   91.595    16.29   16.10     21.48    21.30    84.900   83.000     22.47    22.14
2018-10-03 02:00:00     37.47    36.88    31.10   30.49   50.340  48.380    64.15   62.80     92.33   90.212    16.38   16.26     21.56    21.30    83.790   82.620     22.54    22.05
2018-10-04 02:00:00     38.38    37.45    31.23   30.30   50.050  47.660    63.07   62.12     92.72   86.530    16.31   16.14     21.36    21.10    85.600   82.615     22.47    22.06
2018-10-05 02:00:00     38.36    37.61    31.16   30.42   48.368  44.627    63.77   62.90     88.34   83.790    16.22   16.04     21.34    21.03    84.518   82.460     22.54    22.06
2018-10-08 02:00:00     38.20    37.37    31.10   30.57   45.760  44.020    64.21   62.98     85.79   81.739    16.16   16.02     21.30    21.00    84.340   82.399     22.24    21.96
                    AEE.High AEE.Low AAWW.High AAWW.Low ABG.High ABG.Low TREX.High TREX.Low MNK.High MNK.Low ORBK.High ORBK.Low AHL.High AHL.Low AAON.High AAON.Low HURN.High HURN.Low
2018-10-01 02:00:00    63.42   62.70     64.47    61.73    69.78   67.90     77.50    73.72    29.90   29.12     59.92    59.00    41.92   41.65     38.10    36.37     49.64    48.74
2018-10-02 02:00:00    64.47   63.47     62.59    61.83    68.47   66.93     75.50    73.78    30.06   28.89     60.00    58.99    41.86   41.73     36.67    35.35     49.22    48.59
2018-10-03 02:00:00    64.66   63.08     63.23    61.92    67.05   65.70     75.10    73.79    31.41   29.93     60.35    59.31    42.02   41.82     36.06    35.37     49.48    48.35
2018-10-04 02:00:00    64.08   62.87     63.38    61.94    65.71   64.18     74.28    71.90    29.88   25.39     59.76    58.75    41.96   41.80     35.72    34.83     48.69    48.05
2018-10-05 02:00:00    65.29   63.94     61.08    59.44    64.55   62.47     73.42    69.80    27.25   25.12     59.87    58.60    42.16   41.75     35.03    34.01     49.38    48.17
2018-10-08 02:00:00    66.36   65.07     59.84    58.42    63.83   62.46     72.26    70.47    26.45   25.38     59.20    58.45    41.86   41.73     35.14    33.85     50.42    48.58
                    ATHN.High ATHN.Low AGIO.High AGIO.Low LBTYA.High LBTYA.Low AVYA.High AVYA.Low NBHC.High NBHC.Low OUT.High OUT.Low QUAD.High QUAD.Low GTT.High GTT.Low BKD.High
2018-10-01 02:00:00    133.80   126.18     79.17    76.30      29.48     28.02    22.358    21.66     37.80    37.10    20.07   19.82     21.08    20.17    43.96   42.69     9.98
2018-10-02 02:00:00    128.99   125.65     76.65    73.44      28.42     27.95    21.890    21.45     37.35    36.57    20.11   19.83     20.40    19.34    44.64   42.86     9.73
2018-10-03 02:00:00    127.67   125.27     74.28    70.54      28.56     27.22    22.030    21.60     37.83    36.54    20.08   19.44     20.14    19.20    46.22   44.06     9.56
2018-10-04 02:00:00    125.99   122.02     74.00    70.25      27.48     26.78    21.895    21.42     38.22    37.25    19.48   19.17     20.01    19.02    44.87   43.29     9.41
2018-10-05 02:00:00    126.72   121.95     72.05    67.96      27.19     26.23    21.980    21.27     37.88    36.92    19.56   19.29     19.21    18.50    45.92   42.27     9.21
2018-10-08 02:00:00    126.32   124.01     69.18    66.24      27.24     26.46    21.790    20.74     37.57    36.92    19.49   19.25     19.49    18.81    43.37   41.45     9.30
                    BKD.Low ATU.High ATU.Low CAKE.High CAKE.Low PCH.High PCH.Low MXL.High MXL.Low CATM.High CATM.Low RBA.High RBA.Low CNC.High CNC.Low CRUS.High CRUS.Low SMTC.High
2018-10-01 02:00:00    9.62    28.48   27.68     53.68    52.36    41.38   39.95    20.20   19.89     32.52    31.47    36.46   35.86   146.41  144.87     38.77    37.81     56.39
2018-10-02 02:00:00    9.35    28.11   27.66     53.19    51.99    40.39   39.33    20.20   19.75     31.94    31.20    36.59   36.10   145.95  144.16     38.44    37.80     55.77
2018-10-03 02:00:00    9.19    28.03   27.71     52.66    51.63    40.20   38.08    19.99   19.35     32.52    30.96    36.48   36.04   145.37  144.29     38.02    37.18     54.82
2018-10-04 02:00:00    9.05    28.10   27.61     52.02    51.20    38.81   37.84    19.79   19.08     32.04    31.43    36.57   36.11   145.90  142.65     38.03    37.35     54.66
2018-10-05 02:00:00    8.70    27.97   27.29     52.56    51.55    38.91   38.38    19.09   17.89     32.74    31.22    36.67   36.10   144.45  142.66     37.70    35.71     54.45
2018-10-08 02:00:00    8.96    27.55   27.20     52.32    51.35    39.34   38.44    17.79   17.15     33.22    32.11    36.77   36.05   144.33  141.05     36.60    35.38     52.39
                    SMTC.Low AGNC.High AGNC.Low DDD.High DDD.Low NUE.High NUE.Low ATGE.High ATGE.Low ANDE.High ANDE.Low TECH.High TECH.Low PCTY.High PCTY.Low WY.High WY.Low CNK.High
2018-10-01 02:00:00    54.52     18.68    18.48    19.13   17.93    64.83   63.42     48.68    47.77     38.26    37.04    205.74   202.75    81.300   78.730   32.37  31.83    40.46
2018-10-02 02:00:00    54.35     18.74    18.58    18.13   17.16    65.42   64.08     48.33    46.25     37.35    36.66    203.51   200.33    78.830   75.860   32.14  31.56    39.63
2018-10-03 02:00:00    53.80     18.76    18.42    18.51   17.65    65.63   65.04     46.94    46.17     37.70    36.53    204.29   199.34    77.720   75.700   31.93  30.65    39.53
2018-10-04 02:00:00    53.40     18.44    18.22    18.51   17.51    66.03   64.56     46.86    45.26     37.31    36.72    199.20   192.76    77.049   72.871   30.86  30.20    39.46
2018-10-05 02:00:00    51.71     18.34    18.09    19.19   17.72    65.05   63.54     46.17    44.95     37.14    36.34    195.24   190.62    74.200   70.410   30.79  30.18    39.83
2018-10-08 02:00:00    50.91     18.30    18.08    18.03   17.19    64.80   63.77     46.37    45.21     37.59    36.66    192.37   187.11    72.140   67.990   30.96  30.26    40.36
                    CNK.Low PRIM.High PRIM.Low JCOM.High JCOM.Low LOGM.High LOGM.Low ALRM.High ALRM.Low HUN.High HUN.Low
2018-10-01 02:00:00   39.45     25.20    24.23     83.25    81.52     92.89    87.34    60.200    56.67    27.41   26.73
2018-10-02 02:00:00   38.60     24.51    23.98     81.59    78.97     88.69    86.76    56.910    53.79    27.48   26.74
2018-10-03 02:00:00   38.80     24.42    24.08     80.38    79.06     88.44    86.79    56.430    54.55    27.77   26.82
2018-10-04 02:00:00   38.74     24.29    23.66     78.97    77.46     86.78    85.14    56.000    54.84    27.45   26.88
2018-10-05 02:00:00   39.17     23.87    23.26     78.31    76.82     87.45    82.60    53.790    49.27    27.10   25.82
2018-10-08 02:00:00   39.17     23.61    23.05     77.27    75.49     84.09    81.57    51.485    47.93    26.16   25.65

What I'm having trouble doing is figuring out how to extract the high and low from this data set at the date of purchase and sale for the corresponding symbol in hilo.

Below is some code that will return what you need. I'm using quantmod, purrr and dplyr as packages. Note the use of pmap with getSymbols to get the data. In your use of getSymbols for every ticker you would get the same amount of rows. This is overkill for most request as the holding period is in most cases smaller. Now every ticker will only fetch the data between purchase date and sale date.

I created a helper function to get the high and lows of the purchase date and sales dates. Assumptions here are that the last available record is indeed the sales date. Adjust this part if it is not. I removed the open, high, low and close columns from hilo as these where not needed for this example.

I leave the checking if a buy / sales price is between the high and low to you.

library(quantmod)
library(purrr)
library(dplyr)

# use pmap so 3 columns can correctly be passed to getSymbols    
stocklist <- pmap(list(hilo$Symbol, hilo$Date_of_Purchase, hilo$Date_of_Sale), function(x, y, z) getSymbols(Symbols = x, src = "yahoo", from = y, to = z + 1, auto.assign = FALSE))
names(stocklist) <- hilo$Symbol

# function described below
stock_hilo <- map_dfr(stocklist, hi_lo, hilo = hilo, .id = "Symbol")

hilo %>% 
  left_join(stock_hilo)

Joining, by = "Symbol"
  Symbol Date_of_Purchase Date_of_Sale Purchase_price Sale_Price pu_hi  pu_lo sa_hi sa_lo
1    PCH       2018-10-02   2018-10-03           40.2       38.4 40.39 39.330 40.39 39.33
2   NBHC       2018-10-03   2018-10-11           37.8       36.2 37.83 36.540 37.78 37.07
3   STWD       2018-10-08   2018-10-10           21.3       21.0 21.30 21.000 21.42 21.19
4    RWT       2018-10-08   2018-10-11           16.1       16.0 16.16 16.020 16.39 16.16
5   NVEE       2018-10-08   2018-10-10           84.3       83.0 84.34 82.399 85.50 84.15
6   PRIM       2018-10-08   2018-10-10           23.5       23.1 23.61 23.050 23.65 23.38

function used in code

hi_lo <- function(stock_data, hilo){
  stock_symbol <- stringi::stri_extract(names(stocklist$PCH)[1], regex = "^[A-Z]+")
  output <- data.frame(matrix(nrow = 1, ncol = 4))
  output <- setNames(output, c("pu_hi", "pu_lo", "sa_hi", "sa_lo"))

  # purchase date is the first record of the timeseries
  pu_data <- xts::first(stock_data)
  output$pu_hi <- as.numeric(quantmod::Hi(pu_data))
  output$pu_lo <- as.numeric(quantmod::Lo(pu_data))

  # assuming sales date is the last record of the timeseries
  sa_data <- xts::last(stock_data)
  output$sa_hi <- as.numeric(quantmod::Hi(sa_data))
  output$sa_lo <- as.numeric(quantmod::Lo(sa_data))

  return(output)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM