"没有交易/头寸到图表"使用quantstrat我的代码出错

时间:2017-01-17 02:06:14

标签: r trading algorithmic-trading quantstrat

说明:

我是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}}

1 个答案:

答案 0 :(得分:2)

错误是不言自明的。您没有可用于图表位置的任何交易,chart.Posn的目的是什么?

有几件事是错的:

  • 您错过了对applyStrategy的调用以实际执行回溯测试(并生成交易)。
  • 您还缺少"longentry"的相关信号列,add.rule显然想要使用,尚未定义。您需要适当地定义add.signal(s)。
  • 如果您的目标是使用applyIndicators进行回溯测试,则无需致电applyStrategyapplyIndicators只是运行您在add.indicator中定义的内容,并且不会运行信号或规则。