Skip to content

Commit

Permalink
ReplayClient Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ssnyder-intrinio authored Oct 12, 2023
1 parent 1e72ee2 commit 923ba7e
Show file tree
Hide file tree
Showing 7 changed files with 720 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.9.5
20.7.0
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 16.13.0
nodejs 20.7.0
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16.13.0
FROM node:20.7.0

RUN mkdir /intrinio

Expand Down
201 changes: 152 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SDK for working with Intrinio's realtime Multi-Exchange prices feed. Intrinio’
[Intrinio](https://intrinio.com/) provides real-time stock prices via a two-way WebSocket connection. To get started, [subscribe to a real-time data feed](https://intrinio.com/marketplace/data/prices/realtime) and follow the instructions below.

## Requirements
- NodeJS 16.13.0 (for NodeJS usage), or
- NodeJS 20.7.0 (for NodeJS usage), or
- A modern web browser and web server (for vanilla JS usage)

## Docker
Expand Down Expand Up @@ -67,77 +67,180 @@ npm install intrinio-realtime --save

## Example Usage (NodeJS)
```javascript
const IntrinioRealtimeClient = require('intrinio-realtime')
const accessKey = ""
const provider = "REALTIME" // or "DELAYED_SIP" or "NASDAQ_BASIC"
const config = {
tradesOnly: true,
}
"use strict"
const Client = require("./index").RealtimeClient;
//const Client = require("./index").ReplayClient;
const accessKey = "";

let trades = new Map()
let quotes = new Map()
let maxTradeCount = 0
let maxCountTrade = null
let maxQuoteCount = 0
let maxCountQuote = null
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;

// Set up a callback for trades
// This keeps track of the most active ticker symbol (by trade frequency)
function onTrade(trade) {
let key = trade.Symbol
let key = trade.Symbol;
if (trades.has(key)) {
let value = trades.get(key)
let value = trades.get(key);
if (value + 1 > maxTradeCount) {
trades.set(key, value + 1)
maxTradeCount = value + 1
maxCountTrade = trade
trades.set(key, value + 1);
maxTradeCount = value + 1;
maxCountTrade = trade;
}
}
else trades.set(key, 1)
else trades.set(key, 1);
}

// Set up a callback for quotes
// This keeps track of the most active ticker symbol (by quote frequency)
function onQuote(quote) {
let key = quote.Symbol + ":" + quote.Type
let key = quote.Symbol + ":" + quote.Type;
if (quotes.has(key)) {
let value = quotes.get(key)
let value = quotes.get(key);
if (value + 1 > maxQuoteCount) {
quotes.set(key, value + 1)
maxQuoteCount = value + 1
maxCountQuote = quote
quotes.set(key, value + 1);
maxQuoteCount = value + 1;
maxCountQuote = quote;
}
}
else quotes.set(key, 1)
else quotes.set(key, 1);
}

// Create an IntrinioRealtimeClient instance
// 'accessKey', 'provider', and 'onTrade' are required.
const client = new Client(accessKey, provider, onTrade, onQuote, config)
let client = new Client(accessKey, onTrade, onQuote, config);
await client.join("AAPL", false); //use $lobby for firehose.

// Join channels
client.join(["AAPL", "MSFT", "GOOG"])

// Set up a timer to print out tracked metrics every 10 seconds
setInterval(() => {
if (maxTradeCount > 0) {
console.log("Most active security (by trade frequency): %s (%i updates)", maxCountTrade, maxTradeCount)
}
if (maxQuoteCount > 0) {
console.log("Most active security (by quote frequency): %s (%i updates)", maxCountQuote, maxQuoteCount)
}
let totalMsgCount = client.getTotalMsgCount()
if (totalMsgCount > 0) {
console.log("Total updates received = %i", totalMsgCount)
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.
```javascript
"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 {
console.log("No updates")
}
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;
}
}, 10000)
}
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);

```

For another example, see the `realtime.js` file. Make sure to use your API key as the `accessKey` parameter.
Make sure to use your API key as the `accessKey` parameter, and changing the `replayDate` parameter

## Handling Quotes

Expand Down
Loading

0 comments on commit 923ba7e

Please sign in to comment.