VERSION 1.12 @ 2018-02-14 07:05:47.033501
Cannot read property 'contains' of null #2442 Terminal: custom sorting for account manager tables #2344 Datafeed not closed after removing chart #2270 order.setPrice() has stopped working #2267 Volume in DOM rounded to 0 #2255 Error on bars request and update order lines #2237 Console error: Uncaught (in promise) formatter not received #2228 DOME doesn't work in new version #2211 Add featureset to hide toolbars by default #2209 Add possibility to move studies through z-order #2187 Unexpected resolution values in getBars #2179 Session breaks line is stuck to the left on reload #2153 Typescript declaration has mistakes #2144 Terminal: cannot specify step less than 1 #2141 Namespace the types in charting_library.d.ts #2137 Widget logo showing momentarily on paid account #2132 Add API to apply overrides for created studies #2098 Baseline chart style #2097 How to hide legend "Data Provided by ICE Data services" #2046 Stoch RSI Calculation #2038 Customize loading screen #2012 Supertrend indicator #1950 Translations in market details widget #1946 "Track time" chart setting #1918 Add tabs for positions #1906 Trading Terminal: Notifications Log #1896 Previous Close Price Line #1843 Set Overlay/Compare styles using createStudy/overrides and applyStudiesOverrides #1812 Session under Symbol info is displaying wrong trading interval(s)? #1787 Add "Go to" specific date #1753 Add session breaks #1752 Allow users to specify a volume for the Long/Short Position drawing tools #1691 Add API to use own charts save/load adapter #1679 Drawing toolbar not available in mobile #1673 Typescript definitions #1591 Support for symbols containing lowercase letters #1581 createMultipointShape long_position stop&profit setting #1459 Cannot change awesome oscillator width #1213 New adaptive drawings panel #1145 Edit shapes, studies and series #1101 Hide an indicator with API #1025 VWAP INDICATOR #106
This commit is contained in:
3
datafeeds/README.md
Normal file
3
datafeeds/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Charting Library Datafeeds
|
||||
|
||||
This folder contains implementation of Charting Library Datafeeds.
|
||||
1
datafeeds/udf/.npmrc
Normal file
1
datafeeds/udf/.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
package-lock=false
|
||||
44
datafeeds/udf/README.md
Normal file
44
datafeeds/udf/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# UDF Compatible Datafeed
|
||||
|
||||
This folder contains [UDF](https://github.com/tradingview/charting_library/wiki/UDF) datafeed adapter. It implements [JS API](https://github.com/tradingview/charting_library/wiki/JS%20API) and makes HTTP requests using [UDF](https://github.com/tradingview/charting_library/wiki/UDF) protocol.
|
||||
|
||||
You can use this datafeed adapter to plug your data if you implement [UDF](https://github.com/tradingview/charting_library/wiki/UDF) on your server. You can also scrutinize how it works before writing your own adapter.
|
||||
|
||||
This datafeed is implemented in [TypeScript](https://github.com/Microsoft/TypeScript/).
|
||||
|
||||
## Folders content
|
||||
|
||||
- `./src` folder contains the source code in TypeScript.
|
||||
|
||||
- `./lib` folder contains transpiled in es5 code. So, if you do not know how to use TypeScript - you can modify these files to change the result bundle later.
|
||||
|
||||
- `./dist` folder contains bundled JavaScript files which can be inlined into a page and used in the Widget Constructor.
|
||||
|
||||
## Build & bundle
|
||||
|
||||
Before building or bundling your code you need to run `npm install` to install dependencies.
|
||||
|
||||
`package.json` contains some handy scripts to build or generate the bundle:
|
||||
- `npm run compile` to compile TypeScript source code into JavaScript files (output will be in `./lib` folder)
|
||||
- `npm run bundle-js` to bundle multiple JavaScript files into one bundle (it also bundle polyfills)
|
||||
- `npm run build` to compile and bundle (it is a combination of all above commands)
|
||||
|
||||
NOTE: if you want to minify the bundle code, you need to set `ENV` environment variable to a value different from `development`.
|
||||
|
||||
For example:
|
||||
```bash
|
||||
export ENV=prod
|
||||
npm run bundle-js # or npm run build
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
ENV=prod npm run bundle-js
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
ENV=prod npm run build
|
||||
```
|
||||
1
datafeeds/udf/dist/bundle.js
vendored
Normal file
1
datafeeds/udf/dist/bundle.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
datafeeds/udf/dist/polyfills.js
vendored
Normal file
1
datafeeds/udf/dist/polyfills.js
vendored
Normal file
File diff suppressed because one or more lines are too long
107
datafeeds/udf/lib/data-pulse-provider.js
Normal file
107
datafeeds/udf/lib/data-pulse-provider.js
Normal file
@@ -0,0 +1,107 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
var DataPulseProvider = /** @class */ (function () {
|
||||
function DataPulseProvider(historyProvider, updateFrequency) {
|
||||
this._subscribers = {};
|
||||
this._requestsPending = 0;
|
||||
this._historyProvider = historyProvider;
|
||||
setInterval(this._updateData.bind(this), updateFrequency);
|
||||
}
|
||||
DataPulseProvider.prototype.subscribeBars = function (symbolInfo, resolution, newDataCallback, listenerGuid) {
|
||||
if (this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
logMessage("DataPulseProvider: already has subscriber with id=" + listenerGuid);
|
||||
return;
|
||||
}
|
||||
this._subscribers[listenerGuid] = {
|
||||
lastBarTime: null,
|
||||
listener: newDataCallback,
|
||||
resolution: resolution,
|
||||
symbolInfo: symbolInfo,
|
||||
};
|
||||
logMessage("DataPulseProvider: subscribed for #" + listenerGuid + " - {" + symbolInfo.name + ", " + resolution + "}");
|
||||
};
|
||||
DataPulseProvider.prototype.unsubscribeBars = function (listenerGuid) {
|
||||
delete this._subscribers[listenerGuid];
|
||||
logMessage("DataPulseProvider: unsubscribed for #" + listenerGuid);
|
||||
};
|
||||
DataPulseProvider.prototype._updateData = function () {
|
||||
var _this = this;
|
||||
if (this._requestsPending > 0) {
|
||||
return;
|
||||
}
|
||||
this._requestsPending = 0;
|
||||
var _loop_1 = function (listenerGuid) {
|
||||
this_1._requestsPending += 1;
|
||||
this_1._updateDataForSubscriber(listenerGuid)
|
||||
.then(function () {
|
||||
_this._requestsPending -= 1;
|
||||
logMessage("DataPulseProvider: data for #" + listenerGuid + " updated successfully, pending=" + _this._requestsPending);
|
||||
})
|
||||
.catch(function (reason) {
|
||||
_this._requestsPending -= 1;
|
||||
logMessage("DataPulseProvider: data for #" + listenerGuid + " updated with error=" + getErrorMessage(reason) + ", pending=" + _this._requestsPending);
|
||||
});
|
||||
};
|
||||
var this_1 = this;
|
||||
for (var listenerGuid in this._subscribers) {
|
||||
_loop_1(listenerGuid);
|
||||
}
|
||||
};
|
||||
DataPulseProvider.prototype._updateDataForSubscriber = function (listenerGuid) {
|
||||
var _this = this;
|
||||
var subscriptionRecord = this._subscribers[listenerGuid];
|
||||
var rangeEndTime = parseInt((Date.now() / 1000).toString());
|
||||
// BEWARE: please note we really need 2 bars, not the only last one
|
||||
// see the explanation below. `10` is the `large enough` value to work around holidays
|
||||
var rangeStartTime = rangeEndTime - periodLengthSeconds(subscriptionRecord.resolution, 10);
|
||||
return this._historyProvider.getBars(subscriptionRecord.symbolInfo, subscriptionRecord.resolution, rangeStartTime, rangeEndTime)
|
||||
.then(function (result) {
|
||||
_this._onSubscriberDataReceived(listenerGuid, result);
|
||||
});
|
||||
};
|
||||
DataPulseProvider.prototype._onSubscriberDataReceived = function (listenerGuid, result) {
|
||||
// means the subscription was cancelled while waiting for data
|
||||
if (!this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
logMessage("DataPulseProvider: Data comes for already unsubscribed subscription #" + listenerGuid);
|
||||
return;
|
||||
}
|
||||
var bars = result.bars;
|
||||
if (bars.length === 0) {
|
||||
return;
|
||||
}
|
||||
var lastBar = bars[bars.length - 1];
|
||||
var subscriptionRecord = this._subscribers[listenerGuid];
|
||||
if (subscriptionRecord.lastBarTime !== null && lastBar.time < subscriptionRecord.lastBarTime) {
|
||||
return;
|
||||
}
|
||||
var isNewBar = subscriptionRecord.lastBarTime !== null && lastBar.time > subscriptionRecord.lastBarTime;
|
||||
// Pulse updating may miss some trades data (ie, if pulse period = 10 secods and new bar is started 5 seconds later after the last update, the
|
||||
// old bar's last 5 seconds trades will be lost). Thus, at fist we should broadcast old bar updates when it's ready.
|
||||
if (isNewBar) {
|
||||
if (bars.length < 2) {
|
||||
throw new Error('Not enough bars in history for proper pulse update. Need at least 2.');
|
||||
}
|
||||
var previousBar = bars[bars.length - 2];
|
||||
subscriptionRecord.listener(previousBar);
|
||||
}
|
||||
subscriptionRecord.lastBarTime = lastBar.time;
|
||||
subscriptionRecord.listener(lastBar);
|
||||
};
|
||||
return DataPulseProvider;
|
||||
}());
|
||||
export { DataPulseProvider };
|
||||
function periodLengthSeconds(resolution, requiredPeriodsCount) {
|
||||
var daysCount = 0;
|
||||
if (resolution === 'D') {
|
||||
daysCount = requiredPeriodsCount;
|
||||
}
|
||||
else if (resolution === 'M') {
|
||||
daysCount = 31 * requiredPeriodsCount;
|
||||
}
|
||||
else if (resolution === 'W') {
|
||||
daysCount = 7 * requiredPeriodsCount;
|
||||
}
|
||||
else {
|
||||
daysCount = requiredPeriodsCount * parseInt(resolution) / (24 * 60);
|
||||
}
|
||||
return daysCount * 24 * 60 * 60;
|
||||
}
|
||||
19
datafeeds/udf/lib/helpers.js
Normal file
19
datafeeds/udf/lib/helpers.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* If you want to enable logs from datafeed set it to `true`
|
||||
*/
|
||||
var isLoggingEnabled = false;
|
||||
export function logMessage(message) {
|
||||
if (isLoggingEnabled) {
|
||||
var now = new Date();
|
||||
console.log(now.toLocaleTimeString() + "." + now.getMilliseconds() + "> " + message);
|
||||
}
|
||||
}
|
||||
export function getErrorMessage(error) {
|
||||
if (error === undefined) {
|
||||
return '';
|
||||
}
|
||||
else if (typeof error === 'string') {
|
||||
return error;
|
||||
}
|
||||
return error.message;
|
||||
}
|
||||
66
datafeeds/udf/lib/history-provider.js
Normal file
66
datafeeds/udf/lib/history-provider.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { getErrorMessage, } from './helpers';
|
||||
var HistoryProvider = /** @class */ (function () {
|
||||
function HistoryProvider(datafeedUrl, requester) {
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._requester = requester;
|
||||
}
|
||||
HistoryProvider.prototype.getBars = function (symbolInfo, resolution, rangeStartDate, rangeEndDate) {
|
||||
var _this = this;
|
||||
var requestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
resolution: resolution,
|
||||
from: rangeStartDate,
|
||||
to: rangeEndDate,
|
||||
};
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this._requester.sendRequest(_this._datafeedUrl, 'history', requestParams)
|
||||
.then(function (response) {
|
||||
if (response.s !== 'ok' && response.s !== 'no_data') {
|
||||
reject(response.errmsg);
|
||||
return;
|
||||
}
|
||||
var bars = [];
|
||||
var meta = {
|
||||
noData: false,
|
||||
};
|
||||
if (response.s === 'no_data') {
|
||||
meta.noData = true;
|
||||
meta.nextTime = response.nextTime;
|
||||
}
|
||||
else {
|
||||
var volumePresent = response.v !== undefined;
|
||||
var ohlPresent = response.o !== undefined;
|
||||
for (var i = 0; i < response.t.length; ++i) {
|
||||
var barValue = {
|
||||
time: response.t[i] * 1000,
|
||||
close: Number(response.c[i]),
|
||||
open: Number(response.c[i]),
|
||||
high: Number(response.c[i]),
|
||||
low: Number(response.c[i]),
|
||||
};
|
||||
if (ohlPresent) {
|
||||
barValue.open = Number(response.o[i]);
|
||||
barValue.high = Number(response.h[i]);
|
||||
barValue.low = Number(response.l[i]);
|
||||
}
|
||||
if (volumePresent) {
|
||||
barValue.volume = Number(response.v[i]);
|
||||
}
|
||||
bars.push(barValue);
|
||||
}
|
||||
}
|
||||
resolve({
|
||||
bars: bars,
|
||||
meta: meta,
|
||||
});
|
||||
})
|
||||
.catch(function (reason) {
|
||||
var reasonString = getErrorMessage(reason);
|
||||
console.warn("HistoryProvider: getBars() failed, error=" + reasonString);
|
||||
reject(reasonString);
|
||||
});
|
||||
});
|
||||
};
|
||||
return HistoryProvider;
|
||||
}());
|
||||
export { HistoryProvider };
|
||||
0
datafeeds/udf/lib/iquotes-provider.js
Normal file
0
datafeeds/udf/lib/iquotes-provider.js
Normal file
28
datafeeds/udf/lib/quotes-provider.js
Normal file
28
datafeeds/udf/lib/quotes-provider.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
var QuotesProvider = /** @class */ (function () {
|
||||
function QuotesProvider(datafeedUrl, requester) {
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._requester = requester;
|
||||
}
|
||||
QuotesProvider.prototype.getQuotes = function (symbols) {
|
||||
var _this = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this._requester.sendRequest(_this._datafeedUrl, 'quotes', { symbols: symbols })
|
||||
.then(function (response) {
|
||||
if (response.s === 'ok') {
|
||||
resolve(response.d);
|
||||
}
|
||||
else {
|
||||
reject(response.errmsg);
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
var errorMessage = getErrorMessage(error);
|
||||
logMessage("QuotesProvider: getQuotes failed, error=" + errorMessage);
|
||||
reject("network error: " + errorMessage);
|
||||
});
|
||||
});
|
||||
};
|
||||
return QuotesProvider;
|
||||
}());
|
||||
export { QuotesProvider };
|
||||
51
datafeeds/udf/lib/quotes-pulse-provider.js
Normal file
51
datafeeds/udf/lib/quotes-pulse-provider.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
var QuotesPulseProvider = /** @class */ (function () {
|
||||
function QuotesPulseProvider(quotesProvider) {
|
||||
this._subscribers = {};
|
||||
this._requestsPending = 0;
|
||||
this._quotesProvider = quotesProvider;
|
||||
setInterval(this._updateQuotes.bind(this, 1 /* Fast */), 10000 /* Fast */);
|
||||
setInterval(this._updateQuotes.bind(this, 0 /* General */), 60000 /* General */);
|
||||
}
|
||||
QuotesPulseProvider.prototype.subscribeQuotes = function (symbols, fastSymbols, onRealtimeCallback, listenerGuid) {
|
||||
this._subscribers[listenerGuid] = {
|
||||
symbols: symbols,
|
||||
fastSymbols: fastSymbols,
|
||||
listener: onRealtimeCallback,
|
||||
};
|
||||
logMessage("QuotesPulseProvider: subscribed quotes with #" + listenerGuid);
|
||||
};
|
||||
QuotesPulseProvider.prototype.unsubscribeQuotes = function (listenerGuid) {
|
||||
delete this._subscribers[listenerGuid];
|
||||
logMessage("QuotesPulseProvider: unsubscribed quotes with #" + listenerGuid);
|
||||
};
|
||||
QuotesPulseProvider.prototype._updateQuotes = function (updateType) {
|
||||
var _this = this;
|
||||
if (this._requestsPending > 0) {
|
||||
return;
|
||||
}
|
||||
var _loop_1 = function (listenerGuid) {
|
||||
this_1._requestsPending++;
|
||||
var subscriptionRecord = this_1._subscribers[listenerGuid];
|
||||
this_1._quotesProvider.getQuotes(updateType === 1 /* Fast */ ? subscriptionRecord.fastSymbols : subscriptionRecord.symbols)
|
||||
.then(function (data) {
|
||||
_this._requestsPending--;
|
||||
if (!_this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
return;
|
||||
}
|
||||
subscriptionRecord.listener(data);
|
||||
logMessage("QuotesPulseProvider: data for #" + listenerGuid + " (" + updateType + ") updated successfully, pending=" + _this._requestsPending);
|
||||
})
|
||||
.catch(function (reason) {
|
||||
_this._requestsPending--;
|
||||
logMessage("QuotesPulseProvider: data for #" + listenerGuid + " (" + updateType + ") updated with error=" + getErrorMessage(reason) + ", pending=" + _this._requestsPending);
|
||||
});
|
||||
};
|
||||
var this_1 = this;
|
||||
for (var listenerGuid in this._subscribers) {
|
||||
_loop_1(listenerGuid);
|
||||
}
|
||||
};
|
||||
return QuotesPulseProvider;
|
||||
}());
|
||||
export { QuotesPulseProvider };
|
||||
29
datafeeds/udf/lib/requester.js
Normal file
29
datafeeds/udf/lib/requester.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { logMessage } from './helpers';
|
||||
var Requester = /** @class */ (function () {
|
||||
function Requester(headers) {
|
||||
if (headers) {
|
||||
this._headers = headers;
|
||||
}
|
||||
}
|
||||
Requester.prototype.sendRequest = function (datafeedUrl, urlPath, params) {
|
||||
if (params !== undefined) {
|
||||
var paramKeys = Object.keys(params);
|
||||
if (paramKeys.length !== 0) {
|
||||
urlPath += '?';
|
||||
}
|
||||
urlPath += paramKeys.map(function (key) {
|
||||
return encodeURIComponent(key) + "=" + encodeURIComponent(params[key].toString());
|
||||
}).join('&');
|
||||
}
|
||||
logMessage('New request: ' + urlPath);
|
||||
var options = {};
|
||||
if (this._headers !== undefined) {
|
||||
options.headers = this._headers;
|
||||
}
|
||||
return fetch(datafeedUrl + "/" + urlPath, options)
|
||||
.then(function (response) { return response.text(); })
|
||||
.then(function (responseTest) { return JSON.parse(responseTest); });
|
||||
};
|
||||
return Requester;
|
||||
}());
|
||||
export { Requester };
|
||||
169
datafeeds/udf/lib/symbols-storage.js
Normal file
169
datafeeds/udf/lib/symbols-storage.js
Normal file
@@ -0,0 +1,169 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
function extractField(data, field, arrayIndex) {
|
||||
var value = data[field];
|
||||
return Array.isArray(value) ? value[arrayIndex] : value;
|
||||
}
|
||||
var SymbolsStorage = /** @class */ (function () {
|
||||
function SymbolsStorage(datafeedUrl, datafeedSupportedResolutions, requester) {
|
||||
this._exchangesList = ['NYSE', 'FOREX', 'AMEX'];
|
||||
this._symbolsInfo = {};
|
||||
this._symbolsList = [];
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._datafeedSupportedResolutions = datafeedSupportedResolutions;
|
||||
this._requester = requester;
|
||||
this._readyPromise = this._init();
|
||||
this._readyPromise.catch(function (error) {
|
||||
// seems it is impossible
|
||||
console.error("SymbolsStorage: Cannot init, error=" + error.toString());
|
||||
});
|
||||
}
|
||||
// BEWARE: this function does not consider symbol's exchange
|
||||
SymbolsStorage.prototype.resolveSymbol = function (symbolName) {
|
||||
var _this = this;
|
||||
return this._readyPromise.then(function () {
|
||||
var symbolInfo = _this._symbolsInfo[symbolName];
|
||||
if (symbolInfo === undefined) {
|
||||
return Promise.reject('invalid symbol');
|
||||
}
|
||||
return Promise.resolve(symbolInfo);
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype.searchSymbols = function (searchString, exchange, symbolType, maxSearchResults) {
|
||||
var _this = this;
|
||||
return this._readyPromise.then(function () {
|
||||
var weightedResult = [];
|
||||
var queryIsEmpty = searchString.length === 0;
|
||||
searchString = searchString.toUpperCase();
|
||||
var _loop_1 = function (symbolName) {
|
||||
var symbolInfo = _this._symbolsInfo[symbolName];
|
||||
if (symbolInfo === undefined) {
|
||||
return "continue";
|
||||
}
|
||||
if (symbolType.length > 0 && symbolInfo.type !== symbolType) {
|
||||
return "continue";
|
||||
}
|
||||
if (exchange && exchange.length > 0 && symbolInfo.exchange !== exchange) {
|
||||
return "continue";
|
||||
}
|
||||
var positionInName = symbolInfo.name.toUpperCase().indexOf(searchString);
|
||||
var positionInDescription = symbolInfo.description.toUpperCase().indexOf(searchString);
|
||||
if (queryIsEmpty || positionInName >= 0 || positionInDescription >= 0) {
|
||||
var alreadyExists = weightedResult.some(function (item) { return item.symbolInfo === symbolInfo; });
|
||||
if (!alreadyExists) {
|
||||
var weight = positionInName >= 0 ? positionInName : 8000 + positionInDescription;
|
||||
weightedResult.push({ symbolInfo: symbolInfo, weight: weight });
|
||||
}
|
||||
}
|
||||
};
|
||||
for (var _i = 0, _a = _this._symbolsList; _i < _a.length; _i++) {
|
||||
var symbolName = _a[_i];
|
||||
_loop_1(symbolName);
|
||||
}
|
||||
var result = weightedResult
|
||||
.sort(function (item1, item2) { return item1.weight - item2.weight; })
|
||||
.slice(0, maxSearchResults)
|
||||
.map(function (item) {
|
||||
var symbolInfo = item.symbolInfo;
|
||||
return {
|
||||
symbol: symbolInfo.name,
|
||||
full_name: symbolInfo.full_name,
|
||||
description: symbolInfo.description,
|
||||
exchange: symbolInfo.exchange,
|
||||
params: [],
|
||||
type: symbolInfo.type,
|
||||
ticker: symbolInfo.name,
|
||||
};
|
||||
});
|
||||
return Promise.resolve(result);
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._init = function () {
|
||||
var _this = this;
|
||||
var promises = [];
|
||||
var alreadyRequestedExchanges = {};
|
||||
for (var _i = 0, _a = this._exchangesList; _i < _a.length; _i++) {
|
||||
var exchange = _a[_i];
|
||||
if (alreadyRequestedExchanges[exchange]) {
|
||||
continue;
|
||||
}
|
||||
alreadyRequestedExchanges[exchange] = true;
|
||||
promises.push(this._requestExchangeData(exchange));
|
||||
}
|
||||
return Promise.all(promises)
|
||||
.then(function () {
|
||||
_this._symbolsList.sort();
|
||||
logMessage('SymbolsStorage: All exchanges data loaded');
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._requestExchangeData = function (exchange) {
|
||||
var _this = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this._requester.sendRequest(_this._datafeedUrl, 'symbol_info', { group: exchange })
|
||||
.then(function (response) {
|
||||
try {
|
||||
_this._onExchangeDataReceived(exchange, response);
|
||||
}
|
||||
catch (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(function (reason) {
|
||||
logMessage("SymbolsStorage: Request data for exchange '" + exchange + "' failed, reason=" + getErrorMessage(reason));
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
SymbolsStorage.prototype._onExchangeDataReceived = function (exchange, data) {
|
||||
var symbolIndex = 0;
|
||||
try {
|
||||
var symbolsCount = data.symbol.length;
|
||||
var tickerPresent = data.ticker !== undefined;
|
||||
for (; symbolIndex < symbolsCount; ++symbolIndex) {
|
||||
var symbolName = data.symbol[symbolIndex];
|
||||
var listedExchange = extractField(data, 'exchange-listed', symbolIndex);
|
||||
var tradedExchange = extractField(data, 'exchange-traded', symbolIndex);
|
||||
var fullName = tradedExchange + ':' + symbolName;
|
||||
var ticker = tickerPresent ? extractField(data, 'ticker', symbolIndex) : symbolName;
|
||||
var symbolInfo = {
|
||||
ticker: ticker,
|
||||
name: symbolName,
|
||||
base_name: [listedExchange + ':' + symbolName],
|
||||
full_name: fullName,
|
||||
listed_exchange: listedExchange,
|
||||
exchange: tradedExchange,
|
||||
description: extractField(data, 'description', symbolIndex),
|
||||
has_intraday: definedValueOrDefault(extractField(data, 'has-intraday', symbolIndex), false),
|
||||
has_no_volume: definedValueOrDefault(extractField(data, 'has-no-volume', symbolIndex), false),
|
||||
minmov: extractField(data, 'minmovement', symbolIndex) || extractField(data, 'minmov', symbolIndex) || 0,
|
||||
minmove2: extractField(data, 'minmove2', symbolIndex) || extractField(data, 'minmov2', symbolIndex),
|
||||
fractional: extractField(data, 'fractional', symbolIndex),
|
||||
pricescale: extractField(data, 'pricescale', symbolIndex),
|
||||
type: extractField(data, 'type', symbolIndex),
|
||||
session: extractField(data, 'session-regular', symbolIndex),
|
||||
timezone: extractField(data, 'timezone', symbolIndex),
|
||||
supported_resolutions: definedValueOrDefault(extractField(data, 'supported-resolutions', symbolIndex), this._datafeedSupportedResolutions),
|
||||
force_session_rebuild: extractField(data, 'force-session-rebuild', symbolIndex),
|
||||
has_daily: definedValueOrDefault(extractField(data, 'has-daily', symbolIndex), true),
|
||||
intraday_multipliers: definedValueOrDefault(extractField(data, 'intraday-multipliers', symbolIndex), ['1', '5', '15', '30', '60']),
|
||||
has_weekly_and_monthly: extractField(data, 'has-weekly-and-monthly', symbolIndex),
|
||||
has_empty_bars: extractField(data, 'has-empty-bars', symbolIndex),
|
||||
volume_precision: definedValueOrDefault(extractField(data, 'volume-precision', symbolIndex), 0),
|
||||
};
|
||||
this._symbolsInfo[ticker] = symbolInfo;
|
||||
this._symbolsInfo[symbolName] = symbolInfo;
|
||||
this._symbolsInfo[fullName] = symbolInfo;
|
||||
this._symbolsList.push(symbolName);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
throw new Error("SymbolsStorage: API error when processing exchange " + exchange + " symbol #" + symbolIndex + " (" + data.symbol[symbolIndex] + "): " + error.message);
|
||||
}
|
||||
};
|
||||
return SymbolsStorage;
|
||||
}());
|
||||
export { SymbolsStorage };
|
||||
function definedValueOrDefault(value, defaultValue) {
|
||||
return value !== undefined ? value : defaultValue;
|
||||
}
|
||||
243
datafeeds/udf/lib/udf-compatible-datafeed-base.js
Normal file
243
datafeeds/udf/lib/udf-compatible-datafeed-base.js
Normal file
@@ -0,0 +1,243 @@
|
||||
import { getErrorMessage, logMessage, } from './helpers';
|
||||
import { HistoryProvider, } from './history-provider';
|
||||
import { DataPulseProvider } from './data-pulse-provider';
|
||||
import { QuotesPulseProvider } from './quotes-pulse-provider';
|
||||
import { SymbolsStorage } from './symbols-storage';
|
||||
function extractField(data, field, arrayIndex) {
|
||||
var value = data[field];
|
||||
return Array.isArray(value) ? value[arrayIndex] : value;
|
||||
}
|
||||
/**
|
||||
* This class implements interaction with UDF-compatible datafeed.
|
||||
* See UDF protocol reference at https://github.com/tradingview/charting_library/wiki/UDF
|
||||
*/
|
||||
var UDFCompatibleDatafeedBase = /** @class */ (function () {
|
||||
function UDFCompatibleDatafeedBase(datafeedURL, quotesProvider, requester, updateFrequency) {
|
||||
if (updateFrequency === void 0) { updateFrequency = 10 * 1000; }
|
||||
var _this = this;
|
||||
this._configuration = defaultConfiguration();
|
||||
this._symbolsStorage = null;
|
||||
this._datafeedURL = datafeedURL;
|
||||
this._requester = requester;
|
||||
this._historyProvider = new HistoryProvider(datafeedURL, this._requester);
|
||||
this._quotesProvider = quotesProvider;
|
||||
this._dataPulseProvider = new DataPulseProvider(this._historyProvider, updateFrequency);
|
||||
this._quotesPulseProvider = new QuotesPulseProvider(this._quotesProvider);
|
||||
this._configurationReadyPromise = this._requestConfiguration()
|
||||
.then(function (configuration) {
|
||||
if (configuration === null) {
|
||||
configuration = defaultConfiguration();
|
||||
}
|
||||
_this._setupWithConfiguration(configuration);
|
||||
});
|
||||
}
|
||||
UDFCompatibleDatafeedBase.prototype.onReady = function (callback) {
|
||||
var _this = this;
|
||||
this._configurationReadyPromise.then(function () {
|
||||
callback(_this._configuration);
|
||||
});
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.getQuotes = function (symbols, onDataCallback, onErrorCallback) {
|
||||
this._quotesProvider.getQuotes(symbols).then(onDataCallback).catch(onErrorCallback);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.subscribeQuotes = function (symbols, fastSymbols, onRealtimeCallback, listenerGuid) {
|
||||
this._quotesPulseProvider.subscribeQuotes(symbols, fastSymbols, onRealtimeCallback, listenerGuid);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.unsubscribeQuotes = function (listenerGuid) {
|
||||
this._quotesPulseProvider.unsubscribeQuotes(listenerGuid);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.calculateHistoryDepth = function (resolution, resolutionBack, intervalBack) {
|
||||
return undefined;
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.getMarks = function (symbolInfo, startDate, endDate, onDataCallback, resolution) {
|
||||
if (!this._configuration.supports_marks) {
|
||||
return;
|
||||
}
|
||||
var requestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
from: startDate,
|
||||
to: endDate,
|
||||
resolution: resolution,
|
||||
};
|
||||
this._send('marks', requestParams)
|
||||
.then(function (response) {
|
||||
if (!Array.isArray(response)) {
|
||||
var result = [];
|
||||
for (var i = 0; i < response.id.length; ++i) {
|
||||
result.push({
|
||||
id: extractField(response, 'id', i),
|
||||
time: extractField(response, 'time', i),
|
||||
color: extractField(response, 'color', i),
|
||||
text: extractField(response, 'text', i),
|
||||
label: extractField(response, 'label', i),
|
||||
labelFontColor: extractField(response, 'labelFontColor', i),
|
||||
minSize: extractField(response, 'minSize', i),
|
||||
});
|
||||
}
|
||||
response = result;
|
||||
}
|
||||
onDataCallback(response);
|
||||
})
|
||||
.catch(function (error) {
|
||||
logMessage("UdfCompatibleDatafeed: Request marks failed: " + getErrorMessage(error));
|
||||
onDataCallback([]);
|
||||
});
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.getTimescaleMarks = function (symbolInfo, startDate, endDate, onDataCallback, resolution) {
|
||||
if (!this._configuration.supports_timescale_marks) {
|
||||
return;
|
||||
}
|
||||
var requestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
from: startDate,
|
||||
to: endDate,
|
||||
resolution: resolution,
|
||||
};
|
||||
this._send('timescale_marks', requestParams)
|
||||
.then(function (response) {
|
||||
if (!Array.isArray(response)) {
|
||||
var result = [];
|
||||
for (var i = 0; i < response.id.length; ++i) {
|
||||
result.push({
|
||||
id: extractField(response, 'id', i),
|
||||
time: extractField(response, 'time', i),
|
||||
color: extractField(response, 'color', i),
|
||||
label: extractField(response, 'label', i),
|
||||
tooltip: extractField(response, 'tooltip', i),
|
||||
});
|
||||
}
|
||||
response = result;
|
||||
}
|
||||
onDataCallback(response);
|
||||
})
|
||||
.catch(function (error) {
|
||||
logMessage("UdfCompatibleDatafeed: Request timescale marks failed: " + getErrorMessage(error));
|
||||
onDataCallback([]);
|
||||
});
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.getServerTime = function (callback) {
|
||||
if (!this._configuration.supports_time) {
|
||||
return;
|
||||
}
|
||||
this._send('time')
|
||||
.then(function (response) {
|
||||
var time = parseInt(response);
|
||||
if (!isNaN(time)) {
|
||||
callback(time);
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
logMessage("UdfCompatibleDatafeed: Fail to load server time, error=" + getErrorMessage(error));
|
||||
});
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.searchSymbols = function (userInput, exchange, symbolType, onResult) {
|
||||
if (this._configuration.supports_search) {
|
||||
var params = {
|
||||
limit: 30 /* SearchItemsLimit */,
|
||||
query: userInput.toUpperCase(),
|
||||
type: symbolType,
|
||||
exchange: exchange,
|
||||
};
|
||||
this._send('search', params)
|
||||
.then(function (response) {
|
||||
if (response.s !== undefined) {
|
||||
logMessage("UdfCompatibleDatafeed: search symbols error=" + response.errmsg);
|
||||
onResult([]);
|
||||
return;
|
||||
}
|
||||
onResult(response);
|
||||
})
|
||||
.catch(function (reason) {
|
||||
logMessage("UdfCompatibleDatafeed: Search symbols for '" + userInput + "' failed. Error=" + getErrorMessage(reason));
|
||||
onResult([]);
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (this._symbolsStorage === null) {
|
||||
throw new Error('UdfCompatibleDatafeed: inconsistent configuration (symbols storage)');
|
||||
}
|
||||
this._symbolsStorage.searchSymbols(userInput, exchange, symbolType, 30 /* SearchItemsLimit */)
|
||||
.then(onResult)
|
||||
.catch(onResult.bind(null, []));
|
||||
}
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.resolveSymbol = function (symbolName, onResolve, onError) {
|
||||
logMessage('Resolve requested');
|
||||
var resolveRequestStartTime = Date.now();
|
||||
function onResultReady(symbolInfo) {
|
||||
logMessage("Symbol resolved: " + (Date.now() - resolveRequestStartTime) + "ms");
|
||||
onResolve(symbolInfo);
|
||||
}
|
||||
if (!this._configuration.supports_group_request) {
|
||||
var params = {
|
||||
symbol: symbolName,
|
||||
};
|
||||
this._send('symbols', params)
|
||||
.then(function (response) {
|
||||
if (response.s !== undefined) {
|
||||
onError('unknown_symbol');
|
||||
}
|
||||
else {
|
||||
onResultReady(response);
|
||||
}
|
||||
})
|
||||
.catch(function (reason) {
|
||||
logMessage("UdfCompatibleDatafeed: Error resolving symbol: " + getErrorMessage(reason));
|
||||
onError('unknown_symbol');
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (this._symbolsStorage === null) {
|
||||
throw new Error('UdfCompatibleDatafeed: inconsistent configuration (symbols storage)');
|
||||
}
|
||||
this._symbolsStorage.resolveSymbol(symbolName).then(onResultReady).catch(onError);
|
||||
}
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.getBars = function (symbolInfo, resolution, rangeStartDate, rangeEndDate, onResult, onError) {
|
||||
this._historyProvider.getBars(symbolInfo, resolution, rangeStartDate, rangeEndDate)
|
||||
.then(function (result) {
|
||||
onResult(result.bars, result.meta);
|
||||
})
|
||||
.catch(onError);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.subscribeBars = function (symbolInfo, resolution, onTick, listenerGuid, onResetCacheNeededCallback) {
|
||||
this._dataPulseProvider.subscribeBars(symbolInfo, resolution, onTick, listenerGuid);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype.unsubscribeBars = function (listenerGuid) {
|
||||
this._dataPulseProvider.unsubscribeBars(listenerGuid);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype._requestConfiguration = function () {
|
||||
return this._send('config')
|
||||
.catch(function (reason) {
|
||||
logMessage("UdfCompatibleDatafeed: Cannot get datafeed configuration - use default, error=" + getErrorMessage(reason));
|
||||
return null;
|
||||
});
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype._send = function (urlPath, params) {
|
||||
return this._requester.sendRequest(this._datafeedURL, urlPath, params);
|
||||
};
|
||||
UDFCompatibleDatafeedBase.prototype._setupWithConfiguration = function (configurationData) {
|
||||
this._configuration = configurationData;
|
||||
if (configurationData.exchanges === undefined) {
|
||||
configurationData.exchanges = [];
|
||||
}
|
||||
if (!configurationData.supports_search && !configurationData.supports_group_request) {
|
||||
throw new Error('Unsupported datafeed configuration. Must either support search, or support group request');
|
||||
}
|
||||
if (configurationData.supports_group_request || !configurationData.supports_search) {
|
||||
this._symbolsStorage = new SymbolsStorage(this._datafeedURL, configurationData.supported_resolutions || [], this._requester);
|
||||
}
|
||||
logMessage("UdfCompatibleDatafeed: Initialized with " + JSON.stringify(configurationData));
|
||||
};
|
||||
return UDFCompatibleDatafeedBase;
|
||||
}());
|
||||
export { UDFCompatibleDatafeedBase };
|
||||
function defaultConfiguration() {
|
||||
return {
|
||||
supports_search: false,
|
||||
supports_group_request: true,
|
||||
supported_resolutions: ['1', '5', '15', '30', '60', '1D', '1W', '1M'],
|
||||
supports_marks: false,
|
||||
supports_timescale_marks: false,
|
||||
};
|
||||
}
|
||||
17
datafeeds/udf/lib/udf-compatible-datafeed.js
Normal file
17
datafeeds/udf/lib/udf-compatible-datafeed.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as tslib_1 from "tslib";
|
||||
import { UDFCompatibleDatafeedBase } from './udf-compatible-datafeed-base';
|
||||
import { QuotesProvider } from './quotes-provider';
|
||||
import { Requester } from './requester';
|
||||
var UDFCompatibleDatafeed = /** @class */ (function (_super) {
|
||||
tslib_1.__extends(UDFCompatibleDatafeed, _super);
|
||||
function UDFCompatibleDatafeed(datafeedURL, updateFrequency) {
|
||||
if (updateFrequency === void 0) { updateFrequency = 10 * 1000; }
|
||||
var _this = this;
|
||||
var requester = new Requester();
|
||||
var quotesProvider = new QuotesProvider(datafeedURL, requester);
|
||||
_this = _super.call(this, datafeedURL, quotesProvider, requester, updateFrequency) || this;
|
||||
return _this;
|
||||
}
|
||||
return UDFCompatibleDatafeed;
|
||||
}(UDFCompatibleDatafeedBase));
|
||||
export { UDFCompatibleDatafeed };
|
||||
20
datafeeds/udf/package.json
Normal file
20
datafeeds/udf/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"promise-polyfill": "6.0.2",
|
||||
"tslib": "1.7.1",
|
||||
"whatwg-fetch": "2.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rollup": "0.49.2",
|
||||
"rollup-plugin-buble": "0.15.0",
|
||||
"rollup-plugin-node-resolve": "3.0.0",
|
||||
"rollup-plugin-uglify": "2.0.1",
|
||||
"typescript": "2.5.3"
|
||||
},
|
||||
"scripts": {
|
||||
"compile": "tsc",
|
||||
"bundle-js": "rollup -c rollup.config.js",
|
||||
"build": "npm run compile && npm run bundle-js"
|
||||
}
|
||||
}
|
||||
39
datafeeds/udf/rollup.config.js
Normal file
39
datafeeds/udf/rollup.config.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/* globals process */
|
||||
|
||||
var buble = require('rollup-plugin-buble');
|
||||
var uglify = require('rollup-plugin-uglify');
|
||||
var nodeResolve = require('rollup-plugin-node-resolve');
|
||||
|
||||
var environment = process.env.ENV || 'development';
|
||||
var isDevelopmentEnv = (environment === 'development');
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
input: 'lib/udf-compatible-datafeed.js',
|
||||
name: 'Datafeeds',
|
||||
sourceMap: false,
|
||||
output: {
|
||||
format: 'umd',
|
||||
file: 'dist/bundle.js',
|
||||
},
|
||||
plugins: [
|
||||
nodeResolve({ jsnext: true, main: true }),
|
||||
buble(),
|
||||
!isDevelopmentEnv && uglify({ output: { inline_script: true } }),
|
||||
],
|
||||
},
|
||||
{
|
||||
input: 'src/polyfills.es6',
|
||||
sourceMap: false,
|
||||
context: 'window',
|
||||
output: {
|
||||
format: 'iife',
|
||||
file: 'dist/polyfills.js',
|
||||
},
|
||||
plugins: [
|
||||
nodeResolve({ jsnext: true, main: true }),
|
||||
buble(),
|
||||
uglify({ output: { inline_script: true } }),
|
||||
],
|
||||
},
|
||||
];
|
||||
144
datafeeds/udf/src/data-pulse-provider.ts
Normal file
144
datafeeds/udf/src/data-pulse-provider.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import {
|
||||
LibrarySymbolInfo,
|
||||
SubscribeBarsCallback,
|
||||
} from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
GetBarsResult,
|
||||
HistoryProvider,
|
||||
} from './history-provider';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
logMessage,
|
||||
} from './helpers';
|
||||
|
||||
interface DataSubscriber {
|
||||
symbolInfo: LibrarySymbolInfo;
|
||||
resolution: string;
|
||||
lastBarTime: number | null;
|
||||
listener: SubscribeBarsCallback;
|
||||
}
|
||||
|
||||
interface DataSubscribers {
|
||||
[guid: string]: DataSubscriber;
|
||||
}
|
||||
|
||||
export class DataPulseProvider {
|
||||
private readonly _subscribers: DataSubscribers = {};
|
||||
private _requestsPending: number = 0;
|
||||
private readonly _historyProvider: HistoryProvider;
|
||||
|
||||
public constructor(historyProvider: HistoryProvider, updateFrequency: number) {
|
||||
this._historyProvider = historyProvider;
|
||||
setInterval(this._updateData.bind(this), updateFrequency);
|
||||
}
|
||||
|
||||
public subscribeBars(symbolInfo: LibrarySymbolInfo, resolution: string, newDataCallback: SubscribeBarsCallback, listenerGuid: string): void {
|
||||
if (this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
logMessage(`DataPulseProvider: already has subscriber with id=${listenerGuid}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this._subscribers[listenerGuid] = {
|
||||
lastBarTime: null,
|
||||
listener: newDataCallback,
|
||||
resolution: resolution,
|
||||
symbolInfo: symbolInfo,
|
||||
};
|
||||
|
||||
logMessage(`DataPulseProvider: subscribed for #${listenerGuid} - {${symbolInfo.name}, ${resolution}}`);
|
||||
}
|
||||
|
||||
public unsubscribeBars(listenerGuid: string): void {
|
||||
delete this._subscribers[listenerGuid];
|
||||
logMessage(`DataPulseProvider: unsubscribed for #${listenerGuid}`);
|
||||
}
|
||||
|
||||
private _updateData(): void {
|
||||
if (this._requestsPending > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._requestsPending = 0;
|
||||
for (const listenerGuid in this._subscribers) { // tslint:disable-line:forin
|
||||
this._requestsPending += 1;
|
||||
this._updateDataForSubscriber(listenerGuid)
|
||||
.then(() => {
|
||||
this._requestsPending -= 1;
|
||||
logMessage(`DataPulseProvider: data for #${listenerGuid} updated successfully, pending=${this._requestsPending}`);
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
this._requestsPending -= 1;
|
||||
logMessage(`DataPulseProvider: data for #${listenerGuid} updated with error=${getErrorMessage(reason)}, pending=${this._requestsPending}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private _updateDataForSubscriber(listenerGuid: string): Promise<void> {
|
||||
const subscriptionRecord = this._subscribers[listenerGuid];
|
||||
|
||||
const rangeEndTime = parseInt((Date.now() / 1000).toString());
|
||||
|
||||
// BEWARE: please note we really need 2 bars, not the only last one
|
||||
// see the explanation below. `10` is the `large enough` value to work around holidays
|
||||
const rangeStartTime = rangeEndTime - periodLengthSeconds(subscriptionRecord.resolution, 10);
|
||||
|
||||
return this._historyProvider.getBars(subscriptionRecord.symbolInfo, subscriptionRecord.resolution, rangeStartTime, rangeEndTime)
|
||||
.then((result: GetBarsResult) => {
|
||||
this._onSubscriberDataReceived(listenerGuid, result);
|
||||
});
|
||||
}
|
||||
|
||||
private _onSubscriberDataReceived(listenerGuid: string, result: GetBarsResult): void {
|
||||
// means the subscription was cancelled while waiting for data
|
||||
if (!this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
logMessage(`DataPulseProvider: Data comes for already unsubscribed subscription #${listenerGuid}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const bars = result.bars;
|
||||
if (bars.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastBar = bars[bars.length - 1];
|
||||
const subscriptionRecord = this._subscribers[listenerGuid];
|
||||
|
||||
if (subscriptionRecord.lastBarTime !== null && lastBar.time < subscriptionRecord.lastBarTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isNewBar = subscriptionRecord.lastBarTime !== null && lastBar.time > subscriptionRecord.lastBarTime;
|
||||
|
||||
// Pulse updating may miss some trades data (ie, if pulse period = 10 secods and new bar is started 5 seconds later after the last update, the
|
||||
// old bar's last 5 seconds trades will be lost). Thus, at fist we should broadcast old bar updates when it's ready.
|
||||
if (isNewBar) {
|
||||
if (bars.length < 2) {
|
||||
throw new Error('Not enough bars in history for proper pulse update. Need at least 2.');
|
||||
}
|
||||
|
||||
const previousBar = bars[bars.length - 2];
|
||||
subscriptionRecord.listener(previousBar);
|
||||
}
|
||||
|
||||
subscriptionRecord.lastBarTime = lastBar.time;
|
||||
subscriptionRecord.listener(lastBar);
|
||||
}
|
||||
}
|
||||
|
||||
function periodLengthSeconds(resolution: string, requiredPeriodsCount: number): number {
|
||||
let daysCount = 0;
|
||||
|
||||
if (resolution === 'D') {
|
||||
daysCount = requiredPeriodsCount;
|
||||
} else if (resolution === 'M') {
|
||||
daysCount = 31 * requiredPeriodsCount;
|
||||
} else if (resolution === 'W') {
|
||||
daysCount = 7 * requiredPeriodsCount;
|
||||
} else {
|
||||
daysCount = requiredPeriodsCount * parseInt(resolution) / (24 * 60);
|
||||
}
|
||||
|
||||
return daysCount * 24 * 60 * 60;
|
||||
}
|
||||
37
datafeeds/udf/src/helpers.ts
Normal file
37
datafeeds/udf/src/helpers.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export interface RequestParams {
|
||||
[paramName: string]: string | string[] | number;
|
||||
}
|
||||
|
||||
export interface UdfResponse {
|
||||
s: string;
|
||||
}
|
||||
|
||||
export interface UdfOkResponse extends UdfResponse {
|
||||
s: 'ok';
|
||||
}
|
||||
|
||||
export interface UdfErrorResponse {
|
||||
s: 'error';
|
||||
errmsg: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* If you want to enable logs from datafeed set it to `true`
|
||||
*/
|
||||
const isLoggingEnabled = false;
|
||||
export function logMessage(message: string): void {
|
||||
if (isLoggingEnabled) {
|
||||
const now = new Date();
|
||||
console.log(`${now.toLocaleTimeString()}.${now.getMilliseconds()}> ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: string | Error | undefined): string {
|
||||
if (error === undefined) {
|
||||
return '';
|
||||
} else if (typeof error === 'string') {
|
||||
return error;
|
||||
}
|
||||
|
||||
return error.message;
|
||||
}
|
||||
119
datafeeds/udf/src/history-provider.ts
Normal file
119
datafeeds/udf/src/history-provider.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import {
|
||||
Bar,
|
||||
HistoryMetadata,
|
||||
LibrarySymbolInfo,
|
||||
} from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
RequestParams,
|
||||
UdfErrorResponse,
|
||||
UdfOkResponse,
|
||||
UdfResponse,
|
||||
} from './helpers';
|
||||
|
||||
import { Requester } from './requester';
|
||||
|
||||
interface HistoryPartialDataResponse extends UdfOkResponse {
|
||||
t: number[];
|
||||
c: number[];
|
||||
o?: never;
|
||||
h?: never;
|
||||
l?: never;
|
||||
v?: never;
|
||||
}
|
||||
|
||||
interface HistoryFullDataResponse extends UdfOkResponse {
|
||||
t: number[];
|
||||
c: number[];
|
||||
o: number[];
|
||||
h: number[];
|
||||
l: number[];
|
||||
v: number[];
|
||||
}
|
||||
|
||||
interface HistoryNoDataResponse extends UdfResponse {
|
||||
s: 'no_data';
|
||||
nextTime?: number;
|
||||
}
|
||||
|
||||
type HistoryResponse = HistoryFullDataResponse | HistoryPartialDataResponse | HistoryNoDataResponse;
|
||||
|
||||
export interface GetBarsResult {
|
||||
bars: Bar[];
|
||||
meta: HistoryMetadata;
|
||||
}
|
||||
|
||||
export class HistoryProvider {
|
||||
private _datafeedUrl: string;
|
||||
private readonly _requester: Requester;
|
||||
|
||||
public constructor(datafeedUrl: string, requester: Requester) {
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._requester = requester;
|
||||
}
|
||||
|
||||
public getBars(symbolInfo: LibrarySymbolInfo, resolution: string, rangeStartDate: number, rangeEndDate: number): Promise<GetBarsResult> {
|
||||
const requestParams: RequestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
resolution: resolution,
|
||||
from: rangeStartDate,
|
||||
to: rangeEndDate,
|
||||
};
|
||||
|
||||
return new Promise((resolve: (result: GetBarsResult) => void, reject: (reason: string) => void) => {
|
||||
this._requester.sendRequest<HistoryResponse>(this._datafeedUrl, 'history', requestParams)
|
||||
.then((response: HistoryResponse | UdfErrorResponse) => {
|
||||
if (response.s !== 'ok' && response.s !== 'no_data') {
|
||||
reject(response.errmsg);
|
||||
return;
|
||||
}
|
||||
|
||||
const bars: Bar[] = [];
|
||||
const meta: HistoryMetadata = {
|
||||
noData: false,
|
||||
};
|
||||
|
||||
if (response.s === 'no_data') {
|
||||
meta.noData = true;
|
||||
meta.nextTime = response.nextTime;
|
||||
} else {
|
||||
const volumePresent = response.v !== undefined;
|
||||
const ohlPresent = response.o !== undefined;
|
||||
|
||||
for (let i = 0; i < response.t.length; ++i) {
|
||||
const barValue: Bar = {
|
||||
time: response.t[i] * 1000,
|
||||
close: Number(response.c[i]),
|
||||
open: Number(response.c[i]),
|
||||
high: Number(response.c[i]),
|
||||
low: Number(response.c[i]),
|
||||
};
|
||||
|
||||
if (ohlPresent) {
|
||||
barValue.open = Number((response as HistoryFullDataResponse).o[i]);
|
||||
barValue.high = Number((response as HistoryFullDataResponse).h[i]);
|
||||
barValue.low = Number((response as HistoryFullDataResponse).l[i]);
|
||||
}
|
||||
|
||||
if (volumePresent) {
|
||||
barValue.volume = Number((response as HistoryFullDataResponse).v[i]);
|
||||
}
|
||||
|
||||
bars.push(barValue);
|
||||
}
|
||||
}
|
||||
|
||||
resolve({
|
||||
bars: bars,
|
||||
meta: meta,
|
||||
});
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
const reasonString = getErrorMessage(reason);
|
||||
console.warn(`HistoryProvider: getBars() failed, error=${reasonString}`);
|
||||
reject(reasonString);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
14
datafeeds/udf/src/iquotes-provider.ts
Normal file
14
datafeeds/udf/src/iquotes-provider.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { QuoteData } from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
UdfOkResponse,
|
||||
} from './helpers';
|
||||
|
||||
export interface UdfQuotesResponse extends UdfOkResponse {
|
||||
d: QuoteData[];
|
||||
}
|
||||
|
||||
export interface IQuotesProvider {
|
||||
// tslint:disable-next-line:variable-name tv-variable-name
|
||||
getQuotes(symbols: string[]): Promise<QuoteData[]>;
|
||||
}
|
||||
2
datafeeds/udf/src/polyfills.es6
Normal file
2
datafeeds/udf/src/polyfills.es6
Normal file
@@ -0,0 +1,2 @@
|
||||
import 'promise-polyfill';
|
||||
import 'whatwg-fetch';
|
||||
37
datafeeds/udf/src/quotes-provider.ts
Normal file
37
datafeeds/udf/src/quotes-provider.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { UdfQuotesResponse, IQuotesProvider } from './iquotes-provider';
|
||||
import { QuoteData } from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
logMessage,
|
||||
UdfErrorResponse,
|
||||
} from './helpers';
|
||||
import { Requester } from './requester';
|
||||
|
||||
export class QuotesProvider implements IQuotesProvider {
|
||||
private readonly _datafeedUrl: string;
|
||||
private readonly _requester: Requester;
|
||||
|
||||
public constructor(datafeedUrl: string, requester: Requester) {
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._requester = requester;
|
||||
}
|
||||
|
||||
public getQuotes(symbols: string[]): Promise<QuoteData[]> {
|
||||
return new Promise((resolve: (data: QuoteData[]) => void, reject: (reason: string) => void) => {
|
||||
this._requester.sendRequest<UdfQuotesResponse>(this._datafeedUrl, 'quotes', { symbols: symbols })
|
||||
.then((response: UdfQuotesResponse | UdfErrorResponse) => {
|
||||
if (response.s === 'ok') {
|
||||
resolve(response.d);
|
||||
} else {
|
||||
reject(response.errmsg);
|
||||
}
|
||||
})
|
||||
.catch((error?: string | Error) => {
|
||||
const errorMessage = getErrorMessage(error);
|
||||
logMessage(`QuotesProvider: getQuotes failed, error=${errorMessage}`);
|
||||
reject(`network error: ${errorMessage}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
85
datafeeds/udf/src/quotes-pulse-provider.ts
Normal file
85
datafeeds/udf/src/quotes-pulse-provider.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import {
|
||||
QuoteData,
|
||||
QuotesCallback,
|
||||
} from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
logMessage,
|
||||
} from './helpers';
|
||||
|
||||
import { IQuotesProvider } from './iquotes-provider';
|
||||
|
||||
interface QuoteSubscriber {
|
||||
symbols: string[];
|
||||
fastSymbols: string[];
|
||||
listener: QuotesCallback;
|
||||
}
|
||||
|
||||
interface QuoteSubscribers {
|
||||
[listenerId: string]: QuoteSubscriber;
|
||||
}
|
||||
|
||||
const enum SymbolsType {
|
||||
General,
|
||||
Fast,
|
||||
}
|
||||
|
||||
const enum UpdateTimeouts {
|
||||
Fast = 10 * 1000,
|
||||
General = 60 * 1000,
|
||||
}
|
||||
|
||||
export class QuotesPulseProvider {
|
||||
private readonly _quotesProvider: IQuotesProvider;
|
||||
private readonly _subscribers: QuoteSubscribers = {};
|
||||
private _requestsPending: number = 0;
|
||||
|
||||
public constructor(quotesProvider: IQuotesProvider) {
|
||||
this._quotesProvider = quotesProvider;
|
||||
|
||||
setInterval(this._updateQuotes.bind(this, SymbolsType.Fast), UpdateTimeouts.Fast);
|
||||
setInterval(this._updateQuotes.bind(this, SymbolsType.General), UpdateTimeouts.General);
|
||||
}
|
||||
|
||||
public subscribeQuotes(symbols: string[], fastSymbols: string[], onRealtimeCallback: QuotesCallback, listenerGuid: string): void {
|
||||
this._subscribers[listenerGuid] = {
|
||||
symbols: symbols,
|
||||
fastSymbols: fastSymbols,
|
||||
listener: onRealtimeCallback,
|
||||
};
|
||||
|
||||
logMessage(`QuotesPulseProvider: subscribed quotes with #${listenerGuid}`);
|
||||
}
|
||||
|
||||
public unsubscribeQuotes(listenerGuid: string): void {
|
||||
delete this._subscribers[listenerGuid];
|
||||
logMessage(`QuotesPulseProvider: unsubscribed quotes with #${listenerGuid}`);
|
||||
}
|
||||
|
||||
private _updateQuotes(updateType: SymbolsType): void {
|
||||
if (this._requestsPending > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const listenerGuid in this._subscribers) { // tslint:disable-line:forin
|
||||
this._requestsPending++;
|
||||
|
||||
const subscriptionRecord = this._subscribers[listenerGuid];
|
||||
this._quotesProvider.getQuotes(updateType === SymbolsType.Fast ? subscriptionRecord.fastSymbols : subscriptionRecord.symbols)
|
||||
.then((data: QuoteData[]) => {
|
||||
this._requestsPending--;
|
||||
if (!this._subscribers.hasOwnProperty(listenerGuid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
subscriptionRecord.listener(data);
|
||||
logMessage(`QuotesPulseProvider: data for #${listenerGuid} (${updateType}) updated successfully, pending=${this._requestsPending}`);
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
this._requestsPending--;
|
||||
logMessage(`QuotesPulseProvider: data for #${listenerGuid} (${updateType}) updated with error=${getErrorMessage(reason)}, pending=${this._requestsPending}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
37
datafeeds/udf/src/requester.ts
Normal file
37
datafeeds/udf/src/requester.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { RequestParams, UdfResponse, UdfErrorResponse, logMessage } from './helpers';
|
||||
|
||||
export class Requester {
|
||||
private _headers: object | undefined;
|
||||
|
||||
public constructor(headers?: object) {
|
||||
if (headers) {
|
||||
this._headers = headers;
|
||||
}
|
||||
}
|
||||
|
||||
public sendRequest<T extends UdfResponse>(datafeedUrl: string, urlPath: string, params?: RequestParams): Promise<T | UdfErrorResponse>;
|
||||
public sendRequest<T>(datafeedUrl: string, urlPath: string, params?: RequestParams): Promise<T>;
|
||||
public sendRequest<T>(datafeedUrl: string, urlPath: string, params?: RequestParams): Promise<T> {
|
||||
if (params !== undefined) {
|
||||
const paramKeys = Object.keys(params);
|
||||
if (paramKeys.length !== 0) {
|
||||
urlPath += '?';
|
||||
}
|
||||
|
||||
urlPath += paramKeys.map((key: string) => {
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(params[key].toString())}`;
|
||||
}).join('&');
|
||||
}
|
||||
|
||||
logMessage('New request: ' + urlPath);
|
||||
|
||||
const options: RequestInit = {};
|
||||
if (this._headers !== undefined) {
|
||||
options.headers = this._headers;
|
||||
}
|
||||
|
||||
return fetch(`${datafeedUrl}/${urlPath}`, options)
|
||||
.then((response: Response) => response.text())
|
||||
.then((responseTest: string) => JSON.parse(responseTest));
|
||||
}
|
||||
}
|
||||
266
datafeeds/udf/src/symbols-storage.ts
Normal file
266
datafeeds/udf/src/symbols-storage.ts
Normal file
@@ -0,0 +1,266 @@
|
||||
import {
|
||||
LibrarySymbolInfo,
|
||||
SearchSymbolResultItem,
|
||||
} from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
logMessage,
|
||||
} from './helpers';
|
||||
|
||||
import { Requester } from './requester';
|
||||
|
||||
interface SymbolInfoMap {
|
||||
[symbol: string]: LibrarySymbolInfo | undefined;
|
||||
}
|
||||
|
||||
interface ExchangeDataResponseOptionalValues {
|
||||
'ticker': string;
|
||||
|
||||
'minmov2': number;
|
||||
'minmove2': number;
|
||||
|
||||
'minmov': number;
|
||||
'minmovement': number;
|
||||
|
||||
'supported-resolutions': string[];
|
||||
|
||||
'force-session-rebuild': boolean;
|
||||
|
||||
'has-intraday': boolean;
|
||||
'has-daily': boolean;
|
||||
'has-weekly-and-monthly': boolean;
|
||||
'has-empty-bars': boolean;
|
||||
'has-no-volume': boolean;
|
||||
|
||||
'intraday-multipliers': string[];
|
||||
|
||||
'volume-precision': number;
|
||||
}
|
||||
|
||||
interface ExchangeDataResponseMandatoryValues {
|
||||
'type': string;
|
||||
'timezone': LibrarySymbolInfo['timezone'];
|
||||
'description': string;
|
||||
|
||||
'exchange-listed': string;
|
||||
'exchange-traded': string;
|
||||
|
||||
'session-regular': string;
|
||||
|
||||
'fractional': boolean;
|
||||
|
||||
'pricescale': number;
|
||||
}
|
||||
|
||||
// Here is some black magic with types to get compile-time checks of names and types
|
||||
type ValueOrArray<T> = T | T[];
|
||||
type ExchangeDataResponse =
|
||||
{
|
||||
symbol: string[];
|
||||
} &
|
||||
{
|
||||
[K in keyof ExchangeDataResponseMandatoryValues]: ValueOrArray<ExchangeDataResponseMandatoryValues[K]>;
|
||||
} &
|
||||
{
|
||||
[K in keyof ExchangeDataResponseOptionalValues]?: ValueOrArray<ExchangeDataResponseOptionalValues[K]>;
|
||||
};
|
||||
|
||||
function extractField<Field extends keyof ExchangeDataResponseMandatoryValues>(data: ExchangeDataResponse, field: Field, arrayIndex: number): ExchangeDataResponseMandatoryValues[Field];
|
||||
function extractField<Field extends keyof ExchangeDataResponseOptionalValues>(data: ExchangeDataResponse, field: Field, arrayIndex: number): ExchangeDataResponseOptionalValues[Field] | undefined;
|
||||
function extractField<Field extends keyof ExchangeDataResponseMandatoryValues>(data: ExchangeDataResponse, field: Field, arrayIndex: number): (ExchangeDataResponseMandatoryValues & ExchangeDataResponseOptionalValues)[Field] | undefined {
|
||||
const value = data[field];
|
||||
return Array.isArray(value) ? value[arrayIndex] : value;
|
||||
}
|
||||
|
||||
export class SymbolsStorage {
|
||||
private readonly _exchangesList: string[] = ['NYSE', 'FOREX', 'AMEX'];
|
||||
private readonly _symbolsInfo: SymbolInfoMap = {};
|
||||
private readonly _symbolsList: string[] = [];
|
||||
private readonly _datafeedUrl: string;
|
||||
private readonly _readyPromise: Promise<void>;
|
||||
private readonly _datafeedSupportedResolutions: string[];
|
||||
private readonly _requester: Requester;
|
||||
|
||||
public constructor(datafeedUrl: string, datafeedSupportedResolutions: string[], requester: Requester) {
|
||||
this._datafeedUrl = datafeedUrl;
|
||||
this._datafeedSupportedResolutions = datafeedSupportedResolutions;
|
||||
this._requester = requester;
|
||||
this._readyPromise = this._init();
|
||||
this._readyPromise.catch((error: Error) => {
|
||||
// seems it is impossible
|
||||
console.error(`SymbolsStorage: Cannot init, error=${error.toString()}`);
|
||||
});
|
||||
}
|
||||
|
||||
// BEWARE: this function does not consider symbol's exchange
|
||||
public resolveSymbol(symbolName: string): Promise<LibrarySymbolInfo> {
|
||||
return this._readyPromise.then(() => {
|
||||
const symbolInfo = this._symbolsInfo[symbolName];
|
||||
if (symbolInfo === undefined) {
|
||||
return Promise.reject('invalid symbol');
|
||||
}
|
||||
|
||||
return Promise.resolve(symbolInfo);
|
||||
});
|
||||
}
|
||||
|
||||
public searchSymbols(searchString: string, exchange: string, symbolType: string, maxSearchResults: number): Promise<SearchSymbolResultItem[]> {
|
||||
interface WeightedItem {
|
||||
symbolInfo: LibrarySymbolInfo;
|
||||
weight: number;
|
||||
}
|
||||
|
||||
return this._readyPromise.then(() => {
|
||||
const weightedResult: WeightedItem[] = [];
|
||||
const queryIsEmpty = searchString.length === 0;
|
||||
|
||||
searchString = searchString.toUpperCase();
|
||||
|
||||
for (const symbolName of this._symbolsList) {
|
||||
const symbolInfo = this._symbolsInfo[symbolName];
|
||||
|
||||
if (symbolInfo === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (symbolType.length > 0 && symbolInfo.type !== symbolType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (exchange && exchange.length > 0 && symbolInfo.exchange !== exchange) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const positionInName = symbolInfo.name.toUpperCase().indexOf(searchString);
|
||||
const positionInDescription = symbolInfo.description.toUpperCase().indexOf(searchString);
|
||||
|
||||
if (queryIsEmpty || positionInName >= 0 || positionInDescription >= 0) {
|
||||
const alreadyExists = weightedResult.some((item: WeightedItem) => item.symbolInfo === symbolInfo);
|
||||
if (!alreadyExists) {
|
||||
const weight = positionInName >= 0 ? positionInName : 8000 + positionInDescription;
|
||||
weightedResult.push({ symbolInfo: symbolInfo, weight: weight });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = weightedResult
|
||||
.sort((item1: WeightedItem, item2: WeightedItem) => item1.weight - item2.weight)
|
||||
.slice(0, maxSearchResults)
|
||||
.map((item: WeightedItem) => {
|
||||
const symbolInfo = item.symbolInfo;
|
||||
return {
|
||||
symbol: symbolInfo.name,
|
||||
full_name: symbolInfo.full_name,
|
||||
description: symbolInfo.description,
|
||||
exchange: symbolInfo.exchange,
|
||||
params: [],
|
||||
type: symbolInfo.type,
|
||||
ticker: symbolInfo.name,
|
||||
};
|
||||
});
|
||||
|
||||
return Promise.resolve(result);
|
||||
});
|
||||
}
|
||||
|
||||
private _init(): Promise<void> {
|
||||
interface BooleanMap {
|
||||
[key: string]: boolean | undefined;
|
||||
}
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
const alreadyRequestedExchanges: BooleanMap = {};
|
||||
|
||||
for (const exchange of this._exchangesList) {
|
||||
if (alreadyRequestedExchanges[exchange]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
alreadyRequestedExchanges[exchange] = true;
|
||||
promises.push(this._requestExchangeData(exchange));
|
||||
}
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(() => {
|
||||
this._symbolsList.sort();
|
||||
logMessage('SymbolsStorage: All exchanges data loaded');
|
||||
});
|
||||
}
|
||||
|
||||
private _requestExchangeData(exchange: string): Promise<void> {
|
||||
return new Promise((resolve: () => void, reject: (error: Error) => void) => {
|
||||
this._requester.sendRequest<ExchangeDataResponse>(this._datafeedUrl, 'symbol_info', { group: exchange })
|
||||
.then((response: ExchangeDataResponse) => {
|
||||
try {
|
||||
this._onExchangeDataReceived(exchange, response);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
logMessage(`SymbolsStorage: Request data for exchange '${exchange}' failed, reason=${getErrorMessage(reason)}`);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private _onExchangeDataReceived(exchange: string, data: ExchangeDataResponse): void {
|
||||
let symbolIndex = 0;
|
||||
|
||||
try {
|
||||
const symbolsCount = data.symbol.length;
|
||||
const tickerPresent = data.ticker !== undefined;
|
||||
|
||||
for (; symbolIndex < symbolsCount; ++symbolIndex) {
|
||||
const symbolName = data.symbol[symbolIndex];
|
||||
const listedExchange = extractField(data, 'exchange-listed', symbolIndex);
|
||||
const tradedExchange = extractField(data, 'exchange-traded', symbolIndex);
|
||||
const fullName = tradedExchange + ':' + symbolName;
|
||||
|
||||
const ticker = tickerPresent ? (extractField(data, 'ticker', symbolIndex) as string) : symbolName;
|
||||
|
||||
const symbolInfo: LibrarySymbolInfo = {
|
||||
ticker: ticker,
|
||||
name: symbolName,
|
||||
base_name: [listedExchange + ':' + symbolName],
|
||||
full_name: fullName,
|
||||
listed_exchange: listedExchange,
|
||||
exchange: tradedExchange,
|
||||
description: extractField(data, 'description', symbolIndex),
|
||||
has_intraday: definedValueOrDefault(extractField(data, 'has-intraday', symbolIndex), false),
|
||||
has_no_volume: definedValueOrDefault(extractField(data, 'has-no-volume', symbolIndex), false),
|
||||
minmov: extractField(data, 'minmovement', symbolIndex) || extractField(data, 'minmov', symbolIndex) || 0,
|
||||
minmove2: extractField(data, 'minmove2', symbolIndex) || extractField(data, 'minmov2', symbolIndex),
|
||||
fractional: extractField(data, 'fractional', symbolIndex),
|
||||
pricescale: extractField(data, 'pricescale', symbolIndex),
|
||||
type: extractField(data, 'type', symbolIndex),
|
||||
session: extractField(data, 'session-regular', symbolIndex),
|
||||
timezone: extractField(data, 'timezone', symbolIndex),
|
||||
supported_resolutions: definedValueOrDefault(extractField(data, 'supported-resolutions', symbolIndex), this._datafeedSupportedResolutions),
|
||||
force_session_rebuild: extractField(data, 'force-session-rebuild', symbolIndex),
|
||||
has_daily: definedValueOrDefault(extractField(data, 'has-daily', symbolIndex), true),
|
||||
intraday_multipliers: definedValueOrDefault(extractField(data, 'intraday-multipliers', symbolIndex), ['1', '5', '15', '30', '60']),
|
||||
has_weekly_and_monthly: extractField(data, 'has-weekly-and-monthly', symbolIndex),
|
||||
has_empty_bars: extractField(data, 'has-empty-bars', symbolIndex),
|
||||
volume_precision: definedValueOrDefault(extractField(data, 'volume-precision', symbolIndex), 0),
|
||||
};
|
||||
|
||||
this._symbolsInfo[ticker] = symbolInfo;
|
||||
this._symbolsInfo[symbolName] = symbolInfo;
|
||||
this._symbolsInfo[fullName] = symbolInfo;
|
||||
|
||||
this._symbolsList.push(symbolName);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`SymbolsStorage: API error when processing exchange ${exchange} symbol #${symbolIndex} (${data.symbol[symbolIndex]}): ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function definedValueOrDefault<T>(value: T | undefined, defaultValue: T): T {
|
||||
return value !== undefined ? value : defaultValue;
|
||||
}
|
||||
355
datafeeds/udf/src/udf-compatible-datafeed-base.ts
Normal file
355
datafeeds/udf/src/udf-compatible-datafeed-base.ts
Normal file
@@ -0,0 +1,355 @@
|
||||
import {
|
||||
DatafeedConfiguration,
|
||||
ErrorCallback,
|
||||
GetMarksCallback,
|
||||
HistoryCallback,
|
||||
HistoryDepth,
|
||||
IDatafeedChartApi,
|
||||
IDatafeedQuotesApi,
|
||||
IExternalDatafeed,
|
||||
LibrarySymbolInfo,
|
||||
Mark,
|
||||
OnReadyCallback,
|
||||
QuotesCallback,
|
||||
ResolutionBackValues,
|
||||
ResolutionString,
|
||||
ResolveCallback,
|
||||
SearchSymbolResultItem,
|
||||
SearchSymbolsCallback,
|
||||
ServerTimeCallback,
|
||||
SubscribeBarsCallback,
|
||||
TimescaleMark,
|
||||
} from '../../../charting_library/datafeed-api';
|
||||
|
||||
import {
|
||||
getErrorMessage,
|
||||
logMessage,
|
||||
RequestParams,
|
||||
UdfErrorResponse,
|
||||
} from './helpers';
|
||||
|
||||
import {
|
||||
GetBarsResult,
|
||||
HistoryProvider,
|
||||
} from './history-provider';
|
||||
|
||||
import { IQuotesProvider } from './iquotes-provider';
|
||||
import { DataPulseProvider } from './data-pulse-provider';
|
||||
import { QuotesPulseProvider } from './quotes-pulse-provider';
|
||||
import { SymbolsStorage } from './symbols-storage';
|
||||
import { Requester } from './requester';
|
||||
|
||||
export interface UdfCompatibleConfiguration extends DatafeedConfiguration {
|
||||
// tslint:disable
|
||||
supports_search?: boolean;
|
||||
supports_group_request?: boolean;
|
||||
// tslint:enable
|
||||
}
|
||||
|
||||
export interface ResolveSymbolResponse extends LibrarySymbolInfo {
|
||||
s: undefined;
|
||||
}
|
||||
|
||||
// it is hack to let's TypeScript make code flow analysis
|
||||
export interface UdfSearchSymbolsResponse extends Array<SearchSymbolResultItem> {
|
||||
s?: undefined;
|
||||
}
|
||||
|
||||
export const enum Constants {
|
||||
SearchItemsLimit = 30,
|
||||
}
|
||||
|
||||
type UdfDatafeedMarkType<T extends TimescaleMark | Mark> = {
|
||||
[K in keyof T]: T[K] | T[K][];
|
||||
} & {
|
||||
id: (string | number)[];
|
||||
};
|
||||
|
||||
type UdfDatafeedMark = UdfDatafeedMarkType<Mark>;
|
||||
type UdfDatafeedTimescaleMark = UdfDatafeedMarkType<TimescaleMark>;
|
||||
|
||||
function extractField<Field extends keyof Mark>(data: UdfDatafeedMark, field: Field, arrayIndex: number): Mark[Field];
|
||||
function extractField<Field extends keyof TimescaleMark>(data: UdfDatafeedTimescaleMark, field: Field, arrayIndex: number): TimescaleMark[Field];
|
||||
function extractField<Field extends keyof (TimescaleMark & Mark)>(data: UdfDatafeedMark & UdfDatafeedTimescaleMark, field: Field, arrayIndex: number): (TimescaleMark & Mark)[Field] {
|
||||
const value = data[field];
|
||||
return Array.isArray(value) ? value[arrayIndex] : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class implements interaction with UDF-compatible datafeed.
|
||||
* See UDF protocol reference at https://github.com/tradingview/charting_library/wiki/UDF
|
||||
*/
|
||||
export class UDFCompatibleDatafeedBase implements IExternalDatafeed, IDatafeedQuotesApi, IDatafeedChartApi {
|
||||
protected _configuration: UdfCompatibleConfiguration = defaultConfiguration();
|
||||
private readonly _datafeedURL: string;
|
||||
private readonly _configurationReadyPromise: Promise<void>;
|
||||
|
||||
private _symbolsStorage: SymbolsStorage | null = null;
|
||||
|
||||
private readonly _historyProvider: HistoryProvider;
|
||||
private readonly _dataPulseProvider: DataPulseProvider;
|
||||
|
||||
private readonly _quotesProvider: IQuotesProvider;
|
||||
private readonly _quotesPulseProvider: QuotesPulseProvider;
|
||||
|
||||
private readonly _requester: Requester;
|
||||
|
||||
protected constructor(datafeedURL: string, quotesProvider: IQuotesProvider, requester: Requester, updateFrequency: number = 10 * 1000) {
|
||||
this._datafeedURL = datafeedURL;
|
||||
this._requester = requester;
|
||||
this._historyProvider = new HistoryProvider(datafeedURL, this._requester);
|
||||
this._quotesProvider = quotesProvider;
|
||||
|
||||
this._dataPulseProvider = new DataPulseProvider(this._historyProvider, updateFrequency);
|
||||
this._quotesPulseProvider = new QuotesPulseProvider(this._quotesProvider);
|
||||
|
||||
this._configurationReadyPromise = this._requestConfiguration()
|
||||
.then((configuration: UdfCompatibleConfiguration | null) => {
|
||||
if (configuration === null) {
|
||||
configuration = defaultConfiguration();
|
||||
}
|
||||
|
||||
this._setupWithConfiguration(configuration);
|
||||
});
|
||||
}
|
||||
|
||||
public onReady(callback: OnReadyCallback): void {
|
||||
this._configurationReadyPromise.then(() => {
|
||||
callback(this._configuration);
|
||||
});
|
||||
}
|
||||
|
||||
public getQuotes(symbols: string[], onDataCallback: QuotesCallback, onErrorCallback: (msg: string) => void): void {
|
||||
this._quotesProvider.getQuotes(symbols).then(onDataCallback).catch(onErrorCallback);
|
||||
}
|
||||
|
||||
public subscribeQuotes(symbols: string[], fastSymbols: string[], onRealtimeCallback: QuotesCallback, listenerGuid: string): void {
|
||||
this._quotesPulseProvider.subscribeQuotes(symbols, fastSymbols, onRealtimeCallback, listenerGuid);
|
||||
}
|
||||
|
||||
public unsubscribeQuotes(listenerGuid: string): void {
|
||||
this._quotesPulseProvider.unsubscribeQuotes(listenerGuid);
|
||||
}
|
||||
|
||||
public calculateHistoryDepth(resolution: ResolutionString, resolutionBack: ResolutionBackValues, intervalBack: number): HistoryDepth | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getMarks(symbolInfo: LibrarySymbolInfo, startDate: number, endDate: number, onDataCallback: GetMarksCallback<Mark>, resolution: ResolutionString): void {
|
||||
if (!this._configuration.supports_marks) {
|
||||
return;
|
||||
}
|
||||
|
||||
const requestParams: RequestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
from: startDate,
|
||||
to: endDate,
|
||||
resolution: resolution,
|
||||
};
|
||||
|
||||
this._send('marks', requestParams)
|
||||
.then((response: Mark[] | UdfDatafeedMark) => {
|
||||
if (!Array.isArray(response)) {
|
||||
const result: Mark[] = [];
|
||||
for (let i = 0; i < response.id.length; ++i) {
|
||||
result.push({
|
||||
id: extractField(response, 'id', i),
|
||||
time: extractField(response, 'time', i),
|
||||
color: extractField(response, 'color', i),
|
||||
text: extractField(response, 'text', i),
|
||||
label: extractField(response, 'label', i),
|
||||
labelFontColor: extractField(response, 'labelFontColor', i),
|
||||
minSize: extractField(response, 'minSize', i),
|
||||
});
|
||||
}
|
||||
|
||||
response = result;
|
||||
}
|
||||
|
||||
onDataCallback(response);
|
||||
})
|
||||
.catch((error?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Request marks failed: ${getErrorMessage(error)}`);
|
||||
onDataCallback([]);
|
||||
});
|
||||
}
|
||||
|
||||
public getTimescaleMarks(symbolInfo: LibrarySymbolInfo, startDate: number, endDate: number, onDataCallback: GetMarksCallback<TimescaleMark>, resolution: ResolutionString): void {
|
||||
if (!this._configuration.supports_timescale_marks) {
|
||||
return;
|
||||
}
|
||||
|
||||
const requestParams: RequestParams = {
|
||||
symbol: symbolInfo.ticker || '',
|
||||
from: startDate,
|
||||
to: endDate,
|
||||
resolution: resolution,
|
||||
};
|
||||
|
||||
this._send('timescale_marks', requestParams)
|
||||
.then((response: TimescaleMark[] | UdfDatafeedTimescaleMark) => {
|
||||
if (!Array.isArray(response)) {
|
||||
const result: TimescaleMark[] = [];
|
||||
for (let i = 0; i < response.id.length; ++i) {
|
||||
result.push({
|
||||
id: extractField(response, 'id', i),
|
||||
time: extractField(response, 'time', i),
|
||||
color: extractField(response, 'color', i),
|
||||
label: extractField(response, 'label', i),
|
||||
tooltip: extractField(response, 'tooltip', i),
|
||||
});
|
||||
}
|
||||
|
||||
response = result;
|
||||
}
|
||||
|
||||
onDataCallback(response);
|
||||
})
|
||||
.catch((error?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Request timescale marks failed: ${getErrorMessage(error)}`);
|
||||
onDataCallback([]);
|
||||
});
|
||||
}
|
||||
|
||||
public getServerTime(callback: ServerTimeCallback): void {
|
||||
if (!this._configuration.supports_time) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._send('time')
|
||||
.then((response: string) => {
|
||||
const time = parseInt(response);
|
||||
if (!isNaN(time)) {
|
||||
callback(time);
|
||||
}
|
||||
})
|
||||
.catch((error?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Fail to load server time, error=${getErrorMessage(error)}`);
|
||||
});
|
||||
}
|
||||
|
||||
public searchSymbols(userInput: string, exchange: string, symbolType: string, onResult: SearchSymbolsCallback): void {
|
||||
if (this._configuration.supports_search) {
|
||||
const params: RequestParams = {
|
||||
limit: Constants.SearchItemsLimit,
|
||||
query: userInput.toUpperCase(),
|
||||
type: symbolType,
|
||||
exchange: exchange,
|
||||
};
|
||||
|
||||
this._send('search', params)
|
||||
.then((response: UdfSearchSymbolsResponse | UdfErrorResponse) => {
|
||||
if (response.s !== undefined) {
|
||||
logMessage(`UdfCompatibleDatafeed: search symbols error=${response.errmsg}`);
|
||||
onResult([]);
|
||||
return;
|
||||
}
|
||||
|
||||
onResult(response);
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Search symbols for '${userInput}' failed. Error=${getErrorMessage(reason)}`);
|
||||
onResult([]);
|
||||
});
|
||||
} else {
|
||||
if (this._symbolsStorage === null) {
|
||||
throw new Error('UdfCompatibleDatafeed: inconsistent configuration (symbols storage)');
|
||||
}
|
||||
|
||||
this._symbolsStorage.searchSymbols(userInput, exchange, symbolType, Constants.SearchItemsLimit)
|
||||
.then(onResult)
|
||||
.catch(onResult.bind(null, []));
|
||||
}
|
||||
}
|
||||
|
||||
public resolveSymbol(symbolName: string, onResolve: ResolveCallback, onError: ErrorCallback): void {
|
||||
logMessage('Resolve requested');
|
||||
|
||||
const resolveRequestStartTime = Date.now();
|
||||
function onResultReady(symbolInfo: LibrarySymbolInfo): void {
|
||||
logMessage(`Symbol resolved: ${Date.now() - resolveRequestStartTime}ms`);
|
||||
onResolve(symbolInfo);
|
||||
}
|
||||
|
||||
if (!this._configuration.supports_group_request) {
|
||||
const params: RequestParams = {
|
||||
symbol: symbolName,
|
||||
};
|
||||
|
||||
this._send('symbols', params)
|
||||
.then((response: ResolveSymbolResponse | UdfErrorResponse) => {
|
||||
if (response.s !== undefined) {
|
||||
onError('unknown_symbol');
|
||||
} else {
|
||||
onResultReady(response);
|
||||
}
|
||||
})
|
||||
.catch((reason?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Error resolving symbol: ${getErrorMessage(reason)}`);
|
||||
onError('unknown_symbol');
|
||||
});
|
||||
} else {
|
||||
if (this._symbolsStorage === null) {
|
||||
throw new Error('UdfCompatibleDatafeed: inconsistent configuration (symbols storage)');
|
||||
}
|
||||
|
||||
this._symbolsStorage.resolveSymbol(symbolName).then(onResultReady).catch(onError);
|
||||
}
|
||||
}
|
||||
|
||||
public getBars(symbolInfo: LibrarySymbolInfo, resolution: ResolutionString, rangeStartDate: number, rangeEndDate: number, onResult: HistoryCallback, onError: ErrorCallback): void {
|
||||
this._historyProvider.getBars(symbolInfo, resolution, rangeStartDate, rangeEndDate)
|
||||
.then((result: GetBarsResult) => {
|
||||
onResult(result.bars, result.meta);
|
||||
})
|
||||
.catch(onError);
|
||||
}
|
||||
|
||||
public subscribeBars(symbolInfo: LibrarySymbolInfo, resolution: ResolutionString, onTick: SubscribeBarsCallback, listenerGuid: string, onResetCacheNeededCallback: () => void): void {
|
||||
this._dataPulseProvider.subscribeBars(symbolInfo, resolution, onTick, listenerGuid);
|
||||
}
|
||||
|
||||
public unsubscribeBars(listenerGuid: string): void {
|
||||
this._dataPulseProvider.unsubscribeBars(listenerGuid);
|
||||
}
|
||||
|
||||
protected _requestConfiguration(): Promise<UdfCompatibleConfiguration | null> {
|
||||
return this._send<UdfCompatibleConfiguration>('config')
|
||||
.catch((reason?: string | Error) => {
|
||||
logMessage(`UdfCompatibleDatafeed: Cannot get datafeed configuration - use default, error=${getErrorMessage(reason)}`);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private _send<T>(urlPath: string, params?: RequestParams): Promise<T> {
|
||||
return this._requester.sendRequest<T>(this._datafeedURL, urlPath, params);
|
||||
}
|
||||
|
||||
private _setupWithConfiguration(configurationData: UdfCompatibleConfiguration): void {
|
||||
this._configuration = configurationData;
|
||||
|
||||
if (configurationData.exchanges === undefined) {
|
||||
configurationData.exchanges = [];
|
||||
}
|
||||
|
||||
if (!configurationData.supports_search && !configurationData.supports_group_request) {
|
||||
throw new Error('Unsupported datafeed configuration. Must either support search, or support group request');
|
||||
}
|
||||
|
||||
if (configurationData.supports_group_request || !configurationData.supports_search) {
|
||||
this._symbolsStorage = new SymbolsStorage(this._datafeedURL, configurationData.supported_resolutions || [], this._requester);
|
||||
}
|
||||
|
||||
logMessage(`UdfCompatibleDatafeed: Initialized with ${JSON.stringify(configurationData)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function defaultConfiguration(): UdfCompatibleConfiguration {
|
||||
return {
|
||||
supports_search: false,
|
||||
supports_group_request: true,
|
||||
supported_resolutions: ['1', '5', '15', '30', '60', '1D', '1W', '1M'],
|
||||
supports_marks: false,
|
||||
supports_timescale_marks: false,
|
||||
};
|
||||
}
|
||||
11
datafeeds/udf/src/udf-compatible-datafeed.ts
Normal file
11
datafeeds/udf/src/udf-compatible-datafeed.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { UDFCompatibleDatafeedBase } from './udf-compatible-datafeed-base';
|
||||
import { QuotesProvider } from './quotes-provider';
|
||||
import { Requester } from './requester';
|
||||
|
||||
export class UDFCompatibleDatafeed extends UDFCompatibleDatafeedBase {
|
||||
public constructor(datafeedURL: string, updateFrequency: number = 10 * 1000) {
|
||||
const requester = new Requester();
|
||||
const quotesProvider = new QuotesProvider(datafeedURL, requester);
|
||||
super(datafeedURL, quotesProvider, requester, updateFrequency);
|
||||
}
|
||||
}
|
||||
25
datafeeds/udf/tsconfig.json
Normal file
25
datafeeds/udf/tsconfig.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "./src",
|
||||
"importHelpers": true,
|
||||
"lib": [
|
||||
"dom",
|
||||
"es2015.promise",
|
||||
"es2015.symbol.wellknown",
|
||||
"es5"
|
||||
],
|
||||
"module": "es6",
|
||||
"moduleResolution": "node",
|
||||
"noEmitOnError": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": true,
|
||||
"outDir": "./lib/",
|
||||
"sourceMap": false,
|
||||
"strict": true,
|
||||
"target": "es5"
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user