Skip to content

Instantly share code, notes, and snippets.

@einSelbst
Forked from QuantBits/script.js
Last active June 2, 2021 13:47
Show Gist options
  • Save einSelbst/006585cb2e30ee85c66dbbeeb5b4c26c to your computer and use it in GitHub Desktop.
Save einSelbst/006585cb2e30ee85c66dbbeeb5b4c26c to your computer and use it in GitHub Desktop.
BitMex USD Converter - For TemperMonkey
// ==UserScript==
// @name BitMex USD Converter
// @namespace https://bitmex.com/
// @version 0.14
// @description Get some sanity into your gambling.
// @author koinkraft, @Jolly, einSelbst
// @grant none
// @include https://bitmex.com/*
// @include https://www.bitmex.com/*
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @downloadURL https://gist.githubusercontent.com/einSelbst/006585cb2e30ee85c66dbbeeb5b4c26c/raw/5caed2d20c5217e471720d500c060bd0304585ff/bitmex-usd-converter.js
// @updateURL https://gist.githubusercontent.com/einSelbst/006585cb2e30ee85c66dbbeeb5b4c26c/raw/5caed2d20c5217e471720d500c060bd0304585ff/bitmex-usd-converter.js
// ==/UserScript==
// 0.13 = Adds Margin Balance
// 0.14 = Refactoring, Improvements
// Updated to show BTC and USD prices in more locations.
// USD profit/loss does not taken into account the change in value of your BTC.
// ex - if you do a 1x btc short for 10kusd worth of btc, and BTC goes from 11k to 15k,
// bitmex will say you lost .19btc. my script will say you lost -2.7k USD. but your actual USD net p&l is 0. )
(function() {
'use strict';
// Script vars
let rates = new Map();
let currentBalance = {total: 0, avail: 0, margin:0};
const $ = window.jQuery;
// Extract all BitMex prices
// rework sly
const updateIndexPrices = () => {
let pairs = document.querySelectorAll("span.tickerBarItem.instrument");
pairs.forEach((pair) => {
//console.log(pair);
//console.log("pair setter");
let group_pair = pair.querySelector("a.symbol").innerHTML;
let group_price = pair.querySelector("span.price").innerHTML;
rates.set(group_pair, parseFloat(group_price));
//console.log(group_pair);
});
//console.log(pairs);
//console.log(rates);
setTimeout(function() {
updateIndexPrices();
}, 1000);
};
// Normalize position value, convert to XBT
const normalizeToXBT = (value, currencyDisplay) => {
if(currencyDisplay == 'XBT') {
return value;
} else if(currencyDisplay == 'mXBT') {
return value * 0.001;
} else if(currencyDisplay == 'XBt') {
return value * 0.00001;
} else if(currencyDisplay == 'μXBT') {
return value * 0.000001;
}
return false;
};
// Update PNLs
const updatePNLs = (setTimeoutCycle) => {
// Unrealized PNL
$('td.unrealisedPnl').each(function() {
let obj = this;
let content;
let isSpan = false;
if($(this).children('div:first-child').children('span').length > 0) {
content = $(this).children('div:first-child').children('span:first-child').html();
isSpan = true;
} else {
content = $(this).children('div:first-child').html();
}
let target = $(obj).children('div.unrealizedPnlUSD');
updateHtmlField(target, content);
});
// Realized PNL
$('td.combinedRealisedPnl').each(function() {
let obj = this;
let content = $(obj).children('.hoverContainer:first-child').children('.hoverHidden').children('span').html()
let contentHover = $(obj).children('.hoverContainer:first-child').children('.hoverVisible').children('.tooltipWrapper').children('span').html();
let target = $(obj).children('.realizedPNLContainer').children('.hoverHidden').children('span');
let targetHover = $(obj).children('.realizedPNLContainer').children('.hoverVisible').children('.tooltipWrapper').children('span');
updateHtmlField(target, content);
updateHtmlField(targetHover, contentHover);
});
if(setTimeoutCycle) {
setTimeout(function() {
updatePNLs(true);
}, 50);
}
};
// Update HTML
const updateHtmlField = (selector, originalContent) => {
var valueBTC, currencyDisplay;
[valueBTC, currencyDisplay] = originalContent.split(' ');
let valueUSD = normalizeToXBT(parseFloat(valueBTC), currencyDisplay) * rates.get("XBTUSD");
let content = '$ ' + (valueUSD).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' | ' + originalContent ;
if(selector.html() != content) {
selector.html(content);
selector[0].classList.toggle('neg', valueUSD < 0);
selector[0].classList.toggle('pos', valueUSD > 0);
}
};
// Initialize PNL wrapper
const initPNLWrapper = (setTimeoutCycle) => {
if($('td.unrealisedPnl').length > 0 && $('.unrealizedPnlUSD').length == 0) {
// Unrealized PNL
$('td.unrealisedPnl').css('position', 'relative');
$('td.unrealisedPnl > div').css('opacity', '0').css('position','absolute').css('left','0').css('top','0').css('right','0').css('bottom','0');
$('td.unrealisedPnl > div').after('<div class="unrealizedPnl unrealizedPnlUSD">0.00 USD (0.00%)</div>');
// Realized PNL
$('td.combinedRealisedPnl > .hoverContainer').hide();
$('td.combinedRealisedPnl > .hoverContainer').after('<span class="hoverContainer realizedPNLContainer"><span class="hoverVisible"><span class="tooltipWrapper"><span>0.00 USD</span></span></span><span class="hoverHidden"><span>0.00 USD</span></span></span>');
}
if(setTimeoutCycle) {
setTimeout(function() {
initPNLWrapper(true);
}, 100);
}
};
// Wait for window to load
$(window).load(function() {
// Init PNL Wrapper
initPNLWrapper(true);
$(window).resize(function() {
initPNLWrapper(false);
});
updateIndexPrices();
updatePNLs(true);
$('td.unrealisedPnl > div').hover(function() {
updatePNLs(false);
});
// Update Functions
setInterval(() => {
updateIndexPrices();
updatePNLs(true);
$('td.unrealisedPnl > div').hover(function() {
updatePNLs(false);
});
}, 45000);
});
})();
@AntoineRv
Copy link

Sorry @AntoineRv, I stoped using bitmex long time ago.

Ok, thanks for you reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment