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), open Questrade, and run the snippet (Ctrl + Enter).


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 is the snippet.

// 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()

Copy that script.

Go to Snippets in Chrome DevTools.

Snippet in Chrome DevTools

Create a new Snippet.

Create a new Snippet

Paste it in, press Ctrl + Enter and you’re done!

Console

Mine didn’t run because the stock market was closed at the time of writing this blog (because who has time to write blogs and shit when they gonna make bank?!)

But hey, if you want your script to run all the time and don’t care about whether or not your browser is running a useless script in the background all the time, you can use this shorter script instead.

// 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()

Now it will keep pressing that button FOREVER, unless you refresh or close that page.

Chrome console

With a Chrome Snippet, the button is refreshed every second


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