指数行情

URL: https://api.bqi.com/api/public/ticker
参数: group, 必填, 值 0, 20, 100, 500

说明:
group: 0=all, 20=bqi20

  1. curl "https://api.bqi.com/api/public/ticker?group=0"
  2. response:
  3. {
  4. "Group": 0, //指数类型
  5. "Data": {
  6. "Last": 101.61, //最新指数
  7. "High24H": 102.87, //24小时最高指数,滚动粒度5分钟
  8. "Low24H": 100.68, //24小时最低指数,滚动粒度5分钟
  9. "Change24H": -0.49, //24小时涨跌值,滚动粒度5分钟
  10. "ChangeRate24H": -0.48, //24小时涨跌幅%,滚动粒度5分钟
  11. "Time": 1577322393 //时间戳,秒
  12. },
  13. "Code": 200, //成功则返回200
  14. "Message": "" //错误原因
  15. }

所有指数行情

URL: https://api.bqi.com/api/public/tickers

  1. curl "https://api.bqi.com/api/public/tickers"
  2. response:
  3. {
  4. "Data": [{
  5. "Group": 0, //指数类型
  6. "Last": 101.57, //最新指数
  7. "High24H": 102.87, //24小时最高指数,滚动粒度5分钟
  8. "Low24H": 100.68, //24小时最低指数,滚动粒度5分钟
  9. "Change24H": -0.57, //24小时涨跌值,滚动粒度5分钟
  10. "ChangeRate24H": -0.55, //24小时涨跌幅%,滚动粒度5分钟
  11. "Time": 1577322994 //时间戳,秒
  12. }, {
  13. "Group": 20,
  14. "Last": 101.21,
  15. "High24H": 102.4,
  16. "Low24H": 100.36,
  17. "Change24H": -0.47,
  18. "ChangeRate24H": -0.46,
  19. "Time": 1577322994
  20. }, {
  21. "Group": 100,
  22. "Last": 78.46,
  23. "High24H": 126.04,
  24. "Low24H": 77.54,
  25. "Change24H": -23.68,
  26. "ChangeRate24H": -23.18,
  27. "Time": 1577322994
  28. }, {
  29. "Group": 500,
  30. "Last": 104.1,
  31. "High24H": 104.82,
  32. "Low24H": 101.45,
  33. "Change24H": 2.3,
  34. "ChangeRate24H": 2.26,
  35. "Time": 1577322994
  36. }],
  37. "Code": 200, //成功则返回200
  38. "Message": "" //错误原因
  39. }

指数K线行情

URL: https://api.bqi.com/api/public/kline
参数: group, 必填, 值 0, 20, 100, 500
参数: granularity, 必填, 值 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 604800
参数: begin, 可选, 开始时间(时间戳秒)
参数: end, 可选, 结束时间(时间戳秒)
参数: size, 可选, 返回条数(最大2000条)

说明:
group: 0=all, 20=bqi20
granularity: 1分钟, 3分钟, 5分钟, 15分钟, 30分钟, 1小时, 2小时, 4小时, 6小时, 12小时, 1天, 1周

  1. curl "https://api.bqi.com/api/public/kline?group=0&granularity=60"
  2. response:
  3. {
  4. "Group": 0, //指数类型
  5. "Granularity": 60, //k线粒度
  6. "Data": [
  7. // 格式 [ 时间戳(秒), 开盘, 最高, 最低, 收盘 ]
  8. [1577202900, 102.78, 102.79, 102.78, 102.79],
  9. [1577202960, 102.77, 102.77, 102.7, 102.7],
  10. [1577323680, 101.6, 101.6, 101.6, 101.6]
  11. ],
  12. "Code": 200, //成功则返回200
  13. "Message": "" //错误原因
  14. }

指数成员

URL: https://api.bqi.com/api/public/members
参数: group, 必填, 值 0, 20, 100, 500
参数: lang, 可选, 值 en-us, zh-cn

