vee 3 years ago
parent
commit
7d62a2ea15
  1. BIN
      .DS_Store
  2. 2
      components/kline/tradingView/chartConfig.js
  3. 1841
      components/kline/tradingView/contract.js
  4. 46
      components/kline/tradingView/datafeed.js
  5. 46
      components/kline/tradingView/datafeed2.js
  6. 108
      components/kline/tradingView/websocket.js
  7. 108
      components/kline/tradingView/websocket2.js

BIN
.DS_Store

Binary file not shown.

2
components/kline/tradingView/chartConfig.js

@ -39,7 +39,7 @@ var chartConfig = {
"header_resolutions",
"left_toolbar",
"header_screenshot",
"header_chart_type",
//"header_chart_type",
"header_settings",
"header_fullscreen_button",
"header_undo_redo",

1841
components/kline/tradingView/contract.js

File diff suppressed because it is too large

46
components/kline/tradingView/datafeed.js

@ -15,7 +15,7 @@ import {
// 历史数据 第一条数据的 时间撮 因为k线图一次性历史数据只拿一部分,用户吧图往前滑动,就会用这个时间撮去请求更早的 历史数据
var detafeed_historyTime = null
var detafeed_historyTime = 0
// 上一次的 K线周期 切换产品的时候 需要从websock 取消订阅这个
var detafeed_lastResolution = null
// 上一次的产品 切换产品的时候 需要从websock 取消订阅这个
@ -46,14 +46,9 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
"type": "bitcoin",
"volume_precision": 10,
"has_intraday": true,
"seconds_multipliers":['1', '5', '15', '30', '60', '1D', '1W','1M'],
"intraday_multipliers": ['1', '5', '15', '30', '60', '1D', '1W','1M'],
"has_seconds":true,
"supports_marks": true,
"supports_timescale_marks": true,
"supported_time": true,
"has_weekly_and_monthly": true,
"has_no_volume": true,
"intraday_multipliers": ['1', '5', '15', '30', '60', '240', '1440'], // 时间
"has_weekly_and_monthly": false,
"has_no_volume": false,
"regular_session": "24x7",
})
@ -70,7 +65,7 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
const resolutionFormat = (resolution, name, to) => {
let req = `market.${name}.kline.${resolution}min`;
let minutes = resolution;
console.log(resolution)
if (resolution.includes('D')) {
if (resolution.length < 2) resolution = '1' + resolution;
req = `market.${name}.kline.${parseInt(resolution)}day`;
@ -91,12 +86,11 @@ const resolutionFormat = (resolution, name, to) => {
let from = null;
if (to) {
from = to - 50 * minutes * 500;
from = to - 50 * minutes * 60;
if (resolution.includes('M') || resolution.includes('W')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
from = to - 50 * minutes * 500;
from = to - 50 * minutes * 60;
}
}
return {
minutes,
req,
@ -106,13 +100,11 @@ const resolutionFormat = (resolution, name, to) => {
};
FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) {
console.log("获取历史数据",periodParams)
console.log("获取历史数据")
// 切换产品周期 或者 切换产品 会执行这个函数
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// if (resolution.includes('M') || resolution.includes('W')|| resolution.includes('D')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
// reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// }
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to > detafeed_historyTime ? periodParams
.to :
detafeed_historyTime)
// 是历史数据
var history = true
/*
@ -136,18 +128,15 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
第三个参数 是否是历史数据
*/
socket.sendData({
event: "req",
type: "kline",
channel: [reso.req],
fromDate:reso.from,
toDate:reso.to
req: reso.req,
id: "id10",
from: reso.from,
to: reso.to,
}, reso.req, history)
Event.off('data')
Event.on('data', data => {
if (data && Array.isArray(data)) {
// 记录这次请求的时间周期
detafeed_lastResolution = resolution
@ -163,13 +152,12 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
datas.push(i)
}
} else {
console.log('进来')
meta = {
noData: 'no_data',
noData: true,
nextTime: detafeed_historyTime
}
}
onHistoryCallback(datas, {noData:data.length==0})
onHistoryCallback(datas, meta)
}
})
}

