Giter Club home page Giter Club logo

qbitmex's Introduction

qbitmex

kdb+/q interface for BitMex API (REST and WebSocket)

examples

0.1 implementation of HMACSHA256

For Windows, use qx.dll or build DLLs. For Linux, build shared objects.

https://github.com/ogay/hmac

https://github.com/bitmx/btceQ/blob/master/c/hmac512.cpp

or use CryptoQ : https://github.com/asatirahul/cryptoq

0.2 load the functions

c:>set SSL_VERIFY_SERVER=NO (linux: export ...)

c:>q qbitmex.q

0.3 settings: apiHost,apiKey,apiSecret

settings:`apiHost`apiKey`apiSecret!("testnet.bitmex.com";"";"")   //testnet
restapi[host;verb;path;data;apiKey;apiSecret]  // Not authenticating when apiKey or apiSecret = ""

r:restapi[settings`apiHost;"GET";"/api/v1/position";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/apiKey?reverse=false";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/chat?count=100&reverse=true";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/announcement";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/announcement/urgent";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/execution";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/execution/tradeHistory";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/funding";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/instrument";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/order";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/orderBook/L2?symbol=XBTUSD";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/quote?symbol=XBTUSD&reverse=true";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/schema";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/schema/websocketHelp";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/stats";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/trade?symbol=XBTUSD&count=5&start=0&startTime=2018-03-01 00:20:00";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/user";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/user/wallet";"";settings`apiKey;settings`apiSecret];r`body
r:restapi[settings`apiHost;"GET";"/api/v1/user/walletSummary";"";settings`apiKey;settings`apiSecret];r`body

wsapi[host;apiKey;apiSecret]: connect to websocket: Not authenticating when apiKey or apiSecret = ""

wsapi_cmd[wshandle;command]: send command

wshandle: the first element returned from wsapi[...], command: a dict for command args, ex: `op`args!(`subscribe;enlist `$"trade:XBTUSD")

wsapi_sub[wshandle;sub_args]: subscribe

r:wsapi[settings`apiHost; settings`apiKey; settings`apiSecret];  wsapi_sub[first[r];"trade:XBTUSD"]

wsapi_unsub[wshandle;unsub_args] :unsubscribe

wsapi_unsub[first[r];"trade:XBTUSD"] 

wsapi_ping[wshandle]: send ping

WebSocket API examples:

/subscribe trade

trade:([]timestamp:`timestamp$();price:`float$();size:`float$());

.z.ws:{xx::.j.k[x];if[key[xx]~`table`action`data;
	if[xx[`action]~"insert"; `trade insert select ltime`timestamp$"Z"$timestamp,`float$price,`float$size from xx[`data] ]];
	};

wsh:wsapi[settings`apiHost; settings`apiKey; settings`apiSecret];  

wsapi_sub[first[wsh];"trade:XBTUSD"]

wsapi_unsub[first[wsh];"trade:XBTUSD"]
wsapi_dt:.z.P;
WS:([]time:`time$();data:());
.z.ws:{wsapi_dt::.z.P; `WS insert (.z.T;x);0N!(.z.T;.z.w)};
wsh:wsapi[settings`apiHost; settings`apiKey; settings`apiSecret];  
wsapi_sub[first[wsh];"trade:XBTUSD"];
.z.ts:{if[00:00:05<.z.P-wsapi_dt; @[wsapi_ping;first wsh;`]];
    if[00:00:05<.z.P-wsapi_dt;
		@[hclose;first[wsh];`];
        wsh::.[wsapi;(settings`apiHost;settings`apiKey;settings`apiSecret);`];
        if[0<first wsh;.[wsapi_sub;(first[wsh];"trade:XBTUSD");`];wsapi_dt::.z.P];
        0N!(.z.Z;first[wsh];`reconnected);
	  ];};
system"t 1000";

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.