Skip to content

yurikoval/ex_okex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExOkex

OKEX API client for Elixir.

Build Status hex.pm version

Installation

List the Hex package in your application dependencies.

def deps do
  [{:ex_okex, "~> 0.6"}]
end

Run mix deps.get to install.

Configuration

Static API Key

Static API Key is the key you setup once and would never change. And this is what we need for most cases.

Add the following configuration variables in your config/config.exs file:

use Mix.Config

config :ex_okex, api_key:        {:system, "OKEX_API_KEY"},
                 api_secret:     {:system, "OKEX_API_SECRET"},
                 api_passphrase: {:system, "OKEX_API_PASSPHRASE"}

Alternatively to hard coding credentials, the recommended approach is to use environment variables as follows:

use Mix.Config

config :ex_okex, api_key:        System.get_env("OKEX_API_KEY"),
                 api_secret:     System.get_env("OKEX_API_SECRET"),
                 api_passphrase: System.get_env("OKEX_API_PASSPHRASE")

If you're using websocket then you need to do this config

use Mix.Config

config :ex_okex, api_key:        System.get_env("OKEX_API_KEY"),
                 api_secret:     System.get_env("OKEX_API_SECRET"),
                 api_passphrase: System.get_env("OKEX_API_PASSPHRASE")

Alternatively, if you need to work with multiple OKEX accounts, the private API call functions accept an additional config (ExOkex.Config struct) parameter:

config = %ExOkex.Config{
  api_key: "API_KEY",
  api_secret: "API_SECRET",
  api_passphrase: "API_PASSPHRASE",
  api_url: "API_URL" # optional
  ws_endpoint: "wss://real.okex.com:10442/ws/v3" # optional
}
ExOkex.list_accounts() # use config as specified in config.exs
ExOkex.list_accounts(config) # use the passed config struct param

Dynamic API Key

There will be cases when we want to switch to different API keys based on different info or need. That's why we're supporting this.

One of the use case when you want to have the dynamic API key feature is: when you want to using multiple API keys in the same app. In that case you simply need to spawn a process which encapsulate the config info. And each process with have it's own credentials to interact with Okex.

So we can tell either API or Websocket module to use certain access keys to retrieve the API keys that we want.

NOTE: The access key must be in string.

SECURITY: Access key is passed around instead of actual value of the key is to reduce the security risk. People can not inspect the key when the program up and running. This follow Tell, don't ask principle.

Websocket

During the setup you can pass the access keys as argument. Ex:

defmodule WsWrapper do
  @moduledoc false

  require Logger
  use ExOkex.Ws
end

WsWrapper.start_link(%{
  channels: ["futures/trade:BTC-USD-190904"],
  require_auth: true,
  config: %{access_keys: ["OK_1_API_KEY", "OK_1_API_SECRET", "OK_1_API_PASSPHRASE"]}
})

WsWrapper.start_link(%{
  channels: ["futures/trade:BTC-USD-190904"],
  require_auth: true,
  config: %{access_keys: ["OK_2_API_KEY", "OK_2_API_SECRET", "OK_2_API_PASSPHRASE"]}
})

Then Websocket will use the above access_keys to get the key value from the environment variables.

API

Simply pass the config to the API call

Example:

config = %{access_keys: ["OK_1_API_KEY", "OK_1_API_SECRET", "OK_1_API_PASSPHRASE"]}

ExOkex.Futures.create_bulk_orders(
  [
    %{
      "instrument_id":"BTC-USD-180213",
      "type":"1",
      "price":"432.11",
      "size":"2",
      "match_price":"0",
      "leverage":"10"
    },
  ],
  config
)

Usage

Place a limit order

iex> ExOkex.create_order(%{
  "client_oid" => "20180728",
  "instrument_id" => "btc-usdt",
  "side" => "sell",
  "type" => "limit",
  "size" => "0.1",
  "price" => "1",
  "margin_trading" => 1
})
{:ok,
 %{"order_id" => "234652",
   "client_oid" => "23",
   "result" => true}

Websocket

You can subscrube to private and public Okex feeds by adding this to your application.ex, and creating a handler:

worker(OkexWebSocketHandler, [
  %{
    channels: [
      "ok_sub_futureusd_trades",
      "ok_sub_futureusd_userinfo",
      "ok_sub_futureusd_positions"
    ],
    require_auth: true
  }
])
defmodule OkexWebSocketHandler do
  use ExOkex.Ws

  def handle_response(resp, _state) do
    IO.inspect(resp)
    {:ok, resp}
  end
end

Additional Links

Full Documentation

OKEX API Docs

License

MIT

Acknowledgement

Many parts of this client were taken from ExGdax and adapted for OKEx API.