<commit_before>const notifier = require('node-notifier');

var BitMEXClient = require('bitmex-realtime-api');
// See 'options' reference below
var client = new BitMEXClient({testnet: false});

// handle errors here. If no 'error' callback is attached. errors will crash the client.
client.on('error', console.error);
client.on('open', () => console.log('Connection opened.'));
client.on('close', () => console.log('Connection closed.'));
client.on('initialize', () => console.log('Client initialized, data is flowing.'));

let oldData = null;
let oldTime = null;

client.addStream('XBTUSD', 'instrument', function(data, symbol, tableName) {
  // console.log(`Got update for ${tableName}:${symbol}. Current state:\n${JSON.stringify(data)}...`);
  if (data && data.length > 0) {
    const newData = data[0];
    if (!oldData) {
      console.log(`Init data`);
      oldData = newData;
      oldTime = +new Date(oldData['timestamp']);
    } else {
      const newTime = +new Date(newData['timestamp']);
      const delta = newTime - oldTime;
      if (delta > 10000) {
        // In short time: 10 seconds

        const diff = Math.abs(oldData[ 'midPrice' ] - newData[ 'midPrice' ]) / oldData[ 'midPrice' ];
        console.log(`Change: ${diff} = (${oldData[ 'midPrice' ]} - ${newData[ 'midPrice' ]}) / ${oldData[ 'midPrice' ]}`);
        if (diff > 0.005) {
          // Price change more than 5%

          const t = +new Date(oldData['timestamp']);
          if (newTime - t < 300000) {
            console.warn(`Big change! Diff: ${(diff * 100).toFixed(2)}, new price: ${newData[ 'midPrice' ]}`);
            notifier.notify(`Big change!`);
          } else {
            console.warn(`Small change! Diff: ${(diff * 100).toFixed(2)}, new price: ${newData[ 'midPrice' ]}`);
            notifier.notify(`Small change!`);
          }
          oldData = newData;
        }

        oldTime = newTime;
      }
    }
  }
});

/**
 * Sample data
 {
    "symbol": "XBTUSD",
    "rootSymbol": "XBT",<commit_msg>Add notification<commit_after>const notifier = require('node-notifier');

var BitMEXClient = require('bitmex-realtime-api');
// See 'options' reference below
var client = new BitMEXClient({testnet: false});

// handle errors here. If no 'error' callback is attached. errors will crash the client.
client.on('error', console.error);
client.on('open', () => console.log('Connection opened.'));
client.on('close', () => console.log('Connection closed.'));
client.on('initialize', () => console.log('Client initialized, data is flowing.'));

let oldData = null;
let oldTime = null;

client.addStream('XBTUSD', 'instrument', function(data, symbol, tableName) {
  // console.log(`Got update for ${tableName}:${symbol}. Current state:\n${JSON.stringify(data)}...`);
  if (data && data.length > 0) {
    const newData = data[0];
    if (!oldData) {
      console.log(`Init data`);
      oldData = newData;
      oldTime = +new Date(oldData['timestamp']);
    } else {
      const newTime = +new Date(newData['timestamp']);
      const delta = newTime - oldTime;
      if (delta > 10000) {
        // In short time: 10 seconds

        const diff = (oldData[ 'midPrice' ] - newData[ 'midPrice' ]) / oldData[ 'midPrice' ];
        console.log(`Change: ${diff} = (${oldData[ 'midPrice' ]} - ${newData[ 'midPrice' ]}) / ${oldData[ 'midPrice' ]}`);
        if (diff > 0.005 || diff < -0.005) {
          // Price change more than 5%

          const t = +new Date(oldData['timestamp']);
          if (newTime - t < 300000) {
            notifier.notify({
              title: `Big change!`,
              message: `Diff: ${(diff * 100).toFixed(2)}, new price: ${newData[ 'midPrice' ]}`
            });
          } else {
            notifier.notify({
              title: `Small change!`,
              message: `Diff: ${(diff * 100).toFixed(2)}, new price: ${newData[ 'midPrice' ]}`
            });
          }
          oldData = newData;
        }

        oldTime = newTime;
      }
    }
  }
});

/**
 * Sample data
 {
    "symbol": "XBTUSD",
    "rootSymbol": "XBT",
{'fraction_non_alphanumeric': 0.1815944881889764, 'fraction_numerical': 0.014763779527559055, 'mean_word_length': 2.7191216834400733, 'pattern_counts': {'":': 4, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4449612', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1263, 'n_words': 320}