说明:
group: 0=all, 20=bqi20
结果按照市值排序

  1. curl "https://api.bqi.com/api/public/members?group=0"
  2. response:
  3. {
  4. "Group": 0,
  5. "Data": [{
  6. "CoinCode": "bitcoin", //币代码
  7. "CoinName": "Bitcoin", //币名称
  8. "CoinSymbol": "BTC", //币符号
  9. "NativeName": "Bitcoin", //本土名
  10. "MaxSupply": 21000000, //货币总量
  11. "TotalSupply": 17901187, //现有流通量
  12. "CirculatingSupply": 17901187, //循环供应量
  13. "MarketCap": 128903549134, //市值
  14. "Volume": 6449145269.4336, //成交量
  15. "LastPrice": 7200.838086, //当前价格
  16. "HighPrice24H": 7269.954023, //24小时最高
  17. "LowPrice24H": 7142.958062, //24小时最低
  18. "ChangePrice24H": -36.675691, //24小时涨跌额
  19. "ChangePriceRate24H": -0.51, //24小时涨跌幅
  20. "Time": 1577324733 //时间戳,秒
  21. }, {
  22. "CoinCode": "ethereum",
  23. "CoinName": "Ethereum",
  24. "CoinSymbol": "ETH",
  25. "NativeName": "Ethereum",
  26. "MaxSupply": 107100935,
  27. "TotalSupply": 107100935,
  28. "CirculatingSupply": 107100935,
  29. "MarketCap": 13385208818,
  30. "Volume": 2313965685.4661,
  31. "LastPrice": 124.977516,
  32. "HighPrice24H": 126.500768,
  33. "LowPrice24H": 123.988757,
  34. "ChangePrice24H": -1.283036,
  35. "ChangePriceRate24H": -1.02,
  36. "Time": 1577324733
  37. }],
  38. "Code": 200,
  39. "Message": ""
  40. }

WebSocket API

服务器地址:
wss://api.bqi.com/ws/public

