<tbody id="futuresInfo"> <tr> <th>代码</th> <th>合约名称</th> <th>最新价</th> <th>涨跌</th> <th>涨跌幅</th> <th>成交量</th> <th>持仓量</th> <th style="color: red">基差</th> </tr> </tbody>
$('#futuresInfo').ready(function(){
var tqsdk = new TQSDK()
const quote_code = ["KQ.m@DCE.c", "KQ.m@CZCE.ZC", "KQ.m@CZCE.OI"]
const quote_attr = ['underlying_symbol', 'ins_name', 'last_price', 'change', 'change_percent', 'volume', 'open_interest']
tqsdk.on('ready', function() {
var futures_info = document.getElementById('futuresInfo')
for (var i=0; i<quote_code.length; i++) {
tqsdk.subscribe_quote(quote_code[i])
var quote = tqsdk.get_quote(quote_code[i])
let contract_row = document.createElement("tr")
for (var k=0; k<quote_attr.length; k++) {
let contract_info = document.createElement("td")
let attr = document.createAttribute("id")
attr.value = quote.underlying_symbol.split(".")[1] + '_' + quote_attr[k]
contract_info.setAttributeNode(attr)
if (quote_attr[k] == 'underlying_symbol') {
contract_info.innerHTML = quote[quote_attr[k]].split(".")[1]
}
else if (quote_attr[k] == 'ins_name') {
contract_info.innerHTML = quote[quote_attr[k]]
}
contract_row.appendChild(contract_info)
}
futures_info.appendChild(contract_row)
}
tqsdk.on('rtn_data', function() {
for (var m=0; m<quote_code.length; m++) {
var quote = tqsdk.get_quote(quote_code[m])
for (var n=2; n<=quote_attr.length; n++) {
let data_cell = document.getElementById(quote.underlying_symbol.split(".")[1] + '_' + quote_attr[n])
data_cell.innerHTML = ((m, n) => {if (typeof quote[quote_attr[n]] == 'number') {return quote[quote_attr[n]].toFixed(2)} else {return '-'}})()
}
}
})
})
})
想实现各主力合约的价格监控,只能实现最后订阅的合约的信息反馈,之前订阅的合约没有信息反馈
Tridro Cho 选择最佳答案 2019年11月18日
你好,https://github.com/shinnytech/tqsdk-js 里有个例子,订阅多个合约应该直接传入一个 Array,不需要一个一个订阅,一个一个订阅后面的会把前面的覆盖掉,只会订阅最后一个。

const quote_code = ["KQ.m@DCE.c", "KQ.m@CZCE.ZC", "KQ.m@CZCE.OI"]
tqsdk.subscribe_quote(quote_code)
Tridro Cho 发表新评论 2019年11月18日
谢谢,已解决