Backtests

Build a Backtesting Dashboard in R Shiny in Just 10 Minutes! [Video Tutorial + Code]

Build a Backtesting Dashboard in R Shiny in Just 10 Minutes! [Video Tutorial + Code]

Introduction:

Creating a web dashboard might seem complex, especially if you lack web development experience. But with R Shiny, you can build a powerful, interactive dashboard in just minutes—no coding skills required! In this tutorial, I’ll walk you through the steps to create a backtesting dashboard in R Shiny in only 10 minutes.

Check out the full video tutorial below, and feel free to follow along with the provided code.

Video Tutorial:

📹 Watch the full video here:
https://youtu.be/iqO-IWvvZxo?si=Y4CucDEkXqAdD3pE

In this video, I demonstrate how easy it is to build a web-based dashboard from scratch. Whether you’re interested in finance or data analysis or want to learn R Shiny, this tutorial is perfect for beginners and experienced users.

Why R Shiny?

R Shiny is a powerful package that easily turns R code into interactive web applications. It allows you to build professional-looking dashboards without knowing HTML, CSS, or JavaScript. Here’s why R Shiny is a great choice:

  • User-Friendly: No need for web development experience.
  • Quick & Efficient: You can create dashboards in minutes.
  • Interactive: Ideal for data-driven applications and visualizations.

Code:

Below is the complete code I used to build the backtesting dashboard in R Shiny. Feel free to copy, modify, and experiment with it to suit your projects

library(shiny)
library(DT)

source("simple_backtest.R")

ui <- fluidPage(
  h1("Backtest"),
  selectInput('symbol', label = 'Symbol', choices = c('AAPL.US', 'TSLA.US', 'MSFT.US')), 
  numericInput('fast_ma', 'Fast MA', value = 10), 
  numericInput('slow_ma', 'Slow MA', value = 200), 
  plotOutput("chart_perf"), 
  DTOutput("tb_perf")
)

server <- function(input, output, session){
  
  calc_strategy <- reactive({
    
    symb <- input$symbol
    fast <- input$fast_ma
    slow <- input$slow_ma
    
    return <- calculate_simple_strategy(symb, fast, slow)
    
    return
    
  })
  
  output$chart_perf <- renderPlot({
    
    returns <- calc_strategy()
    
    charts.PerformanceSummary(returns)
    
  })
  
  output$tb_perf <- renderDataTable({
    
    returns <- calc_strategy()
    
    metrics <- table.AnnualizedReturns(returns)
    
    metrics
  })
  
}

shinyApp(ui, server)

Here is the code for the simple backtest function I used:

library(tidyverse)
library(TTR)
library(PerformanceAnalytics)
library(httr)
library(jsonlite) 

.token <- Sys.getenv('EOD_ticker') 

calculate_simple_strategy <- function(symbol = 'AAPL.US', fast_ma = 10, slow_ma = 200){
  
  df <- fromJSON(str_c('https://eodhd.com/api/eod/',symbol,'?api_token=' ,.token, '&fmt=json'))
  
  df <- df %>%
    mutate(
      date = as.Date(date)
    ) %>%
    arrange(date) %>%
    mutate(
      pnl = replace_na(adjusted_close / lag(adjusted_close) - 1, 0)
    ) %>%
    filter(date >= '2000-01-01')
  
  str <- df %>%
    arrange(date) %>%
    mutate(
      fast_ma = SMA(adjusted_close, fast_ma),
      slow_ma = SMA(adjusted_close, slow_ma),
      pos     = if_else(fast_ma > slow_ma, 1, 0) %>% lag() %>% replace_na(0),
      str_pnl = pos * pnl 
    )
   
  daily_xts <- xts(str$str_pnl, order.by = str$date)
  
  daily_xts
}

This code creates a simple backtesting dashboard where users can interact with the data by selecting different parameters. You can expand this template to include more advanced features like financial data integration, performance metrics, and custom visualizations.

Next Steps:

Once you’ve built your dashboard, try experimenting with additional features, such as:

  • Adding more interactive elements like dropdowns, checkboxes, and tables.
  • Connecting your dashboard to real-time data for more dynamic analysis.
  • Customizing the appearance of your dashboard using CSS.

Conclusion:

With R Shiny, creating web dashboards has never been easier! I hope this tutorial helps you get started on your projects. Don’t forget to watch the full video tutorial and download the code to follow along.

If you have any questions or need help, feel free to comment below or connect with me on social media.

Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news