Giter Club home page Giter Club logo

ex_okex's Introduction

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.4"}]
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"),
                 ping_interval:  System.get_env("OKEX_PING_INTERVAL") # default is 5000

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.

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.