-
Notifications
You must be signed in to change notification settings - Fork 0
/
niexer.py
415 lines (412 loc) · 14.8 KB
/
niexer.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from binance.client import Client
import psycopg2
import json
import pgpasslib
import time
import random
from decimal import Decimal
from tendo import singleton
from datetime import datetime, timedelta
me = singleton.SingleInstance()
passw = pgpasslib.getpass('wytspace.cufjkhoqk6sk.us-east-1.rds.amazonaws.com',
5432,
'niexbot',
'highlander')
conn = psycopg2.connect(
host='wytspace.cufjkhoqk6sk.us-east-1.rds.amazonaws.com',
user='highlander',
dbname='niexbot',
password=passw,
port=5432,
connect_timeout=500)
symbols = [
'TRXBTC',
'XMRBTC',
'XRPBTC',
'ETHBTC',
'ETCBTC',
'XLMBTC',
'LTCBTC',
'RVNBTC'
]
client = Client('xxkeyxx','xxsecretxx')
data_dict=[]
cur = conn.cursor()
insert_cmd = ""
save_balances = False
def open_orders(symbol):
order_count = 0
for order in client.get_open_orders(symbol=symbol):
order_count+=1
return order_count
btc_cmd = """
WITH price_value AS (
SELECT price, symbol, tick_time
FROM binance_ticker_2019
WHERE symbol = 'BTCUSDT'
ORDER BY tick_time DESC
LIMIT 1)
SELECT ROUND(((AVG(t.price)-MAX(l.price))/MAX(l.price))*100,8) btc_price_diff,
CASE WHEN max(l.tick_time) > now() - '10 minutes'::interval THEN True
ELSE False
END btc_fresh,
MAX(l.price) btc_price,
ROUND(AVG(t.price),8) btc_3_hour_average,
now()
FROM binance_ticker_2019 t
JOIN price_value l ON t.symbol = l.symbol
WHERE t.tick_time > now() - '3 Hours'::interval
AND t.symbol = 'BTCUSDT';
"""
cur.execute(btc_cmd)
prev_btc = cur.fetchone()
conn.commit()
print(prev_btc)
price_btc_diff = prev_btc[0]
btc_fresh = prev_btc[1]
btc_price = prev_btc[2]
btc_three_hour_average = prev_btc[3]
nowtime = prev_btc[4]
print('The time is: %(t)s' % {"t": nowtime})
print('btc_fresh: %(l)s' % {"l": btc_fresh})
print('btc PRICE: %(p)s' % {"p": btc_price})
print('btc 3 HOUR: %(p)s' % {"p": btc_three_hour_average})
for symbol in symbols:
print('------------')
print(symbol)
print('------------')
print(datetime.now())
fresh = False
select_cmd = """
WITH price_value AS (
SELECT price, symbol, tick_time
FROM binance_ticker_2019
WHERE symbol = '%(c)s'
ORDER BY tick_time DESC
LIMIT 1)
SELECT ROUND(((AVG(t.price)-MAX(l.price))/MAX(l.price))*100,8) price_diff,
CASE WHEN max(l.tick_time) > now() - '10 minutes'::interval THEN True
ELSE False
END fresh,
MAX(l.price) "price",
ROUND(AVG(t.price),8) "3_hour_average",
now()
FROM binance_ticker_2019 t
JOIN price_value l ON t.symbol = l.symbol
WHERE t.tick_time > now() - '3 Hours'::interval
AND t.symbol = '%(c)s'
""" % {"c": symbol}
cur.execute(select_cmd)
prev_tick = cur.fetchone()
price_diff = prev_tick[0]
fresh = prev_tick[1]
price = Decimal(prev_tick[2])
three_hour_average = prev_tick[3]
conn.commit()
threeday_cmd = """
WITH price_value AS (
SELECT price, symbol, tick_time
FROM binance_ticker_2019
WHERE symbol = '%(c)s'
ORDER BY tick_time DESC
LIMIT 1)
SELECT ROUND(((AVG(t.price)-MAX(l.price))/MAX(l.price))*100,8) price_diff,
ROUND(AVG(t.price),8) btc_3_hour_average
FROM binance_ticker_2019 t
JOIN price_value l ON t.symbol = l.symbol
WHERE t.tick_time > now() - '1 day'::interval
AND t.symbol = '%(c)s'
""" % {"c": symbol}
cur.execute(threeday_cmd)
prevthree_tick = cur.fetchone()
three_diff = prevthree_tick[0]
three_price = prevthree_tick[1]
conn.commit()
usdt_symbol = '%(s)sUSDT' % {"s": symbol[:3]}
usdt_cmd = """
SELECT price, symbol, tick_time
FROM binance_ticker_2019
WHERE symbol = '%(s)s'
ORDER BY tick_time DESC
LIMIT 1
""" % {"s": usdt_symbol}
cur.execute(usdt_cmd)
prev_usdt = cur.fetchone()
conn.commit()
usdt_price = 0.0
if prev_usdt is not None:
usdt_price = prev_usdt[0]
conn.commit()
print('fresh: %(l)s' % {"l": fresh})
print('price: %(l)s' % {"l": price})
print('usdt_price: %(l)s' % {"l": usdt_price})
print('3 hour average: %(l)s' % {"l": three_hour_average})
print('3 day average: %(l)s' % {"l": three_price})
print('price_diff: %(l)s' % {"l": price_diff})
print('three_diff: %(p)s' % {"p": three_diff})
print('price_btc_diff: %(l)s' % {"l": price_btc_diff})
print('three_diff > 0.025.... %(t)s > .025' % {"t": three_diff})
if three_diff > 0.025:
print('PASSED')
btc_balance_cmd = """
SELECT free
FROM balances_binance
WHERE asset = 'BTC'
AND tick_time = (SELECT max(tick_time) FROM balances_binance WHERE asset = 'BTC');"""
cur.execute(btc_balance_cmd)
btc_balance = cur.fetchone()[0]
conn.commit()
btc_to_spend = Decimal(.7)
btc_perc_per_trade = Decimal(.025)
print('BTC Current Holdings: %(b)s' % {"b": btc_balance})
print('BTC To Spend on buy: %(b)s' % {"b": btc_to_spend})
print('BTC Percent per Trade: %(b)s' % {"b": btc_perc_per_trade})
if fresh and btc_fresh:
if symbol in ('XMRBTC','BCCBTC', 'REPBTC','ETHBTC'):
amount = round((btc_to_spend*btc_perc_per_trade)/price,3)
elif symbol.strip() in ('XLMBTC','XRPBTC','ETCBTC','LTCBTC', 'TRXBTC','RVNBTC'):
amount = round((btc_to_spend*btc_perc_per_trade)/price,0)
elif symbol in ('XMRBTC'):
amount = round((btc_to_spend*btc_perc_per_trade)/price,2)
else:
amount = round((btc_to_spend*btc_perc_per_trade)/price,4)
print('amount: %(l)s' % {"l": amount})
hours_since_price_buy_cmd = """
SELECT EXTRACT(EPOCH FROM current_timestamp-max(created))/3600
FROM set_trades_binance
WHERE currencypair = '%(c)s'
AND not closed
AND not cancelled;
""" % {"c": symbol}
cur.execute(hours_since_price_buy_cmd)
hours_since_price_buy = cur.fetchone()[0]
conn.commit()
if hours_since_price_buy is None:
hours_since_price_buy = 33
current_holdings_cmd = """
SELECT ROUND(t.price*free,8) btc_value
from balances_binance b
join (SELECT * FROM binance_ticker_2019 WHERE symbol = '%(c)s' ORDER BY tick_time DESC LIMIT 1) t on symbol = asset || 'BTC'
where asset = '%(d)s'
AND b.tick_time = (SELECT MAX(tick_time) FROM balances_binance WHERE asset = '%(d)s');
""" % {"c": symbol, "d": symbol[:-3]}
cur.execute(current_holdings_cmd)
current_holdings_query = cur.fetchone()
current_holdings = 0.0
conn.commit()
if current_holdings_query is not None:
current_holdings = current_holdings_query[0]
print('current holdings: %(c)s BTC' % {"c": round(current_holdings,5)})
set_trades_binance_count_cmd = """
SELECT COUNT(*), min(rate)
FROM set_trades_binance
WHERE currencypair = '%(c)s'
AND not closed
AND not cancelled;
""" % {"c": symbol}
cur.execute(set_trades_binance_count_cmd)
set_trades_binance_count = cur.fetchone()
conn.commit()
trade_count = 0
min_buy = price
if set_trades_binance_count[1] is not None:
trade_count = set_trades_binance_count[0]
min_buy = Decimal(set_trades_binance_count[1])*Decimal(0.92)
print('trade_count: %(l)s' % {"l": trade_count})
buy_limit = price
print('buy_limit %(b)s' % {"b": buy_limit})
open_order_count = 0
open_order_count = open_orders(symbol)
all_rules_passed = False
print('open_order_count: %(l)s' % {"l": open_order_count})
print('btc_balance: %(b)s' % {"b": btc_balance})
print('price under buy_limit %(p)s <= %(b)s...' % {"p": price, "b": buy_limit})
if price <= buy_limit:
print('PASSED')
print('1 hours since buy %(h)s....' % {"h": hours_since_price_buy})
if hours_since_price_buy > 1:
print('PASSED')
print('price <= min_buy %(p)s < %(m)s' % {"p": price, "m": min_buy})
if price <= min_buy:
print('PASSED')
print('open_order_count < 1 and trade_count < 5: %(o)s, %(t)s...' % {"o": open_order_count, "t": trade_count})
if open_order_count < 1 and trade_count < 5:
print('PASSED')
all_rules_passed = True
if all_rules_passed:
print('buy...............................')
order_number = 999999999
buy_price = round(Decimal(price),8)
try:
print('AMOUNT: %(a)s' % {"a":amount})
print('PRICE: %(a)s' % {"a":buy_price})
print('SYMBOL: %(a)s' % {"a":symbol})
buy = client.create_order(
symbol=symbol,
side=Client.SIDE_BUY,
type=Client.ORDER_TYPE_MARKET,
quantity=amount)
print(buy)
time.sleep(1)
order_number = buy["orderId"]
opened = False
final_price = buy_price
if buy["status"] == 'filled':
opened = True
final_price = buy['fills'][0]['price']
insert_cmd = """
INSERT INTO set_trades_binance (
currencypair,
rate,
amount,
btc_value,
usdt_value,
orderNumber,
opened,
trader_id)
VALUES (
'%(c)s',
%(r)s,
%(a)s,
%(b)s,
%(u)s,
%(o)s,
%(s)s,
2);
""" % {
"c": symbol,
"r": final_price,
"a": amount,
"b": btc_price,
"u": usdt_price,
"o": order_number,
"s": opened
}
cur.execute(insert_cmd)
rowcount = cur.rowcount
conn.commit()
print('set_trade ROWS INSERTED: %(r)s' % {"r": rowcount})
except Exception as ex:
print(ex)
print('BUY PASSED OR FAILED')
print('CHECK FOR SELL....')
sell_count_limit = 0
trade_count = 1
set_trades_binance_cmd = """
SELECT *,
(EXTRACT(epoch from age(now(),created)))::int
FROM set_trades_binance
WHERE currencypair = '%(c)s'
AND not closed
AND not cancelled
and trader_id = 2
ORDER BY id;
""" % {"c": symbol}
cur.execute(set_trades_binance_cmd)
set_trades_binance = cur.fetchall()
conn.commit()
set_trade_counter = 0
for set_trade in set_trades_binance:
set_trade_counter += 1
print(set_trade)
trade_count+=1
differences_cmd = """
SELECT (%(b)s - %(lb)s)/%(lb)s btc_difference,
(%(r)s - %(l)s)/%(l)s trade_difference;
""" % {
"b": set_trade[4],
"lb": btc_price,
"r": set_trade[2],
"l": price}
cur.execute(differences_cmd)
differences = cur.fetchone()
conn.commit()
set_trade_price = Decimal(set_trade[2])
print('set_trade_price: %(s)s' % {"s": set_trade_price})
conn.commit()
created_time_diff = set_trade[17]
print('created_time_diff %(c)s' % {"c": created_time_diff})
set_trade_price_ten = set_trade_price + (set_trade_price * Decimal(.05))
if created_time_diff < 7200:
set_trade_price_ten = set_trade_price + (set_trade_price * Decimal(.01))
elif created_time_diff < 14400:
set_trade_price_ten = set_trade_price + (set_trade_price * Decimal(.014))
elif created_time_diff < 25200:
set_trade_price_ten = set_trade_price + (set_trade_price * Decimal(.02))
elif created_time_diff < 50400:
set_trade_price_ten = set_trade_price + (set_trade_price * Decimal(.03))
trade_limit = set_trade_price_ten
print('set_trade_price_ten: %(s)s' % {"s": set_trade_price_ten})
print('trade_limit: %(s)s' % {"s": trade_limit})
print('usdt_price: %(s)s' % {"s": usdt_price})
print('set_trade[14]: %(s)s' % {"s": Decimal(set_trade[14])})
if Decimal(set_trade[14]) > .00000001:
print('(usdt_price-set_trade[14])/set_trade[14] %(p)s' % {"p": (Decimal(usdt_price)-Decimal(set_trade[14]))/Decimal(set_trade[14])})
sell_amount = round(Decimal(set_trade[3]),3)
print('sell_amount: %(s)s' % {"s": sell_amount})
print('sell_rate: %(r)s' % {"r": price})
if symbol.strip() in ('XRPBTC','TRXBTC'):
sell_amount = round(sell_amount,0)
elif symbol.strip() == 'LTCBTC':
sell_amount = round(sell_amount,2)
else:
sell_amount = round(sell_amount,1)
print('new sell_amount: %(s)s' % {"s": sell_amount})
if (trade_limit < price and sell_count_limit < 1):
print('possible sell')
print('sell..........................')
try:
sell = client.create_order(
symbol=symbol,
side=Client.SIDE_SELL,
type=Client.ORDER_TYPE_MARKET,
quantity=sell_amount)
print(sell)
time.sleep(1)
close_trade_cmd = """
UPDATE set_trades_binance
SET closed = True,
btc_close = %(b)s,
close = '%(c)s',
c_date=now()
WHERE id = %(o)s
""" % {"o": set_trade[0], "b": btc_price, "c": price}
cur.execute(close_trade_cmd)
rowcount = cur.rowcount
conn.commit()
print('set_trade ROWS UPDATED: %(r)s' % {"r": rowcount})
sell_count_limit += 1
except Exception as ex:
print(ex)
elif Decimal(set_trade[14]) > .00000001:
if (Decimal(usdt_price)-Decimal(set_trade[14]))/Decimal(set_trade[14]) > .0565:
print('SELL TO USDT')
try:
sell = client.create_order(
symbol=usdt_symbol,
side=Client.SIDE_SELL,
type=Client.ORDER_TYPE_MARKET,
quantity=sell_amount)
print(sell)
time.sleep(1)
close_trade_cmd = """
UPDATE set_trades_binance
SET closed = True,
usdt_close = %(u)s,
btc_close = %(b)s,
close = '%(c)s',
c_date=now()
WHERE id = %(o)s
""" % {"o": set_trade[0], "u": usdt_price, "b": btc_price, "c": price}
cur.execute(close_trade_cmd)
rowcount = cur.rowcount
conn.commit()
print('set_trade ROWS UPDATED: %(r)s' % {"r": rowcount})
sell_count_limit += 1
except Exception as ex:
print(ex)
else:
if set_trade_price is not None:
print('Not suitable for sell: %(r)s > %(l)s or sell_count_limit' % {
"r": trade_limit,
"l": price})