[Blotter-commits] r1742 - in pkg/blotter/R: . .Rproj.user .Rproj.user/2C25DC80 .Rproj.user/2C25DC80/pcs .Rproj.user/2C25DC80/sdb .Rproj.user/2C25DC80/sdb/per .Rproj.user/2C25DC80/sdb/prop .Rproj.user/2C25DC80/sdb/s-3A141202

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Apr 3 00:37:36 CEST 2016


Author: jaymon0703
Date: 2016-04-03 00:37:35 +0200 (Sun, 03 Apr 2016)
New Revision: 1742

Added:
   pkg/blotter/R/.Rhistory
   pkg/blotter/R/.Rproj.user/
   pkg/blotter/R/.Rproj.user/2C25DC80/
   pkg/blotter/R/.Rproj.user/2C25DC80/build_options
   pkg/blotter/R/.Rproj.user/2C25DC80/cpp-definition-cache
   pkg/blotter/R/.Rproj.user/2C25DC80/ctx/
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/debug-breakpoints.pper
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/files-pane.pper
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/source-pane.pper
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/windowlayoutstate.pper
   pkg/blotter/R/.Rproj.user/2C25DC80/pcs/workbench-pane.pper
   pkg/blotter/R/.Rproj.user/2C25DC80/persistent-state
   pkg/blotter/R/.Rproj.user/2C25DC80/presentation/
   pkg/blotter/R/.Rproj.user/2C25DC80/saved_source_markers
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/per/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/per/t/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/per/u/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/209001
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/27FA44DF
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/318E0CA1
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/4E8BB848
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/561640DF
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/842BCF43
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/965080CC
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/96D0BF0B
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/B69FB098
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/CCC9F2BC
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/F41C06CF
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/INDEX
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/32FAAB64
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/34B3D60D
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/47E8EEFD
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/A2AD7327
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/DDB53310
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/DEC3EC88
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/E52466C3
   pkg/blotter/R/.Rproj.user/2C25DC80/sdb/s-3A141202/F337EE61
   pkg/blotter/R/.Rproj.user/2C25DC80/viewer-cache/
   pkg/blotter/R/.Rproj.user/2C25DC80/viewer_history/
   pkg/blotter/R/R.Rproj
   pkg/blotter/R/mcsim.R
Log:
mcsim.R function to graph bands (5%:95%, 25%:50%) from Monte Carlo simulations of back-test results (using summary$EndEq)

