All pastes #2102113 Raw Edit

bitcoinica.user.js

public javascript v1 · immutable
#2102113 ·published 2012-01-11 09:02 UTC
rendered paste body
// ==UserScript==// @name Bitcoinica Helper// @version  0.9.1var version="0.9.1";// @description    Adds new functionality to Bitcoinica website// @include        https://bitcoinica.com/trading*// @author         m_r// @license        MIT// ==/UserScript==// Emulate unsafe window// https://gist.github.com/1143845window.unsafeWindow || (	unsafeWindow = (function() {		var el = document.createElement('p');		el.setAttribute('onclick', 'return window;');		return el.onclick();	}()));console.log(unsafeWindow);// Initalize scriptfunction USER_Init($, unsafeWindow) {	// Account data global variables	var bid, ask, usd, btc, margin, leverage, min_percent;	// Possible leverage levels	var leverageLevels = [1, 2.5, 5, 10];	// Percentage levels for forced liquidation	var liquidationLevels = [0.06, 0.16, 0.36, 0.96];	// Parse string with currency to its float value	function USER_ParseCurrency(value) {		var search = value.match(/[0-9,.]+/);		return parseFloat(search[0].replace(/,/, ""));	}	// Parse string with leverage	function USER_ParseLeverage(value) {		return parseFloat(value.substring(0, value.indexOf(":") - 1));	}	function USER_Update() {		bid = USER_ParseCurrency($('#buying-btcusd')[0].innerHTML);		ask = USER_ParseCurrency($('#selling-btcusd')[0].innerHTML);		// ACCOUNT OVERVIEW				// Get HTML of account overview		var account_overview = $('div#account_overview');		var account_data = account_overview.find('table.data').find('td');		// Exit if we're not on right page		if (!account_overview[0]) return;		// Parse the HTML to get the details		usd = USER_ParseCurrency(account_data[1].innerHTML);		btc = USER_ParseCurrency(account_data[3].innerHTML);		margin = USER_ParseCurrency(account_data[5].innerHTML);		leverage = USER_ParseLeverage(account_data[9].innerHTML);		net_value = USER_ParseCurrency(account_data[13].innerHTML);		maintenance = USER_ParseCurrency(account_data[15].innerHTML);		// Get level for forced liquidation based on leverage		min_percent;		for(var i = 0; i < leverageLevels.length; i++) {			if (leverage == leverageLevels[i]) min_percent = liquidationLevels[i];		}		// POSITIONS		// Check for Min. price header cell		var positionsHeaderCheck = $('th#min_price_header');		if (!positionsHeaderCheck[0])			$($('table#positions thead').find('th')[2]).after('<th id="min_price_header">Min.price</th>');		// Get all positions (usually only one)		var positions = $('table#positions tbody');		positions.each(function(index, position) { 			var position_data = $(position).find('td');			var amount = parseFloat(position_data[1].innerHTML);			var base_price = USER_ParseCurrency(position_data[2].innerHTML);			var min_price;			// Long			if (amount > 0)				min_price = Math.round((ask - ((net_value - maintenance)/(amount + btc))) * 10000) / 10000;			// Short			else				min_price = Math.round((bid - ((net_value - maintenance)/(amount - btc))) * 10000) / 10000;			// Add the price to the table			if (!positionsHeaderCheck[0])				$($(position).find('td')[2]).after('<td id="min_price">$' + min_price + '</th>');			else				$('min_price').html('$' + min_price);					});	}	// Update the data every time sort_positions() is called	var _sort_positions = unsafeWindow.sort_positions;	unsafeWindow.sort_positions = function () {		USER_Update();		_sort_positions();	}	USER_Update();	};USER_Init(unsafeWindow.jQuery, unsafeWindow);