I am trying to make a web app with the intention of using quantstrat. However I am having a bit of difficulty integrating the two. There is no documentation on this so it's tough to find a place to start. Here is the code I have right now. It would be much appreciated if you could let me know what I am doing wrong. Thank you
library(shiny)
library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(png)
library(dplyr)
Sys.setenv(TZ = "UTC")
currency('USD')
ui <- fluidPage(
# Application title
titlePanel("myfirst"),
sidebarLayout(
sidebarPanel(
selectInput(
"stocks", label = "chose stock", choices =
c("AAPL", "CAT")
),
dateInput("init_date", "chose init date",
value = Sys.Date() -100),
dateInput("start_date", "chose start date",
value = Sys.Date() - 99),
dateInput("end_date", "chose end date",
value = Sys.Date()),
selectInput("init_equity", "starting
equity", choices = c(1000, 50000))
),
mainPanel(
plotOutput("plot"),
textOutput("text")
)
)
)
server <- function(input, output) {
init_date = reactive({
input$init_date
})
start_date = reactive({
input$start_date
})
end_date = reactive({
input$end_date
})
init_equity = reactive({
input$init_equity
})
V = reactive({
getSymbols(input$stocks, from = start_date(),
to = end_date(), index.class = "POSIXct",
adjust = T)
})
observe({
stock(input$stocks, currency = "USD", multiplier
= 1)
})
portfolio.st = account.st = strategy.st =
"my.first"
rm.strat(portfolio.st)
rm.strat(account.st)
observe({
initPortf(name = portfolio.st,
symbols = "V",
initDate = init_date())
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date(),
initEq = init_equity())
initOrders(portfolio = portfolio.st,
symbols = "V",
initDate = init_date()
)
strategy(strategy.st, store = T)
})
observe({ add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x =
quote(Cl(mktdata)),
n = 10),
label = "nFast")
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x =
quote(Cl(mktdata)),
n = 30),
label = "nSlow")
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("nFast", "nSlow"),
relationship = "gte"),
label = "long")
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("nFast", "nSlow"),
relationship = "lt"),
label = "short")
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderqty = 100,
ordertype = "stoplimit",
orderside = "long",
threshold = 0.0005,
prefer = "High",
TxnFees = -10,
replace = FALSE),
type = "enter",
label = "EnterLONG")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderqty = -100,
ordertype = "stoplimit",
threshold = -0.005,
orderside = "short",
replace = FALSE,
TxnFees = -10,
prefer = "Low"),
type = "enter",
label = "EnterSHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderside = "long",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
type = "exit",
label = "Exit2SHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderside = "short",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
type = "exit",
label = "Exit2LONG")
applyStrategy(strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
})
output$plot = reactive(
chart.Posn(portfolio.st, Symbol = "V")
)
}
# Run the application
shinyApp(ui = ui, server = server)