-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
247 lines (200 loc) · 9.32 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from tkinter import *
from tkinter import messagebox
import time
import threading
import requests
from decimal import *
from utils import read_default_settings, save_default_settings
API_URL_BTC_USD = 'https://www.okex.com/api/index/v3/BTC-USD/constituents'
API_URL_ETH_USD = 'https://www.okex.com/api/index/v3/ETH-USD/constituents'
API_URL_CNY_UDST = 'https://www.okex.com/v3/c2c/otc-ticker/quotedPrice?baseCurrency=USDT"eCurrency=CNY&side=sell&amount=&standard=1&paymentMethod=bank'
global pause_forced
getcontext().prec = 8
'''Event when cliking Start Button --- for starting watch BTC price '''
def start_watch():
global pause_forced
if not pause_forced:
return
btc_usdt_min = btc_min_threshold_field.get()
if not btc_usdt_min:
messagebox.showerror("Input Error", "Please input BTC-USDT minimum threshold")
pause_forced = True
return
btc_usdt_max = btc_max_threshold_field.get()
if not btc_usdt_max:
pause_forced = True
messagebox.showerror("Input Error", "Please input BTC-USDT maximum threshold")
return
btc_usdt_sleep_time = btc_sleep_time_field.get()
eth_usdt_min = eth_min_threshold_field.get()
if not eth_usdt_min:
messagebox.showerror("Input Error", "Please input BTH-USDT minimum threshold")
pause_forced = True
return
eth_usdt_max = eth_max_threshold_field.get()
if not eth_usdt_max:
pause_forced = True
messagebox.showerror("Input Error", "Please input ETH-USDT maximum threshold")
return
eth_usdt_sleep_time = eth_sleep_time_field.get()
usdt_cny_min = sell_min_threshold_field.get()
if not usdt_cny_min:
messagebox.showerror("Input Error", "Please input USDT-CNY minimum threshold")
pause_forced = True
return
usdt_cny_max = sell_max_threshold_field.get()
if not usdt_cny_max:
pause_forced = True
messagebox.showerror("Input Error", "Please input USDT-CNY maximum threshold")
return
usdt_cny_sleep_time = sell_sleep_time_field.get()
btc_usdt_settings = {
"min": btc_usdt_min,
"max": btc_usdt_max,
"sleep_time": btc_usdt_sleep_time
}
eth_usdt_settings = {
"min": eth_usdt_min,
"max": eth_usdt_max,
"sleep_time": eth_usdt_sleep_time
}
usdt_cny_settings = {
"min": usdt_cny_min,
"max": usdt_cny_max,
"sleep_time": usdt_cny_sleep_time
}
save_default_settings("btc_usdt", btc_usdt_settings)
save_default_settings("eth_usdt", eth_usdt_settings)
save_default_settings("usdt_cny", usdt_cny_settings)
pause_forced = False
watch_btc_thread = threading.Thread(target=watch_btc_price, args=(API_URL_BTC_USD, "BTC", btc_usdt_min, btc_usdt_max, btc_usdt_sleep_time,))
watch_btc_thread.start()
watch_eth_thread = threading.Thread(target=watch_btc_price, args=(API_URL_ETH_USD, "ETH", eth_usdt_min, eth_usdt_max, eth_usdt_sleep_time,))
watch_eth_thread.start()
watch_sell_thread = threading.Thread(target=watch_sell_price, args=(usdt_cny_min, usdt_cny_max, usdt_cny_sleep_time,))
watch_sell_thread.start()
''' Event when clicking Pause Button --- for pausing watch BTC price '''
def pause_watch():
global pause_forced
pause_forced = True
def exit():
global pause_forced
pause_forced = True
win.destroy()
''' Thread for watching BTC price '''
def watch_btc_price(api_url, asset_name, min_threshold_val, max_threshold_val, sleep_time_val):
global pause_forced
print("min_threshold_val", min_threshold_val)
while(not pause_forced):
jsonresp = requests.get(api_url).json()
last_price = jsonresp['data']['last']
print(asset_name)
if Decimal(last_price) < Decimal(min_threshold_val):
messagebox.showinfo(
f"{asset_name} Price Alert", f"Current {asset_name} price is below than minimum threshold.\nCurrent {asset_name} Price: {last_price}"
)
if Decimal(last_price) > Decimal(max_threshold_val):
messagebox.showinfo(
"Price Alert", f"Current {asset_name} price is over than maximum threshold.\nCurrent {asset_name} Price: {last_price}"
)
print(f"{asset_name} price... {last_price}")
time.sleep(int(sleep_time_val))
print(f"Watching {asset_name} is finished")
def watch_sell_price(min_threshold_val, max_threshold_val, sleep_time_val):
global pause_forced
while(not pause_forced):
jsonresp = requests.get(API_URL_CNY_UDST).json()
bank_item_list = list(item['price'] for item in jsonresp['data'] if item['payment'] == 'bank')
last_price = bank_item_list[0]
if Decimal(last_price) < Decimal(min_threshold_val):
messagebox.showinfo(
"CNY Price Alert", f"Current price is below than minimum threshold.\nCurrent CNY Price: {last_price}"
)
if Decimal(last_price) > Decimal(max_threshold_val):
messagebox.showinfo(
"CNY Price Alert", f"Current price is over than maximum threshold.\nCurrent CNY Price: {last_price}"
)
print(f"CNY price... {last_price}")
time.sleep(int(sleep_time_val))
print("Watching CNY is finished")
## Read current setings
btc_usdt_settings = read_default_settings("btc_usdt")
eth_usdt_settings = read_default_settings("eth_usdt")
usdt_cny_settings = read_default_settings("usdt_cny")
win = Tk()
win.wm_attributes('-topmost', 1)
win.title("OKEX Price Monitor")
win.geometry("280x500")
win.configure(background='Orange1')
pause_forced = True
BTC_FRAME_ROW = 0
ETH_FRAME_ROW = 1
SELL_FRAME_ROW = 2
ACTION_FRAME_ROW = 3
#Bitcoin frame
btc_frame = LabelFrame(win, text='BTC-USDT')
btc_frame.configure(background='LightBlue2')
btc_frame.grid(row=BTC_FRAME_ROW, column=0, sticky=NSEW, padx=8, pady=8)
#Min Threshold
btc_min_threshold = Label(btc_frame, text='Min Threshold: ').grid(row=0, column=0)
btc_min_threshold_field = Entry(btc_frame, textvariable=btc_min_threshold)
btc_min_threshold_field.insert(END, btc_usdt_settings["min"])
btc_min_threshold_field.grid(row=0, column=1)
#Max Threshold
btc_max_threshold = Label(btc_frame, text='Max Threshold:').grid(row=1, column=0, pady=2)
btc_max_threshold_field = Entry(btc_frame, textvariable=btc_max_threshold)
btc_max_threshold_field.insert(END, btc_usdt_settings["max"])
btc_max_threshold_field.grid(row=1, column=1, padx=8, pady=8)
#API sleeping time
btc_sleep_time = Label(btc_frame, text='Sleep Time(seconds):').grid(row=2, column=0, pady=2)
btc_sleep_time_field = Entry(btc_frame, textvariable=btc_sleep_time)
btc_sleep_time_field.insert(END, btc_usdt_settings["sleep_time"])
btc_sleep_time_field.grid(row=2, column=1, padx=8, pady=8)
#ETH frame
eth_frame = LabelFrame(win, text='ETH-USDT')
eth_frame.configure(background='LightBlue2')
eth_frame.grid(row=1, column=0, sticky=NSEW, padx=8, pady=8)
#Min Threshold
eth_min_threshold = Label(eth_frame, text='Min Threshold: ').grid(row=0, column=0)
eth_min_threshold_field = Entry(eth_frame, textvariable=eth_min_threshold)
eth_min_threshold_field.insert(END, eth_usdt_settings["min"])
eth_min_threshold_field.grid(row=0, column=1)
#Max Threshold
eth_max_threshold = Label(eth_frame, text='Max Threshold:').grid(row=1, column=0, pady=2)
eth_max_threshold_field = Entry(eth_frame, textvariable=eth_max_threshold)
eth_max_threshold_field.insert(END, eth_usdt_settings["max"])
eth_max_threshold_field.grid(row=1, column=1, padx=8, pady=8)
#API sleeping time
eth_sleep_time = Label(eth_frame, text='Sleep Time(seconds):').grid(row=2, column=0, pady=2)
eth_sleep_time_field = Entry(eth_frame, textvariable=eth_sleep_time)
eth_sleep_time_field.insert(END, eth_usdt_settings["sleep_time"])
eth_sleep_time_field.grid(row=2, column=1, padx=8, pady=8)
# Sell frame
sell_frame = LabelFrame(win, text='USDT-CNY')
sell_frame.configure(background='LightBlue2')
sell_frame.grid(row=SELL_FRAME_ROW, column=0, sticky=NSEW, padx=8, pady=8)
## Min Threshold
sell_min_threshold = Label(sell_frame, text='Min Threshold: ').grid(row=0, column=0)
sell_min_threshold_field = Entry(sell_frame, textvariable=sell_min_threshold)
sell_min_threshold_field.insert(END, usdt_cny_settings["min"])
sell_min_threshold_field.grid(row=0, column=1)
## Max Threshold
sell_max_threshold = Label(sell_frame, text='Max Threshold:').grid(row=1, column=0, pady=2)
sell_max_threshold_field = Entry(sell_frame, textvariable=sell_max_threshold)
sell_max_threshold_field.insert(END, usdt_cny_settings["max"])
sell_max_threshold_field.grid(row=1, column=1, padx=8, pady=8)
## API sleeping time
sell_sleep_time = Label(sell_frame, text='Sleep Time(seconds):').grid(row=2, column=0, pady=2)
sell_sleep_time_field = Entry(sell_frame, textvariable=sell_sleep_time)
sell_sleep_time_field.insert(END, usdt_cny_settings["sleep_time"])
sell_sleep_time_field.grid(row=2, column=1, padx=8, pady=8)
action_frame = LabelFrame(win, text='Action panel')
action_frame.configure(background='LightBlue2')
action_frame.grid(row=ACTION_FRAME_ROW, column=0, sticky=NSEW, padx=8, pady=4)
btn_start = Button(action_frame, text="Start", width=12, command=start_watch)
btn_start.grid(row=3, column=0, padx=8, pady=4)
btn_pause = Button(action_frame, text="Pause", width=12, command=pause_watch)
btn_pause.grid(row=4, column=0, padx=8, pady=4)
btn_exit=Button(action_frame, text="Exit ", width=12, command=exit)
btn_exit.grid(row=5, column=0)
win.mainloop()