加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[bigdata-104] 多交易所的多币种的行情

发布时间:2020-12-14 03:27:20 所属栏目:大数据 来源:网络整理
导读:每十秒更新一次,从若干websoket读取数据然后显示各种计算。 js文件不列出。仅仅作为参考文档。 !DOCTYPE htmlheadmeta charset="utf-8" /meta http-equiv="refresh" content="50" titleWebSocket Test/titlescript type="text/javascript" src="MD5.js"/scr

每十秒更新一次,从若干websoket读取数据然后显示各种计算。

js文件不列出。仅仅作为参考文档。

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="50"> 
<title>WebSocket Test</title>

<script type="text/javascript" src="MD5.js"></script>
<script src="pako.js"></script>
<script src="jquery.min.js"></script>

<script type="text/javascript">
    //交易费用 火币和bfx的
	var huobi_bfx_btc_jiaoyi = 0.6;
	var huobi_bfx_ltc_jiaoyi = 0.7;
	var huobi_bfx_eth_jiaoyi = 0.8;

	//交易费用 ok币和bfx的
	var ok_bfx_btc_jiaoyi = 0.6;
	var ok_bfx_ltc_jiaoyi = 0.7;
	var ok_bfx_eth_jiaoyi = 0.8;

	//各价格	
	var ok_btc_cny,ok_ltc_cny,ok_eth_cny;
    var huobi_btc_cny,huobi_ltc_cny,huobi_eth_cny;
	var bfx_btc_usd,bfx_btc_cny;
	var bfx_ltc_usd,bfx_ltc_cny;
	var bfx_eth_usd,bfx_eth_cny;
    //美元汇率
	var cny_1_usd = -1;

	var okCoinWebSocket = {};

	function changeTwoDecimal(x){
		var f_x = parseFloat(x);
		if (isNaN(f_x)){
			return false;
		}
		var f_x = Math.round(x*100)/100;
		return f_x;
	}

	function curDateTime(){
		var d = new Date(); 
		var year = d.getFullYear(); 
		var month = d.getMonth()+1; 
		var date = d.getDate(); 
		var day = d.getDay(); 
		var hours = d.getHours(); 
		var minutes = d.getMinutes(); 
		var seconds = d.getSeconds(); 
		var ms = d.getMilliseconds();   
		var curDateTime= year;

		if(month>9)
			curDateTime = curDateTime +"-"+month;
		else
			curDateTime = curDateTime +"-0"+month;

		if(date>9)
			curDateTime = curDateTime +"-"+date;
		else
			curDateTime = curDateTime +"-0"+date;

		if(hours>9)
			curDateTime = curDateTime +" "+hours;
		else
			curDateTime = curDateTime +" 0"+hours;	
		if(minutes>9)
			curDateTime = curDateTime +":"+minutes;
		else
			curDateTime = curDateTime +":0"+minutes;
		if(seconds>9)
			curDateTime = curDateTime +":"+seconds;
		else
			curDateTime = curDateTime +":0"+seconds;
		return curDateTime; 
}

	//okcoin 实时读取 btc价格 ltc价格
	okCoinWebSocket.init = function() {
		this.wsUri = "wss://real.okcoin.cn:10440/websocket/okcoinapi";
		this.apiKey = '';
		this.secretKey = '';
		this.lastHeartBeat = new Date().getTime();
		this.overtime = 8000;
		okCoinWebSocket.websocket_btc = new WebSocket(okCoinWebSocket.wsUri);
		okCoinWebSocket.websocket_btc.onopen = function(evt) {
			onOpen_btc(evt)
		};
		okCoinWebSocket.websocket_btc.onmessage = function(evt) {
			onMessage_btc(evt)
		};
	}
	function onOpen_btc(evt) {
		doSend_btc("{'event':'addChannel','channel':'ok_sub_spotcny_btc_ticker'}");
		doSend_btc("{'event':'addChannel','channel':'ok_sub_spotcny_ltc_ticker'}");
		doSend_btc("{'event':'addChannel','channel':'ok_sub_spotcny_eth_ticker'}");
	}
	function onMessage_btc(e) {
		var array = JSON.parse(e.data);
		if (array[0]['channel'] == "ok_sub_spotcny_btc_ticker"){
			//console.log('---------------------------');
			//console.log(array);
			//console.log(array[0]['data']);
			ok_btc_cny = parseFloat(array[0]['data']['last']);
			update_gap_profit();
			//document.getElementById("ok_btc").innerHTML = array[0]['data']['last'];
			//document.getElementById("btc_gap").innerHTML = changeTwoDecimal(ok_btc-huobi_btc);
		}else if (array[0]['channel'] == "ok_sub_spotcny_ltc_ticker"){
			//console.log('---------------------------');
			//console.log(array);
			//console.log(array[0]['data']);
			ok_ltc_cny = parseFloat(array[0]['data']['last']);
			update_gap_profit();
			//document.getElementById("ok_ltc").innerHTML = array[0]['data']['last'];
			//document.getElementById("ltc_gap").innerHTML = changeTwoDecimal(ok_ltc-huobi_ltc);
		}else if (array[0]['channel'] == "ok_sub_spotcny_eth_ticker"){
			//console.log('---------------------------');
			//console.log(array);
			//console.log(array[0]['data']);
			ok_eth_cny = parseFloat(array[0]['data']['last']);
			update_gap_profit();
			//document.getElementById("ok_eth").innerHTML = array[0]['data']['last'];
			//document.getElementById("eth_gap").innerHTML = changeTwoDecimal(ok_eth-huobi_eth);
		}
	}
	function doSend_btc(message) {
		okCoinWebSocket.websocket_btc.send(message);
	}

	//火币
	function huobi(){
		//huobi enth 实时读取
		var huobiWebSocket = new WebSocket("wss://be.huobi.com/ws");
		huobiWebSocket.binaryType = "arraybuffer";
		huobiWebSocket.onopen = function(event){
			huobiWebSocket.send(JSON.stringify({'sub': 'market.ethcny.kline.1min','id': 'depth ' + new Date()}));
			//huobiWebSocket.send(JSON.stringify({'sub': 'market.btccny.kline.1min','id': 'depth2 ' + new Date()}));
		}
		huobiWebSocket.onmessage = function(event){
			//console.log('---------------------------');
			//console.log(event);

			var ua = new Uint8Array(event.data);
			var json = pako.inflate(ua,{to:"string"});
			var data = JSON.parse(json);
			//console.log(data.ch)
			if(data.ch == "market.ethcny.kline.1min"){
				huobi_eth_cny = parseFloat(data.tick.close);
				update_gap_profit();
				//$("#huobi_eth").html(data.tick.close);
				//document.getElementById("eth_gap").innerHTML = changeTwoDecimal(ok_eth-huobi_eth);
			}
		}

		//huobi btc ltc实时行情
		var huobiWebSocket2 = new WebSocket("wss://api.huobi.com/ws");
		huobiWebSocket2.binaryType = "arraybuffer";
		huobiWebSocket2.onopen = function(event){
			huobiWebSocket2.send(JSON.stringify({'sub': 'market.btccny.kline.1min','id': 'depth ' + new Date()}));
			huobiWebSocket2.send(JSON.stringify({'sub': 'market.ltccny.kline.1min','id': 'depth ' + new Date()}));
		}
		huobiWebSocket2.onmessage = function(event){
			//console.log('---------------------------');
			//console.log(event);

			var ua = new Uint8Array(event.data);
			var json = pako.inflate(ua,{to:"string"});
			var data = JSON.parse(json);
			//console.log(data.ch)
			if(data.ch == "market.btccny.kline.1min"){
				huobi_btc_cny = parseFloat(data.tick.close);
				update_gap_profit();
				//$("#huobi_btc").html(data.tick.close);
				//document.getElementById("btc_gap").innerHTML = changeTwoDecimal(ok_btc-huobi_btc);
			}else if(data.ch == "market.ltccny.kline.1min"){
				huobi_ltc_cny = parseFloat(data.tick.close);
				update_gap_profit();
				//$("#huobi_ltc").html(data.tick.close);
				//document.getElementById("ltc_gap").innerHTML = changeTwoDecimal(ok_ltc-huobi_ltc);
			}
		}
	}

	
	function bitfinex(){
		//bitfinex 实时行情btcusd
		var bitfinexWebSocket = new WebSocket("wss://api.bitfinex.com/ws");
		bitfinex_chan_id = new Object();
		//记录通道的id
		//https://bitfinex.readme.io/v1/reference#ws-public-order-books
		//btcusd,ltcusd,ethusd,ltcbtc,ethbtc
		bitfinex_chan_id.btcusd = -1;
		bitfinex_chan_id.ltcusd = -1;
		bitfinex_chan_id.ethusd = -1;
		bitfinexWebSocket.onopen = function(){
			bitfinexWebSocket.send(JSON.stringify({'event': 'subscribe','channel': 'ticker','pair':'BTCUSD'}));
			bitfinexWebSocket.send(JSON.stringify({'event': 'subscribe','pair':'LTCUSD'}));
			bitfinexWebSocket.send(JSON.stringify({'event': 'subscribe','pair':'ETHUSD'}));
		}
		bitfinexWebSocket.onmessage = function(event){
			//console.log('---------------------------');
			//console.log(event);
			data = JSON.parse(event.data);
			if (data["event"]=="subscribed"){
				if (data["pair"]=="BTCUSD"){
					bitfinex_chan_id.btcusd = data["chanId"];
				}else if (data["pair"]=="LTCUSD"){
					bitfinex_chan_id.ltcusd = data["chanId"];
				}else if (data["pair"]=="ETHUSD"){
					bitfinex_chan_id.ethusd = data["chanId"];
				}
			}
			if (data.length == 11){
				//alert(curDateTime());
				$("#cur_time").html(curDateTime());
				$("#usd_rat").html(cny_1_usd);
				
				if (data[0] == bitfinex_chan_id.btcusd){
					bfx_btc_usd = data[7];
					bfx_btc_cny = data[7]*cny_1_usd;
					update_gap_profit();
					//$("#bitnefix_btc").html(changeTwoDecimal(bitf_btcusd));
				}else if (data[0] == bitfinex_chan_id.ltcusd){
					bfx_ltc_usd = data[7];
					bfx_ltc_cny = data[7]*cny_1_usd;
					update_gap_profit();
					//$("#bitnefix_ltc").html(changeTwoDecimal(bitf_ltcusd));
				}else if (data[0] == bitfinex_chan_id.ethusd){
					bfx_eth_usd = parseFloat(data[7]);
					bfx_eth_cny = parseFloat(data[7])*cny_1_usd;
					update_gap_profit();
					//$("#bitnefix_eth").html(changeTwoDecimal(bitf_ethusd));
				}
			}
		}
	}

	function update_gap_profit(){
		$(".bfx_btc_usd").html(changeTwoDecimal(bfx_btc_usd));
		$(".bfx_btc_cny").html(changeTwoDecimal(bfx_btc_cny));
		$(".bfx_ltc_usd").html(changeTwoDecimal(bfx_ltc_usd));
		$(".bfx_ltc_cny").html(changeTwoDecimal(bfx_ltc_cny));
		$(".bfx_eth_usd").html(changeTwoDecimal(bfx_eth_usd));
		$(".bfx_eth_cny").html(changeTwoDecimal(bfx_eth_cny));
		$(".huobi_btc_cny").html(changeTwoDecimal(huobi_btc_cny));
		$(".huobi_ltc_cny").html(changeTwoDecimal(huobi_ltc_cny));
		$(".huobi_eth_cny").html(changeTwoDecimal(huobi_eth_cny));
		$(".ok_btc_cny").html(changeTwoDecimal(ok_btc_cny));
		$(".ok_ltc_cny").html(changeTwoDecimal(ok_ltc_cny));
		$(".ok_eth_cny").html(changeTwoDecimal(ok_eth_cny));
		//火币除以bfx的价格-1
		var huobi_bfx_btc_per = 100.0*huobi_btc_cny/bfx_btc_cny-100.0
        $(".bfx_huobi_btc_gap").html(changeTwoDecimal(huobi_bfx_btc_per)+"%");
		var huobi_bfx_ltc_per = 100.0*huobi_ltc_cny/bfx_ltc_cny-100.0
        $(".bfx_huobi_ltc_gap").html(changeTwoDecimal(huobi_bfx_ltc_per)+"%");
		var huobi_bfx_eth_per = 100.0*huobi_eth_cny/bfx_eth_cny-100.0
        $(".bfx_huobi_eth_gap").html(changeTwoDecimal(huobi_bfx_eth_per)+"%");
		var min_1 = Math.min(huobi_bfx_btc_per,huobi_bfx_ltc_per,huobi_bfx_eth_per);	
        $(".bfx_huobi_btc_profit").html(changeTwoDecimal(huobi_bfx_btc_per-min_1-huobi_bfx_btc_jiaoyi)+"%");
        $(".bfx_huobi_ltc_profit").html(changeTwoDecimal(huobi_bfx_ltc_per-min_1-huobi_bfx_ltc_jiaoyi)+"%");
        $(".bfx_huobi_eth_profit").html(changeTwoDecimal(huobi_bfx_eth_per-min_1-huobi_bfx_eth_jiaoyi)+"%");
		
			

		//OK币除以bfx的价格-1
		var ok_bfx_btc_per = 100.0*ok_btc_cny/bfx_btc_cny-100.0
        $(".bfx_ok_btc_gap").html(changeTwoDecimal(ok_bfx_btc_per)+"%");
		var ok_bfx_ltc_per = 100.0*ok_ltc_cny/bfx_ltc_cny-100.0
        $(".bfx_ok_ltc_gap").html(changeTwoDecimal(ok_bfx_ltc_per)+"%");
		var ok_bfx_eth_per = 100.0*ok_eth_cny/bfx_eth_cny-100.0
        $(".bfx_ok_eth_gap").html(changeTwoDecimal(ok_bfx_eth_per)+"%");
		var min_2 = Math.min(ok_bfx_btc_per,ok_bfx_ltc_per,ok_bfx_eth_per);
        $(".bfx_ok_btc_profit").html(changeTwoDecimal(ok_bfx_btc_per-min_2-ok_bfx_btc_jiaoyi)+"%");
        $(".bfx_ok_ltc_profit").html(changeTwoDecimal(ok_bfx_ltc_per-min_2-ok_bfx_ltc_jiaoyi)+"%");
        $(".bfx_ok_eth_profit").html(changeTwoDecimal(ok_bfx_eth_per-min_2-ok_bfx_eth_jiaoyi)+"%");
		
	}

	function run_loop(){
		$.get("http://api.fixer.io/latest?base=USD",function(data){
			cny_1_usd = data["rates"]["CNY"];
		});
        
		okCoinWebSocket.init();
		huobi();
		bitfinex();
	}
	window.onload = run_loop(); 
</script>
</head>

<body>

<p align="center" id="cur_time"></p>
<p id="usd_rat"></p>

<br/>
<br/>

<table border="1">
  <tr>
    <th width='100'></th>
    <th width='100'>BFXUSD</th>
    <th width='100'>BFXCNY</th>
    <th width='100'>HUOBI</th>
    <th width='100'>GAP</th>
    <th width='100'>PROFIT</th>
  </tr>

  <tr>
    <td>btc</td>    
    <td class="bfx_btc_usd"></td>
    <td class="bfx_btc_cny"></td>
    <td class="huobi_btc_cny"></td>
    <td class="bfx_huobi_btc_gap"></td>
    <td class="bfx_huobi_btc_profit"></td>
  </tr>

  <tr>
    <td>ltc</td>    
    <td class="bfx_ltc_usd"></td>
    <td class="bfx_ltc_cny"></td>
    <td class="huobi_ltc_cny"></td>
    <td class="bfx_huobi_ltc_gap"></td>
    <td class="bfx_huobi_ltc_profit"></td>
  </tr>


  <tr>
    <td>eth</td>    
    <td class="bfx_eth_usd"></td>
    <td class="bfx_eth_cny"></td>
    <td class="huobi_eth_cny"></td>
    <td class="bfx_huobi_eth_gap"></td>
    <td class="bfx_huobi_eth_profit"></td>
  </tr>
</table>

<br/>
<br/>

<table border="1">
  <tr>
    <th width='100'></th>
    <th width='100'>BFXUSD</th>
    <th width='100'>BFXCNY</th>
    <th width='100'>OKCOIN</th>
    <th width='100'>GAP</th>
    <th width='100'>PROFIT</th>
  </tr>

  <tr>
    <td>btc</td>    
    <td class="bfx_btc_usd"></td>
    <td class="bfx_btc_cny"></td>
    <td class="ok_btc_cny"></td>
    <td class="bfx_ok_btc_gap"></td>
    <td class="bfx_ok_btc_profit"></td>
  </tr>

  <tr>
    <td>ltc</td>    
    <td class="bfx_ltc_usd"></td>
    <td class="bfx_ltc_cny"></td>
    <td class="ok_ltc_cny"></td>
    <td class="bfx_ok_ltc_gap"></td>
    <td class="bfx_ok_ltc_profit"></td>
  </tr>

  <tr>
    <td>eth</td>    
    <td class="bfx_eth_usd"></td>
    <td class="bfx_eth_cny"></td>
    <td class="ok_eth_cny"></td>
    <td class="bfx_ok_eth_gap"></td>
    <td class="bfx_ok_eth_profit"></td>
  </tr>
</table>




</body>

</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读