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:
Jenkins
2018-02-14 01:10:34 -06:00
parent 7feb3edc93
commit d15c5cfb60
98 changed files with 4647 additions and 2034 deletions

View 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;
}

View 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;
}

View 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);
});
});
}
}

View 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[]>;
}

View File

@@ -0,0 +1,2 @@
import 'promise-polyfill';
import 'whatwg-fetch';

View 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}`);
});
});
}
}

View 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}`);
});
}
}
}

View 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));
}
}

View 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;
}

View 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,
};
}

View 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);
}
}