-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcrypto.py
70 lines (60 loc) · 2.15 KB
/
crypto.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
import os
from time import time
from binance.client import Client
from binance.exceptions import BinanceAPIException
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("BINANCE_API_KEY")
SECRET_KEY = os.getenv("BINANCE_SECRET_KEY")
client = Client(API_KEY, SECRET_KEY)
def generate_watchpair_msg(watchpair):
ticker = client.get_ticker(symbol=watchpair[1])
last_price = float(ticker['lastPrice'])
change = float(ticker['priceChangePercent'])
if last_price > watchpair[3]:
arrow = '↑'
elif last_price < watchpair[3]:
arrow = '↓'
else:
arrow = '→'
if last_price > 1:
formatted_price = f'{"%.2f" % last_price}'
else:
formatted_price = f'{"%.8f" % last_price}'
return f'{arrow} {watchpair[1]} {formatted_price} ({"%+.1f" % change}%)', last_price
def create_watchpair(chat_id, args):
if len(args) != 2:
return False, 'Usage:\n/watch <pair> <interval>'
refresh = int(args[1])
if not 1 <= refresh <= 1440:
return False, 'Invalid interval.'
pair = args[0]
try:
price = client.get_ticker(symbol=pair)['lastPrice']
except BinanceAPIException:
return False, 'Invalid pair.'
return [[chat_id, pair, refresh, float(price), int(time())], None]
def create_watchtrade(chat_id, args):
if len(args) != 3:
return False, 'Usage:\n/wtrade <pair> <interval> <enterPrice>'
try:
refresh = int(args[1])
if not 1 <= refresh <= 1440:
raise ValueError
except ValueError:
return False, 'Invalid interval.'
pair = args[0]
try:
price = client.get_ticker(symbol=pair)['lastPrice']
except BinanceAPIException:
return False, 'Invalid pair.'
try:
enterprice = float(args[2])
if enterprice < 0:
raise ValueError
except ValueError:
return False, 'Invalid enterPrice.'
return [[chat_id, pair, refresh, float(price), int(time()), enterprice], None]
def generate_tradeposition_msg(enterprice, actualprice):
change = actualprice / enterprice
return f'[{"%.2f" % change}]'