A Simple Shiny App for Monitoring Trading Strategies – Part II

This is a follow up on my previous post “A Simple Shiny App for Monitoring Trading Strategies“.  I added a few improvements that make the app a bit better (at least for me!). Below is the list of new features :

  • A sample  .csv file (the one that contains the raw data)
  • A “EndDate”  drop down box allowing to specify the end of the period.
  • A “Risk” page containing a VaR analysis and a chart of worst performance over various horizons
  • A “How To” page explaining how to use and tailor the app to individual needs

I also made the app totally self contained. It is now available as a stand alone product and there is no need to have R/RStudio installed on your computer to run it. It can be downloaded from the R Trader Google drive account. This version of the app runs using portable R and portable Chrome. For the keen reader, this link explains in full details how to package a Shiny app into a desktop app (Windows only for now).

1 – How to install & run the app on your computer

  • Create a specific folder
  • Unzip the contain of the .zip file onto that new folder.
  • Change the paths in the runShinyApp file to match your setings
  • To run the app, you just have launch the run.vbs file. I also included an icon (RTraderTradingApp.ico) should you want to create a shortcut on your desktop.

2 – How to use the app as it is?

The app uses as input several csv files (one for each strategy). Each file has two columns: date and daily return. There is an example of such a file in the Github repository. The code is essentially made of 3 files.
  • ui.R: controls the layout and appearance of the app
  • server.R: contains the instructions needed to build the app. You can load as much strategies as you want as long as the corresponding csv file has the right format (see below).
  • shinyStrategyGeneral.R: loads the required packages and launches the app
put ui.R and server.R file in a separate directory
In the server.R file change the inputPath, inputFile and keepColumns parameters to match your settings. The first two are self explanatory the third one is a list of column names within the csv file. Keep only date and daily return.

3 – How to add a trading strategy?

  • Create the corresponding .csv file in the right directory
  • Create a new input in the data reactive function (within the server.R file)
  • Add an extra element to the choice parameter in the first selectInput in the sidebarPanel (within the ui.R file). The element’s name should match the name of the new input above.

4 – How to remove a trading strategy?

  • Remove the input in the data reactive function corresponding to the strategy you want to remove (within the server.R file)
  • Remove the element in the choice parameter in the first selectInput in the sidebarPanel corresponding to the strategy you want to remove (within the ui.R file).

Please feel free to get in touch should you have any suggestion.

 

 

13 Comments

  1. O Popoola says:

    Hi

    Thanks for providing this app. Just a suggestion. Perhaps a few screenshots of the app will bring your description to life.

  2. O Popoola says:

    ignore the above post. I have seen some screenshots in the first article. thanks

  3. MusX says:

    Is there any reason why you could not provide server.R, ui.R and runShinyApp.R instead of full portable chrome and portable R?
    Regards

  4. Chuck says:

    This is great, but there is an issue with the R session not ending when you close the browser. This is easily fixed by adding the code below to your server.R file. Taken directly from here: http://blog.analytixware.com/2014/03/packaging-your-shiny-app-as-windows.html

    Now WAIT! Don’t click on run.vbs just yet! I advise you to add the following to the server.R inside the shinyServer(function(input, output, session) { … }). Please make sure you pass session as the third argument!

    # close the R session when Chrome closes
    session$onSessionEnded(function() {
    stopApp()
    q(“no”)
    })

    • The R Trader says:

      Chuck,

      Thank you for reaching out and the for the tips. You’re right the R session is not ending when you close the browser I just realised that.
      I’m going to test your suggestions in the next few days and post the necessary changes on the Gist repository.

  5. macRo says:

    I’m working on making Shiny apps more usable and this post is perfect. Thank you for sharing your knowledge.

    Is it possible to fetch data from Bloomberg (using rbbg or rblpapi) and create Shiny apps? I’m a recent grad and I don’t have access to Bloomberg yet, but I’m wondering if Shiny can pull BBG data once I start working. Thanks again.

    • The R Trader says:

      Hi,

      It really depends on what you mean by fetching data from Bloomberg. If you mean historical data (daily and/or intraday) this is perfectly doable. Just use the standard tools (rbbg or rblpapi) to store data onto an R object that you will use in Shiny: very straightforward. This is exactly what this post does using other data sources: Introducing fidlr: FInancial Data LoadeR
      Now if you mean streaming live data from Bloomberg (or any other provider) into a Shiny app it might be more tricky. I’ve never done it but I would be curious to know if anyone has ever tried his.

      HTH

      Arnaud

  6. macRo says:

    Thanks, I meant historical daily and this is great to know. Definitely curious to learn more about pulling real time data as well.

  7. Ankita says:

    Hi Arnaud,

    I’m new to finance, and I think your site is a great place for me to learn some hands-on skills. I’m also fairly new to computer programming. I would really appreciate if you told me what you mean by “Change the paths in the runShinyApp file to match your settings.”

    Thanks,
    Ankita

  8. abd-ul-Basit says:

    Peace be on you brother,

    Really appreciate you sharing this app. Really like your work at R-Bloggers page as well.

    Thanks and Kind Regards

Leave a Reply