【发布时间】:2014-11-23 23:29:23
【问题描述】:
我的 quanstrat 策略返回了一个我还没有讨论过的错误。
策略非常简单:计算给定时间段内的滚动总和。如果滚动总和超过某个阈值,则进入多头并同时提交两个 oco 订单,止盈和止损在 +/- 5% 的距离内。
代码是:
require("quantstrat")
from <- "2014-09-25"
to <- "2014-10-01"
rm(strategy.st)
try(rm("account.st","portfolio.st"),silent=TRUE)
.blotter <- new.env()
.strategy <- new.env()
initDate <- as.character(as.Date(from) - 1)
currency("USD")
Sys.setenv(TZ = "UTC")
symbols <- "data"
stock(symbols, currency = "USD", multiplier = 1) # Initialisation of the instrument
tradeSize <- 1 # Initialisation of trade size
initEq <- 1000 # Initialisation of initial equity
strategy.st <- "btc" # Initialisation of the strategy
portfolio.st <- "btc" # Initialisation of the strategy, must be after strategy
account.st <- "btc" # Initialisation of the strategy, must be after strategy and portolio
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
### Parametres
lookBackVol <- 5
thresholdVol <- 20
stopLoss <- -0.05
profitTarget <- 0.05
### Indicators
add.indicator(strategy.st, name = "runSum", arguments = list(x = quote(data$ask.vol), n = lookBackVol), label = "volRunSum")
### Signals
add.signal(strategy.st, name = "sigThreshold", arguments = list(column = "volRunSum", threshold = thresholdVol, relationship = "gte", cross = TRUE), label = "longSig")
### Rules
add.rule(strategy = strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longSig", sigval = 1,
orderqty = tradeSize,
ordertype = "market",
orderside = "long",
replace = FALSE,
orderset = "ocolong"
),
type = "enter",
label = "enterLong"
)
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longSig", sigval = 1,
orderqty = "all",
ordertype = "stoplimit",
orderside = "long",
replace = FALSE,
tmult = TRUE,
threshold = stopLoss,
orderset = "ocolong"
),
type = "chain",
parent = "enterLong",
label = "stopLossLong",
)
add.rule(portfolio.st, name = "ruleSignal",
arguments = list(sigcol = "longSig", sigval = 1,
orderqty = "all",
ordertype = "limit",
orderside = "long",
replace = FALSE,
tmult = TRUE,
threshold = profitTarget,
orderset = "ocolong"
),
type = "chain",
parent = "enterLong",
label = "profitTargetLong",
)
### Results
results <- applyStrategy(strategy.st, portfolio.st)
View(getOrderBook(portfolio.st)$btc$data)
数据结构如下:
> dput(head(data))
structure(c(0, 0.0423759, 0.0299792, 0, 0, 0, 0.0722401, 0.0430572,
0.1648549, 2.9369966, 0, 0, 0.0722401, 0.0854331, 0.1948341,
2.9369966, 0, 0, 0, 1, 1, 0, 0, 0, 1, 2, 4, 9, 0, 0, 1, 3, 5,
9, 0, 0, NA, 408.11, 408.106, 408.106, 408.106, 408.106, 408.11,
408.111, 408.112, 407.5, 407.5, 407.5, 408.11, 408.111, 408.112,
407.5, 407.5, 407.5), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = structure("UTC", .Names = "TZ"), tclass = c("POSIXct",
"POSIXt"), tzone = structure("UTC", .Names = "TZ"), index = structure(c(1411596001,
1411596002, 1411596003, 1411596004, 1411596005, 1411596006), tzone = structure("UTC", .Names = "TZ"), tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 9L), .Dimnames = list(NULL, c("bid.vol",
"ask.vol", "vol", "bid.freq", "ask.freq", "freq", "bid.price",
"ask.price", "price")))
这是一个 xts 对象,显示一秒钟内的买卖量/交易频率,并且提到的错误说:
[1] "2014-09-24 22:00:17 data 1 @ 407"
Error in dindexOrderProc(openOrderSubset[i, ], mktPrices, curIndex) :
no price discernable for limit in applyRules
订单链似乎没有问题,因为订单簿包含所有三个价格正确的订单:
Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime Prefer Order.Set Txn.Fees
2014-09-24 22:00:16 "1" "407" "market" "long" NA "closed" "2014-09-24 22:00:17" "ask" "ocolong" "0"
2014-09-24 22:00:17 "all" "386.65" "stoplimit" "long" "-20.35" "open" NA "" "ocolong" "0"
2014-09-24 22:00:17 "all" "427.35" "limit" "long" "20.35" "open" NA "" "ocolong" "0"
有什么想法吗?
我在某处发现指定限价单价格,例如:
order.price=quote(data$ask.price[timestamp])
但没有成功。
【问题讨论】:
-
嗨 Steef,还没有深入了解它,但希望这会有所帮助。生成的错误消息是由 if (is.na(mktPrice) || is.null(mktPrice)) stop("no price contrastable for ", orderType, " in applyRules") 触发的,错误出现在 applyStrategy 上吧?
-
嗨 Olie,我也追踪了错误,是的,你是对的。错误来自 applyStrategy,指标和信号是正确的。我认为错误来自限价订单规则中的 order.price 语句,因为我的数据不是(也不能是)OHLC 格式,因此限价订单不知道从哪里获取价格。我尝试了类似 order.price=quote(data$ask.price[timestamp]) 的方法,但没有成功。
-
重新排列您的数据,让价格排在第一位。如果您不指定价格列,例如通过
prefer参数,quantstrat 将调用getPrice,并尝试猜测。它应该能够检测出价/要价数据,但预计价格会出现在 BBO 数据集中的补充数据之前。 -
您能否说得更具体一些或提供解决方案?我首先尝试重新排列列,以便价格、bid.price 和 ask.price 先行,但这没有帮助并以同样的错误结束。然后我尝试将
prefer = "ask.price"包含在止损和止盈订单中,但它也抛出了同样的错误。唯一的区别是,订单簿在 Prefer 列中为进入订单显示“ask”,为两个退出订单显示“ask.price”。包括 prefer = "ask.price" 来输入订单也无济于事。
标签: r quantstrat