Importing TradingView Watchlist Into Google Sheets

Importing TradingView Watchlist Into Google Sheets

Last Updated on 23/05/2024 by Ndanileka

TradingView is a robust platform that offers a comprehensive suite of technical tools, stock screeners, and an internal programming language called Pine Script. Over the years it has become the go-to platform for many traders due to its powerful features. However, there are times when you might need to conduct in-depth analysis or direct data manipulation. In such cases using Google Sheets for interacting with the data can be a viable solution.

A part that lacks though, is integration between the two platforms and since there isn’t an official API, I built a custom function that returns the symbols from a Shared TradingView Watchlist as an array in Google Sheets. Once we have the symbols in Google Sheets, they can be used to fetch price data using the Google Finance function, providing a basic form of integration.

TradingView Watchlist

TradingView Watchlist of the DJIA

Google Sheets Import

TradingView Watchlist imported into googlesheets

Custom Function

The function retrieves the HTML content of the Tradingview shared watchlist page and extracts stock symbols. It takes a single parameter, which is the URL of the page.

Import Tradingview watchlist into googlesheets function
Function Description

Apps Script Code

/**
 * Fetches the HTML content of a TradingView shared watchlist page and extracts stock symbols.
 *
 * @param {string} url - The URL of the TradingView watchlist page.
 * @return {Array<string>} An array of stock symbols.
 * @customfunction
 */
function IMPORTWATCHLIST(url) {
  // Fetch the HTML content of the TradingView watchlist page
  var htmlContent = UrlFetchApp.fetch(url).getContentText();
  
  // Extract symbols from the HTML content
  return extractSymbols(htmlContent);
}

/**
 * Extracts stock symbols from the provided HTML content.
 *
 * @param {string} html - The HTML content of the TradingView watchlist page.
 * @return {Array<string>} An array of stock symbols.
 */
function extractSymbols(html) {
  // Regular expression to find the JSON object containing symbols
  const regex = /"symbols"\s*:\s*(\[[^\]]*\])/;
  const match = regex.exec(html);

  if (match) {
    try {
      // Parse JSON and return the array of symbols
      const symbolsArray = JSON.parse(match[1]);
      return symbolsArray; // Return the array of symbols
    } catch (e) {
      console.error("Error parsing symbols JSON:", e);
    }
  }

  return [];
}

Steps to Use the Function

  • Open Google Sheets: Create a new or open an existing Google Sheet.
  • Access Script Editor: Go to Extensions > Apps Script.
  • Add the Script: Copy and paste the above script into the script editor.
  • Save and Close: Save the script and close the editor.
  • Use the Function: In any cell, use the function like =IMPORTWATCHLIST(“URL”) where “URL” is your TradingView watchlist URL.

Known Issues

The script doesn’t update the data dynamically. If there are changes made to the TradingView watchlist, it won’t automatically update on the Google Sheet. To bring updates dynamically, you need to set up a script trigger. This trigger instructs the script to run on a specific event type.

Setting Up a Trigger for Dynamic Updates

  • Open Apps Script: Go to Extensions > Apps Script in your Google Sheet.
  • Navigate to Triggers: In the script editor, click on Triggers in the left menu.
  • Add Trigger: Click the Add Trigger button.
  • Choose Function: Select the function that fetches the symbols (IMPORTWATCHLIST).
  • Select Event Source: Here you choose what triggers the script to run:
    • From Spreadsheet: The script is triggered by actions within the spreadsheet.
    • Time-Driven: The script runs at a specific time or on a recurring schedule (e.g., every hour).
    • From Calendar: The script runs when a Google Calendar event happens. (Not relevant in this context)
  • Select Event Type: The options here depend on the chosen Event Source:
    • From Spreadsheet: Has three event types, on Open/Close/Edit
    • Time-Driven allows you to set a specific time (e.g., “hour”)
  • Save: Click Save to create the trigger


Alternative Manual Update

If your symbol list doesn’t require frequent changes, you can update the list manually. As in my case, I generally update my watchlist at the end of each quarter and my shortlists upon trade execution. 
You can refresh the list by simply adding or removing a forward slash (/) at the end of the URL. This minor change will force the script to fetch the symbols from the TradingView watchlist page. 

TradingVeiwUrl Example

Spreadsheet Template

The spreadsheet consists of the Script and Dow Jones Watchlist

Happy Trading!

If you don’t have a Trading View account and are considering getting one, please support me by signing up using my affiliate link.


Discover more from DEUS HARVEST

Subscribe to get the latest posts sent to your email.

Leave a Reply

Back To Top