An Addition to GDAX Exchange to Automatically Calculate Coin Amount in Limit Buy Order

If you are trading on GDAX digital currency exchange, you probably know the pain of calculating the limit buy order size if you want to use all or part of your USD/EUR balance, rather than specifying the number of coins. Sure, Excel helps, but copying and pasting takes time which, sometimes you cannot afford to “catch” that price you want to put the limit order at, so you can avoid fees.

Having said that, I created a small JavaScript you can inject into the page that will, hopefully, save you some time, as well as make you some money.

Here it is:

globalBalanceToApply = null;
globalBalanceToApply = null;
if (document.getElementById('globalBalanceToApplyInput') == null) {
	globalBalanceToApplyInput = document.createElement('input');
	globalBalanceToApplyInput.setAttribute('id', 'globalBalanceToApplyInput');
	globalBalanceToApplyInput.setAttribute('type', 'button');
	globalBalanceToApplyInput.setAttribute('style', 'width:18px;height:18px;display:inline-block;padding:0;margin:0 4px 0 8px;color:#008000;font-weight:bold;');
	globalBalanceToApplyInput.setAttribute('value', '$');
	globalBalanceToApplyInput.onclick = function(e) {
		var valText = window.prompt('Enter balance to apply: ');
		if (valText == null || valText == '') { return false; }
		var val = parseFloat(valText);
		if (isNaN(val) || val <= 0.00) { alert('Invalid balance to apply.'); return false; }
		globalBalanceToApply = val;
		calculateCoinAmount(false);
	}
	document.getElementsByTagName('article')[0].children[0].children[1].children[0].children[0].appendChild(globalBalanceToApplyInput);
}
if (document.getElementById('globalPlaceImmediateOrderCheck') == null) {
	globalPlaceImmediateOrderCheck = document.createElement('input');
	globalPlaceImmediateOrderCheck.setAttribute('id', 'globalPlaceImmediateOrderCheck');
	globalPlaceImmediateOrderCheck.setAttribute('type', 'checkbox');
	globalPlaceImmediateOrderCheck.setAttribute('style', 'margin: 0 4px;');
	document.getElementsByTagName('article')[0].children[0].children[1].children[0].children[0].appendChild(globalPlaceImmediateOrderCheck);
	var div = document.createElement('span');
	div.setAttribute('style', 'font-size: 8px;display:inline-block;');
	div.innerHTML = 'immediate
buy'; document.getElementsByTagName('article')[0].children[0].children[1].children[0].children[0].appendChild(div); } $('[class^="BalanceInfo_term-description_"]').onclick = function(e) { calculateCoinAmount(true); } function calculateCoinAmount(reset) { var usdBal = parseFloat(document.getElementsByTagName('article')[0].children[0].children[1].children[1].children[0].children[0].innerText); if (globalBalanceToApply != null && reset === false) { usdBal = globalBalanceToApply; } var form = document.getElementsByTagName('article')[1].children[0].children[0]; if (!form.children[0].children[1].className.includes('_active_')) { alert('Please select \'limit order\'.'); return false; } if (!form.children[1].children[0].className.includes('_active_')) { alert('Please select \'buy.\' side.'); return false; } var limitPrice = parseFloat(form.children[2].children[1].getElementsByTagName('input')[0].value); if (isNaN(limitPrice) || limitPrice <= 0.00) { var spread = parseInt(document.getElementById('page_content').children[0].children[2].children[1].children[0].children[1].children[1].children[0].children[1].children[1].children[0].innerText) + parseInt(document.getElementById('page_content').children[0].children[2].children[1].children[0].children[1].children[1].children[0].children[1].children[1].children[1].innerText) / 100; var midMarketPrice = parseFloat(document.getElementById('page_content').children[0].children[2].children[1].children[1].children[1].children[1].children[0].children[5].children[0].children[1].children[0].innerText); var limitPrice = Math.floor(Math.round((midMarketPrice - spread / 2) * 100)) / 100; form.children[2].children[1].getElementsByTagName('input')[0].value = '' + limitPrice; } var amount = Math.floor(usdBal / limitPrice * 100000000) / 100000000; form.children[3].children[1].innerText = usdBal.toFixed(2); setTimeout(function() { form.children[2].children[0].getElementsByTagName('input')[0].value = amount; if (!document.getElementById('globalPlaceImmediateOrderCheck').checked) { return; } form.children[2].children[0].getElementsByTagName('input')[0].focus(); form.getElementsByTagName('button')[0].click(); }, 10); globalBalanceToApply = usdBal; }

For those who want to break it down a bit, the script is very simple. First, we add a button so the amount you want to spend can be input and a checkbox that allows for placing the limit buy order right away, in order to catch that price that keeps changing very fast. These would be the first two if's. Then, we update the onlick event to call the

calculateCoinAmount

function that calculates the amount of coins, as well as places an order if the checkbox is clicked.

Now, you may have noticed the term "inject". If you are not too technical, here is what you need to do:

First, logon to your GDAX account and make sure you are in Trade view. Righ-click on the page and select Inspect from the context menu (if using IE for some weird reason, select "Inspect Element"). In the opened inspector window, switch to Console tab, copy and paste the Javascript from above and hit Enter. That's it! You should now see a button and a checkbox next to USD/EUR under the Balance section:

GDAX Add-on to Calculate Order Size

Here is how this works:

  • Make sure you have switched to LIMIT tab and selected the BUY side.
  • Enter the limit price you want to apply or keep the field blank to use the current best buy price.
  • If you click on your balance, the Amount will populate with the number of coins you can buy with your total current balance.
  • If you click on the $ button, you will be prompted to enter the balance you want to apply first and the Amount will populate with the number of coins you can buy with the entered amount.
  • If the "immediate buy" checkbox is checked, the script will "click" the "Place Order" button for you automatically.
This entry was posted in Blockchain & Digital Currency and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *