tradingView使用UDF方法刷新最近一条k线

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) {
	// console.log(symbolInfo,'symbolInfo')  //当前获取的开始时间的时间戳
    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));
            }
        });
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值