Dan D Kim

Let's share stories

Chrome Snippets - Automatically Refresh Data on Questrade

2020-04-13 Dan D. Kimdebugging

TLDR: Copy-paste this snippet into a new Chrome Snippet (DevTools -> Sources -> Snippets), run the snippet (Ctrl + Enter), then open Questrade.


Hello all.

If you are like me and you do some light trading on Questrade, you probably didn’t pay for any add-ons that give you live-time data.

But you might be annoyed having to click that refresh button every time you want to see the real-time stock quotes.

If so, I created a little snippet for myself that clicks the button automatically every second. This script is running as long as the stock market is open, and automatically shuts itself off when the stock market closes.

Snippet: Javascript code that you can save in your browser and run it on any page

So without further ah-dew, here it is:

Steps

  1. Copy the script below
// Store loop id in the global scope
let loopId

function letsMakeMoney () {
  clearInterval(loopId)

  loopId = setInterval(clickRefreshButton, 1000)

  return loopId
}

function clickRefreshButton () {
  const buttons = document.querySelectorAll('button[title="Click or tap to get real-time quotes"]')
  buttons.forEach(click)
}

function click (element) {
  if (!element) {
    return
  }

  const clickEvent = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': false
  })

  element.dispatchEvent(clickEvent)

  console.log('refreshed')
}

letsMakeMoney()
  1. Open Chrome DevTools F12
  2. Go to Sources > Snippets in Chrome DevTools.

Snippet in Chrome DevTools

  1. Create a new Snippet.

Create a new Snippet

  1. Paste it in

Pasted code inside snippet

  1. Pess Ctrl + Enter and you’re done!

Console output Chrome console With a Chrome Snippet, the button is refreshed every second

  1. Don’t forget to Ctrl + S to save the snippet :)

(Optional) Ignore if the stock market is closed

For whatever reason, if you don’t want to be running document.querySelectorAll() outside of stock market hours, you can use the following script. The script below is also hosted here

Or do something better, go crazy :)

// Store loop id in the global scope
let loopId

function letsMakeMoney () {
  clearInterval(loopId)

  loopId = setInterval(clickRefreshButton, 1000)

  return loopId
}

function clickRefreshButton () {
  if (isStockMarketOpen()) {
    const buttons = document.querySelectorAll('button[title="Click or tap to get real-time quotes"]')
    buttons.forEach(click)
  } else if (isStockMarketClosed()) {
    // No point in refreshing when the stock market is closed
    stopRefreshing()
  } else {
    const message = 'Stock market isn\'t open yet'
    console.log(message)
  }
}

function isStockMarketOpen () {
  let currentTime = new Date()
  currentTime.setMinutes(currentTime.getMinutes() + currentTime.getTimezoneOffset())

  // Based on UTC time, TSX NYX opens at 12:30 and closes at 20:00
  let openingTime = new Date(currentTime)
  openingTime.setHours(13, 30, 0, 0)

  let closingTime = new Date(currentTime)
  closingTime.setHours(20, 0, 0, 0)

  const isWeekday = currentTime.getDay() > 0 && currentTime.getDay() < 6
  const isOpeningHours = currentTime >= openingTime && currentTime <= closingTime

  if (isWeekday && isOpeningHours) {
    return true
  } else {
    return false
  }
}

function isStockMarketClosed () {
  let currentTime = new Date()
  currentTime.setMinutes(currentTime.getMinutes() + currentTime.getTimezoneOffset())

  // Based on UTC time, TSX NYX opens at 12:30 and closes at 20:00
  let closingTime = new Date(currentTime)
  closingTime.setHours(20, 0, 0, 0)

  const isWeekend = [0, 6].includes(currentTime.getDay())
  const isAfterClosingTime = currentTime > closingTime

  if (isWeekend || isAfterClosingTime) {
    // Closed on weekends or after 8 PM UTC
    return true
  } else {
    return false
  }
}

function click (element) {
  if (!element) {
    return
  }

  const clickEvent = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': false
  })

  element.dispatchEvent(clickEvent)

  console.log('refreshed')
}

function stopRefreshing () {
  clearInterval(loopId)
  console.log(`Stock market is currently closed!`)
}

letsMakeMoney()

That’s all. Hope you find this useful. Happy investing! :)