npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

intrinio-realtime

v5.3.1

Published

Intrinio SDK for Real-Time Stock Prices

Downloads

43

Readme

Intrinio Web and NodeJS SDK for Real-Time Stock Prices

SDK for working with Intrinio's realtime Multi-Exchange prices feed. Intrinio’s Multi-Exchange feed bridges the gap by merging real-time equity pricing from the IEX and MEMX exchanges. Get a comprehensive view with increased market volume and enjoy no exchange fees, no per-user requirements, no permissions or authorizations, and little to no paperwork.

Intrinio provides real-time stock prices via a two-way WebSocket connection. To get started, subscribe to a real-time data feed and follow the instructions below.

Requirements

  • NodeJS 20.7.0 (for NodeJS usage), or
  • A modern web browser and web server (for vanilla JS usage)

Docker

Add your API key to the realtime.js file, then

docker compose build
docker compose run client

Features

  • Receive streaming, real-time price quotes (last trade, bid, ask)
  • Subscribe to updates from individual securities
  • Subscribe to updates for all securities
  • Multiple sources of data - REALTIME or DELAYED_SIP or NASDAQ_BASIC

Script

To use the Web SDK (non-NodeJS), include the index.js script (found in this repository) at the end of your <body> tag:

<script src='index.js' type='text/javascript'></script>

Example Usage (Web)

const accessKey = ""

function onTrade(trade) {
  let tradeElement = $.parseHTML('<div>' + trade.Symbol + '(trade): $' + trade.Price + '</div>')
  $('.container').prepend(tradeElement)
}

function onQuote(quote) {
  let quoteType = null
  if (quote.Type === 1) quoteType = "ask"
  else if (quote.Type === 2) quoteType = "bid"
  let quoteElement = $.parseHTML('<div>' + quote.Symbol + '(' + quote.Type + '): $' + quote.Price + '</div>')
  $('.container').prepend(quoteElement)
}

let config = {
  isPublicKey: true
}

let client = new IntrinioRealtime(accessKey, onTrade, onQuote, config)

client.join("GOOG", true)

For another example, see the /sample folder. Make sure to substitute your own Public Access Key.

Public Access Key

You can create a Public Access Key after creating an account. On your Account page, scroll down to Access Keys, click Add New Key, name it, and specify Public. The key will appear on your Account page, which you will need for to use the SDK. You will also need a subscription to a real-time data feed for one of the providers listed below.

NodeJS Installation

npm install intrinio-realtime --save

Example Usage (NodeJS)

"use strict"
const Client = require("./index").RealtimeClient; //local development
//const Client = require("intrinio-realtime").RealtimeClient; //load from package
//const Client = require("./index").ReplayClient;
const accessKey = "";

const config = {
  provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
  ipAddress: undefined,
  tradesOnly: false,
  isPublicKey: false
};

// const config = { //replay config
//     provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
//     ipAddress: undefined,
//     tradesOnly: false,
//     isPublicKey: false,
//     replayDate: '2023-10-06',
//     replayAsIfLive: false,
//     replayDeleteFileWhenDone: true
// };

let trades = new Map();
let quotes = new Map();
let maxTradeCount = 0;
let maxCountTrade = null;
let maxQuoteCount = 0;
let maxCountQuote = null;

function onTrade(trade) {
  let key = trade.Symbol;
  if (trades.has(key)) {
    let value = trades.get(key);
    if (value + 1 > maxTradeCount) {
      trades.set(key, value + 1);
      maxTradeCount = value + 1;
      maxCountTrade = trade;
    }
  }
  else trades.set(key, 1);
}

function onQuote(quote) {
  let key = quote.Symbol + ":" + quote.Type;
  if (quotes.has(key)) {
    let value = quotes.get(key);
    if (value + 1 > maxQuoteCount) {
      quotes.set(key, value + 1);
      maxQuoteCount = value + 1;
      maxCountQuote = quote;
    }
  }
  else quotes.set(key, 1);
}

let client = new Client(accessKey, onTrade, onQuote, config);
await client.join("AAPL", false); //use $lobby for firehose.

