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