1.首先声明有要求数量不能超过1024个;
2.本代码是链接了数据库的,可以从数据库中获取不同种类的币种数据
import json
import websocket
import pymysql
conn = pymysql.connect(host='127.0.0.1',user='root',password='111111',database='bian',charset='utf8',port=3306)
cursor = conn.cursor()
cursor.execute('SELECT * FROM k_symbol')
cursor2 = conn.cursor()
cursor2.execute('SELECT * FROM bian_transaction_config where id = 8')
config = cursor2.fetchone()
print(f"涨幅配置参数当前值为: {config[-1]}.")
# streams = ""
# for row in cursor.fetchall():
# print(row)
# streams = streams + f"{row[1]}@kline_1m/"
# print(f"拼接后的streams: {streams[0: -1]}")
def on_open(ws):
print("open")
def on_message(ws,message):
json_message = json.loads(message)
candle = json_message['data']['k']
resp = json_message['data']
print(message)
per = float(resp['k']['c'])/float(resp['k']['o'])
print(f"当前币对: {resp['s']}, 涨幅是: {per}.")
if per > config[-1] :
sql = ""
print(f"插入数据sql: {sql}")
c.execute(sql)
conn.commit()
print("插入数据成功")
is_candle_closed = candle['x']
if is_candle_closed:
print(json.dumps(candle, indent=2))
def on_close(ws, close_status_code, close_msg):
print("closed")
SOCK = f"wss://stream.binance.com:9443/stream?streams=BTCUSDT@kline_1m/LTCUSDT@kline_1m/ETHUSDT@kline_1m/NEOUSDT@kline_1m/BNBUSDT@kline_1m/QTUMUSDT@kline_1m/EOSUSDT@kline_1m/SNTUSDT@kline_1m/BNTUSDT@kline_1m/GASUSDT@kline_1m/BCCUSDT@kline_1m/USDTUSDT@kline_1m/HSRUSDT@kline_1m/OAXUSDT@kline_1m/DNTUSDT@kline_1m/MCOUSDT@kline_1m/ICNUSDT@kline_1m/ZRXUSDT@kline_1m/OMGUSDT@kline_1m/WTCUSDT@kline_1m/YOYOUSDT@kline_1m/LRCUSDT@kline_1m/TRXUSDT@kline_1m/SNGLSUSDT@kline_1m/STRATUSDT@kline_1m/BQXUSDT@kline_1m/FUNUSDT@kline_1m/KNCUSDT@kline_1m/CDTUSDT@kline_1m/XVGUSDT@kline_1m/IOTAUSDT@kline_1m/SNMUSDT@kline_1m/LINKUSDT@kline_1m/CVCUSDT@kline_1m/TNTUSDT@kline_1m/REPUSDT@kline_1m/MDAUSDT@kline_1m/MTLUSDT@kline_1m/SALTUSDT@kline_1m/NULSUSDT@kline_1m/SUBUSDT@kline_1m/STXUSDT@kline_1m/MTHUSDT@kline_1m/ADXUSDT@kline_1m/ETCUSDT@kline_1m/ENGUSDT@kline_1m/ZECUSDT@kline_1m/ASTUSDT@kline_1m/GNTUSDT@kline_1m/DGDUSDT@kline_1m/BATUSDT@kline_1m/DASHUSDT@kline_1m/POWRUSDT@kline_1m/BTGUSDT@kline_1m/REQUSDT@kline_1m/XMRUSDT@kline_1m/EVXUSDT@kline_1m/VIBUSDT@kline_1m/ENJUSDT@kline_1m/VENUSDT@kline_1m/ARKUSDT@kline_1m/XRPUSDT@kline_1m/MODUSDT@kline_1m/STORJUSDT@kline_1m/KMDUSDT@kline_1m/RCNUSDT@kline_1m/EDOUSDT@kline_1m/DATAUSDT@kline_1m/DLTUSDT@kline_1m/MANAUSDT@kline_1m/PPTUSDT@kline_1m/RDNUSDT@kline_1m/GXSUSDT@kline_1m/AMBUSDT@kline_1m/ARNUSDT@kline_1m/BCPTUSDT@kline_1m/CNDUSDT@kline_1m/GVTUSDT@kline_1m/POEUSDT@kline_1m/BTSUSDT@kline_1m/FUELUSDT@kline_1m/XZCUSDT@kline_1m/QSPUSDT@kline_1m/LSKUSDT@kline_1m/BCDUSDT@kline_1m/TNBUSDT@kline_1m/ADAUSDT@kline_1m/LENDUSDT@kline_1m/XLMUSDT@kline_1m/CMTUSDT@kline_1m/WAVESUSDT@kline_1m/WABIUSDT@kline_1m/GTOUSDT@kline_1m/ICXUSDT@kline_1m/OSTUSDT@kline_1m/ELFUSDT@kline_1m/AIONUSDT@kline_1m/WINGSUSDT@kline_1m/BRDUSDT@kline_1m/NEBLUSDT@kline_1m/NAVUSDT@kline_1m/VIBEUSDT@kline_1m/LUNUSDT@kline_1m/TRIGUSDT@kline_1m/APPCUSDT@kline_1m/CHATUSDT@kline_1m/RLCUSDT@kline_1m/INSUSDT@kline_1m/PIVXUSDT@kline_1m/IOSTUSDT@kline_1m/STEEMUSDT@kline_1m/NANOUSDT@kline_1m/AEUSDT@kline_1m/VIAUSDT@kline_1m/BLZUSDT@kline_1m/SYSUSDT@kline_1m/RPXUSDT@kline_1m/NCASHUSDT@kline_1m/POAUSDT@kline_1m/ONTUSDT@kline_1m/ZILUSDT@kline_1m/STORMUSDT@kline_1m/XEMUSDT@kline_1m/WANUSDT@kline_1m/WPRUSDT@kline_1m/QLCUSDT@kline_1m/GRSUSDT@kline_1m/CLOAKUSDT@kline_1m/LOOMUSDT@kline_1m/BCNUSDT@kline_1m/TUSDUSDT@kline_1m/ZENUSDT@kline_1m/SKYUSDT@kline_1m/THETAUSDT@kline_1m/IOTXUSDT@kline_1m/QKCUSDT@kline_1m/AGIUSDT@kline_1m/NXSUSDT@kline_1m/SCUSDT@kline_1m/NPXSUSDT@kline_1m/KEYUSDT@kline_1m/NASUSDT@kline_1m/MFTUSDT@kline_1m/DENTUSDT@kline_1m/IQUSDT@kline_1m/ARDRUSDT@kline_1m/HOTUSDT@kline_1m/VETUSDT@kline_1m/DOCKUSDT@kline_1m/POLYUSDT@kline_1m/VTHOUSDT@kline_1m/ONGUSDT@kline_1m/PHXUSDT@kline_1m/HCUSDT@kline_1m/GOUSDT@kline_1m/PAXUSDT@kline_1m/RVNUSDT@kline_1m/DCRUSDT@kline_1m/USDCUSDT@kline_1m/MITHUSDT@kline_1m/BCHABCUSDT@kline_1m/BCHSVUSDT@kline_1m/RENUSDT@kline_1m/BTTUSDT@kline_1m/USDSUSDT@kline_1m/FETUSDT@kline_1m/TFUELUSDT@kline_1m/CELRUSDT@kline_1m/MATICUSDT@kline_1m/ATOMUSDT@kline_1m/PHBUSDT@kline_1m/ONEUSDT@kline_1m/FTMUSDT@kline_1m/BTCBUSDT@kline_1m/USDSBUSDT@kline_1m/CHZUSDT@kline_1m/COSUSDT@kline_1m/ALGOUSDT@kline_1m/ERDUSDT@kline_1m/DOGEUSDT@kline_1m/BGBPUSDT@kline_1m/DUSKUSDT@kline_1m/ANKRUSDT@kline_1m/WINUSDT@kline_1m/TUSDBUSDT@kline_1m/COCOSUSDT@kline_1m/PERLUSDT@kline_1m/TOMOUSDT@kline_1m/BUSDUSDT@kline_1m/BANDUSDT@kline_1m/BEAMUSDT@kline_1m/HBARUSDT@kline_1m/XTZUSDT@kline_1m/NGNUSDT@kline_1m/DGBUSDT@kline_1m/NKNUSDT@kline_1m/GBPUSDT@kline_1m/EURUSDT@kline_1m/KAVAUSDT@kline_1m/RUBUSDT@kline_1m/UAHUSDT@kline_1m/ARPAUSDT@kline_1m/TRYUSDT@kline_1m/CTXCUSDT@kline_1m/AERGOUSDT@kline_1m/BCHUSDT@kline_1m/TROYUSDT@kline_1m/ARSUSDT@kline_1m"
ws = websocket.WebSocketApp(SOCK, on_open=on_open,on_close=on_close, on_message=on_message)
ws.run_forever()