Skip to content

Instantly share code, notes, and snippets.

@thertrader
Last active June 17, 2023 15:59
Show Gist options
  • Save thertrader/8e777452b8e0e10320ee to your computer and use it in GitHub Desktop.
Save thertrader/8e777452b8e0e10320ee to your computer and use it in GitHub Desktop.
A Simple Shiny App for Monitoring Trading Strategies
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Please visit: <http://www.gnu.org/licenses/>.
###############################################################################
# Copyright (C) 2014 The R Trader
#
# For more information please visit my blog at www.thertrader.com
# or you can reach me at: TheRTrader at gmail
###############################################################################
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
date rtn
01/03/2012 0.25%
01/04/2012 0.01%
01/05/2012 0.02%
01/06/2012 -0.06%
01/09/2012 0.11%
01/10/2012 -0.01%
01/11/2012 0.00%
01/12/2012 -0.01%
1/13/2012 -0.01%
1/17/2012 -0.01%
1/18/2012 0.45%
1/19/2012 -0.01%
1/20/2012 0.00%
1/23/2012 -0.01%
1/24/2012 -0.01%
1/25/2012 -0.01%
1/26/2012 -0.01%
1/27/2012 0.09%
1/30/2012 -0.01%
1/31/2012 0.09%
02/01/2012 0.00%
02/02/2012 0.00%
02/03/2012 -0.01%
02/06/2012 0.00%
02/07/2012 0.00%
02/08/2012 -0.01%
02/09/2012 -0.01%
02/10/2012 -0.30%
2/13/2012 0.18%
2/14/2012 -0.01%
2/15/2012 -0.13%
2/16/2012 0.33%
2/17/2012 -0.01%
2/21/2012 -0.01%
2/22/2012 -0.05%
2/23/2012 0.14%
2/24/2012 -0.01%
2/27/2012 -0.09%
2/28/2012 0.00%
2/29/2012 0.00%
03/01/2012 0.21%
03/02/2012 -0.01%
03/05/2012 0.00%
03/06/2012 -0.55%
03/07/2012 0.39%
03/08/2012 0.45%
03/09/2012 0.00%
03/12/2012 -0.01%
3/13/2012 0.16%
3/14/2012 -0.01%
3/15/2012 0.18%
3/16/2012 0.00%
3/19/2012 0.06%
3/20/2012 -0.01%
3/21/2012 0.00%
3/22/2012 -0.10%
3/23/2012 0.10%
3/26/2012 0.30%
3/27/2012 -0.01%
3/28/2012 -0.05%
3/29/2012 -0.01%
3/30/2012 -0.01%
04/02/2012 0.15%
04/03/2012 -0.01%
04/04/2012 -0.01%
04/05/2012 -0.03%
04/09/2012 -0.30%
04/10/2012 -0.51%
04/11/2012 0.69%
04/12/2012 0.45%
4/13/2012 -0.17%
4/16/2012 0.08%
4/17/2012 0.37%
4/18/2012 -0.01%
4/19/2012 -0.15%
4/20/2012 0.03%
4/23/2012 -0.32%
4/24/2012 0.14%
4/25/2012 0.82%
4/26/2012 -0.01%
4/27/2012 -0.01%
4/30/2012 0.00%
05/01/2012 0.00%
05/02/2012 -0.01%
05/03/2012 -0.01%
05/04/2012 -0.31%
05/07/2012 0.16%
05/08/2012 -0.23%
05/09/2012 -0.26%
05/10/2012 0.07%
05/11/2012 -0.27%
5/14/2012 -0.77%
5/15/2012 -0.63%
5/16/2012 -0.66%
5/17/2012 -0.57%
5/18/2012 -0.70%
5/21/2012 1.20%
5/22/2012 -0.06%
5/23/2012 -0.08%
5/24/2012 -0.04%
5/25/2012 -0.07%
5/29/2012 0.64%
5/30/2012 -0.01%
5/31/2012 0.18%
06/01/2012 -0.29%
06/04/2012 0.38%
06/05/2012 0.12%
06/06/2012 0.57%
06/07/2012 -0.01%
06/08/2012 -0.24%
06/11/2012 -0.01%
06/12/2012 0.35%
6/13/2012 0.00%
6/14/2012 0.34%
6/15/2012 0.07%
6/18/2012 -0.01%
6/19/2012 0.00%
6/20/2012 0.00%
6/21/2012 0.00%
6/22/2012 0.36%
6/25/2012 -0.23%
6/26/2012 0.40%
6/27/2012 0.40%
6/28/2012 -0.05%
6/29/2012 0.44%
07/02/2012 -0.01%
07/03/2012 0.00%
07/05/2012 0.00%
07/06/2012 -0.01%
07/09/2012 -0.01%
07/10/2012 -0.01%
07/11/2012 -0.04%
07/12/2012 -0.01%
7/13/2012 0.28%
7/16/2012 0.00%
7/17/2012 0.03%
7/18/2012 0.00%
7/19/2012 -0.01%
7/20/2012 0.00%
7/23/2012 -0.39%
7/24/2012 -0.19%
7/25/2012 -0.08%
7/26/2012 0.63%
7/27/2012 -0.01%
7/30/2012 -0.01%
7/31/2012 -0.01%
08/01/2012 -0.25%
08/02/2012 -0.36%
08/03/2012 1.42%
08/06/2012 0.02%
08/07/2012 0.26%
08/08/2012 -0.07%
08/09/2012 -0.01%
08/10/2012 0.01%
8/13/2012 -0.01%
8/14/2012 -0.06%
8/15/2012 -0.13%
8/16/2012 0.12%
8/17/2012 -0.01%
8/20/2012 -0.01%
8/21/2012 -0.01%
8/22/2012 -0.05%
8/23/2012 -0.01%
8/24/2012 0.02%
8/27/2012 0.02%
8/28/2012 -0.05%
8/29/2012 -0.05%
8/30/2012 -0.31%
8/31/2012 0.35%
09/04/2012 -0.08%
09/05/2012 -0.07%
09/06/2012 0.76%
09/07/2012 -0.01%
09/10/2012 -0.01%
09/11/2012 0.17%
09/12/2012 0.04%
9/13/2012 0.00%
9/14/2012 -0.01%
9/17/2012 -0.07%
9/18/2012 -0.06%
9/19/2012 -0.01%
9/20/2012 -0.01%
9/21/2012 0.00%
9/24/2012 -0.09%
9/25/2012 -0.14%
9/26/2012 -0.52%
9/27/2012 0.46%
9/28/2012 -0.09%
10/01/2012 0.12%
10/02/2012 -0.03%
10/03/2012 -0.01%
10/04/2012 0.24%
10/05/2012 -0.02%
10/08/2012 -0.15%
10/09/2012 -0.10%
10/10/2012 -0.34%
10/11/2012 0.12%
10/12/2012 -0.09%
10/15/2012 0.38%
10/16/2012 0.16%
10/17/2012 0.00%
10/18/2012 0.06%
10/19/2012 -0.01%
10/22/2012 0.30%
10/23/2012 -0.07%
10/24/2012 -0.13%
10/25/2012 0.25%
10/26/2012 0.02%
10/31/2012 0.07%
11/01/2012 0.72%
11/02/2012 0.00%
11/05/2012 -0.06%
11/06/2012 -0.01%
11/07/2012 -0.01%
11/08/2012 -0.39%
11/09/2012 0.10%
11/12/2012 -0.01%
11/13/2012 -0.25%
11/14/2012 -0.60%
11/15/2012 0.18%
11/16/2012 0.20%
11/19/2012 0.67%
11/20/2012 -0.06%
11/21/2012 -0.01%
11/23/2012 -0.01%
11/26/2012 -0.01%
11/27/2012 -0.01%
11/28/2012 0.06%
11/29/2012 -0.01%
11/30/2012 -0.01%
12/03/2012 -0.01%
12/04/2012 0.02%
12/05/2012 0.14%
12/06/2012 0.23%
12/07/2012 0.00%
12/10/2012 0.02%
12/11/2012 0.00%
12/12/2012 0.00%
12/13/2012 -0.13%
12/14/2012 -0.07%
12/17/2012 0.51%
12/18/2012 0.14%
12/19/2012 0.00%
12/20/2012 0.09%
12/21/2012 -0.01%
12/24/2012 -0.01%
12/26/2012 -0.01%
12/27/2012 -0.03%
12/28/2012 0.00%
12/31/2012 0.83%
01/02/2013 0.42%
01/03/2013 -0.01%
01/04/2013 0.00%
01/07/2013 -0.01%
01/10/2013 0.13%
01/11/2013 0.00%
1/14/2013 0.00%
1/15/2013 0.00%
1/16/2013 -0.01%
1/17/2013 0.00%
1/18/2013 0.00%
1/22/2013 0.00%
1/23/2013 0.00%
1/24/2013 0.00%
1/25/2013 0.00%
1/28/2013 -0.01%
1/29/2013 0.18%
1/30/2013 0.14%
1/31/2013 0.00%
02/01/2013 0.47%
02/04/2013 -0.21%
02/05/2013 0.48%
02/06/2013 0.00%
02/07/2013 0.00%
02/08/2013 0.11%
02/11/2013 -0.01%
02/12/2013 -0.01%
2/13/2013 0.00%
2/14/2013 -0.12%
2/15/2013 -0.01%
2/19/2013 0.21%
2/20/2013 -0.01%
2/21/2013 -0.50%
2/22/2013 0.44%
2/25/2013 -0.18%
2/26/2013 0.66%
2/27/2013 0.15%
2/28/2013 0.01%
03/01/2013 0.07%
03/04/2013 -0.01%
03/05/2013 0.00%
03/06/2013 0.00%
03/07/2013 -0.01%
03/08/2013 -0.01%
03/11/2013 -0.01%
03/12/2013 0.00%
3/13/2013 0.02%
3/14/2013 0.06%
3/15/2013 0.00%
3/18/2013 -0.22%
3/19/2013 -0.05%
3/20/2013 0.08%
3/21/2013 -0.19%
3/22/2013 0.39%
3/25/2013 -0.02%
3/26/2013 0.51%
3/27/2013 0.00%
3/28/2013 -0.01%
04/01/2013 0.00%
04/02/2013 0.20%
04/03/2013 -0.08%
04/04/2013 0.69%
04/05/2013 -0.09%
04/08/2013 0.14%
04/09/2013 0.08%
04/10/2013 -0.01%
04/11/2013 0.00%
04/12/2013 0.00%
4/15/2013 -0.01%
4/16/2013 1.13%
4/17/2013 -0.01%
4/18/2013 -0.30%
4/19/2013 0.58%
4/22/2013 -0.01%
4/23/2013 -0.01%
4/24/2013 0.00%
4/25/2013 0.00%
4/26/2013 -0.04%
4/29/2013 -0.01%
4/30/2013 -0.01%
05/01/2013 -0.01%
05/02/2013 0.26%
05/03/2013 0.18%
05/06/2013 -0.01%
05/07/2013 0.08%
05/08/2013 -0.01%
05/09/2013 -0.01%
05/10/2013 0.00%
5/13/2013 -0.01%
5/14/2013 -0.01%
5/15/2013 -0.01%
5/16/2013 -0.01%
5/17/2013 0.26%
5/20/2013 0.00%
5/21/2013 0.00%
5/22/2013 -0.09%
5/23/2013 -0.58%
5/24/2013 -0.15%
5/28/2013 0.18%
5/29/2013 -0.15%
5/30/2013 0.08%
5/31/2013 -0.22%
06/03/2013 0.25%
06/04/2013 -0.11%
06/05/2013 -0.52%
06/06/2013 0.36%
06/07/2013 0.03%
06/10/2013 -0.20%
06/11/2013 -0.22%
06/12/2013 -0.17%
6/13/2013 0.83%
6/14/2013 -0.22%
6/17/2013 0.71%
6/18/2013 0.24%
6/19/2013 -0.08%
6/20/2013 -1.28%
6/21/2013 0.40%
6/24/2013 -0.38%
6/25/2013 0.53%
6/26/2013 0.15%
6/27/2013 -0.01%
6/28/2013 -0.02%
07/01/2013 0.13%
07/02/2013 -0.09%
07/03/2013 0.07%
07/05/2013 0.12%
07/08/2013 0.01%
07/09/2013 -0.01%
07/10/2013 -0.01%
07/11/2013 0.58%
07/12/2013 -0.01%
7/15/2013 0.00%
7/16/2013 0.00%
7/17/2013 0.06%
7/18/2013 0.08%
7/19/2013 -0.01%
7/22/2013 0.00%
7/23/2013 -0.01%
7/24/2013 -0.07%
7/25/2013 0.00%
7/26/2013 0.01%
7/29/2013 -0.01%
7/30/2013 0.05%
7/31/2013 -0.02%
08/01/2013 0.45%
08/02/2013 0.12%
08/05/2013 0.00%
08/06/2013 0.00%
08/07/2013 -0.21%
08/08/2013 0.13%
08/09/2013 -0.01%
08/12/2013 0.12%
8/13/2013 0.00%
8/14/2013 -0.01%
8/15/2013 -0.12%
8/16/2013 -0.13%
8/19/2013 -0.33%
8/20/2013 0.20%
8/21/2013 -0.43%
8/22/2013 0.89%
8/23/2013 0.20%
8/26/2013 0.00%
8/27/2013 -0.62%
8/28/2013 0.12%
8/29/2013 0.27%
8/30/2013 -0.41%
09/03/2013 0.48%
09/04/2013 0.09%
09/05/2013 -0.01%
09/06/2013 0.00%
09/09/2013 0.00%
09/10/2013 -0.01%
09/11/2013 -0.01%
09/12/2013 -0.01%
9/13/2013 0.08%
9/16/2013 0.00%
9/17/2013 0.00%
9/18/2013 -0.01%
9/19/2013 0.00%
9/20/2013 -0.11%
9/23/2013 -0.02%
9/24/2013 -0.05%
9/25/2013 -0.11%
9/26/2013 0.19%
9/27/2013 -0.09%
9/30/2013 -0.16%
10/01/2013 0.28%
10/02/2013 -0.01%
10/03/2013 0.00%
10/04/2013 0.14%
10/07/2013 0.00%
10/08/2013 -0.20%
10/09/2013 0.26%
10/10/2013 0.37%
10/11/2013 0.10%
10/14/2013 0.00%
10/15/2013 0.00%
10/16/2013 -0.01%
10/17/2013 0.00%
10/18/2013 -0.01%
10/21/2013 -0.01%
10/22/2013 0.00%
10/23/2013 -0.01%
10/24/2013 -0.02%
10/25/2013 0.02%
10/28/2013 0.00%
10/29/2013 0.05%
10/30/2013 -0.01%
10/31/2013 -0.01%
11/01/2013 -0.25%
11/04/2013 0.02%
11/05/2013 -0.01%
11/06/2013 -0.04%
11/07/2013 -0.12%
11/08/2013 0.30%
11/11/2013 -0.05%
11/12/2013 0.08%
11/13/2013 0.00%
11/14/2013 0.10%
11/15/2013 -0.01%
11/18/2013 -0.01%
11/19/2013 -0.05%
11/20/2013 -0.06%
11/21/2013 0.44%
11/22/2013 0.12%
11/25/2013 -0.01%
11/26/2013 -0.07%
11/27/2013 0.00%
11/29/2013 0.02%
12/02/2013 -0.20%
12/03/2013 -0.28%
12/04/2013 -0.12%
12/05/2013 -0.10%
12/06/2013 0.66%
12/09/2013 -0.01%
12/10/2013 -0.01%
12/11/2013 -0.33%
12/12/2013 -0.22%
12/13/2013 -0.02%
12/16/2013 0.26%
12/17/2013 -0.20%
12/18/2013 0.40%
12/19/2013 -0.03%
12/20/2013 -0.04%
12/23/2013 0.07%
12/24/2013 0.00%
12/26/2013 0.21%
12/27/2013 0.15%
12/30/2013 0.17%
12/31/2013 -0.01%
01/02/2014 -0.49%
01/03/2014 0.11%
01/06/2014 -0.10%
01/07/2014 0.38%
01/08/2014 -0.03%
01/09/2014 -0.05%
01/10/2014 0.09%
1/13/2014 -0.01%
1/14/2014 0.33%
1/15/2014 0.00%
1/16/2014 -0.01%
1/17/2014 -0.01%
1/21/2014 0.01%
1/22/2014 -0.01%
1/23/2014 0.00%
1/24/2014 -0.16%
1/27/2014 -0.54%
1/28/2014 0.13%
1/29/2014 -0.27%
1/30/2014 0.87%
1/31/2014 -0.01%
02/03/2014 -0.83%
02/04/2014 0.63%
02/05/2014 -0.03%
02/06/2014 0.13%
02/07/2014 -0.01%
02/10/2014 0.00%
02/11/2014 -0.01%
02/12/2014 -0.01%
2/13/2014 -0.01%
2/14/2014 -0.01%
2/18/2014 -0.01%
2/19/2014 -0.01%
2/20/2014 -0.05%
2/21/2014 -0.01%
2/24/2014 0.31%
2/25/2014 -0.01%
2/26/2014 -0.06%
2/27/2014 0.07%
2/28/2014 0.00%
03/03/2014 -0.23%
03/04/2014 0.48%
03/05/2014 -0.01%
03/06/2014 0.24%
03/07/2014 0.00%
03/10/2014 -0.02%
03/11/2014 -0.01%
03/12/2014 -0.09%
3/13/2014 -0.25%
3/14/2014 -0.11%
3/17/2014 0.32%
3/18/2014 0.12%
3/19/2014 -0.10%
3/20/2014 0.01%
3/21/2014 -0.01%
3/24/2014 0.05%
3/25/2014 0.12%
3/26/2014 -0.06%
3/27/2014 0.13%
3/28/2014 0.20%
3/31/2014 0.33%
04/01/2014 0.55%
04/02/2014 -0.01%
04/03/2014 0.00%
04/04/2014 -0.01%
04/07/2014 -0.34%
04/08/2014 -0.06%
04/09/2014 0.38%
04/10/2014 -0.01%
04/11/2014 -0.28%
4/14/2014 0.40%
4/15/2014 0.07%
4/16/2014 0.27%
4/17/2014 -0.01%
4/21/2014 0.00%
4/22/2014 -0.01%
4/23/2014 -0.01%
4/24/2014 -0.06%
4/25/2014 -0.01%
4/28/2014 0.05%
4/29/2014 0.17%
4/30/2014 0.00%
05/01/2014 0.00%
05/02/2014 0.00%
05/05/2014 -0.04%
05/06/2014 -0.14%
05/07/2014 0.09%
05/08/2014 0.00%
05/09/2014 0.15%
05/12/2014 0.15%
5/13/2014 -0.01%
5/14/2014 -0.01%
5/15/2014 -0.21%
5/16/2014 0.16%
5/19/2014 0.06%
5/20/2014 -0.01%
5/21/2014 0.15%
5/22/2014 -0.01%
5/23/2014 -0.01%
5/27/2014 0.00%
5/28/2014 -0.01%
5/29/2014 -0.01%
5/30/2014 -0.01%
06/02/2014 0.00%
06/03/2014 -0.01%
06/04/2014 -0.01%
06/05/2014 0.00%
###################################
# Server functions
#
# thertrader@gmail.com - June 2014
###################################
shinyServer(function(input, output) {
getData <- function(inputPath=inputPath,inputFile=inputFile,keepColumns=keepColumns){
data <- read.csv(paste(inputPath,inputFile,sep=""),sep=",")
keepColumns <- keepColumns
dataDaily <- data[,keepColumns]
colnames(dataDaily) <- c("date","rtn")
days <- as.Date(dataDaily[,"date"],"%m/%d/%Y")
dailyRtn <- as.numeric(substring(dataDaily[,"rtn"],1,nchar(as.character(dataDaily[,"rtn"]))-1)) ##
return(list(date=days,rtn=dailyRtn))
}
data <- reactive({
### ETF
if (input$strategy == "ETF"){
dt <- getData(inputPath="your_path",
inputFile="your_file.csv",
keepColumns=c("date","rtn"))
}
### EQUITY BOND ALLOCATION
if (input$strategy == "Equity Bond Allocation"){
dt <- getData(inputPath="your_path",
inputFile="your_file.csv",
keepColumns=c("date","rtn"))
}
### EQUITY INDEX
if (input$strategy == "Equity Index"){
dt <- getData(inputPath="your_path",
inputFile="your_file.csv",
keepColumns=c("date","rtn"))
}
### FX PRICE ACTION
if (input$strategy == "FX Price Action"){
dt <- getData(inputPath=your_path,
inputFile="your_file.csv",
keepColumns=c("date","rtn"))
}
### PORTFOLIO
if (input$strategy == "Portfolio"){
dt <- getData(inputPath="your_path",
inputFile="your_file.csv",
keepColumns=c("date","rtn"))
}
return(dt)
})
output$plot1 <- renderPlot({
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
if (posStart >= posEnd)
stop("EndingDate must be > StartingDate")
if (posStart < posEnd){
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
xDD <- as.vector(Drawdowns(y/100))
par(mfrow=c(2,1),cex=0.9,mex=0.4)
plot(x,cumsum(y),
type="l",
main=" Equity Curve (%)",
xlab="",
ylab="",
col="royal blue",
lwd=1.5)
grid(col="dark grey")
plot(x,100*xDD,
type="l",
xlab="",
ylab="",
main="DrawDowns (%)",
col="royal blue",
lwd=1.5)
grid(col="dark grey")
}
})
output$plot2 <- renderPlot({
# data <- dt
# input <- NULL
# input$startDate <- "2000-01-01"
# input$endDate <- "2014-01-01"
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
par(mfrow=c(2,1),cex=0.9,mex=0.6)
chart.VaRSensitivity(xts(y,order.by=x),
methods = c("HistoricalVaR", "GaussianVaR"),
colorset = bluefocus,
lwd = 2,
xlab="",
ylab="",
main="Risk Confidence Sensitivity")
aa <- c(min(rollapply(y,5,sum)),
min(rollapply(y,10,sum)),
min(rollapply(y,20,sum)),
min(rollapply(y,60,sum)),
min(rollapply(y,120,sum)))
bp <- barplot(aa,
border = NA,
col=c("light grey","sky blue 3","dark blue","royal blue","light blue"),
ylim=range(floor(min(aa)) - 1,0),
main="Worst 5,10,20,60,120 days return (%)")
abline(h=0)
text(bp,
aa,
labels=as.character(round(aa,2)),
pos=1)
})
output$tablePerformance <- renderTable({
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
dailyDD <- as.vector(Drawdowns(y/100))
nbDays <- length(x)
nbYears <- nbDays/252
totalReturn <- sum(y)
annualizedReturn <- round(totalReturn/nbYears,2)
annualizedVolatility <- round(sd(y)*sqrt(252),2)
sharpeRatio <- round(annualizedReturn/annualizedVolatility,2)
profitFactor <- round(sum(y[y > 0])/abs(sum(y[y < 0])),2)
rtnTable <- rbind(paste(annualizedReturn,"%",sep=""),paste(annualizedVolatility,"%",sep=""),sharpeRatio,profitFactor)
rownames(rtnTable) <- c("Ann.Return","Ann.Volatility","Sharpe Ratio","Profit Factor")
colnames(rtnTable) <- c("Performance")
rtnTable
})
output$tableRisk <- renderTable({
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
dailyDD <- as.vector(Drawdowns(y/100))
maxDD <- 100*round(min(dailyDD),3)
recoveryTime <- round(min(which(dailyDD[match(min(dailyDD),dailyDD):length(dailyDD)] == 0)),0)
painIndex <- round(PainIndex(y),2)
timeInMarket <- 100*round(length(which(y != 0))/length(y),2)
riskTable <- rbind(paste(maxDD,"%",sep=""),paste(recoveryTime," days",sep=""),painIndex,paste(timeInMarket,"%",sep=""))
rownames(riskTable) <- c("Max.DD","Recovery Time","Pain Index","% Time Invested")
colnames(riskTable) <- c("Risk")
riskTable
})
output$tableDaily <- renderTable({
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
avRtn <- round(mean(y,na.rm=TRUE),2)
avRtnPos <- round(mean(y[y >0],na.rm=TRUE),2)
avRtnNeg <- round(mean(y[y <0],na.rm=TRUE),2)
hitRatio <- 100*round(length(which(y > 0))/length(which(y != 0)),2)
worstDay <- round(min(y),2)
bestDay <- round(max(y),2)
dailyTable <- rbind(paste(avRtn,"%",sep=""),paste(avRtnPos,"%",sep=""),paste(avRtnNeg,"%",sep=""),paste(hitRatio,"%",sep=""),paste(worstDay,"%",sep=""),paste(bestDay,"%",sep=""))
rownames(dailyTable) <- c("Av. Rtn","Av. Rtn > 0","Av. Rtn < 0","Hit Ratio","Worst Day","Best Day")
colnames(dailyTable) <- c("Daily")
dailyTable
})
output$tableMonthly <- renderTable({
data <- data()
posStart <- min(which(as.Date(data$date,format="%Y-%m-%d") >= as.Date(input$startDate,format="%Y-%m-%d")))
posEnd <- max(which(as.Date(data$date,format="%Y-%m-%d") <= as.Date(input$endDate,format="%Y-%m-%d")))
x <- data$date[posStart:posEnd]
y <- data$rtn[posStart:posEnd]
months <- sort(unique(substring(x,1,7)))
monthlyRtn <- aggregate(y,by=list(substring(x,1,7)),sum)[,2]
monthlyHitRate <- 100*round(length(which(monthlyRtn > 0))/length(monthlyRtn),2)
monthlyRtnAverage <- round(mean(monthlyRtn),2)
monthlyRtnPositive <- round(mean(monthlyRtn[which(monthlyRtn > 0)]),2)
monthlyRtnNegative <- round(mean(monthlyRtn[which(monthlyRtn < 0)]) ,2)
worstMonth <- round(min(monthlyRtn),2)
bestMonth <- round(max(monthlyRtn),2)
monthlyTable <- rbind(paste(monthlyRtnAverage,"%",sep=""),paste(monthlyRtnPositive,"%",sep=""),paste(monthlyRtnNegative,"%",sep=""),paste(monthlyHitRate,"%",sep=""),paste(worstMonth,"%",sep=""),paste(bestMonth,"%",sep=""))
rownames(monthlyTable) <- c("Av. Rtn","Av. Rtn > 0","Av. Rtn < 0","Hit Ratio","Worst Month","Best Month")
colnames(monthlyTable) <- c("Monthly")
monthlyTable
})
session$onSessionEnded(function() {
stopApp()
q(“n”)
})
})
This Shiny application is designed to help analysing trading strategies. It is an ongoing project that I improve when time allows. Feel free to get in touch should you have any suggestion.
*How to use the App as it is?
The App uses as input several csv files (one for each strategy). Each file has two columns: date and daily return. There is an example of such a file in the Github repository. The code is essentially made of 3 files.
-ui.R: controls the layout and appearance of the app
-server.R: contains the instructions needed to build the app. You can load as much strategies as you want as long as the corresponding csv file has the right format (see below).
-shinyStrategyGeneral.R: loads the required packages and launches the app
put ui.R and server.R file in a separate directory
In the server.R file change the inputPath, inputFile and keepColumns parameters to match your setting. The first two are self explanatory the third one is a list of column names within the csv file. Keep only date and daily return.
*How to add a trading strategy?
-Create the corresponding csv file
-Create a new input in the data reactive function (within the server.R file)
-Add an extra element to the choice parameter in the first selectInput in the sidebarPanel (within the ui.R file). The element's name should match the name of the new input above.
*How to remove a trading strategy?
-Remove the input in the data reactive function corresponding to the strategy you want to remove (within the server.R file)
-Remove the element in the choice parameter in the first selectInput in the sidebarPanel corresponding to the strategy you want to remove (within the ui.R file).
###################################
# Launch Shiny App
#
# thertrader@gmail.com - June 2014
###################################
library(PerformanceAnalytics)
library(shiny)
library(xts)
runApp("D:\\daily\\shinyApps")
###################################
# UI functions
#
# thertrader@gmail.com - June 2014
###################################
theDates <- seq(as.Date("2000-01-01"),Sys.Date(),by="months")
shinyUI(
fluidPage(
headerPanel(""),
sidebarPanel(
selectInput("strategy",
"Strategy:",
choices = c("ETF","Equity Bond Allocation","Equity Index","FX Price Action","Portfolio")),
selectInput("startDate",
"Start:",
choices = theDates),
selectInput("endDate",
"End:",
choices = theDates,
selected = theDates[length(theDates)]),
div(HTML("<br>This app was created by <a href='http://thertrader.com' target='_blank'>the R Trader</a>.
<br>Powered by <a href='http://shiny.rstudio.com/' target='_blank'>Shiny</a>"))
),
mainPanel(
tabsetPanel(
## OVERVIEW
tabPanel("Overview", plotOutput("plot1",height = 550, width = 550)),
## TRADING STATISTICS
tabPanel("Trading Statistics",
fixedRow(
column(8,
fixedRow(column(4,tableOutput("tablePerformance")),
column(4,tableOutput("tableRisk"))),
fixedRow(column(4,tableOutput("tableDaily")),
column(4,tableOutput("tableMonthly"))))
)
),
## RISK
tabPanel("Risk",plotOutput("plot2",height = 550, width = 550)),
## HOW TO
tabPanel("How To",
HTML("<p> This Shiny application is designed to help analysing trading strategies. It is an ongoing project that I improve when time allows. Feel free to get in touch should you have any suggestion.</p>
<b>How to use the App as it is?</b>
<p>The App uses as input 3 csv files one for each strategy. Each has at least two columns: date and daily return. There is an example of such a file in the <a href='https://gist.github.com/thertrader/8e777452b8e0e10320ee' target='_blank'>Github</a> repository. The code is essentially made of 3 files.</p>
<li><i>ui.R</i>: controls the layout and appearance of the app</li>
<li><i>server.R</i>: contains the instructions needed to build the app. You can load as much strategies as you want as long as the corresponding csv file has the right format (see below).</li>
<li><i>shinyStrategyGeneral.R</i>: loads the required packages and launches the app</li>
<p>put ui.R and server.R file in a separate directory</p>
<p>In the server.R file change the inputPath, inputFile and keepColumns parameters to match your setting. The first two are self explanatory the third one is a list of column names within the csv file. Keep only date and daily return.</p>
<b>How to add a trading strategy?</b>
<li>Create the corresponding csv file</li>
<li>Create a new input in the data reactive function (within the server.R file)</li>
<li>Add an extra element to the choice parameter in the first selectInput in the sidebarPanel (within the ui.R file). The element's name should match the name of the new input above.</li>
<p></p>
<b>How to remove a trading strategy?</b>
<li>Remove the input in the data reactive function corresponding to the strategy you want to remove (within the server.R file)</li>
<li>Remove the element in the choice parameter in the first selectInput in the sidebarPanel corresponding to the strategy you want to remove (within the ui.R file).</li>
"))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment