我是R总体的新手,但是他在交易方面经验丰富。
我正在尝试使用quantstrat在我的R代码中编写简单信号。但我收到了一些错误(参见下文)
library( blotter )
library( quantstrat )
getSymbols( "UNVR.JK", from = "2003-01-01",
to = "2016-12-02",
src = "yahoo",
adjust = T
)
unvr<-UNVR.JK
initdate<-"1999-01-01"
from<-"2003-01-01"
to<-"2016-12-02"
Sys.setenv( TZ="UTC" )
currency( "IDR" )
tradesize<-1000000
initeq<-1000000
strategy.st<-"firststrat"
portfolio.st<-strategy.st
account.st<-portfolio.st
initPortf( portfolio.st,
symbols = "unvr",
initDate = initdate,
currency = "IDR"
)
initAcct( account.st,
portfolios = portfolio.st,
initDate = initdate,
currency = "IDR",
initEq = 1000000
)
initOrders( portfolio.st,
initDate = initdate
)
strategy( strategy.st,
store = T
)
add.indicator( strategy = strategy.st,
name = "SMA",
arguments = list( x = quote( Cl( unvr ) ), n = 50 ),
label = "SMA50"
)
add.indicator( strategy = strategy.st,
name = "RSI",
arguments = list( price = quote( Cl( unvr ) ), n = 3 ),
label = "RSI_3"
)
applyIndicators( strategy = strategy.st,
mktdata = OHLC( unvr )
)
add.rule( strategy.st,
name = "ruleSignal",
arguments = list( sigcol = "longentry", # use the longentry column as the sigcol
sigval = T, # set sigval to TRUE
orderqty = 1, # set orderqty to 1
ordertype = "market", # use a market type of order
orderside = "long", # take the long orderside
replace = F, # do not replace other signals
prefer = "Open" # buy at the next day's opening price
),
type = "enter" # this is an enter type rule, not an exit
)
add.rule( strategy = strategy.st, # add a rule that uses an osFUN to size an entry position
name = "ruleSignal",
arguments = list( sigcol = "longentry",
sigval = TRUE,
ordertype = "market",
orderside = "long",
replace = FALSE,
prefer = "Open",
osFUN = "osMaxDollar", # use the osFUN called osMaxDollar
tradeSize = "tradesize", # the tradeSize argument should be equal to tradesize (defined earlier)
maxSize = tradesize # the maxSize argument should be equal to tradesize as well
),
type = "enter"
)
chart.Posn( Portfolio = portfolio.st,
Symbol = "unvr"
)
Error in chart.Posn(Portfolio = portfolio.st, Symbol = "unvr") :
{
{1}}
答案 0 :(得分:2)
错误是不言自明的。您没有可用于图表位置的任何交易,chart.Posn
的目的是什么?
有几件事是错的:
applyStrategy
的调用以实际执行回溯测试(并生成交易)。 "longentry"
的相关信号列,add.rule
显然想要使用,尚未定义。您需要适当地定义add.signal
(s)。applyIndicators
进行回溯测试,则无需致电applyStrategy
。 applyIndicators
只是运行您在add.indicator
中定义的内容,并且不会运行信号或规则。