下次请提供一些代码,尽管您的解释很清楚。
但是,您没有提到如何处理DifFromFv中的大跳跃(例如,如果它从-3到-18),所以我将由您决定。
library(plyr)
firstPosy = FALSE
DiffFair <- c(0, -10, -11.5, -13, -16.6, -12.3, -11, -14.1, -8, -7, -5)
posy <- c(0)
buyPrices <- c(0)
targetProfit <- c(0)
steps <- c(0)
PNL = 0
for (i in 2:length(DiffFair)) {
if (DiffFair[i] <= -10 & DiffFair[i] > -13 & firstPosy == FALSE) {
firstPosy = TRUE
posy <- c(posy, 1)
steps <- c(steps, round_any(DiffFair[i], 10, f = ceiling))
lastChangePosy = DiffFair[i]
buyPrices <- c(buyPrices, DiffFair[i])
targetProfit <- c(targetProfit, -5)
}
else if (DiffFair[i] <= -13 & firstPosy == FALSE) {
firstPosy = TRUE
lastChangePosy = DiffFair[i]
steps <- c(steps, round_any(DiffFair[i] + 10, 3, f = ceiling) - 10)
buyPrices <- c(buyPrices, DiffFair[i])
targetProfit <- c(targetProfit, -5)
posy <- c(posy, tail(posy, n=1) + (-round_any(DiffFair[i] + 10, 3, f = ceiling) / 3) + 1)
}
else if (tail(steps, n=1) > round_any(DiffFair[i] + 10, 3, f = ceiling) - 10 & DiffFair[i] <= -10) {
posy <- c(posy, posy[i-1] + 1)
steps <- c(steps, round_any(DiffFair[i] + 10, 3, f = ceiling) -10)
lastChangePosy = DiffFair[i]
buyPrices <- c(buyPrices, DiffFair[i])
targetProfit <- c(targetProfit, tail(targetProfit, n=1) - 3)
}
else if (DiffFair[i] >= tail(targetProfit, n=1) & tail(posy, n=1) > 0) {
if (tail(targetProfit, n=1) == -5) {
posy <- c(posy, 0)
}
else {
posy <- c(posy, posy[i-1] - 1)
}
lastChangePosy = DiffFair[i]
PNL = PNL + (DiffFair[i] - tail(buyPrices, n=1))
buyPrices <- buyPrices[-length(buyPrices)]
targetProfit <- targetProfit[-length(targetProfit)]
steps <- steps[-length(steps)]
if (DiffFair[i] > -10) {
firstPosy = FALSE
}
}
else {
posy <- c(posy, posy[i-1])
}
}
print(PNL)