Skip to content

Instantly share code, notes, and snippets.

@DRN88
Last active November 27, 2021 18:26
Show Gist options
  • Save DRN88/4c4b37576318624199294ce01a36b840 to your computer and use it in GitHub Desktop.
Save DRN88/4c4b37576318624199294ce01a36b840 to your computer and use it in GitHub Desktop.
Extract tradingview data with tampermonkey script
// ==UserScript==
// @name Extract TradingView Databox on keypress
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.tradingview.com/chart/IQRwR53e/?symbol=USI%3APCC
// @icon https://www.google.com/s2/favicons?domain=tradingview.com
// @grant none
// @run-at document-end
// ==/UserScript==
//
// README
//
// WTF is this for?
// It is for capturing small amount of data from tradingview charts. This capture method is relatively slow.
// This souliton is good for testing market data, before purchasing it from your market data provider. etc.
//
// 1. Install tampermonkey extension import this script, edit target URL for "@match" in the header of this file
// 2. Open tradingview chart, open Data Window on the right side
// 3. Press F12, enter devtools, check out console tab for script output
// 4. Press the RIGHTARROW button on your keyboard, it will scroll the chart 1 bar to the right.
// Do not move your cursor while extracting the data.
// 5. When you are ready with your capture, press ALT + Q to print data to your console
// To reset array "d" reload your browser tab (F5)
// 6. Edit the script to your liking.
//
//
(function() {
'use strict';
var d = [];
var lastTime = '';
function onRightArrow() {
let chartDataWindow = document.getElementsByClassName('chart-data-window-body');
//console.log(chartDataWindow);
let date = (chartDataWindow[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].textContent).split('-').join('');
let time = (chartDataWindow[0].childNodes[1].childNodes[1].childNodes[0].childNodes[0].textContent).split(':').join('');
let open = chartDataWindow[1].childNodes[0].childNodes[1].childNodes[0].childNodes[0].textContent;
let high = chartDataWindow[1].childNodes[1].childNodes[1].childNodes[0].childNodes[0].textContent;
let low = chartDataWindow[1].childNodes[2].childNodes[1].childNodes[0].childNodes[0].textContent;
let close = chartDataWindow[1].childNodes[3].childNodes[1].childNodes[0].childNodes[0].textContent;
//console.log(date);
//console.log(time);
//console.log(open);
//console.log(high);
//console.log(low);
//console.log(close);
// Ninjatrader format: https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?importing.htm
// yyyyMMdd HHmmss;open price;high price;low price;close price;volume
if (lastTime != time) {
let item = `${date} ${time}00;${open};${high};${low};${close};1`;
d.push(item);
lastTime = time;
}
console.log(`${date} ${time} ${close}`);
}
function printData() {
console.log(d.join('\r\n'));
}
function onKeydown(evt) {
// Use https://keycode.info/ to get keys
// RightArrow
if (evt.keyCode == 39) {
onRightArrow();
}
// alt + q
if (evt.altKey && evt.keyCode == 81) {
printData();
}
}
document.addEventListener('keydown', onKeydown, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment