GithubHelp home page GithubHelp logo

bomper / bitmex-plus Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 6.0 31 KB

BitMEX (un)authenticated REST and WebSocket helper methods

License: MIT License

JavaScript 100.00%
bitmex cryptocurrency api api-client javascript exchange exchange-api

bitmex-plus's Introduction

bitmex-plus

This package provides several helper methods for the REST and WebSocket BitMEX APIs. You can think of it as a lightweight, easy to audit, combination of ccxt (which it doesn't use) and the official bitmex-realtime-api.

Features:

  • minimal dependencies
  • both authenticated and unauthenticated REST requests
  • real-time WebSocket API wrappers around the official bitmex-realtime-api library

The module works in Node, but not in the browser, because BitMEX doesn't serve CORS headers on the API.

Usage

npm install bitmex-plus

See the bitmex-realtime-api documentation for parameters.

import { BitMexPlus } from 'bitmex-plus';

const bitmex = new BitMexPlus({
  apiKeyID: '...',
  apiKeySecret: '...',
});

The API key constructor parameters are optional. You only need to specify them if you make authenticated requests.

The bitmex-realtime-api library that this module wraps will automatically connect to the web socket, so your script won't exit until all listeners are removed. If you only make REST requests, or when you're done with the WebSocket API, you can simply call process.exit(0).

Methods

makeRequest(verb, endpoint, data)

Make a GET/PUT/POST/DELETE request to the REST API. See the API explorer for available endpoints and parameters.

Sample unauthenticated request to get the bid/ask over the last minute:

import { BitMexPlus } from 'bitmex-plus';

const bitmex = new BitMexPlus();

(async function () => {
  const [bucket] = (await bitmex.makeRequest('GET', 'quote/bucketed', {
    binSize: '1m',
    symbol: 'XBTUSD',
    reverse: true,
    count: 1,
    columns: ['bidPrice', 'askPrice'],
  }));
  console.log(bucket);
}());

process.exit(0);

Authenticated request to get your XBTUSD position:

import { BitMexPlus } from 'bitmex-plus';

const bitmex = new BitMexPlus({
  // Get your API key at https://www.bitmex.com/app/apiKeys
  apiKeyID: 'REPLACE_ME',
  apiKeySecret: 'REPLACE_ME',
});

(async function () {
  const [pos] = (await bitmex.makeRequest('GET', 'position', {
    filter: { symbol: 'XBTUSD' },  // remove to get all positions
    // limit returned columns, except for several that are always returned:
    // account, symbol, currency, timestamp, simpleQty and markPrice
    columns: ['currentQty', 'liquidationPrice'],   }));
  console.log(pos);
}());

process.exit(0)

monitorStream(symbol, table, callback)

Add a stream and call the callback for each new data item added to the table (e.g. each new trade). This provides more granular processing than the official library, which calls the callback for chunks of data of unspecified length. The callback is passed a data parameter (e.g. a trade object).

Note that if you monitor the orderBookL2 stream, the callback will be called only once, without any parameters. This is because with order book entries, you need to deal with the entire data array returned by getData(), since orders might have been removed, changed, or inserted.

bitmex.monitorStream('XBTUSD', 'trade', data => console.log(data.price));

LICENSE

MIT. Copyright 2018 Ben Bomper.

bitmex-plus's People

Contributors

bomper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

bitmex-plus's Issues

Node-fetch POST request Signature not valid

Hello,

So I've checked my key permissions and the way I make a signature. Everything seems ok, but I can not make a request to the 'www.bitmex.com' as for 'testnet.bitmex.com' and keep getting 'Signature not valid' on my POST requests, therefore all GET requests are working fine.

I've doublechecked issues page but it didn't help.

Here's how it create a signature:

  let query = '';
  let postBody = '';
  if (verb === 'GET') {
    query = '?' + qs.stringify(data);
  } else {
    postBody = JSON.stringify(data);
  }
 
const signature = crypto.createHmac('sha256', apiSecret)
    .update(verb + apiRoot + endpoint + query + expires + postBody).digest('hex');

And the URL is built this way:

  if (verb !== 'GET') requestOptions.body = postBody; 
  const url = req_url + apiRoot + endpoint + query;

What I'm thinking about is that crypto works incorrectly.

If there are any updates on this issue, please, let me know

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.