· 7 years ago · Nov 01, 2018, 10:24 PM
1import json
2import requests
3import urllib
4import win32api
5import time
6import sys
7import os
8from binance.client import Client
9
10
11telegram_api_key = "TELEGRAM_API"
12telegram_base_url = "https://api.telegram.org/bot{}/".format(telegram_api_key)
13
14binance_api_key = "API_KEY"
15binance_api_secret = "SECRET_KEY"
16
17markets_binance_list = ["btcusdt",
18 "ethbtc",
19 "astbtc",
20 "xrpbtc",
21 "wprbtc",
22 "rvnbtc",
23 "gvtbtc",
24 "dentbtc",
25 "keybtc",
26 "phxbtc",
27 "dltbtc",
28 "bcptbtc",
29 "bnbbtc",
30 "tusdbtc",
31 "eosbtc",
32 "batbtc",
33 "wtcbtc",
34 "npxsbtc",
35 "ncashbtc",
36 "stormbtc",
37 "arnbtc",
38 "wanbtc",
39 "bccbtc",
40 "zrxbtc",
41 "dockbtc",
42 "polybtc",
43 "qkcbtc",
44 "paxbtc",
45 "tntbtc",
46 "vetbtc",
47 "ltcbtc",
48 "iotxbtc",
49 "mdabtc",
50 "trxbtc",
51 "adabtc",
52 "mthbtc",
53 "neobtc",
54 "mftbtc",
55 "iotabtc",
56 "scbtc",
57 "dntbtc",
58 "iostbtc",
59 "linkbtc",
60 "icxbtc",
61 "blzbtc",
62 "wabibtc",
63 "xlmbtc",
64 "qspbtc",
65 "neblbtc",
66 "dgdbtc",
67 "gobtc",
68 "xmrbtc",
69 "poebtc",
70 "funbtc",
71 "etcbtc",
72 "zilbtc",
73 "fuelbtc",
74 "appcbtc",
75 "reqbtc",
76 "xvgbtc",
77 "mtlbtc",
78 "nanobtc",
79 "modbtc",
80 "ontbtc",
81 "yoyobtc",
82 "vibebtc",
83 "dashbtc",
84 "subbtc",
85 "lendbtc",
86 "snmbtc",
87 "cdtbtc",
88 "elfbtc",
89 "gtobtc",
90 "oaxbtc",
91 "hcbtc",
92 "qtumbtc",
93 "aebtc",
94 "vibbtc",
95 "zecbtc",
96 "ostbtc",
97 "aionbtc",
98 "adxbtc",
99 "databtc",
100 "rcnbtc",
101 "xembtc",
102 "btgbtc",
103 "insbtc",
104 "hotbtc",
105 "arkbtc",
106 "poabtc",
107 "lunbtc",
108 "bqxbtc",
109 "navbtc",
110 "gntbtc",
111 "nasbtc",
112 "cvcbtc",
113 "tnbbtc",
114 "lskbtc",
115 "manabtc",
116 "nulsbtc",
117 "skybtc",
118 "omgbtc",
119 "snglsbtc",
120 "pptbtc",
121 "saltbtc",
122 "steembtc",
123 "mcobtc",
124 "dcrbtc",
125 "bcdbtc",
126 "thetabtc",
127 "glcbtc",
128 "stratbtc",
129 "ambbtc",
130 "enjbtc",
131 "cndbtc",
132 "sntbtc",
133 "loombtc",
134 "wingsbtc",
135 "kncbtc",
136 "cmtbtc",
137 "zenbtc",
138 "repbtc",
139 "sysbtc",
140 "kmdbtc",
141 "agibtc",
142 "storjbtc",
143 "pivxbtc",
144 "brdbtc",
145 "rdnbtc",
146 "evxbtc",
147 "gasbtc",
148 "btsbtc",
149 "wavesbtc",
150 "edobtc",
151 "engbtc",
152 "grsbtc",
153 "nxsbtc",
154 "lrcbtc",
155 "viabtc",
156 "powrbtc",
157 "gxsbtc",
158 "xzcbtc",
159 "ardrbtc",
160 "rlcbtc",
161 "cloakbtc",
162 "bntbtc",
163 "trigbtc",
164 "icnbtc",
165 "chatbtc"]
166markets_precision_3 = ["ETHBTC",
167 "BCCBTC",
168 "DASHBTC",
169 "XMRBTC",
170 "DLTBTC",
171 "AMBBTC",
172 "ZECBTC",
173 "BCDBTC",
174 "DGDBTC",
175 "REPBTC",
176 "ZENBTC",
177 "DCRBTC"
178 ]
179markets_precision_2 = ["LTCBTC",
180 "NEOBTC",
181 "GASBTC",
182 "MCOBTC",
183 "QTUMBTC",
184 "OMGBTC",
185 "STRATBTC",
186 "SALTBTC",
187 "ETCBTC",
188 "BTGBTC",
189 "ARKBTC",
190 "GVTBTC",
191 "GXSBTC",
192 "XZCBTC",
193 "LSKBTC",
194 "PPTBTC",
195 "WAVESBTC",
196 "ICXBTC",
197 "AIONBTC",
198 "NEBLBTC",
199 "EDOBTC",
200 "NAVBTC",
201 "LUNBTC",
202 "TRIGBTC",
203 "RLCBTC",
204 "INSBTC",
205 "PIVXBTC",
206 "STEEMBTC",
207 "NANOBTC",
208 "VIABTC",
209 "AEBTC",
210 "CLOAKBTC",
211 "NXSBTC",
212 "NASBTC"
213 ]
214
215client = Client(binance_api_key, binance_api_secret)
216
217budget_per_order = 10 # dollar
218btc_budget_per_order = budget_per_order / float(client.get_symbol_ticker(symbol="BTCUSDT")["price"])
219
220
221def set_local_time_to_binance_time():
222 gt = client.get_server_time()
223 tt = time.gmtime(int((gt["serverTime"]) / 1000))
224 win32api.SetSystemTime(tt[0], tt[1], 0, tt[2], tt[3], tt[4], tt[5], 0)
225
226
227def get_json_from_url(url):
228 response = requests.get(url)
229 content = response.content.decode("utf8")
230 return json.loads(content)
231
232
233def get_update(offset):
234 url = telegram_base_url + "getUpdates"
235 if offset:
236 url += "?offset={}".format(offset)
237 return get_json_from_url(url)
238
239
240def get_last_update_id(updates):
241 update_ids = []
242 for update in updates["result"]:
243 update_ids.append(int(update["update_id"]))
244 return max(update_ids)
245
246
247def send_message(text, chat_id):
248 text_parsed = urllib.parse.quote_plus(text)
249 url = telegram_base_url + "sendMessage?text={}&chat_id={}".format(text_parsed, chat_id)
250 response = requests.get(url)
251 content = response.content.decode("utf8")
252
253
254def get_last_text(updates):
255 for update in updates["result"]:
256 try:
257 return update["channel_post"]["text"]
258 except Exception:
259 last_chat_name = update['message']['chat']['first_name']
260 last_chat_id = update["message"]["chat"]["id"]
261 send_message(last_chat_name + ", stuur dit in de MoneyMakers groep!", last_chat_id)
262
263
264def analyse_signal(signal):
265 if "binance" in signal:
266
267 for market_binance in markets_binance_list:
268 if market_binance in signal:
269 market = market_binance.upper()
270
271 for i in range(len(signal)):
272 if signal[i:i + 1] == "L":
273 if signal[i + 1:i + 2] == "o":
274 if signal[i + 2:i + 3] == "s":
275 if signal[i + 3:i + 4] == "s":
276 for j in range(i + 7, len(signal)):
277 if signal[j:j + 1] == " ":
278 space_place = j
279 break
280 stop_loss = signal[i + 7:space_place]
281
282 for k in range(len(signal)):
283 if signal[k:k + 1] == " ":
284 if signal[k + 1:k + 2] == "-":
285 if signal[k + 2:k + 3] == " ":
286
287 for n in range(k + 4, len(signal)):
288 if signal[n:n + 1] == "\n":
289 space_place2 = n
290 break
291 buy_zone_temp = signal[k + 3:space_place2]
292
293 m = k
294 while m >= 0:
295 if signal[m - 1:m] == " ":
296 space_place3 = m
297 break
298 m -= 1
299 buy_zone_temp2 = signal[space_place3:k]
300
301 if buy_zone_temp < buy_zone_temp2:
302 buy_zone_min = buy_zone_temp
303 buy_zone_max = buy_zone_temp2
304 else:
305 buy_zone_min = buy_zone_temp2
306 buy_zone_max = buy_zone_temp
307
308 try:
309 order_information = {
310 "market": market,
311 "stop_loss": stop_loss,
312 "buy_zone_min": buy_zone_min,
313 "buy_zone_max": buy_zone_max}
314 except Exception:
315 send_message("Niet voldoende informatie om een order te openen!", "-1001215784762")
316 order_information = {}
317
318 return order_information
319
320
321def get_latest_price(order_information):
322 return client.get_symbol_ticker(symbol=order_information["market"])["price"]
323
324
325def get_quantity(price):
326 return btc_budget_per_order / price
327
328
329def get_precision(order_information):
330 market = order_information["market"]
331 if market in markets_precision_3:
332 return 3
333 elif market in markets_precision_2:
334 return 2
335 elif market == "NPXSBTC":
336 return 1
337 else:
338 return 0
339
340
341def buy_coin(order_information):
342 latest_price = get_latest_price(order_information)
343 quantity_calculated = get_quantity(float(latest_price))
344 account_balance_btc = client.get_asset_balance("BTC")["free"]
345 precision = get_precision(order_information)
346 precision2 = len(order_information["stop_loss"]) - 2
347
348 if float(account_balance_btc) >= float(latest_price) * quantity_calculated * 1.3:
349 if btc_budget_per_order >= 0.001:
350 if float(order_information["buy_zone_min"]) < float(latest_price) < float(order_information["buy_zone_max"]):
351 try:
352 market_buy_order = client.create_test_order(
353 symbol=order_information["market"],
354 side=Client.SIDE_BUY,
355 type=Client.ORDER_TYPE_MARKET,
356 quantity=float("{:0.0{}f}".format(quantity_calculated, precision)))
357 print(market_buy_order)
358 time.sleep(0.5)
359
360 stop_loss_order = client.create_test_order(
361 symbol=order_information["market"],
362 side=Client.SIDE_SELL,
363 type=Client.ORDER_TYPE_STOP_LOSS_LIMIT,
364 quantity=float("{:0.0{}f}".format(quantity_calculated, precision)),
365 price="{:0.0{}f}".format(float(order_information["stop_loss"]) * 0.995, precision2),
366 stopPrice=order_information["stop_loss"],
367 timeInForce=Client.TIME_IN_FORCE_GTC)
368 print(stop_loss_order)
369 send_message("Haatz a Niffo, order is verwerkt!", "-1001215784762")
370 except Exception:
371 send_message("Niet gelukt door onbekende reden, waarschijnlijk is de code fout!", "-1001215784762")
372 else:
373 send_message("Niet gelukt, prijs zit niet in de buy zone!", "-1001215784762")
374 else:
375 send_message("Niet gelukt, order value is te laag!", "-1001215784762")
376 else:
377 send_message("Niet gelukt, te weinig geld!", "-1001215784762")
378
379
380def restart():
381 print("argv was", sys.argv)
382 print("sys.executable was", sys.executable)
383 print("\nRestart now...")
384 os.execv(sys.executable, ['python'] + sys.argv)
385
386
387def main():
388 # set_local_time_to_binance_time() # when error 1314 happens.. Run this as admin.
389
390 print("Initialized!")
391 last_update_id = None
392
393 while True:
394 updates = get_update(last_update_id)
395 if len(updates["result"]) > 0:
396 last_update_id = get_last_update_id(updates) + 1
397
398 print(
399 "\n(===============================================================================================)\n")
400
401 text = get_last_text(updates)
402 order_information = analyse_signal(text)
403 print(order_information)
404
405 if len(order_information) == 4:
406 buy_coin(order_information)
407 time.sleep(0.5)
408
409
410if __name__ == "__main__":
411 main()