我正在尝试使用quanstrat创建一个Shiny应用程序。代码在正常条件下运行完美;然而,当我把它放在Shiny中它失败了。运行下面的代码时,我得到:
library(shiny)
library(data.table)
shinyUI(fluidPage(
column(3,
actionButton("do", "Submit", styleclass="primary")),
column(9,
wellPanel(
h3("Investment Value"),
dataTableOutput("calendar")))))
我试图尽量减少这个可重复的例子的无关代码,所以它不会很漂亮,但它应该显示问题。
ui.R:
## Load required packages
library(shiny)
library(PerformanceAnalytics)
library(quantstrat)
library(IKTrading)
# Define a server for the Shiny app
shinyServer(function(input, output, session) {
data <- eventReactive(input$do, {
rm.strat("DollarVsATRos")
initDate="1990-01-01"
from="2003-01-01"
to="2012-12-31"
options(width=70)
options("getSymbols.warning4.0" = FALSE)
# Do some house cleaning
rm(list = ls(.blotter), envir = .blotter)
# Set the currency and the timezone
currency('USD')
Sys.setenv(TZ = "UTC")
symbols <- c("QLD", "EEM", "EDV", "BDCL")
getSymbols(symbols, from = from, auto.assign=TRUE,
to = to, src = "yahoo", adjust = TRUE, reload.Symbols=TRUE)
stock(symbols, currency = "USD", multiplier = 1)
#trade sizing and initial equity settings
tradeSize <- 100000
initEq <- 10000*length(symbols)
strategy.st <- "test1"
portfolio.st <- "test1"
account.st <- "test1"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=as.list(symbols), initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
#parameters
pctATR <- .02
period <- 10
atrOrder <- TRUE
nRSI <- 2
buyThresh <- 20
sellThresh <- 80
nSMA <- 200
#indicators
add.indicator(strategy.st, name="lagATR",
arguments=list(HLC=quote(HLC(mktdata)), n=period),
label="atrX")
add.indicator(strategy.st, name="RSI",
arguments=list(price=quote(Cl(mktdata)), n=nRSI),
label="rsi")
add.indicator(strategy.st, name="SMA",
arguments=list(x=quote(Cl(mktdata)), n=nSMA),
label="sma")
#signals
add.signal(strategy.st, name="sigComparison",
arguments=list(columns=c("Close", "sma"), relationship="gt"),
label="filter")
add.signal(strategy.st, name="sigThreshold",
arguments=list(column="rsi", threshold=buyThresh,
relationship="lt", cross=FALSE),
label="rsiLtThresh")
add.signal(strategy.st, name="sigAND",
arguments=list(columns=c("filter", "rsiLtThresh"), cross=TRUE),
label="longEntry")
add.signal(strategy.st, name="sigThreshold",
arguments=list(column="rsi", threshold=sellThresh,
relationship="gt", cross=TRUE),
label="longExit")
add.signal(strategy.st, name="sigCrossover",
arguments=list(columns=c("Close", "sma"), relationship="lt"),
label="filterExit")
#rules
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="longEntry", sigval=TRUE, ordertype="market",
orderside="long", replace=FALSE, prefer="Open",
osFUN=osDollarATR, tradeSize=tradeSize,
pctATR=pctATR, atrMod="X"),
type="enter", path.dep=TRUE)
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="longEntry", sigval=TRUE, ordertype="market",
orderside="long", replace=FALSE, prefer="Open",
osFUN=osMaxDollar, tradeSize=tradeSize, maxSize=tradeSize),
type="enter", path.dep=TRUE)
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="longExit", sigval=TRUE, orderqty="all",
ordertype="market", orderside="long",
replace=FALSE, prefer="Open"),
type="exit", path.dep=TRUE)
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="filterExit", sigval=TRUE, orderqty="all",
ordertype="market", orderside="long", replace=FALSE,
prefer="Open"),
type="exit", path.dep=TRUE)
#apply strategy
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)
test <- data.frame(round(t(tradeStats(portfolio.st)[-c(1,2)]),2))
test <- cbind(row.names(test), test)
list(test=test)
})
output$calendar <- renderDataTable({
returns <- data()$test
returns
})
})
server.R
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
从我的测试中看来,违规行似乎是
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] data.table_1.9.4 IKTrading_1.0
[3] roxygen2_4.1.1 digest_0.6.8
[5] Rcpp_0.11.6 quantstrat_0.9.1687
[7] foreach_1.4.2 blotter_0.9.1666
[9] FinancialInstrument_1.2.0 quantmod_0.4-4
[11] TTR_0.23-0 PerformanceAnalytics_1.4.3541
[13] xts_0.9-7 zoo_1.7-12
[15] shiny_0.12.1
loaded via a namespace (and not attached):
[1] chron_2.3-45 codetools_0.2-11 grid_3.1.1
[4] htmltools_0.2.6 httpuv_1.3.2 iterators_1.0.7
[7] jsonlite_0.9.16 lattice_0.20-29 magrittr_1.5
[10] mime_0.3 NLP_0.1-7 parallel_3.1.1
[13] plyr_1.8.2 R6_2.1.0 reshape2_1.4.1
[16] slam_0.1-32 stringi_0.5-2 stringr_1.0.0
[19] tm_0.6-1 tools_3.1.1 xtable_1.7-4
看起来符号的数据被拉出并保存,正如我所提到的,当我不想使用Shiny时,这段代码正常工作。任何帮助将不胜感激!
SessionInfo()
{{1}}
答案 0 :(得分:1)
对于谁可能会看到这篇文章,因为提供的解决方案@ kng229对我的案例不起作用,作为替代方案,尝试将assign("symbols", symbols, .GlobalEnv)
放在out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
之前。
答案 1 :(得分:0)
我不知道为什么会这样,但确实如此。在getSymbols行之后添加这个解决了这个问题!
saveSymbols(Symbols=symbols, file.path=stop(getwd()))