Skip to content

Repository files navigation

BitMEX

CircleCI

BitMEX client library for Elixir.

Documentation

See the online documentation for more information.

Installation

Add :bitmex to your list of dependencies in mix.exs:

defdepsdo[{:bitmex,"~> 0.2"}]end

Add your app's api_key and api_secret to config/config.exs:

config :bitmex, api_key: ""
config :bitmex, api_secret: ""
config :bitmex, test_mode: false

Set test_mode to true if you want to simulate your app using BitMEX Testnet instead of the production version.

REST API

You may call methods in modules Bitmex.Rest.* (eg. Bitmex.Rest.OrderBook) to access the REST API.

View the Hex Documentation and BitMEX API Explorer for a full list of endpoints and return types.

Usage Examples

Position

Get the current position:

Bitmex.Rest.Position.get()

Orders

Get all your open orders:

Bitmex.REST.Order.get_open()

Create a order:

params_bi=%{"symbol"=>"XBTUSD","side"=>"Buy","orderQty"=>15,"ordType"=>"Market"}Bitmex.Rest.Order.create(params_bi)

Create a bulk order:

p1=%{"symbol"=>"XBTUSD","side"=>"Buy","orderQty"=>15,"price"=>4000.1,"ordType"=>"Limit"}Bitmex.Rest.Order.create_bulk(%{orders: [p1,p1]})

Rate Limit

You may query the rate limit counter using Bitmex.Rest.RateLimiter.remaining(). It automatically logs the rate limit info responded from your last REST API request.

WebSocket API

To enable WebSocket subscriptions, use Bitmex.WS module and override the handle_response function:

defmoduleCaravan.WS.MessageHandlerdouseBitmex.WSdefhandle_response(resp),do: Caravan.WS.process(resp)enddefmoduleCaravan.WSdorequireLoggerimportTask.Supervisor,only: [start_child: 2]# APIdefstart_link(opts\\[])doAgent.start_link(fn->[]end,opts)enddefprocess(resp)doAgent.cast(__MODULE__,fn_->start_child(TemporaryTaskSup,fn->handle_response(resp)end)[]end)end# Your callbacks@doc""" Handles order book data. """defhandle_response(%{"table"=>"orderBook10","action"=>action,"data"=>datums})do# ...end@doc""" Handles position data. """defhandle_response(%{"table"=>"position","action"=>action,"data"=>datums})do# ...end@doc""" Handles margin data. """defhandle_response(%{"table"=>"margin","action"=>_action,"data"=>[_datum]})do# ...end@doc""" Handles order data. """defhandle_response(%{"table"=>"order","action"=>action,"data"=>datums})do# ...end@doc""" Handles table subscriptions. """defhandle_response(%{"request"=>%{"op"=>"subscribe"},"subscribe"=>table,"success"=>true})doLogger.info"Subscribed #{table}"# ...end@doc""" Handles unexpected data. """defhandle_response(resp)doLogger.warninspect(resp,limit: 500)# ...endend

License

The MIT License

About

BitMEX client library for Elixir.

Topics

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages