-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwatch2.py
95 lines (85 loc) · 2.94 KB
/
watch2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import ccxt.pro
import asyncio
import json
from datetime import datetime
import time
import random
import requests
import httpx
exchange = ccxt.binance()
exchange.load_markets()
symbols = []
def init():
for symbol in exchange.markets:
if "/USDT" in symbol:
try:
if len(symbols) > 500:
break
order_book = exchange.fetch_order_book(symbol)
order_book["bids"][0][0]
symbols.append(symbol)
print(symbol)
except Exception as e:
#print(e)
pass
init()
print("监控", len(symbols), "个币", symbols)
secret = json.load(open('secret.json'))
async def sendToWechat(text):
try:
params = {
"corpid": secret["weixin"]['corpid'],
"corpsecret": secret["weixin"]['corpsecret']
}
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
async with httpx.AsyncClient() as client:
r = await client.get(url, params = params)
access_token = r.json()["access_token"]
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
params = {
"access_token": access_token
}
data = {
"touser": "@all",
"msgtype" : "text",
"agentid" : secret["weixin"]['agentid'],
"text" : {
"content" : text
}
}
async with httpx.AsyncClient() as client:
r = await client.post(url, params = params, json = data)
except Exception as e:
print(e)
pass
async def watch_loop(exchange, symbol, n):
await asyncio.sleep(random.randint(1, 60))
timeframe = '3m'
limit = 1
diff = 0.05
try:
ohlcv = await exchange.fetch_ohlcv(symbol, timeframe, None, limit)
#print(ohlcv)
open = float(ohlcv[0][1])
close = float(ohlcv[0][4])
cur = (close - open) / open
if abs(cur) >= diff:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), '{0:>8}'.format(str(symbols.index(symbol)) + "/" + str(len(symbols))) + " " + '{0:>15}'.format(symbol), "价格波动超过", "{0:>8.2%}".format(cur))
#微信通知
text = "品种:" + symbol + "\n"
text += "饺易所:" + str(exchange) + "\n"
text += "k线周期:" + timeframe + "\n"
text += "价格:" + str(close) + "\n"
text += "波动:" + format(cur * 100, "+.2f") + "%" + "\n"
text += "时间:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
await sendToWechat(text)
except Exception as e:
print(type(e).__name__, str(e), symbol)
pass
async def main(exchange, symbols):
print(symbols)
while True:
await asyncio.gather(*[watch_loop(exchange, symbol, n) for n, symbol in enumerate(symbols)])
await asyncio.sleep(1)
exchange = ccxt.pro.binance()
asyncio.run(main(exchange, symbols))