0.0.2 • Published 8 months ago

ig-connect v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

IG API

A barebones IG API wrapper based on fetch() and Lightstreamer.

Installation

npm i ig-connect

Usage

import IG from 'ig-connect';

const demoMode = true;
const ig = new IG('API_KEY', 'ACCOUNT_IDENTIFIER', 'ACCOUNT_PASSWORD', demoMode);

// Arbitrary Requests
const positionsResponse = await ig.request(2, 'GET', '/positions');
const { positions } = await positionsResponse.json();

const updatePositionResponse = await ig.request(2, 'PUT', `/positions/otc/{DEAL_ID}`, {
  limitLevel: 1.2345,
  stopLevel: 1.3456,
});

// Streaming Prices
// 1. Initialise a streaming client
const streamer = await ig.streamer({
  serverError: (code, message) => {
    console.error('Server error', code, message);
  },
  listenStart: () => {
    console.log('Streamer listening');
  },
  statusChange: (status) => {
    if (status === 'CONNECTED:WS-STREAMING') {
      console.log('Streamer connected');
    }
  },
}

// 2. Subscribe to price updates stream
ig.stream(streamer, 'MERGE', ['MARKET:CS.D.EURUSD.TODAY.IP'], ['BID', 'OFFER'], {
  subscriptionError: (code, message) => {
    console.error('Subscription error', code, message);
  },
  itemUpdate: (name, data) => {
    const { BID, OFFER } = data;
    console.log('Price Changed', name, BID, OFFER);
  },
});
0.0.2

8 months ago

0.0.1

8 months ago