我正在尝试实施一项策略,以更新每个酒吧的限价单。该策略将在关闭旧头寸时始终打开一个新头寸,因此我总是需要一个结账单和一个结账单。
如果我使用replace=T
,退出顺序将删除我的进入顺序,这意味着要用两个小节来更改位置而不是一个。如果我使用replace=F
,则它将保持添加订单,而旧订单保持不变。我所寻找的行为也不是。
是否可以通过标签修改未结订单?例如,更改我标记为ShortExit
的限价单和标记为LongEntry
的限价单的价格吗?
require(quantstrat)
stock.str='IBM' # what are we trying it on
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
startDate='2006-12-31'
initEq=1000000
strat.st<-portfolio.st <-account.st <- 'bbands'
rm.strat(portfolio.st)
rm.strat(account.st)
strategy(strat.st, store=TRUE)
initPortf(portfolio.st, symbols=stock.str)
initAcct(account.st,portfolios='bbands')
initOrders(portfolio=portfolio.st)
addPosLimit(portfolio.st, stock.str, startDate, 100, 1 ) #set max pos
kchannel <- function(HLC,n){
center <- SMA(Cl(HLC),n)
atr <- ATR(HLC,n)$atr
up <- center + 2*atr
dn <- center - 2*atr
mom <- momentum(center)
out <- merge(center,atr,up,dn,mom)
colnames(out) <- c("center","atr","up","dn","mom")
return(out)
}
add.indicator(strategy = strat.st,
name = "kchannel",
arguments = list(HLC = quote(HLC(mktdata)),
n=n
),
label='kc')
add.signal(strategy = strat.st,
name="sigThreshold",
arguments = list(column="mom.kc",
thresh=0,
relationship="gt"),
label="long")
add.signal(strategy = strat.st,
name="sigThreshold",
arguments = list(column="mom.kc",
thresh=0,
relationship="lt"),
label="short")
add.rule(strategy = strat.st,name='ruleSignal',
arguments = list(sigcol="long",
sigval=TRUE,
orderqty=200,
orderside="long",
replace=T,
ordertype='limit',
prefer="dn",
osFUN=osMaxPos),
type='enter',
label="LongEntry")
add.rule(strategy = strat.st,name='ruleSignal',
arguments = list(sigcol="short",
sigval=TRUE,
orderqty=-200,
orderside="short",
replace=T,
ordertype='limit',
prefer="up",
osFUN=osMaxPos),
type='enter',
label="ShortEntry")
add.rule(strategy = strat.st,name='ruleSignal',
arguments = list(sigcol="short",
sigval=TRUE,
orderqty="all",
orderside="long",
replace=T,
ordertype='limit',
prefer="dn",
osFUN=osMaxPos),
type='exit',
label="LongExit")
add.rule(strategy = strat.st,name='ruleSignal',
arguments = list(sigcol="long",
sigval=TRUE,
orderqty="all",
orderside="short",
replace=T,
ordertype='limit',
prefer="up",
osFUN=osMaxPos),
type='exit',
label="ShortExit")
getSymbols(stock.str,from=startDate,index.class=c('POSIXt','POSIXct'), src='yahoo')
start_t<-Sys.time()
out<-try(applyStrategy(strategy='bbands' , portfolios='bbands') )
updatePortf(Portfolio='bbands')
chart.Posn(Portfolio='bbands',Symbol=stock.str)