项目作者: nobrick

项目描述 :
BitMEX client library for Elixir.
高级语言: Elixir
项目地址: git://github.com/nobrick/bitmex.git
创建时间: 2017-02-02T08:00:49Z
项目社区:https://github.com/nobrick/bitmex

开源协议:MIT License

下载


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:

  1. def deps do
  2. [{:bitmex, "~> 0.2"}]
  3. end

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

  1. config :bitmex, api_key: ""
  2. config :bitmex, api_secret: ""
  3. 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:

  1. Bitmex.Rest.Position.get()

Orders

Get all your open orders:

  1. Bitmex.REST.Order.get_open()

Create a order:

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

Create a bulk order:

  1. p1 = %{"symbol" => "XBTUSD", "side" => "Buy", "orderQty" => 15,
  2. "price" => 4000.1, "ordType" => "Limit"}
  3. 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:

  1. defmodule Caravan.WS.MessageHandler do
  2. use Bitmex.WS
  3. def handle_response(resp), do: Caravan.WS.process(resp)
  4. end
  5. defmodule Caravan.WS do
  6. require Logger
  7. import Task.Supervisor, only: [start_child: 2]
  8. # API
  9. def start_link(opts \\ []) do
  10. Agent.start_link(fn -> [] end, opts)
  11. end
  12. def process(resp) do
  13. Agent.cast(__MODULE__, fn _ ->
  14. start_child(TemporaryTaskSup, fn -> handle_response(resp) end)
  15. []
  16. end)
  17. end
  18. # Your callbacks
  19. @doc """
  20. Handles order book data.
  21. """
  22. def handle_response(%{"table" => "orderBook10", "action" => action,
  23. "data" => datums}) do
  24. # ...
  25. end
  26. @doc """
  27. Handles position data.
  28. """
  29. def handle_response(%{"table" => "position", "action" => action,
  30. "data" => datums}) do
  31. # ...
  32. end
  33. @doc """
  34. Handles margin data.
  35. """
  36. def handle_response(%{"table" => "margin", "action" => _action,
  37. "data" => [_datum]}) do
  38. # ...
  39. end
  40. @doc """
  41. Handles order data.
  42. """
  43. def handle_response(%{"table" => "order", "action" => action,
  44. "data" => datums}) do
  45. # ...
  46. end
  47. @doc """
  48. Handles table subscriptions.
  49. """
  50. def handle_response(%{"request" => %{"op" => "subscribe"},
  51. "subscribe" => table, "success" => true}) do
  52. Logger.info "Subscribed #{table}"
  53. # ...
  54. end
  55. @doc """
  56. Handles unexpected data.
  57. """
  58. def handle_response(resp) do
  59. Logger.warn inspect(resp, limit: 500)
  60. # ...
  61. end
  62. end

License

The MIT License