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

106
components/kline/tradingView/websocket.js

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

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

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