Skip to content

Instantly share code, notes, and snippets.

@F1LT3R
Last active November 23, 2020 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save F1LT3R/ea43bbbb7465de69fb6db12140e6cc39 to your computer and use it in GitHub Desktop.
Save F1LT3R/ea43bbbb7465de69fb6db12140e6cc39 to your computer and use it in GitHub Desktop.
TradingView Buy/Sell Helper
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'include', // include, *same-origin, omit
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
};
let hideGrowls = () => {
document.querySelector('[data-role="toast-container"]').setAttribute('style', 'hidden;');
}
let getBalance = () => {
const balance = parseFloat(document.querySelectorAll('.tv-account-manager__summary.js-summary .tv-account-manager__el.js-2 .tv-account-manager__data.js-collateral-value')[0].textContent.replace(/[\s\$]/g, ''));
return balance;
}
let lastOrderAmt = 0;
let buy = () => {
const balance = getBalance();
console.log(`Balance: ${balance}`);
const orderAmt = balance * 0.10;
console.log(`Order: ${orderAmt} (10% of balance)`);
let buyOrder = {
symbol: "COINBASE:BTCUSD",
side: "buy",
type: "market",
"qty": orderAmt
};
postData('https://papertrading.tradingview.com/trading/place/', buyOrder)
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
lastOrderAmt = orderAmt;
});
hideGrowls();
};
let displayValue = (value, color) => {
const elem = document.createElement('div');
elem.innerHTML = value;
elem.setAttribute('class', 'fooval');
elem.setAttribute('style', `
color: ${color};
font-size: 100px;
text-align: center;
margin-top: 10%;
z-index: 999999;
position: absolute;
top: 0;
left: 0;
width: 100%;
height 100%;
`);
const body = document.querySelector('body');
body.appendChild(elem);
setTimeout(() => {
body.removeChild(elem);
}, 1500);
}
let sell = () => {
let sellOrder = {
symbol: "COINBASE:BTCUSD",
side: "sell",
type: "market",
qty: lastOrderAmt
};
postData('https://papertrading.tradingview.com/trading/place/', sellOrder)
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
document.querySelectorAll('.tv-tabs__tab.js-tab')[5].click();
setTimeout(() => {
const cell = document.querySelectorAll('.tv-data-table.tv-data-table--trading-cqg.balances tr td')[3];
let color = "#CCC";
if (cell.className.includes('v-data-table__green-text')) {
color = "#0F0";
} else if (cell.className.includes('v-data-table__red-text')) {
color = "#F00";
}
const value = cell.textContent.replace(/\s/g, ',');
const valNum = parseFloat(cell.textContent
.replace(/\s/g, '')
.replace(/\$/g, '')
.replace(/\,/g, ''));
displayValue(`$${value}`, color);
console.log(valNum);
}, 1000)
});
hideGrowls();
};
function keyUp(e) {
if (e.ctrlKey && e.keyCode == 66) {
return buy();
}
if (e.ctrlKey && e.keyCode == 83) {
return sell();
}
}
// register the handler
document.addEventListener('keyup', keyUp, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment