Files
ai/datafeeds/udf/lib/udf-compatible-datafeed-base.js
Jenkins daaa59f286 VERSION 1.13 @ 2018-08-24 09:41:58.695938
Volume Color is reversed in v1.13 dev #3227
XSS in indicatorsFile parameter #3210
Unknown 1 minute resolution in resolutions widget #3207
What does supports_group_request do? #3183
Improved documentation for intraday_multipliers #3176
Please run a spell checker over the Wiki #3172
Disable "This chart layout has more than 1000 drawings" popup #3158
Problem with drawings in `60S` resolution #3147
Pivot Points are same across Hourly and Daily Timeframe #3144
Add useful default for minSize in getMarks #3139
JS API from/to vs. startDate/endDate inconsistency #3136
Minor wiki error: three -> four #3134
Defaults for chart style and interval favorites does not work #3036
Add support to disable modify order #3015
Cannot read property 'contains' of null if chart container is not visible #2999
Spread/Ratio indicators #2875
Initial bars load glitch #2665
Bars are shifted when the exchange time is negative #2652
Question for previous close price line #2643
SymbolInfo ticker is mandatory, despite what docs say #2581
12/31/2017 is missing when TZ time is negative #2571
TERMINAL: notifications log tab in the bottom panel #2538
The problem with translating the timeframes into Russian. #2494
Adaptive top panel #2491
Session and Cookie #2484
Maximum call stack size exceeded #2477
1.12 custom save_load_adapter UI event exceptions #2448
How to change color to the up fractals of the Williams Fractals? #2425
 Timescale marks are not displayed correctly in version 1.11 #2423
Display "Delayed" status on the chart #2369
Add default quantity for symbols #2322
Add Market Depth in TradingTerminal #2316
TERMINAL: Change "Don't show order confirmations" from a broker #2261
Scale Ratio #2240
getBars not called after symbol change #2062
side_toolbar_in_fullscreen_mode doesn't work #2036
Adding link in market details widget #2021
Cannot read property 'favorite' of undefined (indicators) #1998
Add Pivot Points #1747
Multiple watchlists #1697
MACD EMA based #1300
Add Themes #1277
Change study default scale #1170
Show dialog when insert Correlation Coefficient #1169
How to get current status of log scale? #1050
Missing onVisibleRangeChange event #813
Synchronous XMLHttpRequest on the main thread is deprecated #773
"Hide All Drawing Tools" button doesn't hide the price of "Horizontal Line" #477
2018-08-24 12:42:02 +03:00

244 lines
11 KiB
JavaScript

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, from, to, onDataCallback, resolution) {
if (!this._configuration.supports_marks) {
return;
}
var requestParams = {
symbol: symbolInfo.ticker || '',
from: from,
to: to,
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, from, to, onDataCallback, resolution) {
if (!this._configuration.supports_timescale_marks) {
return;
}
var requestParams = {
symbol: symbolInfo.ticker || '',
from: from,
to: to,
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,
};
}