[R-SIG-Finance] Strategy performance summary report

Mark Knecht markknecht at gmail.com
Wed Dec 29 19:25:10 CET 2010


On Wed, Dec 29, 2010 at 9:08 AM, Brian G. Peterson <brian at braverock.com> wrote:
> On 12/28/2010 07:56 PM, Klemen Koselj wrote:
>>
>> Hi,
>>
>> I have been learning R and trying to learn/use quanstrat/blotter for some
>> time now and I find it very nice. I would like to thank developers and the
>> community for creating and improving it.
>>
>> I have a very simple problem: I would like to create a strategy perfomance
>> summary report (since I can not find it in the package or in the
>> r-sig-finance list archives). I would like to display information about
>> strategy performance in simmilar way as is done here
>>
>> http://quantumfinancier.wordpress.com/2010/09/24/new-strategy-performance-summary/.
>> Most of the info was calculating using PerformanceAnalytics as shown
>> in
>>
>> http://quantumfinancier.wordpress.com/2010/06/26/support-vector-machine-rsi-system/.
>> I can replicate that. I am having problems with how to calculate trade
>> statistics Win%, Average/Median Trade/win/loss, Win/Loss ratio etc. The
>> only
>> example I found was from Josh Urlich
>> http://blog.fosstrading.com/2009/06/rsi2-evaluation.html unfortunately not
>> using blotter...
>>
>> My problem is that I do not (yet) understand blotter internal data
>> structures in order to be able to extract data needed for calculations. I
>> would very much appreciate if someone could share an example of how to get
>> this data from blotter.
>>
>> Thanks in advance for any hints/examples/...
>
> Attached please find the prototype of the function that will make it into
> blotter for trade stats (just as soon as I write the documentation).
>  Comments and additions always appreciated.
>
> Regards,
>
>  - Brian
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
There is no question that Brian has forgotten 100 times more about R
than I will ever know but as a second set of code to look at and
comment back on here's part of a larger function file I have in my
work. This assumes an easily definable trade format as input from
TradeStation. The first function reads the TS data, the second adds
the statistics to the data frame.

In the spirit of learning, better coders are encouraged to make me a
little better. (Nicely!) ;-)

Cheers,
Mark

## Read in csv file, convert header names to things R likes
## and handle bad characters in the number fields
## Returns a data.frame names for the trading system
readMSA = function (MyFile) {
	x <- read.csv(MyFile,      header = TRUE, sep = ",", quote="\"",
dec=".",fill = TRUE, comment.char="", skip=1,check.names=FALSE)
	names(x)[1] <- "Trade"
	names(x)[2] <- "SysNumber"
	names(x)[3] <- "Date"
	names(x)[4] <- "PL_Size"
	names(x)[5] <- "PS_Method"
	names(x)[6] <- "Size"
	names(x)[7] <- "PL_Pos"
	names(x)[8] <- "DDDollars"
	names(x)[9] <- "DDPercent"
	names(x)[10]<- "Equity"
	x$Trade     <- as.numeric(sub('[",]', '', x$Trade))
	x$PL_Size   <- as.numeric(sub('[",]', '', x$PL_Size))
	x$PL_Pos    <- as.numeric(sub('[",]', '', x$PL_Pos))
	x$DDDollars <- as.numeric(sub('[",]', '', x$DDDollars))
	x$DDPercent <- round(as.numeric(sub('[",]', '', x$DDPercent)),2)
	x$Equity    <- as.numeric(sub('[",]', '', x$Equity))
	return(x)
	}

#################################################################
## Main function to collect Tharp statistics on Trading System ##
#################################################################

DoTharp = function (System) {
	pos <- System$PL_Pos > 0
	System$Profit[pos] <- System$PL_Pos[pos]
	System$Loss[!pos]  <- System$PL_Pos[!pos]
	System$Winners <- cumsum(System$Profit > 0)
	System$Losers  <- cumsum(System$Loss < 0)
	System$TotalWins   <- cumsum(System$Profit)
	System$TotalLosses <- cumsum(System$Loss)
	System$AvgWin  <- as.integer(System$TotalWins / System$Winners)
	System$AvgLoss <- as.integer(System$TotalLosses / System$Losers)
	System$PercentWins <- round((System$Winners /
(System$Winners+System$Losers)),2)
	System$LargestWin <- cummax(System$Profit)
	System$LargestLoss <- cummin(System$Loss)
	System$TotalProfit <- System$TotalWins + System$TotalLosses
	System$ProfitFactor <- round(System$TotalWins/(-1*System$TotalLosses),2)
	System$AvgWinAvgLoss <- round(System$AvgWin / (-1*System$AvgLoss),2)
	System$EquityHigh <- cummax(System$TotalProfit)
	System$NewHigh <- ifelse(System$EquityHigh != System$TotalProfit,0,1)
	System$DrawDown <- System$EquityHigh - System$TotalProfit
	System$CumSumPL <- cumsum(System$PL_Pos)
	return(System)
	}



More information about the R-SIG-Finance mailing list