setInterval(() => {
  if (maxTradeCount > 0) {
    console.log("Most active security (by trade frequency): %s (%i updates)", JSON.stringify(maxCountTrade, (key, value) =>
            typeof value === 'bigint'
                    ? value.toString()
                    : value // return everything else unchanged
    ), maxTradeCount);
  }
  if (maxQuoteCount > 0) {
    console.log("Most active security (by quote frequency): %s (%i updates)", JSON.stringify(maxCountQuote, (key, value) =>
            typeof value === 'bigint'
                    ? value.toString()
                    : value // return everything else unchanged
    ), maxQuoteCount);
  }
  let totalMsgCount = client.getTotalMsgCount();
  if (totalMsgCount > 0) {
    console.log("Total updates received = %i", totalMsgCount);
  }
  else {
    console.log("No updates");
  }
}, 10000);

Make sure to use your API key as the accessKey parameter.

Example Replay Client Usage (NodeJS)

Used to replay a specific day's data by downloading the replay file from the REST API and then playing it back.

"use strict"
//const Client = require("./index").RealtimeClient;
const Client = require("./index").ReplayClient;
const accessKey = "";

// const config = {
//     provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
//     ipAddress: undefined,
//     tradesOnly: false,
//     isPublicKey: false
// };

const config = { //replay config
  provider: 'REALTIME', //REALTIME or DELAYED_SIP or NASDAQ_BASIC or MANUAL
  ipAddress: undefined,
  tradesOnly: false,
  isPublicKey: false,
  replayDate: '2023-10-06',
  replayAsIfLive: false,
  replayDeleteFileWhenDone: true
};

let trades = new Map();
let quotes = new Map();
let maxTradeCount = 0;
let maxCountTrade = null;
let maxQuoteCount = 0;
let maxCountQuote = null;

function onTrade(trade) {
  let key = trade.Symbol;
  if (trades.has(key)) {
    let value = trades.get(key);
    if (value + 1 > maxTradeCount) {
      trades.set(key, value + 1);
      maxTradeCount = value + 1;
      maxCountTrade = trade;
    }
  }
  else trades.set(key, 1);
}

function onQuote(quote) {
  let key = quote.Symbol + ":" + quote.Type;
  if (quotes.has(key)) {
    let value = quotes.get(key);
    if (value + 1 > maxQuoteCount) {
      quotes.set(key, value + 1);
      maxQuoteCount = value + 1;
      maxCountQuote = quote;
    }
  }
  else quotes.set(key, 1);
}

let client = new Client(accessKey, onTrade, onQuote, config);
await client.join("AAPL", false); //use $lobby for firehose.

setInterval(() => {
  if (maxTradeCount > 0) {
    console.log("Most active security (by trade frequency): %s (%i updates)", JSON.stringify(maxCountTrade, (key, value) =>
            typeof value === 'bigint'
                    ? value.toString()
                    : value // return everything else unchanged
    ), maxTradeCount);
  }
  if (maxQuoteCount > 0) {
    console.log("Most active security (by quote frequency): %s (%i updates)", JSON.stringify(maxCountQuote, (key, value) =>
            typeof value === 'bigint'
                    ? value.toString()
                    : value // return everything else unchanged
    ), maxQuoteCount);
  }
  let totalMsgCount = client.getTotalMsgCount();
  if (totalMsgCount > 0) {
    console.log("Total updates received = %i", totalMsgCount);
  }
  else {
    console.log("No updates");
  }
}, 10000);

Make sure to use your API key as the accessKey parameter, and changing the replayDate parameter

Handling Quotes

There are thousands of securities, each with their own feed of activity. We highly encourage you to make your trade and quote handlers has short as possible and follow a queue pattern so your app can handle the volume of activity.

Trade Message

{
  Symbol: "AAPL",
  Price: 150.99,
  Size: 20,
  Timestamp: 1637092835566268084,
  TotalVolume: 2728543, 
  SubProvider: "IEX",
  MarketCenter: "",
  Condition: ""
}
  • Symbol - Stock 'ticker' symbol for the security
  • Price - The price in USD
  • Size - The number of shares exchanged on the last trade
  • TotalVolume - The total number of shares traded so far today, for the given symbol
  • Timestamp - A unix timestamp (the number of nanoseconds since the unix epoch)
  • SubProvider - Denotes the detailed source within grouped sources.
    • NONE - No subtype specified.
    • CTA_A - CTA_A in the DELAYED_SIP provider.
    • CTA_B - CTA_B in the DELAYED_SIP provider.
    • UTP - UTP in the DELAYED_SIP provider.
    • OTC - OTC in the DELAYED_SIP provider.
    • NASDAQ_BASIC - NASDAQ Basic in the NASDAQ_BASIC provider.
    • IEX - From the IEX exchange in the REALTIME provider.
  • MarketCenter - Provides the market center
  • Condition - Provides the condition

