I didn't intend to cross-post but occurred that this forum may be of more
help on this subject ... please see the question below and thanks for your
help.

 

Hello,

 

Just wondering why I am unable to run this in parallel.

A dput of my dataset is attached at the end. Please use to create my data
object.

 

I want to run this function in parallel (not sure if this is an efficient
implementation):

 

#Function to calculate the time to maturity for the option

require(fCalendar,quietly=TRUE) #Trying to calculate the trading days

require(fractalrock,quietly=TRUE) #Just to calculate the trading days
myFinCenter="Asia/Singapore"

 

getTimeToMaturity <- function(x){   

      tryCatch({

            toDt <- as.Date(as.character(x['EXPIRY_DT']), "%Y-%m-%d")
#Expiry Date

            fromDt <- as.Date(as.character(x['TIMESTAMP']), "%Y-%m-%d")
#Trade Timestamp

            NoOfDays <- NROW(getTradingDates(toDt,fromDt))

            return(NoOfDays/252)

      },

      error = function (ex){

            #print (paste("Error in",toDt,fromDt))

            NoOfDays <- 0

            return(NoOfDays/252)

      }

      )

}

 

 

Question: The following two lines work but the third and parallel one
doesn't ... why?

 

1) > apply(dNiftyOpt,1,getTimeToMaturity) #Works

         1          2          3          4          5          6          7
8          9         10         11         12         13         14
15         16         17         18         19         20 

0.02380952 0.01984127 0.07936508 0.02380952 0.01984127 0.01190476 0.02777778
0.02380952 0.01984127 0.01190476 0.02380952 0.01984127 0.02380952 0.02380952
0.01984127 0.02380952 0.01984127 0.02380952 0.02380952 0.02777778

 

 

library(snowfall)

2) > sfInit()

snowfall 1.84 initialized: sequential execution, one CPU.

 

> sfApply(dNiftyOpt,1,getTimeToMaturity) #Works

         1          2          3          4          5          6          7
8          9         10         11         12         13         14
15         16         17         18         19         20 

0.02380952 0.01984127 0.07936508 0.02380952 0.01984127 0.01190476 0.02777778
0.02380952 0.01984127 0.01190476 0.02380952 0.01984127 0.02380952 0.02380952
0.01984127 0.02380952 0.01984127 0.02380952 0.02380952 0.02777778 

 

> sfStop()

 

 

DOESN'T WORK: 3) 

> sfInit( parallel=TRUE, cpus=4 );

> sfApply(dNiftyOpt,1,getTimeToMaturity) #Added the time to maturity.
DOESN'T WORK?

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 

> sfStop();

 

 

 

My dataset:

dput(dNiftyOpt)

 

structure(list(INSTRUMENT = c("OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", 

"OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", 

"OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", "OPTIDX", 

"OPTIDX", "OPTIDX"), SYMBOL = c("NIFTY", "NIFTY", "NIFTY", "NIFTY", 

"NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", 

"NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", "NIFTY", 

"NIFTY", "NIFTY"), EXPIRY_DT = c("2004-01-29", "2004-01-29", 

"2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", 

"2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", 

"2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", "2004-01-29", 

"2004-01-29", "2004-01-29", "2004-01-29"), STRIKE_PR = c(1780, 

1780, 1800, 1800, 1800, 1800, 1800, 1800, 1800, 1800, 1820, 1820, 

1820, 1830, 1830, 1830, 1830, 1840, 1840, 1850), OPTION_TYP = c("PE", 

"PE", "CE", "CE", "CE", "CE", "PE", "PE", "PE", "PE", "CE", "CE", 

"PE", "CE", "CE", "PE", "PE", "CE", "PE", "CE"), SETTLE_PR = c(27.4, 

5.7, 152.95, 28.6, 70.45, 111.35, 14.75, 39.2, 8.6, 2.35, 20.4, 

54.2, 50.15, 18.35, 47.25, 51.75, 15.5, 14.95, 57.95, 26.3), 

    TIMESTAMP = c("2004-01-22", "2004-01-23", "2004-01-02", "2004-01-22", 

    "2004-01-23", "2004-01-27", "2004-01-21", "2004-01-22", "2004-01-23", 

    "2004-01-27", "2004-01-22", "2004-01-23", "2004-01-22", "2004-01-22", 

    "2004-01-23", "2004-01-22", "2004-01-23", "2004-01-22", "2004-01-22", 

    "2004-01-21"), Underlying = c(1770.5, 1847.55, 1946.05, 1770.5, 

    1847.55, 1904.7, 1824.6, 1770.5, 1847.55, 1904.7, 1770.5, 

    1847.55, 1770.5, 1770.5, 1847.55, 1770.5, 1847.55, 1770.5, 

    1770.5, 1824.6), UnderlyingVol = c(0.293906144944403, 0.331877179605752,


    0.129552369208600, 0.293906144944403, 0.331877179605752, 

    0.348918971622834, 0.276334860399362, 0.293906144944403, 

    0.331877179605752, 0.348918971622834, 0.293906144944403, 

    0.331877179605752, 0.293906144944403, 0.293906144944403, 

    0.331877179605752, 0.293906144944403, 0.331877179605752, 

    0.293906144944403, 0.293906144944403, 0.276334860399362)), .Names =
c("INSTRUMENT", 

"SYMBOL", "EXPIRY_DT", "STRIKE_PR", "OPTION_TYP", "SETTLE_PR", 

"TIMESTAMP", "Underlying", "UnderlyingVol"), row.names = c("1", 

"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 

"14", "15", "16", "17", "18", "19", "20"), class = "data.frame")

 

 


	[[alternative HTML version deleted]]

