-
Notifications
You must be signed in to change notification settings - Fork 98
Home
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).
const connection = new WebSocket('wss://frontend.binaryws.com/websockets/v3?l=EN&app_id=1234');
const api = new DerivAPI({ connection });
const api = new DerivAPI({ endpoint: 'frontend.binaryws.com', lang: 'EN', appId: 1234 });
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).
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 */);
// 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);
})
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);
}
});
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);
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)
const assets = await api.assets();
if (!assets.underlyings.find(u => u.symbol === 'R_100').isTradingSuspended) {
const contract = await api.contract(contractOptions);
// The rest
}
const api = new DerivAPI(/* options here */);
const accout = await api.account('YourToken');
const assets = await api.assets();
assets.openMarkets.forEach(market => {
console.log('Market %s is open', market.name.full);
});
const underlying = await api.underlying('R_100');
const { callput } = underlying.contractGroups;
const { min: duration, unit: durationUnit } = callput.duration.tick;
const { forwardSessions } = callput;
const [ firstSession ] = forwardSessions;
const $el = <input type="date" value={await api.time} min={firstSession.open.value} max={firstSession.close.value}>
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);
});
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);
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,
});
}
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