【发布时间】:2021-09-09 14:53:15
【问题描述】:
我正在尝试在循环内的循环上运行带有%dopar% 的foreach 循环,但出现以下错误:Error in { : task 2 failed - "object 'price_floor' not found"。这是代码(错误似乎发生在foreach (n = 2:nrow(timesaleDat_id)) %dopar%...行之后):
numCores <- parallel::detectCores() - 1
myCluster <- parallel::makeCluster(numCores, type = "PSOCK")
print(myCluster) # Returns "socket cluster with 7 nodes on host ‘localhost’"
doParallel::registerDoParallel(cl = myCluster)
foreach::getDoParRegistered() # Returns "TRUE"
foreach::getDoParWorkers() # Returns "7"
tStopLoss <- seq(0.00, 0.03, by = 0.01)
for (l in 1:length(tStopLoss)) {
for (m in 1:nrow(bids_entity_id)) {
bidTStamp <- bids_entity_id$tStamp[m]
timesaleDat_id <- timesaleDat_time[timesaleDat_time$tStamp >= bidTStamp, ]
if (nrow(timesaleDat_id) >= 2) {
foreach (n = 2:nrow(timesaleDat_id)) %dopar% {
if (n == 2) {
if (timesaleDat_id$price[n] >= timesaleDat_id$price[n - 1] * (1 - tStopLoss[l])) {
price_floor <- timesaleDat_id$price[n - 1]
price_init <- timesaleDat_id$price[n - 1]
time_init <- timesaleDat_id$tStamp[n - 1]
if (timesaleDat_id$price[n] > price_floor) {
price_floor <- timesaleDat_id$price[n]
}
} else if (timesaleDat_id$price[n] < timesaleDat_id$price[n - 1] * (1 - tStopLoss[l])) {
bids_entity_id$priceChange[m] <- timesaleDat_id$price[n] - timesaleDat_id$price[n - 1]
bids_entity_id$priceChangePct[m] <- (timesaleDat_id$price[n] - timesaleDat_id$price[n - 1]) /
timesaleDat_id$price[n - 1]
bids_entity_id$timeChange[m] <- timesaleDat_id$tStamp[n] - timesaleDat_id$tStamp[n - 1]
break
}
} else if (n > 2) {
if (timesaleDat_id$price[n] >= price_floor * (1 - tStopLoss[l])) {
if (timesaleDat_id$price[n] > price_floor) {
price_floor <- timesaleDat_id$price[n]
}
} else if (timesaleDat_id$price[n] < price_floor * (1 - tStopLoss[l])) {
bids_entity_id$priceChange[m] <- timesaleDat_id$price[n] - price_init
bids_entity_id$priceChangePct[m] <- (timesaleDat_id$price[n] - price_init) / price_init
bids_entity_id$timeChange[m] <- timesaleDat_id$tStamp[n] - time_init
break
}
}
}
}
}
}
bids_entity_id 是具有以下结构的数据帧,其中length(unique(bids_entity_id$tick)) == 1、length(unique(bids_entity_id$entity)) == 1 和length(unique(bids_entity_id$bid_ask)) == 1:
'data.frame': 5988 obs. of 11 variables:
$ tStamp : num 1624455002264 1624455010287 1624455011778 1624455012780 1624455013749 ...
$ tick : chr "ATOS" "ATOS" "ATOS" "ATOS" ...
$ entity : chr "MEMX" "MEMX" "MEMX" "MEMX" ...
$ bid_ask : chr "bid" "bid" "bid" "bid" ...
$ price : num 6.5 6.57 6.59 6.61 6.63 6.66 6.65 6.65 6.65 6.65 ...
$ size : int 100 600 500 400 100 300 800 400 400 400 ...
$ x : int 30596619 30610012 30611053 30612308 30613618 30614531 30615664 30615664 30615664 30615664 ...
$ diffFromPrice : chr NA NA NA NA ...
$ priceChange : logi NA NA NA NA NA NA ...
$ priceChangePct: logi NA NA NA NA NA NA ...
$ timeChange : logi NA NA NA NA NA NA ...
timesaleDat_time 是具有以下结构的数据帧,其中length(unique(timesaleDat_time$tick)) == 1:
'data.frame': 48540 obs. of 6 variables:
$ seq : int 595 596 597 598 599 600 601 602 603 604 ...
$ tick : chr "ATOS" "ATOS" "ATOS" "ATOS" ...
$ tStamp: num 1624455000313 1624455000315 1624455000315 1624455000315 1624455000315 ...
$ price : num 6.6 6.61 6.6 6.59 6.59 6.58 6.58 6.57 6.57 6.57 ...
$ size : int 309468 100 100 400 129 165 100 176 400 100 ...
$ index : int 24833 24834 24835 24836 24837 24838 24839 24840 24841 24842 ...
【问题讨论】:
标签: r doparallel