Column operations with trading volumes
The number of shares traded (sold or bought) in a given time period is the volume. You work for a stock brokerage as a Business Analyst. A matrix of daily changes in volume, mat_stocks
, is available for you. Each column contains the change in volume for a stock from the previous day. You want to find out whether the traded volume for these stocks increases consistently for a given period.
The function calculate_runs()
is available for you. This function takes two arguments: 1) the daily changes in volume, 2) a single-value of the period (number of days) for which to calculate the run.
You need to apply calculate_runs()
to every column of mat_stocks
in parallel, specifying five days as the period. The parallel
package has been loaded for you.
This exercise is part of the course
Parallel Programming in R
Exercise instructions
- Export
n_days
to the cluster. - Apply
calculate_runs
to each column of matrixmat_stocks
. - Specify the value for the
period
argument withn_days
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
n_days <- 5
cl <- makeCluster(4)
# Export n_days to cluster
clusterExport(cl, "___", envir = environment())
# Apply calculate_runs to each column of mat_stocks
volume_runs <- ___(___, ___, ___,
# Specify value for period argument
___ = ___)
stopCluster(cl)
print(paste0("Number of ", n_days, "-day increasing runs: ", sum(volume_runs)))