Darwinex

Build Status

Ruby client for the Darwinex API.

Installation

Gemfile

gem 'darwinex'

Install

$ gem install darwinex

Usage

Require the client:

require 'darwinex/client'

Initialise the client:

client = Darwinex::Client.new(
  consumer_key: "<consumer key>",
  consumer_secret: "<consumer secret>"
)

If you would like to use a custom logger:

client = Darwinex::Client.new(
  consumer_key: "<consumer key>",
  consumer_secret: "<consumer secret>",
  logger: Logger.new(STDOUT)
)

If you would like to configure the maximum number of API call retries:

client = Darwinex::Client.new(
  consumer_key: "<consumer key>",
  consumer_secret: "<consumer secret>",
  max_retries: 10 # Default: 5
)

Before you use the client, you must generate a new access token for the client to use:

resp = client.refresh_access_token("<refresh token>")

refresh_token = resp["refresh_token"]

The client will now be ready for use.

You will need to persist the refresh token returned by .refresh_access_token - you will need it the next time you want to generate a new access token.

Examples

Investor Accounts

client.list_investor_accounts
client.("<account id>")
client.("<account id>").summary
client.("<account id>").leverage
client.("<account id>").update_leverage(3)
buy_order = { long: "eurusd" }

client.("<account id>").create_buy_order(buy_order)
sell_order = { long: "eurusd" }

client.("<account id>").create_sell_order(sell_order)
# Will create a stopout for all products
client.("<account id>").create_stopout
product_name = 'DWC.4.20'

client.("<account id>").create_stopout(product_name)
order = {
  'amount' => 215.15,
  'productName' => 'DWC.4.20',
  'quote' => 20.23,
  'side' => 'BUY',
  'type' => 'LESS_THAN_EQUAL',
  'thresholdParameters' => {
    'quoteStopLoss' => 10.05,
    'quoteTakeProfit' => 250.1
  }
}

client.("<account id>").create_conditional_order(order)
order_id = 123

order = {
  'amount' => 215.15,
  'productName' => 'DWC.4.20',
  'quote' => 20.23,
  'side' => 'BUY',
  'type' => 'LESS_THAN_EQUAL',
  'thresholdParameters' => {
    'quoteStopLoss' => 10.05,
    'quoteTakeProfit' => 250.1
  }
}

client.("<account id>").update_conditional_order(order_id, order)
order_id = 123

client.("<account id>").delete_conditional_order(order_id)
client.("<account id>").current_positions
product_name = 'DWC.4.20'

client.("<account id>").current_positions(product_name)
client.("<account id>").executed_orders
options = {
  product_name: 'DWC.4.20',
  page: 2,
  per_page: 10
}

client.("<account id>").executed_orders(options)
order_id = 'DWC.4.20'

client.("<account id>").order(order_id)
client.("<account id>").performance_fees
options = {
  page: 2,
  per_page: 10
}

client.("<account id>").performance_fees(options)
product_name = 'DWC.4.20'

client.("<account id>").product_performance_fees(product_name)
trade_status = 'open'

client.("<account id>").trades(trade_status)
trade_status = 'open'

options = {
  product_name: 'DWC.4.20',
  page: 2,
  per_page: 10
}

client.("<account id>").trades(trade_status, options)
trade_id = 'DWC.4.20'

client.("<account id>").trade(trade_id)

Products

client.list_products
client.product("<product name>")
client.product("<product name>").candles(from: epoch_timestamp, to: epoch_timestamp)
client.product("<product name>").candles(resolution: '1m', from: epoch_timestamp, to: epoch_timestamp)
client.product("<product name>").dxscore
client.product("<product name>").badges
client.product("<product name>").close_strategy
client.product("<product name>").duration_consistency
client.product("<product name>").experience
client.product("<product name>").losing_consistency
client.product("<product name>").market_correlation
client.product("<product name>").performance
client.product("<product name>").open_strategy
client.product("<product name>").capacity
client.product("<product name>").quotes
client.product("<product name>").quotes(from: epoch_timestamp, to: epoch_timestamp)
client.product("<product name>").risk_stability
client.product("<product name>").winning_consistency
client.product("<product name>").order_divergence
client.product("<product name>").return_divergence
client.product("<product name>").monthly_divergence
client.product("<product name>").status
client.product("<product name>").scores
badge = "EXPERIENCE"

client.product("<product name>").scores(badge)

Exceptions

All exceptions extend the base exception class Darwinex::Error.

Invalid Credentials

Raised when the consumer key or secret passed to the client are not valid.

Throttled

Raised when you exceed the API rate limit.

The client will automatically back off and retry throttled API responses until the max retries limit is hit.

Refresh Token Expired

Raised when Darwinex::Client#refresh_access_token is called with a refresh token that has expired.

Refresh tokens are only valid for a short period of time, after which they expire. As such, it's important your application regularly refreshes the access tokens to stop them from expiring.

In order to resolve this issue, you must go to the Darwinex Web console and issue a new refresh token.