Skip to content

Commit

Permalink
Update AlertManager.filterPropertiesForSymbol and `AlertManager.fil…
Browse files Browse the repository at this point in the history
…terTemplatesForSymbol` for options flow properties
  • Loading branch information
bryaningl3 committed Apr 1, 2024
1 parent e0ee1cf commit c5e7bc8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/content/releases/4.19.0.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**New Features**

* Added the `AlertManager.getPropertiesForSymbol` function.
* Updated the `AlertManager.filterPropertiesForSymbol` to exclude options-flow properties for instruments that do not have options.
* Updated the `AlertManager.filterTemplatesForSymbol` to exclude options-flow properties for instruments that do not have options.

**Technical Enhancements**

Expand Down
6 changes: 6 additions & 0 deletions example/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Three Node.js scripts which use this SDK to interact with the Barchart Alerting
* example.js — Instantiates an `AlertManager` and uses it to retrieves a list of alerts owned by a specific user.
* example2.js — Instantiates an `AlertManager` and uses it to create a new alert.
* example3.js — Instantiates an `AlertManager` and uses it to list, create, and delete an alert template.
* example4.js — Instantiates an `AlertManager` and uses it to set ordering for templates (with hardcoded identifiers).
* example5.js — Instantiates an `AlertManager` and uses it to delete a template (with a hardcoded identifier).
* example6.js — Instantiates an `AlertManager` and uses it to subscribe to changes to templates.
* example7.js — Instantiates an `AlertManager` and uses it to retrieve properties and group them into a tree structure.
* example8.js — Instantiates an `AlertManager` and uses it start all alerts (for the user).
* example9.js — Instantiates an `AlertManager` and uses it get properties and templates (then filter them for validity for a symbol).

These scripts also illustrate:

Expand Down
2 changes: 1 addition & 1 deletion example/node/example9.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ alertManager.connect(jwtProvider)
}).then((context) => {
logger.info(`Example: Filtering templates properties for symbol [ ${symbol} ]`);

return AlertManager.filterTemplatesForSymbol(context.templates, symbol, 1)
return AlertManager.filterTemplatesForSymbol(context.templates, symbol, alert_system)
.then((filtered) => {
logger.info(`Example: [ ${filtered.length} ] of [ ${context.templates.length} ] templates are valid for [ ${symbol} ]`);

Expand Down
21 changes: 18 additions & 3 deletions lib/AlertManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const array = require('@barchart/common-js/lang/array'),
Disposable = require('@barchart/common-js/lang/Disposable'),
Event = require('@barchart/common-js/messaging/Event'),
object = require('@barchart/common-js/lang/object'),
promise = require('@barchart/common-js/lang/promise');
promise = require('@barchart/common-js/lang/promise'),
TimeMap = require('@barchart/common-js/collections/specialized/TimeMap');

const EndpointBuilder = require('@barchart/common-js/api/http/builders/EndpointBuilder'),
ErrorInterceptor = require('@barchart/common-js/api/http/interceptors/ErrorInterceptor'),
Expand Down Expand Up @@ -1091,6 +1092,10 @@ module.exports = (() => {
valid = false;
}

if (valid && (property.property_id === 238 || property.property_id === 239)) {
valid = instrument.hasOptions;
}

if (valid && is.number(target)) {
valid = property.target.target_id === target;
}
Expand Down Expand Up @@ -1228,6 +1233,10 @@ module.exports = (() => {
valid = false;
}

if (valid && properties.some(p => p === 238 || p === 239)) {
valid = instrument.hasOptions;
}

return valid;
});
});
Expand Down Expand Up @@ -1552,6 +1561,8 @@ module.exports = (() => {
return alert;
}

const instrumentLookupEndpoints = new Map();

function buildInstrumentLookupEndpoint(host) {
return EndpointBuilder.for('lookup-instrument', 'lookup instrument')
.withVerb(VerbType.GET)
Expand All @@ -1567,7 +1578,7 @@ module.exports = (() => {
.endpoint;
}

const instrumentLookupEndpoints = new Map();
const instrumentLookupCache = new TimeMap(60 * 60 * 1000);

function lookupInstrument(symbol, alertSystem) {
let host;
Expand All @@ -1584,7 +1595,11 @@ module.exports = (() => {

const endpoint = instrumentLookupEndpoints.get(host);

return Gateway.invoke(endpoint, { symbol });
if (!instrumentLookupCache.has(symbol)) {
instrumentLookupCache.set(symbol, Gateway.invoke(endpoint, { symbol }));
}

return instrumentLookupCache.get(symbol);
}

return AlertManager;
Expand Down
4 changes: 4 additions & 0 deletions lib/data/validators/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports = (() => {
if (is.number(instrument.symbolType) && instrument.symbolType === 12 && (property.property_id < 1 || property.property_id > 8)) {
throw new Error(`${symbol} appears to be a futures option. Alerts for futures options can only target price and volume attributes.`);
}

if ((property.property_id === 238 || property.property_id === 239) && !instrument.hasOptions) {
throw new Error(`${symbol} does not have options and cannot be used for option flow alerts`);
}
}
}
};
Expand Down

0 comments on commit c5e7bc8

Please sign in to comment.