Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created November 9, 2012 15:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timelyportfolio/4046316 to your computer and use it in GitHub Desktop.
Save timelyportfolio/4046316 to your computer and use it in GitHub Desktop.
Example to Test Parameters on Moving Average System
#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="FRED")
#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()
})
# Generate a summary stats table of the dataset
output$view <- reactiveTable(function() {
make_table()
})
})
#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)
),
# 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