Skip to content
Amin Marashi edited this page Jul 10, 2019 · 40 revisions

Table of contents

Deriv API

API instantiation

You can either create an instance of WebSocket and pass it as connection or pass the endpoint and appId to the constructor to create the connection for you.

If you pass the connection it's up to you to reconnect in case the connection drops (cause API doesn't know how to create the same connection).

Examples

Passing a connection

const connection = new WebSocket('wss://frontend.binaryws.com/websockets/v3?l=EN&app_id=1234');
const api = new DerivAPI({ connection });

With endpoint and appId

const api = new DerivAPI({ endpoint: 'frontend.binaryws.com', lang: 'EN', appId: 1234 });

API Methods

All API methods are asynchronous, they return a Promise that will resolve to an abstract object with the requested data and onUpdate() which receives the updated instance on every update of that object either through a callback passed to onUpdate() or as an observer listening on the observable returned by onUpdate() (if no callback is specified).

tickStream

const range = { start: await api.time - 10, end: await api.time };

// a ticks object you can get the ticks from or listen to updates
const ticks = await api.tickStream('R_100');

// By default the last 1000 ticks
const listOfTicks = ticks.list;
const listOfOldTicks = await ticks.history(range);

// Get the tick updates with a callback
ticks.onUpdate(tick => /* updates for the ticks */);


// Get the tick updates with an observable
ticks.onUpdate().subscribe(tick => /* updates for the ticks */);

candleStream

// Same as ticks, default is the last 1000 1min candle
// either 'R_100' or { symbol: 'R_100', granularity: ... }
const candles = await api.candleStream('R_100');

const candle = candles.list.slice(-1)[0] // current

candles.onUpdate(candle => {
    chart.add(candle);
})

contract

const contract = await api.contract({
    contractType: 'call',
    symbol: 'R_100',
    duration: 1,
    durationUnit: 't',
    ...contractOptions
});

contract.onUpdate().subscribe(contract => {
    if (contract.isSold) {
        sellPopUp.set(contract);
    }
});

underlying

const underlying = await api.underlying('R_100');

// You can also call ticks and candles related calls on an underlying instance
const ticks = await underlying.tickStream();

// You can create a contract from an underlying
const contract = await underlying.contract(contractOptions);

account

const account = await api.account('AccountToken');
const balance = account.balance;

// You can create a contract from an account instance
const contract = await account.contract(contractOptions);

const transactionStream = await account.transactionStream();

transactionStream.onUpdate(console.log)

assets

const assets = await api.assets();
if (!assets.underlyings.find(u => u.symbol === 'R_100').isTradingSuspended) {
    const contract = await api.contract(contractOptions);
    // The rest
}

Usage examples

Short examples

const api = new DerivAPI(/* options here */);

Authenticate to an account using token

const accout = await api.account('YourToken');

Get all the assets info

const assets = await api.assets();

assets.openMarkets.forEach(market => {
    console.log('Market %s is open', market.name.full);
});

Ask for an specific underlying

const underlying = await api.underlying('R_100');

Get a contract group by its name

const { callput } = underlying.contractGroups;

Get supported durations for a contract group

const { min: duration, unit: durationUnit } = callput.duration.tick;

Create a datepicker for forward starting sessions

const { forwardSessions } = callput;

const [ firstSession ] = forwardSessions;

const $el = <input type="date" value={await api.time} min={firstSession.open.value} max={firstSession.close.value}>

Examples with more details

An account object

const account = await api.account('YourToken');

// Loop through all associated accounts and print whether they're virtual
account.siblings.forEach(u => {
    console.log('Is %s virtual? %s', u.loginid, u.isVirtual);
});

const balance = account.balance;

console.log('%s has %s %s', account.loginid, balance.currency, balance.format);

balance.onUpdate(balance => {
    console.log('%s has now %s %s', account.loginid, balance.currency, balance.format);
});

Create an underlying and get all the ticks history for showing on a chart

const underlying = await api.underlying('R_100');

const ticks = await underlying.tickStream();

ticks.onUpdate(tick => {
    // Update chart info with the new ticks
    chart.show(tick);
});

// Initialize the chart with the list of ticks available
chart.init(ticks.list);

Create a contract if trading is allowed for an underlying

const underlying = await api.underlying('R_100');

if (underlying.isTradingSuspended) {
    console.log('Trading is suspended for %s', underlying.name.full);
} else {
    // Get the contract group for Rise/Fall
    const callput = underlying.contractGroups.callput;

    // Get the duration allowed for tick contracts
    const { value: duration, unit: durationUnit } = callput.durations.tick.min;

    const [contractType] = callput.contractTypes;

    const contract = await api.contract({
        symbol: underlying.symbol,
        contractType,
        duration,
        durationUnit,
    });
}

UML diagram of classes

Each font face indicates a different type of field:

Bold: a link to another class object(s)

endingWithParenthesis(): A method name, usually doing some action on the object

normalProperties: The camel case name of a property accessible from the object

Deriv API UML Diagram

Clone this wiki locally