Giter VIP home page Giter VIP logo

xapi-node's Introduction

Logo

xapi-node

This project makes it 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 X-Trade Brokers xStation5 accounts

WebSocket protocol description: https://peterszombati.github.io/xapi-node/

This module is usable on Front-end too.

Getting started

1. Install via npm

npm i xapi-node

2. Example usage

Authentication

// TypeScript
import XAPI from 'xapi-node'

const x = new XAPI({
    accountId: '(xStation5) accountID',
    password: '(xStation5) password',
    type: 'real' // or demo
})

(async () => {
  await x.connect()
  console.log('Connection is ready')
  x.disconnect().then(() => console.log('Disconnected'))
})().catch((e) => {
  console.error(e)
})

Authentication only for XTB accounts

// TypeScript
import XAPI from 'xapi-node'

const x = new XAPI({
    accountId: '(xStation5) accountID',
    password: '(xStation5) password',
    host: 'ws.xtb.com', // only for XTB accounts
    type: 'real' // or demo
})

(async () => {
  await x.connect()
  x.disconnect().then(() => console.log('Disconnected'))
})().catch((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' )
})

(async () => {
  await x.connect()
  x.Stream.subscribe.getTickPrices('EURUSD')
    .catch(() => { console.error('subscribe for EURUSD failed')})
})()

/* output
EURUSD: 1.10912 | 500000 volume | 0 level
EURUSD: 1.10913 | 1000000 volume | 1 level
EURUSD: 1.10916 | 1000000 volume | 2 level
EURUSD: 1.10922 | 3000000 volume | 3 level
EURUSD: 1.10931 | 3500000 volume | 4 level
...
*/

get EURUSD M1 price history

(async () => {
  await x.connect()
  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)

(async () => {
  await x.connect()
  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)

(async () => {
  await x.connect()
  x.Socket.send.tradeTransaction({
    order: 1234, // position number you can find it in (x.positions)
    type: TYPE_FIELD.MODIFY,
    sl: 1.05, // new stop loss level
  }).then(({order}) => {
    console.log('Success ' + order)
  }).catch(e => {
    console.error('Failed')
    console.error(e)
  })
})()

How to use logger

import {Logger,XAPI} from 'xapi-node'

const l = new Logger()
l.on({
  type: 'debug',
  callback: data => console.log(data)
})
l.on({
  type: 'transaction',
  callback: data => console.log(data)
})
l.on({
  type: 'error',
  callback: data => console.log(data)
})
l.on({
  type: 'info',
  callback: data => console.log(data)
})

const x = new XAPI({
  accountId: '(xStation5) accountID',
  password: '(xStation5) password',
  type: 'real' // or demo
}, l)

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.