46
components/kline/tradingView/datafeed1.js → components/kline/tradingView/datafeed2.js

@ -15,7 +15,7 @@ import {
// 历史数据 第一条数据的 时间撮 因为k线图一次性历史数据只拿一部分,用户吧图往前滑动,就会用这个时间撮去请求更早的 历史数据
var detafeed_historyTime = 0
var detafeed_historyTime = null
// 上一次的 K线周期 切换产品的时候 需要从websock 取消订阅这个
var detafeed_lastResolution = null
// 上一次的产品 切换产品的时候 需要从websock 取消订阅这个
@ -46,9 +46,14 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
"type": "bitcoin",
"volume_precision": 10,
"has_intraday": true,
"intraday_multipliers": ['1', '5', '15', '30', '60', '240', '1440'], // 时间
"has_weekly_and_monthly": false,
"has_no_volume": false,
"seconds_multipliers":['1', '5', '15', '30', '60', '1D', '1W','1M'],
"intraday_multipliers": ['1', '5', '15', '30', '60', '1D', '1W','1M'],
"has_seconds":true,
"supports_marks": true,
"supports_timescale_marks": true,
"supported_time": true,
"has_weekly_and_monthly": true,
"has_no_volume": true,
"regular_session": "24x7",
})
@ -65,7 +70,7 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
const resolutionFormat = (resolution, name, to) => {
let req = `market.${name}.kline.${resolution}min`;
let minutes = resolution;
console.log(resolution)
if (resolution.includes('D')) {
if (resolution.length < 2) resolution = '1' + resolution;
req = `market.${name}.kline.${parseInt(resolution)}day`;
@ -86,11 +91,12 @@ const resolutionFormat = (resolution, name, to) => {
let from = null;
if (to) {
from = to - 50 * minutes * 60;
from = to - 50 * minutes * 500;
if (resolution.includes('M') || resolution.includes('W')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
from = to - 50 * minutes * 60;
from = to - 50 * minutes * 500;
}
}
return {
minutes,
req,
@ -100,11 +106,13 @@ const resolutionFormat = (resolution, name, to) => {
};
FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) {
console.log("获取历史数据")
console.log("获取历史数据",periodParams)
// 切换产品周期 或者 切换产品 会执行这个函数
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to > detafeed_historyTime ? periodParams
.to :
detafeed_historyTime)
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// if (resolution.includes('M') || resolution.includes('W')|| resolution.includes('D')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
// reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// }
// 是历史数据
var history = true
/*
@ -128,15 +136,18 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
第三个参数 是否是历史数据
*/
socket.sendData({
req: reso.req,
id: "id10",
from: reso.from,
to: reso.to,
event: "req",
type: "kline",
channel: [reso.req],
fromDate:reso.from,
toDate:reso.to
}, reso.req, history)
Event.off('data')
Event.on('data', data => {
if (data && Array.isArray(data)) {
// 记录这次请求的时间周期
detafeed_lastResolution = resolution
@ -152,12 +163,13 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
datas.push(i)
}
} else {
console.log('进来')
meta = {
noData: true,
noData: 'no_data',
nextTime: detafeed_historyTime
}
}
onHistoryCallback(datas, meta)
onHistoryCallback(datas, {noData:data.length==0})
}
})
}

108
components/kline/tradingView/websocket.js

