Trading Strategies Performance Report with R and Knitr

I’ve been looking for template reports using R and Knitr for a while but I didn’t find anything that suits my needs so far. I therefore decided to create them myself.

What I like to see about trading strategies are basic performance charts (daily, monthly and yearly), some basic trading statistics and above all most recent performance figures to track potential strategy decay. I also want all of the information to be displayed on a single page. All this already exists obviously but not within a synthetic report like the one I present here. Therefore some custom formatting was required.

The input necessary to create this report is a simple 2 columns csv file with daily date and return. The output is a good looking pdf file (see image below).

I created a GistGitHub repository with 3 files:

  • performanceReportREADME.txt: it contains the usual disclaimer
  • performanceReportWithKnitr.R: This is the main R function that creates the charts and the input for LaTeX tables
  • performanceReport_AssetAllocation.Rnw: This file calls the R function above and generates the pdf report.

Any comments welcome

performanceReport

13 Responses to “Trading Strategies Performance Report with R and Knitr”


  • Great report! I’ve tried to build something similar but when I look at it after seeing this, I just think that my looks crude! :-)

  • Hi,

    Thank you for your valuable post.

    I already create something similar with R and Sweave, but not so detailed and well advanced. My main concerned was to create a report in landscape format.

    Do you know if it’s possible ?

    Have you ever think to do something with HTML et Microsoft Word for example ?

    Thanks

    • Thank you for the nice words.
      It’s definitely possible, it’s a LaTeX command. A simple Google search (landscape + LaTeX) gives the following: \usepackage[landscape]{geometry}
      There are many other options, it’s up to the user to chose one that best matches its need.
      I never considered HTML or Word, however a Shiny web apps is definitely something I have in mind for a while.
      Hope this helps

      • Hi,

        Thanks for your reply. I will try.

        According to shiny I agree with you, I’m currently thinking to create something like that for my blog, but I don’t know if it can be added easily, as an java applet for example, in a blog page. All examples that I see across internet, was full internet pages.

  • I tried to use the code using RStudio. Within performanceReport_AssetAllocation.Rnw file, I just changed the parameters of the source() and performanceReport() functions based on my directory. When I tried to compile PDF, RStudio complained:
    ——————————————–
    Processing code chunks with options …
    Error in match.arg(options$results, c(“verbatim”, “tex”, “hide”)) :
    ‘arg’ should be one of “verbatim”, “tex”, “hide”
    Calls: -> SweaveParseOptions -> check -> match.arg
    Error in rle(filenames) : ‘x’ must be an atomic vector
    Calls: -> SweaveParseOptions -> check -> match.arg
    Execution halted
    —————————————-

    Did I do anything wrong? Would you please help me out? Thanks a lot!

    • I use RStudio as well and I never came across this error but it sounds like a LaTeX error.
      I often have format differences when I run the same code on my laptop and my desktop. What I tend to do is running the LaTeX code directly from TeXworks (assuming you’re on Windows?). Have you tried that?
      You might also want to look at numbers formatting. The code I posted assumes that the format of return numbers are “0.05%”. Then inside the R code, this is turned into a format readable by R.

      HTH

      • Thank you for your reply. However, I still cannot get the code running. I checked the number formatting. It’s the same as you assumed (0.05%). When you say running LaTeX code directly from TeXworks, what is the LaTeX code referred here? Is that the code generated by running the Rnw file? When I ran the Rnw file, the generated tex file only has the similar content as the Rnw file up to the \maketitle line.

        I also tried a simple Rnw file:
        ————————-
        \documentclass{article}
        \begin{document}
        \SweaveOpts{concordance=TRUE}

        Hello World

        \end{document}
        ————————–

        This works fine. Can this rule out the LaTeX error?

        Any further suggestions?

        Thanks!

        • I had a closer look at your problem and it might come from your options setting inside RStudio.
          Check in Tools -> Options -> Sweave. From here you can chose either Sweave or Knitr. Choose the latest.
          Knitr uses results = ‘asis’ where Sweave uses results = ‘tex’ . That might explain your error message.

          Let me know how it goes.

  • Thank you for your reply. As you suggested, I changed the option to Knitr, since results was set to “asis” in the original code. However, when I tried to compile pdf, rstudio still runs into errors.

    ——————————–
    Line 63 Undefined control sequence.
    ——————————–

    In the output pdf file, there is a read line:

    ## Error: cannot open the connection.

    Any more suggestions? Thanks!

  • Nice looking report, thanks for sharing. is it possible to have a look at the input csv? I tried to test it out with some quantmod OHLC data but had problems with the date. Thanks,

    • I can’t share the csv file because it contains proprietary information but I can help you with the date formatting.
      On line 20 of the file performanceReportWithKnitr.R you get this “as.Date(dates, “%m/%d/%y”)” which means I use the following format: “01/14/92″. You can either change the input and keep my initial R code or change the R code and keep your input in its current format. Up to you.
      You might want to use the the latest version of the file as I update (minor changes) the Gist repository from time to time without notice

      Hope this helps

Leave a Reply