Added: pkg/blotter/R/.Rhistory
===================================================================
--- pkg/blotter/R/.Rhistory	                        (rev 0)
+++ pkg/blotter/R/.Rhistory	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,512 @@
+is.na(ret[-1,])
+any(is.na(ret[-1,]))
+plot(ret)
+plot(cumsum(ret))
+ret <- ROC(EndEq)
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+print(h)
+EndEq
+ROC(EndEq)
+plot(ROC(EndEq))
+class(ret)
+#Version 4
+#playing with graphs and shading and legend
+t1 <- Sys.time()
+# Attach packages. You can install packages via:
+# install.packages(c("quantmod","TTR","PerformanceAnalytics"))
+library(quantmod)
+library(TTR)
+library(PerformanceAnalytics)
+library(ggplot2)
+library(timeSeries)
+#Read price data and build xts object
+data <- read.csv("C:/Users/jasen/Work/R/simSample.csv", header = TRUE, stringsAsFactors=F)
+s1.dates <- as.Date(data[,2], format="%d-%m-%Y")
+s1 <- xts(data[,3], s1.dates)
+#Calculate ROC
+ret <- ROC(s1[,1])
+#Chart cum returns
+chart.CumReturns(ret)
+#Set up for Sample() and Replicate()
+n <- 10000 #number of replications
+b <- TRUE #Sample with/without Replacement (Boolean)
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=b)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+#Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+#lets try some shading
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+t2 <- Sys.time()
+difftime(t2,t1)
+class(ret)
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+a$summary
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+EndEq
+class(EndEq)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+#' Retrieves the most recent value of the capital account
+#' @param Account string identifier of account
+#' @param n number of monte carlo simulations
+#' @return Graph ggplot of simulations
+# Build the function ----------------------------------------------
+mcsimr <- function(Account, n, Replace = TRUE){
+aname<-Account
+Account<-try(get(paste("account",aname,sep='.'), envir=.blotter), silent=TRUE)
+if(inherits(Account,"try-error"))
+stop("Account ", aname, " not found, use initAcct() to create a new account")
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+#   # Read price data and build xts object
+#   data <- read.csv("simSample.csv", header = TRUE, stringsAsFactors=F)
+#   s1.dates <- as.Date(data[,2], format="%d-%m-%Y")
+s1.dates <- index(a$summary)
+#   s1 <- xts(data[,3], s1.dates)
+# Calculate ROC
+ret <- ROC(EndEq)
+# Chart cum returns
+chart.CumReturns(ret)
+# Set up for Sample() and Replicate()
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+return(h)
+}
+# Build the function ----------------------------------------------
+mcsim <- function(Account, n, Replace = TRUE){
+aname<-Account
+Account<-try(get(paste("account",aname,sep='.'), envir=.blotter), silent=TRUE)
+if(inherits(Account,"try-error"))
+stop("Account ", aname, " not found, use initAcct() to create a new account")
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+#   # Read price data and build xts object
+#   data <- read.csv("simSample.csv", header = TRUE, stringsAsFactors=F)
+#   s1.dates <- as.Date(data[,2], format="%d-%m-%Y")
+s1.dates <- index(a$summary)
+#   s1 <- xts(data[,3], s1.dates)
+# Calculate ROC
+ret <- ROC(EndEq)
+# Chart cum returns
+chart.CumReturns(ret)
+# Set up for Sample() and Replicate()
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+return(h)
+}
+mcsimr("longtrend", 10000,)
+mcsim("longtrend", 10000,TRUE)
+# This is a very simple trend following strategy for testing the results of:
+# Faber, Mebane T., "A Quantitative Approach to Tactical Asset Allocation."
+# Journal of Risk Management (Spring 2007).
+# The article proposes a very simple quantitative market-timing model.  They
+# test the model in sample on the US stock market since 1900 before testing
+# it out-of-sample in twenty other markets.
+# The article discusses a 200-day simple moving average, which is proposed
+# in Jeremy Seigel's book "Stocks for the Long Run" for timing the DJIA.  He
+# concludes that a simple market timing strategy improves the absolute and
+# risk adjusted returns over a buy-and-hold strategy.  After all transaction
+# costs are included, the timing strategy falls short on the absolute return,
+# but still provides a better risk-adjusted return.  Siegel also tests timing on
+# the Nasdaq composite since 1972 and finds better absolute and risk adjusted
+# returns.
+# The article implements a simpler version of the 200-day SMA, opting for a
+# 10-month SMA.  Monthly data is more easily available for long periods of time,
+# and the lower granularity should translate to lower transaction costs.
+# The rules of the system are relatively simple:
+# - Buy when monthly price > 10-month SMA
+# - Sell and move to cash when monthly price < 10-month SMA
+# 1. All entry and exit prices are on the day of the signal at the close.
+# 2. All data series are total return series including dividends, updated monthly.
+#    For the purposes of this demo, we only use price returns.
+# 3. Cash returns are estimated with 90-day commercial paper.  Margin rates for
+#    leveraged models are estimated with the broker call rate.  Again, for the
+#    purposes of this demo, we ignore interest and leverage.
+# 4. Taxes, commissions, and slippage are excluded.
+# This simple strategy is different from well-known trend-following systems in
+# three respects.  First, there's no shorting.  Positions are converted to cash on
+# a 'sell' signal, rather than taking a short position. Second, the entire position
+# is put on at trade inception.  No assumptions are made about increasing position
+# size as the trend progresses.  Third, there are no stops.  If the trend reverts
+# quickly, this system will wait for a sell signal before selling the position.
+# Data
+# Instead of using total returns data, this demo uses monthly data for the SP500
+# downloaded from Yahoo Finance.  We'll use about 10 years of data, starting at
+# the beginning of 1998.
+# Load required libraries
+require(quantmod)
+require(TTR)
+require(blotter)
+Sys.setenv(TZ="UTC")
+# Try to clean up in case the demo was run previously
+try(rm("account.longtrend","portfolio.longtrend",pos=.blotter),silent=TRUE)
+try(rm("ltaccount","ltportfolio","ClosePrice","CurrentDate","equity","GSPC","i","initDate","initEq","Posn","UnitSize","verbose"),silent=TRUE)
+# Set initial values
+initDate='1997-12-31'
+initEq=100000
+# Load data with quantmod
+print("Loading data")
+currency("USD")
+stock("GSPC",currency="USD",multiplier=1)
+getSymbols('^GSPC', src='yahoo', index.class=c("POSIXt","POSIXct"),from='1998-01-01')
+GSPC=to.monthly(GSPC, indexAt='endof', drop.time=FALSE)
+# Set up indicators with TTR
+print("Setting up indicators")
+GSPC$SMA10m <- SMA(GSPC[,grep('Adj',colnames(GSPC))], 10)
+# Set up a portfolio object and an account object in blotter
+print("Initializing portfolio and account structure")
+ltportfolio='longtrend'
+ltaccount='longtrend'
+initPortf(ltportfolio,'GSPC', initDate=initDate)
+initAcct(ltaccount,portfolios='longtrend', initDate=initDate, initEq=initEq)
+verbose=TRUE
+# Create trades
+for( i in 10:NROW(GSPC) ) {
+# browser()
+CurrentDate=time(GSPC)[i]
+cat(".")
+equity = getEndEq(ltaccount, CurrentDate)
+ClosePrice = as.numeric(Ad(GSPC[i,]))
+Posn = getPosQty(ltportfolio, Symbol='GSPC', Date=CurrentDate)
+UnitSize = as.numeric(trunc(equity/ClosePrice))
+# Position Entry (assume fill at close)
+if( Posn == 0 ) {
+# No position, so test to initiate Long position
+if( as.numeric(Ad(GSPC[i,])) > as.numeric(GSPC[i,'SMA10m']) ) {
+cat('\n')
+# Store trade with blotter
+addTxn(ltportfolio, Symbol='GSPC', TxnDate=CurrentDate, TxnPrice=ClosePrice, TxnQty = UnitSize , TxnFees=0, verbose=verbose)
+}
+} else {
+# Have a position, so check exit
+if( as.numeric(Ad(GSPC[i,]))  <  as.numeric(GSPC[i,'SMA10m'])) {
+cat('\n')
+# Store trade with blotter
+addTxn(ltportfolio, Symbol='GSPC', TxnDate=CurrentDate, TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0, verbose=verbose)
+}
+}
+# Calculate P&L and resulting equity with blotter
+updatePortf(ltportfolio, Dates = CurrentDate)
+updateAcct(ltaccount, Dates = CurrentDate)
+updateEndEq(ltaccount, Dates = CurrentDate)
+} # End dates loop
+cat('\n')
+# Chart results with quantmod
+chart.Posn(ltportfolio, Symbol = 'GSPC', Dates = '1998::')
+plot(add_SMA(n=10,col='darkgreen', on=1))
+#look at a transaction summary
+getTxns(Portfolio="longtrend", Symbol="GSPC")
+# Copy the results into the local environment
+print("Retrieving resulting portfolio and account")
+ltportfolio = getPortfolio("longtrend")
+ltaccount = getAccount("longtrend")
+###############################################################################
+# Blotter: Tools for transaction-oriented trading systems development
+# for R (see http://r-project.org/)
+# Copyright (c) 2008 Peter Carl and Brian G. Peterson
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: longtrend.R 1483 2013-07-22 01:27:14Z bodanker $
+#
+###############################################################################
+# Build the function ----------------------------------------------
+mcsim <- function(Account, n, Replace = TRUE){
+aname<-Account
+Account<-try(get(paste("account",aname,sep='.'), envir=.blotter), silent=TRUE)
+if(inherits(Account,"try-error"))
+stop("Account ", aname, " not found, use initAcct() to create a new account")
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+#   # Read price data and build xts object
+#   data <- read.csv("simSample.csv", header = TRUE, stringsAsFactors=F)
+#   s1.dates <- as.Date(data[,2], format="%d-%m-%Y")
+s1.dates <- index(a$summary)
+#   s1 <- xts(data[,3], s1.dates)
+# Calculate ROC
+ret <- ROC(EndEq)
+# Chart cum returns
+chart.CumReturns(ret)
+# Set up for Sample() and Replicate()
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+return(h)
+}
+mcsim("longtrend", 10000,TRUE)
+Account <- "longtrend"
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+View(EndEq)
+s1.dates <- index(a$summary)
+ret <- ROC(EndEq)
+# Chart cum returns
+chart.CumReturns(ret)
+# Set up for Sample() and Replicate()
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+n <- 100
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+Replace = TRUE
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+h
+return(h)
+mcsim("longtrend", 100, TRUE)
+aname<-Account
+Account<-try(get(paste("account",aname,sep='.'), envir=.blotter), silent=TRUE)
+if(inherits(Account,"try-error"))
+stop("Account ", aname, " not found, use initAcct() to create a new account")
+mcsim <- function(Account, n = 100, Replace = TRUE){
+#   aname<-Account
+#   Account<-try(get(paste("account",aname,sep='.'), envir=.blotter), silent=TRUE)
+#   if(inherits(Account,"try-error"))
+#     stop("Account ", aname, " not found, use initAcct() to create a new account")
+#
+a <- getAccount(Account)
+EndEq <- a$summary$End.Eq
+#   # Read price data and build xts object
+#   data <- read.csv("simSample.csv", header = TRUE, stringsAsFactors=F)
+#   s1.dates <- as.Date(data[,2], format="%d-%m-%Y")
+s1.dates <- index(a$summary)
+#   s1 <- xts(data[,3], s1.dates)
+# Calculate ROC
+ret <- ROC(EndEq)
+# Chart cum returns
+chart.CumReturns(ret)
+# Set up for Sample() and Replicate()
+ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=Replace)) #use ret[-1] so we exclude 1st NA value from ROC calc
+ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
+ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
+# Build the 5% and 95% quantile datasets
+ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
+ret_5 <- as.xts(ret_5)
+ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
+ret_95 <- as.xts(ret_95)
+ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
+ret_25 <- as.xts(ret_25)
+ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
+ret_75 <- as.xts(ret_75)
+charts <- merge(ret_5, ret_95, ret_25, ret_75)
+# Draw the graph with a ribbon
+h <- ggplot(charts, aes(x = index(charts))) +
+geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
+geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
+theme(axis.text.x = element_text(angle=0, hjust = 0),
+axis.title = element_text(face = 'bold', size = 14),
+title = element_text(face = 'bold', size = 16),
+legend.position = 'bottom',
+legend.title = element_blank(),
+legend.text = element_text(size = 12),
+legend.key.width = unit(2, 'cm'))
+#h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
+h <- h + #geom_line(aes(y = EndEq[-1,])), colour = "black", linetype = 1) +
+ylab(label="Cumulative Returns") +
+xlab(label="Time") +
+ggtitle("Returns Distribution")
+#h
+return(h)
+}
+mcsim("longtrend", 100, TRUE)
+load_all()
+require(devtools)
+load_all()
+require(roxygen2)
+load_all()
+require(devtools)
+load_all()
+getwd()
+require(roxygen2)
+devtools::load_all(".")
+devtools::load_all(".")
+load_all()
+install.packages("Rtools")
+R CMD check blotter
+R CMD check <blotter>
+R CMD check "blotter"
+dev_tools::load_all
+require(devtools)
+devtools::load_all
+devtools::load_all()