@ -1,47 +1,45 @@
import
constant
from '@/utils/constant.js'
import {
Event
} from './event.js'
import pakoJs from './pako.js'
var pako = pakoJs
var socket = {
socket: null, // socket name
id:null,
realTimeData: null, // 请求实时数据的参数
intervalObj: null, // 定时器的名字
lastRealTimeData: null, // 上一次请求的产品
sendData(historyData, realTimeDatas, history) {
// 储存历史数据
this.historyData = historyData
this.realTimeData = realTimeDatas
// 如果上一次订阅过产品
if (this.lastRealTimeData) {
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者 切换产品
// 那么就取消订阅上一次的产品实时数据
if (!history) {
console.log('取消订阅' + this.lastRealTimeData)
this.sendWsRequest({
"unsub": this.lastRealTimeData,
"id": "id1"
})
}
// 请求这一次的历史
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
console.log(111111,realTimeDatas)
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者切换产品咯
// 那么就订阅一下 这次产品的或者周期的 实时数据
if (!history) {
//console.log('订阅新的' + realTimeDatas)
//console.log(111111,realTimeDatas)
console.log('订阅新的' + realTimeDatas)
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
"sub": realTimeDatas,
"id": "id1"
})
this.lastRealTimeData = this.realTimeData
}
} else {
// 如果是第一次订阅,就是说刚进入交易所,
// 先存起来这一次请求的产品 作为历史产品
this.lastRealTimeData = this.realTimeData
@ -50,9 +48,13 @@ var socket = {
}
},
initWs() {
this.socket = new WebSocket(constant.WSSURL)
this.socket = new WebSocket('wss://api.huobi.pro/ws')
this.socket.onopen = () => {
this.sendWsRequest(this.historyData)
this.sendWsRequest({
"sub": this.historyData.req,
"id": "id1"
})
}
this.socket.onmessage = resp => {
this.message(resp)
@ -65,52 +67,46 @@ var socket = {
}
},
error(err) {
//console.log(err, 'depth-socket::error')
console.log(err, 'depth-socket::error')
},
close() {
// 如果websocket关闭的话,就从新打开一下。
this.initWs()
//console.log('depth-socket::close')
console.log('depth-socket::close')
},
message(resp) {
resp=JSON.parse(resp.data)
if (resp.channel === 'conn') {
this.id = resp.data
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
}else {
if(resp.event === 'req'){
//Event.emit('data', resp.data)
if (resp.data && Array.isArray(resp.data)) {
Event.emit('data', resp.data)
}
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.sendWsRequest({
event: 'sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
}else{
Event.emit('realTime', resp.data)
//console.log(resp)
let this_ = this
let reader = new FileReader()
reader.onload = function(e) {
// 对数据进行解压
let msg = JSON.parse(pako.ungzip(reader.result, {
to: 'string'
}))
// console.log(msg)
// 如果是实时数据触发Event('realTime') 喂数据
if (msg.tick) {
Event.emit('realTime', msg.tick)
}
//响应服务器,避免断开连接
if (msg.ping) {
this_.socket.send(JSON.stringify({
pong: msg.ping
}));
this_.hasCheck = true
}
this_.lastRealTimeData = this_.realTimeData
// 如果是历史数据触发Event('data') 绘制数据
if (msg.data && Array.isArray(msg.data)) {
console.log(msg.data)
Event.emit('data', msg.data)
}
}
// //将返回的数据解析为字符串格式
reader.readAsArrayBuffer(resp.data);
},
checkSendMessage(options) {
// 这里处理websocket 连接不上的问题
@ -126,7 +122,7 @@ var socket = {
}
if (i >= checkTimes) {
clearInterval(this.intervalObj)
//console.log('send post_data_str timeout.')
console.log('send post_data_str timeout.')
}
}, 500)
},
@ -139,13 +135,13 @@ var socket = {
this.socket.send(JSON.stringify(options))
break
case 2:
//console.log('ws关闭状态')
console.log('ws关闭状态')
break
case 3:
this.initWs()
break
default:
//console.log('ws未知错误')
console.log('ws未知错误')
}
}
}

108
components/kline/tradingView/websocket1.js → components/kline/tradingView/websocket2.js

@ -1,45 +1,47 @@
import
constant
from '@/utils/constant.js'
import {
Event
} from './event.js'
import pakoJs from './pako.js'
var pako = pakoJs
var socket = {
socket: null, // socket name
id:null,
realTimeData: null, // 请求实时数据的参数
intervalObj: null, // 定时器的名字
lastRealTimeData: null, // 上一次请求的产品
sendData(historyData, realTimeDatas, history) {
// 储存历史数据
this.historyData = historyData
this.realTimeData = realTimeDatas
// 如果上一次订阅过产品
if (this.lastRealTimeData) {
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者 切换产品
// 那么就取消订阅上一次的产品实时数据
if (!history) {
console.log('取消订阅' + this.lastRealTimeData)
this.sendWsRequest({
"unsub": this.lastRealTimeData,
"id": "id1"
})
}
// 请求这一次的历史
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
console.log(111111,realTimeDatas)
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者切换产品咯
// 那么就订阅一下 这次产品的或者周期的 实时数据
if (!history) {
console.log('订阅新的' + realTimeDatas)
//console.log('订阅新的' + realTimeDatas)
//console.log(111111,realTimeDatas)
this.sendWsRequest({
"sub": realTimeDatas,
"id": "id1"
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.lastRealTimeData = this.realTimeData
}
} else {
// 如果是第一次订阅,就是说刚进入交易所,
// 先存起来这一次请求的产品 作为历史产品
this.lastRealTimeData = this.realTimeData
@ -48,13 +50,9 @@ var socket = {
}
},
initWs() {
this.socket = new WebSocket('wss://api.huobi.pro/ws')
this.socket = new WebSocket(constant.WSSURL)
this.socket.onopen = () => {
this.sendWsRequest(this.historyData)
this.sendWsRequest({
"sub": this.historyData.req,
"id": "id1"
})
}
this.socket.onmessage = resp => {
this.message(resp)
@ -67,46 +65,52 @@ var socket = {
}
},
error(err) {
console.log(err, 'depth-socket::error')
//console.log(err, 'depth-socket::error')
},
close() {
// 如果websocket关闭的话,就从新打开一下。
this.initWs()
console.log('depth-socket::close')
//console.log('depth-socket::close')
},
message(resp) {
//console.log(resp)
let this_ = this
let reader = new FileReader()
reader.onload = function(e) {
// 对数据进行解压
let msg = JSON.parse(pako.ungzip(reader.result, {
to: 'string'
}))
// console.log(msg)
// 如果是实时数据触发Event('realTime') 喂数据
if (msg.tick) {
Event.emit('realTime', msg.tick)
}
resp=JSON.parse(resp.data)
if (resp.channel === 'conn') {
this.id = resp.data
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
//响应服务器,避免断开连接
if (msg.ping) {
this_.socket.send(JSON.stringify({
pong: msg.ping
}));
this_.hasCheck = true
}
this_.lastRealTimeData = this_.realTimeData
// 如果是历史数据触发Event('data') 绘制数据
if (msg.data && Array.isArray(msg.data)) {
console.log(msg.data)
Event.emit('data', msg.data)
}else {
if(resp.event === 'req'){
//Event.emit('data', resp.data)
if (resp.data && Array.isArray(resp.data)) {
Event.emit('data', resp.data)
}
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.sendWsRequest({
event: 'sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
}else{
Event.emit('realTime', resp.data)
}
}
// //将返回的数据解析为字符串格式
reader.readAsArrayBuffer(resp.data);
},
checkSendMessage(options) {
// 这里处理websocket 连接不上的问题
@ -122,7 +126,7 @@ var socket = {
}
if (i >= checkTimes) {
clearInterval(this.intervalObj)
console.log('send post_data_str timeout.')
//console.log('send post_data_str timeout.')
}
}, 500)
},
@ -135,13 +139,13 @@ var socket = {
this.socket.send(JSON.stringify(options))
break
case 2:
console.log('ws关闭状态')
//console.log('ws关闭状态')
break
case 3:
this.initWs()
break
default:
console.log('ws未知错误')
//console.log('ws未知错误')
}
}
}
Loading…
Cancel
Save