我试图通过将一定比例的投资组合(100%或20%)投资于头寸而不是固定的订单数量(即10股)来使Quantstrat工作。
我尝试从(Quantstrat: Ordersize function)改编ordersize函数,但是我的代码在交易过程中出错:
[1] "2016-06-01 00:00:00 BP 3972.33383061 @ 25.1741178521841"
[1] "2016-06-30 00:00:00 BP -3972.33383061 @ 28.5237037199368"
[1] "2016-09-14 00:00:00 BP 4157.48457062 @ 27.2534200275105"
[1] "2016-11-02 00:00:00 BP 4157.48457062 @ 27.8177899348377"
[1] "2017-03-30 00:00:00 BP -8314.96914124 @ 29.2450509184207"
Error in if (!is.null(orderqty) && orderqty != 0 && length(orderprice)) { :
missing value where TRUE/FALSE needed
我的代码如下:
###########################################################################################
#
# part 1 - initialize things
#
###########################################################################################
initdate <- "2016-01-01"
from <- "2016-01-01" #start of backtest
to <- Sys.Date() #end of backtest
stock(symbols,currency = "GBP",multiplier = 1)
tradesize <-100000 #default trade size
initeq <- 100000 #default initial equity in our portfolio
strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy,portfolio and account
#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st)
#initialize portfolio,account,orders and strategy objects
initPortf(portfolio.st,symbols = symbols,initDate = initdate,currency = "GBP")
initacct(account.st,portfolios = portfolio.st,initEq = initeq)
initOrders(portfolio.st,initDate = initdate)
# set position limits
strategy(strategy.st,store=TRUE)
#################
# #
# add signals #
# #
#################
# custom entry signal
add.signal(strategy.st,name = "sigThreshold",arguments = list(column = "abv_bb",threshold = 1,relationship = "gte",cross = TRUE),label = "thresholdentry")
# custom exit signal
add.signal(strategy.st,arguments = list(column = "blw_bb",label = "thresholdexit")
###############
# #
# add rules #
# #
###############
osInvestAll <- function (data,timestamp,orderqty,ordertype,orderside,equity,portfolio,symbol,ruletype,...,initEq) {
datePos <- format(timestamp,"%Y-%m-%d %H:%M:%OS")
datePos <- strptime(c(datePos),format = "%Y-%m-%d %H:%M:%OS",tz =
"UTC") + 86400 #for daily data
updatePortf(Portfolio=portfolio,Symbol=symbol,Dates=paste0(start(data),"/",datePos))
# After updating portfolio profit,we can extract the Net.Trading.PL earned up to datePos.
trading_pl <- sum(.getPortfolio(portfolio)$summary$Net.Trading.PL)
# The total equity in the strategy for this symbol (and this symbol only in isolation always,# as this is how quantstrat by default works with applyStrategy)
equity <- initEq + trading_pl
ClosePrice <- getPrice(data,prefer = "Close")[datePos]
UnitSize <- as.numeric((equity / ClosePrice))
UnitSize1 <- round(UnitSize,digits = 8)
UnitSize1
}
# add custom entry rule
add.rule(strategy.st,name = "ruleSignal",arguments = list(sigcol = "thresholdentry",sigval = TRUE,ordertype = "market",orderside = "long",prefer = "Close",osFUN = osInvestAll),type = "enter")
# add custom exit rule
add.rule(strategy.st,arguments = list(sigcol = "thresholdexit",orderqty = "all",prefer = "Close"),type = "exit",label="ExitRule",enabled=T)
##################
# #
# run strategy #
# #
##################
out <- applyStrategy(strategy = strategy.st,initEq=initeq)