My own bitmex library. Reference library here
npm i nl-bitmex --save
As always, please test your code out on testnet before going live just to make sure there's no bugs in this library.
All contributions are welcome and appreciated. Open Source is a meritocracy who doesn't care who you are.
- Issues
- Pull Requests
- Donations (BTC: Tippin.me | Trading on bitmex referral saving you fees and giving me a little discount too)
const bitmex = require('nl-bitmex');
// Get the contents of the users bitcoin wallet
bitmex.customprivate({siteprefix: "www", endpoint: "user/wallet", querystring: "currency=XBt", apikey: "r", apisecret: ""}, (cb) => {
console.log(cb);
});
// Get deposit addresss
bitmex.customprivate({siteprefix: "www", endpoint: "user/depositAddress", apikey: "", apisecret: ""}, (cb) => {
console.log(cb);
});
- apikey (but can use environment variable too)
- apisecret (but can use environment variable too)
- siteprefix (Defaults to 'testnet')
bitmex.orders({siteprefix: "www"}, function(c) {
console.log(c);
});
bitmex.customprivate({siteprefix: "testnet", endpoint: "position/leverage", method: "POST", symbol: "ETHM17", leverage: 50, apikey: "", apisecret: ""}, (cb) => {
if (cb.message == "Done") {
console.log(cb.result);
} else if (cb.message == "Error") {
console.log(cb.error);
} else {
console.log(cb.message);
}
});
bitmex.customprivate({siteprefix: "www", endpoint: "order", method: "POST", symbol: "XBTUSD", orderQty: 1, apikey: "", apisecret: ""}, (cb) => {
console.log(cb);
});
bitmex.customprivate({siteprefix: "testnet", endpoint: "order/bulk", method: "POST", orders: JSON.stringify([{symbol: "ETHM17", orderQty: 1, ordType: "Market", side: "Buy"}, {symbol: "ETHM17", orderQty: 1, ordType: "Market", side: "Buy"}]), apikey: "xpdWaDC-40cQFxeJVzGqBsl9", apisecret: ""}, (cb) => {
if (cb.message == "Done") {
console.log(cb.result);
} else if (cb.message == "Error") {
console.log(cb.error);
} else {
console.log(cb.message);
}
});
bitmex.customprivate({siteprefix: "testnet", endpoint: "order", method: "POST", symbol: "ETHM17", orderQty: -5, ordType: "Stop", stopPx: "0.141", side: "Sell", apikey: "", apisecret: ""}, (cb) => {
if (cb.message == "Done") {
console.log(cb.result);
} else if (cb.message == "Error") {
console.log(cb.error);
} else {
console.log(cb.message);
}
});
bitmex.customprivate({siteprefix: "testnet", endpoint: "order", method: "POST", symbol: "ETHM17", orderQty: -5, ordType: "MarketIfTouched", stopPx: "0.145", side: "Sell", apikey: "", apisecret: ""}, (cb) => {
if (cb.message == "Done") {
console.log(cb.result);
} else if (cb.message == "Error") {
console.log(cb.error);
} else {
console.log(cb.message);
}
});
bitmex.customprivate({siteprefix: "www", endpoint: "order", method: "DELETE", querystring: "orderID=THEORDERID", apikey: "", apisecret: ""}, (cb) => {
console.log(cb);
});
Useful for determining whether to close out a position early or not.
This gets the positions of open and closed orders, or open only.
- apikey (but can use environment variable too)
- apisecret (but can use environment variable too)
- siteprefix (Defaults to 'testnet')
- isopen (Defaults to false, which will show all positions open and closed)
The below code grabs the current open positions
bitmex.position({siteprefix: "www", isopen: true}, function(c) {
if (c.message == "Done") {
for (var i = 0; i < c.position.length; i++) {
var row = c.position[i];
var symbol = row["symbol"];
var currency = row["currency"];
var underlyingCurrency = row["underlying"];
var contractsCount = row["currentQty"];
var realisedPnl = row["realisedPnl"];
var unrealisedPnl = row["unrealisedPnl"];
var openStatus = row["isOpen"];
var costPrice = row["avgCostPrice"];
var breakEvenPrice = row["breakEvenPrice"];
var lastPrice = row["lastPrice"];
var leverage = row["leverage"]
console.log("Instrument (#" + (i + 1).toString() + "): " + symbol + " (" + underlyingCurrency + " bought with " + currency + ") QTY=" + contractsCount.toString() + " Open=" + openStatus.toString() + " bought at=" + costPrice.toString() + " currently: " + lastPrice.toString() + " (P/L: Realised: " + (realisedPnl / 100000).toString() + " mBTC Unrealised: " + (unrealisedPnl / 100000).toString() + " mBTC)");
}
} else if (c.message == "Error") {
console.log("An error has occured: " + c.error);
} else {
console.log(c.message);
}
});