开源Python做的火币和ZB搬砖差价监控程序
程序员文章站
2023-12-26 22:34:21
火币和ZB网站域名经常换,页面也经常改,只保证发布文章时能用,将来要用自己改改URL地址一类的。 代码涉及到websocket和requests采集数据,使用pyquery解析HTML页面,colorama输出彩色文本。 功能:自动采集火币和ZB同种货币,实时采集USDT和QC的OTC法币买卖价格, ......
火币和zb网站域名经常换,页面也经常改,只保证发布文章时能用,将来要用自己改改url地址一类的。
代码涉及到websocket和requests采集数据,使用pyquery解析html页面,colorama输出彩色文本。
功能:
自动采集火币和zb同种货币,实时采集usdt和qc的otc法币买卖价格,支持过滤异常价格otc订单。
将数字货币价格换算成法币买卖价格后计算差价,差价超过1%时候,箭头提示搬砖方向。
可以自行配置监控的货币种类,黄色突出显示,达到指定差价时声音报警。
例如:
alert = { 'hsr': {'hb': {'enable': true, 'profit': 2}, 'zb': {'enable': true, 'profit': 2}}, 'eth': {'hb': {'enable': true, 'profit': 2}, 'zb': {'enable': true, 'profit': 2}}, }
可增加多行,按格式复制,enable设置true表示报警,设置false表示不报警。profit指定报警的差价。
安装方法:
下载python 3安装
然后命令行依次执行下列指令安装依赖包
pip install websocket-client pip install requests pip install pyquery pip install colorama
最后执行
python bitcoin.py
即可运行,关闭的话,按ctrl+c。
代码如下,比较简单,就不注释了。
from websocket import create_connection from pyquery import pyquery from colorama import init, fore import requests import socket import gzip import json alert = { 'hsr': {'hb': {'enable': true, 'profit': 2}, 'zb': {'enable': true, 'profit': 2}}, 'eth': {'hb': {'enable': true, 'profit': 2}, 'zb': {'enable': true, 'profit': 2}}, } otc = {'usdt': {'sell': 0.0, 'buy': 0.0}, 'qc': {'sell': 0.0, 'buy': 0.0}} markets = dict() def run_client(): ws = create_connection("wss://www.hbg.com/-/s/pro/ws") ws.send(json.dumps({'sub': 'market.overview'})) while true: data = ws.recv() result = gzip.decompress(data) obj = json.loads(result) if 'ping' in obj: ws.send(json.dumps({'pong': obj['ping']})) refresh() output() elif 'ch' in obj: for data in obj['data']: if data['symbol'].endswith('usdt'): key = data['symbol'].replace('usdt', '') if not key in markets: markets[key] = dict() markets[key]['hb'] = {'last': data['close']} def run_forever(): while true: try: run_client() except exception as e: print(type(e), e.args) except keyboardinterrupt: break def refresh(): try: url = "http://api.zb.cn/data/v1/allticker" r = requests.get(url) obj = r.json() for key in obj: if key.endswith('qc'): currency = key.replace('qc', '') if currency == 'bcc': currency = 'bch' if currency in markets: markets[currency]['zb'] = {'last': float(obj[key]['last']), 'sell': float(obj[key]['sell']), 'buy': float(obj[key]['buy'])} except exception as e: print(type(e), e.args) def output(): get_hb_price(1) get_hb_price(2) get_zb_price(1) get_zb_price(2) beep = '' print('┌───────┬─────────────────────────────────────┬───────┬─────────────────────────────────────┐') print('│ usdt │ sell: {0:.2f} buy: {1:.2f} │ qc │ sell: {2:.3f} buy: {3:.3f} │'.format(\ otc['usdt']['sell'], otc['usdt']['buy'], otc['qc']['sell'], otc['qc']['buy'])) print('├───────┼─────────────────────────────────────┴───────┴───────────────┬─────────────────────┤') for key in markets: if 'zb' in markets[key]: hb = markets[key]['hb']['last'] zb = markets[key]['zb']['last'] profit_hb = 100 / hb / otc['usdt']['sell'] * zb * otc['qc']['buy'] - 100 profit_zb = 100 / otc['qc']['sell'] / zb * hb * otc['usdt']['buy'] - 100 if key in alert: if (alert[key]['hb']['enable'] and profit_hb > alert[key]['hb']['profit']) or (alert[key]['zb']['enable'] and profit_zb > alert[key]['zb']['profit']): beep = '\a' color = fore.yellow else: color = fore.reset if profit_hb > 1: dir = '>>' elif profit_zb > 1: dir = '<<' else: dir = ' ' str = "│{0} {1:^4} \033[0m│{0} {2:>6.2f} {3:>8} {4:>8} {5:} {6:>6.2f} {7:>8} {8:>8} \033[0m│{0} {9:>8} {10:>8} \033[0m│".format(\ color, key.upper(), profit_hb, format_price(hb * otc['usdt']['sell']), format_price(zb * otc['qc']['buy']), dir, \ profit_zb, format_price(hb * otc['usdt']['buy']), format_price(zb * otc['qc']['sell']), format_price(hb), format_price(zb)) print(str) print('└───────┴─────────────────────────────────────────────────────────────┴─────────────────────┘' + beep) def get_hb_price(trade=1): if trade == 1: tradetype = 'buy' else: tradetype = 'sell' try: url = "https://otc-api.hbg.com/v1/data/trade-market?country=37¤cy=1&paymethod=0&currpage=1&coinid=2&tradetype={0}&blocktype=general&online=1".format(tradetype) r = requests.get(url) obj = r.json() for data in obj['data']: if data['mintradelimit'] <= 20000 and data['maxtradelimit'] >= 5000: otc['usdt'][tradetype] = data['price'] break except exception as e: print(type(e), e.args) def get_zb_price(trade=1): if trade == 1: tradetype = 'buy' else: tradetype = 'sell' try: url = "https://vip.zb.cn/otc/trade/qc_cny?type={0}".format(trade) s = requests.session() s.headers['accept-language'] = 'zh-hans-cn, zh-hans; q=0.5' s.headers['user-agent'] = 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/64.0.3282.140 safari/537.36 edge/17.17134' r = s.get(url) if len(r.text): d = pyquery(r.text) tr = d('table.c2c-table')('tr:gt(0) td.price') for td in tr: l = pyquery(td).text().splitlines() price = float(l[0][:-4]) min = float(l[1].split('-')[0]) max = float(l[1].split('-')[1]) if min <= 20000 and max >= 5000: otc['qc'][tradetype] = price break except exception as e: print(type(e), e.args) def format_price(price): price = str(price)[:8] return float(price) def main(): init() socket.setdefaulttimeout(10) run_forever() if __name__ == '__main__': main()