R/tradeGraphs.R

Defines functions tradeGraphs

Documented in tradeGraphs

#' Draw 3D graphs from tradeStats results using rgl
#'
#' @param stats a data frame generated by tradeStats()
#' @param free.params a vector of length 2, containing the column names for the data to use on the x and z axes
#' @param params.filter - a regular expression to reduce dimensions by filtering on certain columns
#' @param statistics a vector containing the column names to produce graphs for
#' @param title an optional title to be printed above each graph
#' @return invisible -- called for side-effect
#' @examples
#' \dontrun{
#' tradeGraphs (
#'      stats = stats,
#'      free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
#'      params.filter = "Param.indicator.2.nSlow < 40 & Param.indicator.1.nFast > 5"
#'      statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades")
#'      title = 'Luxor'
#' )
#' }
#' @author Jan Humme, rewritten by Chinmay Patil
#' @export

tradeGraphs <- function(stats, free.params, params.filter = NULL, statistics, title = NULL)
{
    # TODO: fix axes to use non scientific notation
    # TODO: fix use of full rainbow for small fractions (eg. Profit.Factor, now only uses red)

    if(!requireNamespace("rgl", quietly=TRUE))
        stop('The "rgl" package is required to use this function')

    if(!requireNamespace("reshape2", quietly=TRUE))
        stop('The "reshape2" package is required to use this function')
   
    if(missing(stats))      stop('stats undefined')
   
    if(missing(free.params))        stop('free.params undefined')
    if(length(free.params) != 2)    stop('free.params must be a vector of length 2')
       
    if(missing(statistics))         stop('must specify at least one statistics column to draw graph')

    var1 <- free.params[1]
    var2 <- free.params[2]

    for(var3 in statistics) {
        if (length(params.filter) == 0 ) {
            data <- stats[,c(var1, var2, var3)]
        } else {
            data <- subset(stats, eval(parse(text=params.filter)), select =  c(var1, var2, var3))
        }

        data_r <- reshape2::recast(data, as.formula(paste0(var1, " ~ ", var2)),
                                   id.var=c(var1,var2), measure.var=c(var3))
        x <- data_r[, 1]
        y <- as.numeric(colnames(data_r)[-1])
        z <- unlist(data_r[, -1])

        col <- heat.colors(length(z))[rank(z)]

        rgl::open3d()
        rgl::persp3d(x,y,z, color=col, xlab=var1, ylab=var2, zlab=var3, main = title)
    }
}

###############################################################################
# R (http://r-project.org/) Quantitative Strategy Model Framework
#
# Copyright (c) 2009-2015
# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, Garrett See, and Joshua Ulrich 
#
# This library is distributed under the terms of the GNU Public License (GPL)
# for full details see the file COPYING
#
# $Id$
#
###############################################################################
braverock/quantstrat documentation built on Sept. 15, 2023, 11:32 a.m.