Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address issue #66 #78

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 82 additions & 78 deletions R/chart.ME.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,93 +8,97 @@
#' @param Symbol string identifying the symbol to chart. If missing, the first symbol found in the \code{Portfolio} portfolio will be used
#' @param type string specifying MAE (Adverse) or MFE (Favourable) chart type
#' @param scale string specifying 'cash', or 'percent' for percentage of investment, or 'tick'
#' @param legend.loc string secifying chart legend position as in \pkg{xts}, default is \code{'bottomright'}
#' @param \dots any other passthrough parameters, in particular includeOpenTrades (see perTradeStats())
#' @author Jan Humme
#' @references Tomasini, E. and Jaekle, U. \emph{Trading Systems - A new approach to system development and portfolio optimisation} (ISBN 978-1-905641-79-6), section 3.5
#' @seealso \code{\link{perTradeStats}} for the calculations used by this chart,
#' and \code{\link{tradeStats}} for a summary view of the performance
#' @export
chart.ME <- function(Portfolio, Symbol, type=c('MAE','MFE'), scale=c('cash','percent','tick'), ...)
chart.ME <- function(Portfolio, Symbol, type=c('MAE','MFE'), scale=c('cash','percent','tick'), legend.loc='bottomright', ...)
{ # @author Jan Humme

type=type[1]
scale=scale[1] # can only take the first if the user doesn't specify

trades <- perTradeStats(Portfolio, Symbol, ...)

#multiply Pcct numbers for prettier charting
trades$Pct.Net.Trading.PL <- 100 * trades$Pct.Net.Trading.PL
trades$Pct.MAE <- 100 * trades$Pct.MAE
trades$Pct.MFE <- 100 * trades$Pct.MFE

profitable <- (trades$Net.Trading.PL > 0)

switch(scale,
cash = {
.ylab <- 'Profit/Loss (cash)'
if(type == 'MAE')
{
.cols <- c('MAE','Net.Trading.PL')
.xlab <- 'Drawdown (cash)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('MFE','Net.Trading.PL')
.xlab <- 'Run Up (cash)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
},
percent = {
.ylab <- 'Profit/Loss (%)'
if(type == 'MAE')
{
.cols <- c('Pct.MAE','Pct.Net.Trading.PL')
.xlab <- 'Drawdown (%)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('Pct.MFE','Pct.Net.Trading.PL')
.xlab <- 'Run Up (%)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
},
tick = {
.ylab <- 'Profit/Loss (ticks)'
if(type == 'MAE')
{
.cols <- c('tick.MAE','tick.Net.Trading.PL')
.xlab <- 'Drawdown (ticks)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('tick.MFE','tick.Net.Trading.PL')
.xlab <- 'Run Up (ticks)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
}
)

.main<-paste(Symbol,.main)

plot(abs(trades[, .cols]), type='n', xlab=.xlab, ylab=.ylab, main=.main)

grid()

points(abs(trades[ profitable, .cols]), pch=24, col='green', bg='green', cex=0.6)
points(abs(trades[!profitable, .cols]), pch=25, col='red', bg='red', cex=0.6)

abline(a=0, b=1, lty='dashed', col='darkgrey')


type=type[1]
scale=scale[1] # can only take the first if the user doesn't specify

trades <- perTradeStats(Portfolio, Symbol, ...)

#multiply Pcct numbers for prettier charting
trades$Pct.Net.Trading.PL <- 100 * trades$Pct.Net.Trading.PL
trades$Pct.MAE <- 100 * trades$Pct.MAE
trades$Pct.MFE <- 100 * trades$Pct.MFE

profitable <- (trades$Net.Trading.PL > 0)

switch(scale,
cash = {
.ylab <- 'Profit/Loss (cash)'
if(type == 'MAE')
{
.cols <- c('MAE','Net.Trading.PL')
.xlab <- 'Drawdown (cash)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('MFE','Net.Trading.PL')
.xlab <- 'Run Up (cash)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
},
percent = {
.ylab <- 'Profit/Loss (%)'
if(type == 'MAE')
{
.cols <- c('Pct.MAE','Pct.Net.Trading.PL')
.xlab <- 'Drawdown (%)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('Pct.MFE','Pct.Net.Trading.PL')
.xlab <- 'Run Up (%)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
},
tick = {
.ylab <- 'Profit/Loss (ticks)'
if(type == 'MAE')
{
.cols <- c('tick.MAE','tick.Net.Trading.PL')
.xlab <- 'Drawdown (ticks)'
.main <- 'Maximum Adverse Excursion (MAE)'
}
else # type == 'MFE'
{
.cols <- c('tick.MFE','tick.Net.Trading.PL')
.xlab <- 'Run Up (ticks)'
.main <- 'Maximum Favourable Excursion (MFE)'
}
}
)

.main<-paste(Symbol,.main)

plot(abs(trades[, .cols]), type='n', xlab=.xlab, ylab=.ylab, main=.main)

par(xpd = FALSE)
grid()

points(abs(trades[ profitable, .cols]), pch=24, col='green', bg='green', cex=0.6)
points(abs(trades[!profitable, .cols]), pch=25, col='red', bg='red', cex=0.6)

abline(a=0, b=1, lty='dashed', col='darkgrey')

if (!is.null(legend.loc)) {
legend(
x='bottomright', inset=0.1,
legend=c('Profitable Trade','Losing Trade'),
pch=c(24,25),
col=c('green','red'),
pt.bg=c('green','red')
x=legend.loc, inset=0.1,
legend=c('Profitable Trade','Losing Trade'),
pch=c(24,25),
col=c('green','red'),
pt.bg=c('green','red')
)
}
}

###############################################################################
Expand Down
4 changes: 3 additions & 1 deletion man/chart.ME.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.