

xapi-node
This project is made possible to get data from Forex market, execute market or limit order with NodeJS/JS through WebSocket connection
This module may can be used for BFB Capital and X-Trade Brokers xStation5 accounts
WebSocket protocol description: https://peterszombati.github.io/xapi-node/
This module is usable on Front-end too.
Used by
Price moving
Getting started
1. Install via npm
npm i xapi-node
2. Example usage
Authentication
import XAPI from 'xapi-node'
const x = new XAPI({
accountId: '(xStation5) accountID',
password: '(xStation5) password',
type: 'real'
})
x.connect()
x.onReady(() => {
console.log('Connection is ready')
x.disconnect().then(() => console.log('Disconnected'))
})
x.onReject((e) => {
console.error(e)
})
Authentication only for XTB accounts
import XAPI from 'xapi-node'
const x = new XAPI({
accountId: '(xStation5) accountID',
password: '(xStation5) password',
host: 'ws.xtb.com',
type: 'real'
})
x.connect()
x.onReady(() => {
console.log('Connection is ready')
x.disconnect().then(() => console.log('Disconnected'))
})
x.onReject((e) => {
console.error(e)
})
placing buy limit on BITCOIN [CFD]
x.Socket.send.tradeTransaction({
cmd: CMD_FIELD.BUY_LIMIT,
customComment: null,
expiration: new Date().getTime() + 60000 * 60 * 24 * 365,
offset: 0,
order: 0,
price: 100,
sl: 0,
symbol: 'BITCOIN',
tp: 8000,
type: TYPE_FIELD.OPEN,
volume: 10
}).then(({order}) => {
console.log('Success ' + order)
}).catch(e => {
console.error('Failed')
console.error(e)
})
placing buy limit on US30 (Dow Jones Industrial Average)
x.Socket.send.tradeTransaction({
cmd: CMD_FIELD.BUY_LIMIT,
customComment: null,
expiration: new Date().getTime() + 60000 * 60 * 24 * 365,
offset: 0,
order: 0,
price: 21900,
sl: 0,
symbol: 'US30',
tp: 26500,
type: TYPE_FIELD.OPEN,
volume: 0.2
}).then(({order}) => {
console.log('Success ' + order)
}).catch(e => {
console.error('Failed')
console.error(e)
})
get live EURUSD price data changing
x.Stream.listen.getTickPrices((data) => {
console.log(data.symbol + ': ' + data.ask + ' | ' + data.askVolume + ' volume | ' + data.level + ' level' )
})
x.onReady(() => {
x.Stream.subscribe.getTickPrices('EURUSD')
.catch(() => { console.error('subscribe for EURUSD failed')})
})
get EURUSD M1 price history
x.onReady(() => {
x.getPriceHistory({
symbol:'EURUSD',
period: PERIOD_FIELD.PERIOD_M1
}).then(({candles, digits}) => {
console.log(candles.length)
console.log(candles[0])
console.log('digits = ' + digits)
})
})
market buy EURUSD (1.0 lot / 100000 EUR)
x.onReady(() => {
x.Socket.send.tradeTransaction({
cmd: CMD_FIELD.BUY,
customComment: null,
expiration: x.serverTime + 5000,
offset: 0,
order: 0,
price: 1,
symbol: 'EURUSD',
tp: 0,
sl: 0,
type: TYPE_FIELD.OPEN,
volume: 1
}).then(({order}) => {
console.log('Success ' + order)
}).catch(e => {
console.error('Failed')
console.error(e)
})
})
modify open position (for example set new stop loss)
x.onReady(() => {
x.Socket.send.tradeTransaction({
order: 1234,
type: TYPE_FIELD.MODIFY,
sl: 1.05,
}).then(({order}) => {
console.log('Success ' + order)
}).catch(e => {
console.error('Failed')
console.error(e)
})
})
How to use other log modules than Logger4
import {Logger4V2} from 'logger4'
const x = new XAPI({...loginConfig, logger: new Logger4V2()})
const info = new Writable()
info._write = ((chunk, encoding, next) => {
console.log(chunk.toString())
next()
})
x.logger.onStream('info', info)
const error = new Writable()
error._write = ((chunk, encoding, next) => {
console.error(chunk.toString())
next()
})
x.logger.onStream('error', error)
const debug = new Writable()
debug._write = ((chunk, encoding, next) => {
console.log(chunk.toString())
next()
})
x.logger.onStream('debug', debug)
Buy Me A Coffee! :coffee:
If you can contribute or you want to, feel free to do it at Buy me a coffee! :coffee:, I will be really thankfull for anything even if it is a coffee or just a kind comment towards my work, because that helps me a lot. Whenever you contribute with a donation, I will read your message and it will be shown in my main site.
Be careful and donate just if it is within your possibilities, because there is no refund system. And remember that you don't need to donate, it is just a free choice for you. Thank you!