基本要求:
收发数据使用 gzip压缩
gzip库 https://github.com/imaya/zlib.js.git

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>WebSocket Demo</title>
  6. </head>
  7. <body>
  8. <script src="gzip.min.js"></script>
  9. <script src="gunzip.min.js"></script>
  10. <script>
  11. var WebSocketAPI = function (onopen) {
  12. var obj = {};
  13. var sock = null;
  14. var dispose_func = null;
  15. var timer = null;
  16. var kline_queue = new Array();
  17. var ticker_queue = new Array();
  18. var cointicker_queue = new Array();
  19. obj.onkline = function (data) { };
  20. obj.onticker = function (data) { };
  21. obj.oncointicker = function (data) { };
  22. var gzip_compress = function (data) {
  23. var plain = new TextEncoder().encode(data);
  24. var gzip = new Zlib.Gzip(plain);
  25. var compressed = gzip.compress();
  26. return compressed;
  27. };
  28. var gzip_decompress = function (data) {
  29. var gunzip = new Zlib.Gunzip(data);
  30. var plain = gunzip.decompress();
  31. return new TextDecoder().decode(plain);
  32. };
  33. var init = function () {
  34. var isclose = false;
  35. sock = new WebSocket('wss://api.bqi.com/ws/public');
  36. sock.onopen = function () {
  37. sock.onmessage = function (ev) {
  38. ev.data.arrayBuffer().then(function (buffer) {
  39. if (null === sock) {
  40. return;
  41. }
  42. try {
  43. var data = gzip_decompress(new Uint8Array(buffer));
  44. data = JSON.parse(data);
  45. if (data.Type === "response") {
  46. if (data.Action === 'ticker') {
  47. var item = ticker_queue.shift();
  48. item.Callback(null, data);
  49. }
  50. else if (data.Action === 'kline') {
  51. var item = kline_queue.shift();
  52. item.Callback(null, data);
  53. }
  54. else if (data.Action === 'coin-ticker') {
  55. var item = cointicker_queue.shift();
  56. item.Callback(null, data);
  57. }
  58. }
  59. else if (data.Type === "event") {
  60. if (data.Action === 'ticker') {
  61. obj.onticker(data);
  62. }
  63. else if (data.Action === 'kline') {
  64. obj.onkline(data);
  65. }
  66. else if (data.Action === 'coin-ticker') {
  67. obj.oncointicker(data);
  68. }
  69. }
  70. }
  71. catch (e) {
  72. sock.close();
  73. }
  74. });
  75. };
  76. var clear = function (ev) {
  77. while (kline_queue.length > 0) {
  78. var item = kline_queue.shift();
  79. item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
  80. }
  81. while (ticker_queue.length > 0) {
  82. var item = ticker_queue.shift();
  83. item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
  84. }
  85. while (cointicker_queue.length > 0) {
  86. var item = cointicker_queue.shift();
  87. item.Callback(new Error("code: " + ev.code + ", reason: " + ev.reason), null);
  88. }
  89. sock = null;
  90. };
  91. sock.onclose = function (ev) {
  92. if (false == isclose) {
  93. isclose = true;
  94. clear(ev);
  95. if (null === dispose_func) {
  96. if (null !== timer) clearTimeout(timer);
  97. timer = setTimeout(init, 1000);
  98. }
  99. else {
  100. dispose_func();
  101. }
  102. }
  103. };
  104. sock.onerror = function (ev) {
  105. if (false == isclose) {
  106. isclose = true;
  107. clear(ev);
  108. if (null === dispose_func) {
  109. if (null !== timer) clearTimeout(timer);
  110. timer = setTimeout(init, 1000);
  111. }
  112. else {
  113. dispose_func();
  114. }
  115. }
  116. };
  117. onopen();
  118. };
  119. };
  120. // 指数行情
  121. // group = 0, 20, 100, 500
  122. // enable = true | false
  123. // func = (err,data) =>{}
  124. obj.ticker = function (group, enable, func) {
  125. ticker_queue.push({
  126. Action: 'ticker', Group: group, Enable: enable,
  127. Callback: func
  128. });
  129. sock.send(gzip_compress(JSON.stringify({
  130. Action: 'ticker', Group: group, Enable: enable
  131. })));
  132. };
  133. // K线行情
  134. // group = 0, 20, 100, 500
  135. // granularity = 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 604800
  136. // enable = true | false
  137. // func = (err,data) =>{}
  138. obj.kline = function (group, granularity, enable, func) {
  139. kline_queue.push({
  140. Action: 'kline', Group: group, Granularity: granularity, Enable: enable,
  141. Callback: func
  142. });
  143. sock.send(gzip_compress(JSON.stringify({
  144. Action: 'kline', Group: group, Granularity: granularity, Enable: enable
  145. })));
  146. };
  147. // 币种行情
  148. // coincode = bitcoin,....
  149. // enable = true | false
  150. // func = (err,data) =>{}
  151. obj.cointicker = function (coincode, enable, func) {
  152. cointicker_queue.push({
  153. Action: 'coin-ticker', CoinCode: coincode, Enable: enable,
  154. Callback: func
  155. });
  156. sock.send(gzip_compress(JSON.stringify({
  157. Action: 'coin-ticker', CoinCode: coincode, Enable: enable
  158. })));
  159. };
  160. // 释放
  161. // func = () =>{}
  162. obj.dispose = function (func) {
  163. if (null !== sock) {
  164. if (WebSocket.CONNECTING === sock.readyState) {
  165. sock.close();
  166. sock = null;
  167. func();
  168. }
  169. else {
  170. dispose_func = func;
  171. sock.close();
  172. }
  173. }
  174. else if (null !== timer) {
  175. clearTimeout(timer);
  176. timer = null;
  177. func();
  178. }
  179. else {
  180. func();
  181. }
  182. };
  183. init();
  184. return obj;
  185. };
  186. </script>
  187. <script>
  188. // 例子
  189. // 掉线会自动重连
  190. var api = new WebSocketAPI(function () {
  191. // 事件
  192. api.onticker = function (data) {
  193. console.log(data);
  194. };
  195. api.onkline = function (data) {
  196. console.log(data);
  197. };
  198. api.oncointicker = function (data) {
  199. console.log(data);
  200. };
  201. // 调用接口
  202. api.ticker(20, true, function (err, data) {
  203. console.log(data);
  204. });
  205. api.ticker(100, true, function (err, data) {
  206. console.log(data);
  207. });
  208. api.kline(20, 300, true, function (err, data) {
  209. console.log(data);
  210. });
  211. api.kline(20, 900, true, function (err, data) {
  212. console.log(data);
  213. });
  214. api.cointicker('bitcoin', true, function (err, data) {
  215. console.log(data);
  216. });
  217. api.cointicker('ethereum', true, function (err, data) {
  218. console.log(data);
  219. });
  220. api.cointicker('eos', true, function (err, data) {
  221. console.log(data);
  222. });
  223. api.cointicker('eos', false, function (err, data) {
  224. console.log(data);
  225. });
  226. //关闭
  227. /*api.dispose(function () {
  228. console.log("close");
  229. });*/
  230. });
  231. </script>
  232. </body>
  233. </html>