Added: pkg/blotter/R/.Rproj.user/2C25DC80/build_options
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/build_options	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/build_options	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,4 @@
+auto_roxygenize_for_build_and_reload="1"
+auto_roxygenize_for_build_package="1"
+auto_roxygenize_for_check="1"
+makefile_args=""

Added: pkg/blotter/R/.Rproj.user/2C25DC80/cpp-definition-cache
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/cpp-definition-cache	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/cpp-definition-cache	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+[
+]
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/pcs/debug-breakpoints.pper
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/pcs/debug-breakpoints.pper	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/pcs/debug-breakpoints.pper	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,6 @@
+{
+    "debugBreakpointsState" : {
+        "breakpoints" : [
+        ]
+    }
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/pcs/files-pane.pper
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/pcs/files-pane.pper	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/pcs/files-pane.pper	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,13 @@
+{
+    "path" : "C:/Users/jasen/Personal/blotter/pkg/blotter/R",
+    "sortOrder" : [
+        {
+            "ascending" : false,
+            "columnIndex" : 1
+        },
+        {
+            "ascending" : true,
+            "columnIndex" : 2
+        }
+    ]
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/pcs/source-pane.pper
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/pcs/source-pane.pper	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/pcs/source-pane.pper	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,3 @@
+{
+    "activeTab" : 1
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/pcs/windowlayoutstate.pper
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/pcs/windowlayoutstate.pper	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/pcs/windowlayoutstate.pper	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,14 @@
+{
+    "left" : {
+        "panelheight" : 1798,
+        "splitterpos" : 734,
+        "topwindowstate" : "NORMAL",
+        "windowheight" : 1836
+    },
+    "right" : {
+        "panelheight" : 1798,
+        "splitterpos" : 1101,
+        "topwindowstate" : "NORMAL",
+        "windowheight" : 1836
+    }
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/pcs/workbench-pane.pper
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/pcs/workbench-pane.pper	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/pcs/workbench-pane.pper	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,4 @@
+{
+    "TabSet1" : 2,
+    "TabSet2" : 1
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/persistent-state
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/persistent-state	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/persistent-state	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,9 @@
+build-last-errors="[]"
+build-last-errors-base-dir="C:/Users/jasen/Personal/blotter/pkg/blotter/"
+build-last-outputs="[{\"output\":\"==> devtools::document(roclets=c('rd', 'collate', 'namespace'))\\n\\n\",\"type\":0},{\"output\":\"Updating blotter documentation\\r\\nLoading blotter\\r\\n\",\"type\":2},{\"output\":\"Writing AcctReturns.Rd\\r\\nWriting PortfReturns.Rd\\r\\nWriting addAcctTxn.Rd\\r\\nWriting addPortfInstr.Rd\\r\\nWriting addTxn.Rd\\r\\nWriting pennyPerShare.Rd\\r\\nWriting addDiv.Rd\\r\\n\",\"type\":1},{\"output\":\"Writing blotter-package.Rd\\r\\nWriting calcPortfWgt.Rd\\r\\nWriting calcPosAvgCost.Rd\\r\\nWriting calcTxnAvgCost.Rd\\r\\nWriting calcTxnValue.Rd\\r\\nWriting chart.ME.Rd\\r\\nWriting chart.Posn.Rd\\r\\nWriting chart.Reconcile.Rd\\r\\nWriting chart.Spread.Rd\\r\\nWriting extractTxns.Rd\\r\\nWriting getAccount.Rd\\r\\nWriting is.account.Rd\\r\\nWriting getByPortf.Rd\\r\\nWriting getBySymbol.Rd\\r\\nWriting getEndEq.Rd\\r\\nWriting getPortfAcct.Rd\\r\\nWriting getPortfolio.Rd\\r\\nWriting is.portfolio.Rd\\r\\nWriting getPos.Rd\\r\\nWriting getPosAvgCost.Rd\\r\\nWriting getPosQty.Rd\\r\\n\",\"type\":1},{\"output\":\"Writing getTxns.Rd\\r\\nWriting initAcct.Rd\\r\\nWriting initPortf.Rd\\r\\nWriting initPosPL.Rd\\r\\nWriting initSummary.Rd\\r\\nWriting initTxn.Rd\\r\\nWriting mcsim.Rd\\r\\nWriting perTradeStats.Rd\\r\\nWriting tradeQuantiles.Rd\\r\\nWriting put.account.Rd\\r\\nWriting put.portfolio.Rd\\r\\nWriting tradeStats.Rd\\r\\nWriting dailyTxnPL.Rd\\r\\nWriting updateAcct.Rd\\r\\nWriting updateEndEq.Rd\\r\\nWriting updatePortf.Rd\\r\\nWriting updatePosPL.Rd\\r\\n\",\"type\":1},{\"output\":\"Writing NAMESPACE\\r\\n\",\"type\":1},{\"output\":\"Documentation completed\\n\\n\",\"type\":1},{\"output\":\"==> Rcmd.exe INSTALL --no-multiarch --with-keep.source blotter\\n\\n\",\"type\":0},{\"output\":\"* installing to library 'C:/Users/jasen/Documents/R/win-library/3.2'\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* installing *source* package 'blotter' ...\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"make: Nothing to be done for `all'.\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** libs\\r\\n\",\"type\":1},{\"output\":\"installing to C:/Users/jasen/Documents/R/win-library/3.2/blotter/libs/x64\\r\\n\",\"type\":1},{\"output\":\"** R\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** data\\r\\n\",\"type\":1},{\"output\":\"** demo\\r\\n\",\"type\":1},{\"output\":\"** byte-compile and prepare package for lazy loading\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"Note: no visible binding for '<<-' assignment to '.blotter' \\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** help\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"*** installing help indices\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** building package indices\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** testing if installed package can be loaded\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* DONE (blotter)\\r\\n\",\"type\":1},{\"output\":\"\",\"type\":1}]"
+compile_pdf_state="{\"errors\":[],\"output\":\"\",\"running\":false,\"tab_visible\":false,\"target_file\":\"\"}"
+console_procs="[]"
+files.monitored-path=""
+find-in-files-state="{\"handle\":\"\",\"input\":\"\",\"path\":\"\",\"regex\":true,\"results\":{\"file\":[],\"line\":[],\"lineValue\":[],\"matchOff\":[],\"matchOn\":[]},\"running\":false}"
+imageDirtyState="1"
+saveActionState="-1"

Added: pkg/blotter/R/.Rproj.user/2C25DC80/saved_source_markers
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/saved_source_markers	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/saved_source_markers	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1 @@
+{"active_set":"","sets":[]}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/209001
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/209001	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/209001	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/27FA44DF
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/27FA44DF	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/27FA44DF	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/318E0CA1
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/318E0CA1	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/318E0CA1	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/4E8BB848
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/4E8BB848	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/4E8BB848	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/561640DF
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/561640DF	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/561640DF	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

Added: pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/842BCF43
===================================================================
--- pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/842BCF43	                        (rev 0)
+++ pkg/blotter/R/.Rproj.user/2C25DC80/sdb/prop/842BCF43	2016-04-02 22:37:35 UTC (rev 1742)
@@ -0,0 +1,3 @@
+{
+    "tempName" : "Untitled1"
+}
\ No newline at end of file

[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/blotter -r 1742


More information about the Blotter-commits mailing list