-
-
Save vorandrew/514baadcbf772c9f8d69a1dae0b716d1 to your computer and use it in GitHub Desktop.
Example to Test Parameters on Moving Average System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#almost entirely based on the 02_text and 03_mpg examples provided by RStudio Shiny | |
#all credit belongs to them | |
if (!require(PerformanceAnalytics)) { | |
stop("This app requires the PerformanceAnalytics package. To install it, run 'install.packages(\"PerformanceAnalytics\")'.\n") | |
} | |
if (!require(quantmod)) { | |
stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n") | |
} | |
# Download data for a stock, if needed | |
require_symbol <- function(symbol) { | |
if (!exists(symbol)) | |
getSymbols(symbol, src="yahoo") | |
#getSymbols(symbol, from = "1900-01-01") | |
} | |
library(shiny) | |
# Define server logic required to summarize and view the selected dataset | |
shinyServer(function(input, output) { | |
make_chart <- function(symbol="SP500") { | |
# get price data if does not exist | |
require_symbol(symbol) | |
#would hope not to recalculate each time but for now will leave messy | |
price.monthly <- to.monthly(get(symbol))[,4] | |
ret.monthly <- ROC(price.monthly, type="discrete", n=1) | |
#calculate system returns | |
systemRet <- merge( | |
ifelse(lag(price.monthly > runMean(price.monthly, n=input$nmonths), k=1), 1, 0) * ret.monthly, | |
ret.monthly) | |
colnames(systemRet) <- c(paste(input$nmonths,"MASys",sep=""), symbol) | |
charts.PerformanceSummary(systemRet, ylog=TRUE) | |
} | |
make_table <- function(symbol="SP500") { | |
# get price data if does not exist | |
require_symbol(symbol) | |
#would hope not to recalculate each time but for now will leave messy | |
price.monthly <- to.monthly(get(symbol))[,4] | |
ret.monthly <- ROC(price.monthly, type="discrete", n=1) | |
#calculate system returns | |
systemRet <- merge( | |
ifelse(lag(price.monthly > runMean(price.monthly, n=input$nmonths), k=1), 1, 0) * ret.monthly, | |
ret.monthly) | |
colnames(systemRet) <- c(paste(input$nmonths,"MASys",sep=""), symbol) | |
table.Stats(systemRet) | |
} | |
# Generate a plot of the system and buy/hold benchmark given nmonths parameter | |
# include outliers if requested | |
output$systemPlot <- reactivePlot(function() { | |
make_chart(input$stock) | |
}) | |
# Generate a summary stats table of the dataset | |
output$view <- reactiveTable(function() { | |
make_table(input$stock) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#almost entirely based on the 02_text and 03_mpg examples provided by RStudio Shiny | |
#all credit belongs to them | |
library(shiny) | |
# Define UI for dataset viewer application | |
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Shiny Moving Average Parameter Test"), | |
# Sidebar with controls to select a dataset and specify the number | |
# of observations to view | |
sidebarPanel( | |
numericInput("nmonths", "Number of months for moving average:", 10), | |
textInput("stock", "Ticker", "AAPL") | |
), | |
# Show a summary of the dataset and an HTML table with the requested | |
# number of observations | |
mainPanel( | |
plotOutput("systemPlot"), | |
tableOutput("view") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment