getBars(symbolInfo, resolution, rangeStartDate, rangeEndDate, onDataCallback, onErrorCallback) 通过日期获取历史k线数据
1、symbolInfo:SymbolInfo 商品信息对象
2、resolution:周期
3、from:最左边请求的k线时间
4、to:最右边请求的k线时间
5、onDataCallback:
- bars: Bar对象数组{time, close, open, high, low, volume}[]
- meta: object{noData = true | false, nextTime = unix
6、错误回调
调用实时接口存储全局变量,通过更改最后一条k线的现价实现图表走势更新
Datafeeds.UDFCompatibleDatafeed.prototype.getBars = function (symbolInfo, resolution, rangeStartDate, rangeEndDate, onDataCallback, onErrorCallback) {
if (rangeStartDate > 0 && (rangeStartDate + '').length > 10) {
throw new Error(['Got a JS time instead of Unix one.', rangeStartDate, rangeEndDate]);
};
if (resolution === 'D') {
resolution = 0;
};
storage.setItem(window.location.origin + 'resolution', resolution);
this._send(this._datafeedURL + this._historyURL, {
symbol: symbolInfo.ticker,
resolution: resolution,
from: rangeStartDate * 1000,
to: rangeEndDate * 1000
})
.done(function (response) {
var data = response;
if (data.s === 'no_data') {
data.nextTime = storage.getItem('nextTime');
};
var nodata = data.s === 'no_data';
if (data.s !== 'ok' && !nodata) {
if (!!onErrorCallback) {
onErrorCallback(data.s);
}
return;
};
var bars = [];
var barsCount = nodata ? 0 : data.t.length;
var volumePresent = typeof data.v != 'undefined';
var ohlPresent = typeof data.o != 'undefined';
for (var i = 0; i < barsCount; ++i) {
var barValue = {
time: data.t[i] * 1000,
close: data.c[i],
};
if (ohlPresent) {
barValue.open = data.o[i];
barValue.high = data.h[i];
barValue.low = data.l[i];
} else {
barValue.open = barValue.high = barValue.low = barValue.close;
}
if (volumePresent) {
barValue.volume = Math.abs(data.v[i]);
}
bars.push(barValue);
};
let curVariety = JSON.parse(storage.getItem('curVariety'));
let curResolution = storage.getItem(window.location.origin + 'resolution');
if (window.UDFTimer && !nodata && curVariety.exchangeStatus && window.lastPrice) {
let lastbar = bars.pop();
lastbar.close = window.lastPrice;
bars.push(lastbar)
};
onDataCallback(bars, { noData: nodata, nextTime: data.nb || data.nextTime });
})
.fail(function (arg) {
console.warn(['getBars(): HTTP error', arg]);
if (!!onErrorCallback) {
onErrorCallback('network error: ' + JSON.stringify(arg));
}
});
};