Quote Message

{ 
  Symbol: "GOOG",
  Type: 1,
  Price: 2994.78,
  Size: 105,
  Timestamp: 1637092847907710010,
  SubProvider: "IEX",
  MarketCenter: "",
  Condition: ""
}
  • Symbol - Stock 'ticker' symbol for the security
  • Type - The quote type (either 'ask' or 'bid')
    • 1 - represents an 'ask' type
    • 2 - represents a 'bid' type
  • Price - The price in USD
  • Size - The size of the last ask or bid
  • Timestamp - A unix timestamp (the number of nanoseconds since the unix epoch)
  • SubProvider - Denotes the detailed source within grouped sources.
    • NONE - No subtype specified.
    • CTA_A - CTA_A in the DELAYED_SIP provider.
    • CTA_B - CTA_B in the DELAYED_SIP provider.
    • UTP - UTP in the DELAYED_SIP provider.
    • OTC - OTC in the DELAYED_SIP provider.
    • NASDAQ_BASIC - NASDAQ Basic in the NASDAQ_BASIC provider.
    • IEX - From the IEX exchange in the REALTIME provider.
  • MarketCenter - Provides the market center
  • Condition - Provides the condition

API Keys

You will receive your Intrinio API Key after creating an account. You will need a subscription to the Real-Time Data Feed as well.

Methods

constructor(accessKey, onTrade, ?onQuote, ?config) - Creates a new instance of the IntrinioRealtime client.

  • Parameter accessKey: Your API key. See the section on API Keys, above.
  • Parameter onTrade: A callback invoked when a 'trade' has been received. The trade will be passed as an argument to the callback.
  • Parameter onQuote: Optional. A callback invoked when a 'quote' has been received. The quote will be passed as an argument to the callback. If 'onQuote' is not provided, the client will NOT request to receive quote updates from the server.
  • Parameter config: Optional. An object with properties provider, ipAddress, and tradesOnly corresponding to a provider code ("REALTIME" (default) or "MANUAL"), the ipAddress of the websocket server (only necessay when provider = "MANUAL"), and a boolean value indicating whether the server should return trade data only (as opposed to trade and quote data).
function onTrade(trade) {
  console.log("TRADE: ", trade)
}
function onQuote(quote) {
  console.log("QUOTE: ", quote)
}
const client = new IntrinioRealtimeClient("INTRINIO_API_KEY", onTrade, onQuote, { tradesOnly: true })

stop() - Closes the WebSocket, stops the self-healing and heartbeat intervals. You MUST call this to dispose of the client. Called automatically on 'SIGINT'.


join(symbols, tradesOnly) - Joins the given channels. This can be called at any time. The client will automatically register joined channels and establish the proper subscriptions with the WebSocket connection.

  • Parameter symbols - A string representing a single ticker symbol (e.g. "AAPL") or an array of ticker symbols (e.g. ["AAPL", "MSFT", "GOOG"]) to join. You can also use the special symbol, "$lobby" to join the firehose channel and recieved updates for all ticker symbols. You must have a valid "firehose" subscription.
  • Parameter tradesOnly - Optional (default: false). A boolean value indicating whether the server should return trade data only (as opposed to trade and quote data).
client.join(["AAPL", "MSFT", "GOOG"])
client.join("GE", true)
client.join("$lobby") //must have a valid 'firehose' subscription

leave(symbols) - Leaves the given channels.

  • Parameter symbols - Optional (default = all channels). A string representing a single ticker symbol (e.g. "AAPL") or an array of ticker symbols (e.g. ["AAPL", "MSFT", "GOOG"]) to leave. If not provided, all subscribed channels will be unsubscribed.
client.leave(["AAPL", "MSFT", "GOOG"])
client.leave("GE")
client.leave("$lobby")
client.leave()