除了mktdata之外,如何添加基于XTS的信号?

时间:2019-06-12 21:26:57

标签: r quantstrat

我正在尝试执行一项策略,即我预测Quantastra代码之外的NVDA价格,然后在该预测价格大于或小于NVDA上的布林带时进行交易。我已经有了预测的价格,并且不确定如何将其添加到mktdata中,因此我可以将它们与Quantstrat一起使用。

symbol <- 'NVDA'

currency("USD")
stock(symbol, currency="USD", multiplier=1)

# Fetch historical data:

initDate <- '2010-10-03'
startDate <- '2011-10-03'
endDate <- '2019-03-29'
initEq <- 10000000

Sys.setenv(TZ="UTC")
getSymbols(symbol, from=startDate, to=endDate, index.class="POSIXct", adjust=TRUE)
NVDAData <- get(symbol)
NVDAData <- cbind(NVDAData, NVDA.Predicted)

# Bollinger Band settings
SD = 2

N = 20

# Plot the raw series
myTheme<-chartTheme('white', up.col='blue', dn.col='red')
chartSeries(x = get(symbol), TA='addBBands()', theme = myTheme, name = symbol)
chartSeries(x = NVDA.Predicted, theme = myTheme, name = "Predicted")



stratName <- "lsbb"
portName <- "lsbb"
acctName <- "lsbb"
suppressWarnings(rm.strat("lsbb")) # reset
nShs <- 1000   # number of shares in one transaction

initPortf(name=portName, symbols = symbol, initDate=initDate)
initAcct(name=acctName, portfolios=portName,
     initDate=initDate, initEq=initEq)
initOrders(portfolio=portName, initDate=initDate)
strategy(name = stratName, store=TRUE)

add.indicator(strategy = stratName, name = "BBands",
          arguments = list(HLC = quote(HLC(mktdata)),  maType='SMA'), label='BBands')

add.signal(strategy = stratName, name="sigCrossover",
          arguments=list(columns=c("Predicted","up"),relationship="gt"),
       label="Pred.gt.UpperBand")

add.signal(strategy = stratName, name="sigCrossover",
        arguments=list(columns=c("Predicted","dn"),relationship="lt"),
       label="Pred.lt.LowerBand")

add.signal(strategy = stratName, name="sigCrossover",
       arguments=list(columns=c("High","Low","mavg"),relationship="op"),
       label="Cross.Mid")

add.rule(strategy = stratName, name='ruleSignal',
     arguments=list(sigcol="Pred.gt.UpperBand",sigval=TRUE, orderqty=-nShs,
                    ordertype='market',   orderside=NULL),type='enter', 
     label = "Enter.Short")
add.rule(strategy = stratName, name='ruleSignal',
     arguments=list(sigcol="Pred.lt.LowerBand",sigval=TRUE, orderqty=nShs,
                    ordertype='market',   orderside=NULL),type='enter',
     label = "Enter.Long")
add.rule(strategy = stratName, name='ruleSignal',
     arguments=list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all',
                    ordertype='market', orderside=NULL),type='exit',
     label = "Exit.All")
mktdata <- NVDAData
applyStrategy(strategy = stratName,
          portfolios=portName,parameters=list(sd=SD,n=N))

updatePortf(Portfolio = portName)
updateAcct(name = acctName)
updateEndEq(Account = acctName)

high <- paste0(symbol, ".High")
low <- paste0(symbol, ".Low")
close <- paste0(symbol, ".Close")

sigData <- NVDAData[,c(high, low, close, 'NVDA.Predicted', 
                  "dn.BBands", "mavg.BBands", "up.BBands",
                  "Pred.gt.UpperBand", "Pred.lt.LowerBand", "Cross.Mid")] 

txns <- as.xts(getTxns(Portfolio = portName, Symbol = symbol))
pts <- perTradeStats(Portfolio = portName)
tstats <- tradeStats(Portfolios = portName)
View(t(tstats))


tsUnion <- merge.xts(sigData,txns[, c("Txn.Qty", "Txn.Price", "Txn.Value", "Net.Txn.Realized.PL")],join="outer")
View(tsUnion)


View(txns)
View(pts)


chart.Posn(Portfolio = portName, symbol = symbol, TA="add_BBands(n=20,sd=2)")

chart.Posn(Portfolio = portName, symbol = symbol, TA="add_BBands(n=20,sd=2)",
       Dates="1999-02-01::1999-03-31")

chart.Posn(Portfolio = portName, symbol = symbol, TA="add_BBands(n=20,sd=2)",
       Dates="2012-09-30::2013-08-31")    

a <- getAccount(stratName)
last(a$summary,5)

equity <- a$summary$End.Eq
plot(equity,main="Long-Short Bollinger Band Strategy Equity Curve")
ret <- Return.calculate(equity,method="log")
charts.PerformanceSummary(ret, colorset = bluefocus,
                      main="Long-Short Bollinger Band Strategy Performance")

chart.ME(Portfolio=portName, Symbol=symbol, type='MAE', scale='percent')
chart.ME(Portfolio=portName, Symbol=symbol, type='MFE', scale='percent')


library(lattice)
xyplot(a$summary,type="h",col=4)

我正在尝试使用指标中显示的预计价格,但我似乎无法将数据导入mktdata xts中。我相信这就是造成我问题的原因。

0 个答案:

没有答案