forked from wookpump/wookpump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
305 lines (242 loc) · 14.1 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
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
import time
from bittrex import Bittrex
import json
from decimal import *
import sys
import threading
from threading import Thread
import datetime
import traceback
import slackweb
import socket
slack = slackweb.Slack(url="https://hooks.slack.com/services/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")
with open("run_param_bittrex.json") as run_param_yobit:
param = json.load(run_param_yobit)
run_param_yobit.close()
# set in run_param_yobit.json file
# AUTO_TRADE = False # True or False ex)False = Display CoinName Only
# BUY_COIN_UNIT = 0.0002 # Total Buy bit at least 0.0001 ex)0.1 = 0.1BIT
# ACCEPT_PRICE_GAP = 0.10 # Gap of prev between curr price ex)0.1 = 10%
# IGNORE_GAP_SECONDS = 5 # accept time gap under 10 ex)10 = 10 second
# BUY_PRICE_RATE = 1.01 # Buy coin at Current price * 1.2 ex)1.2 = 120%
# SELL_PRICE_RATE = 1.02 # Sell coin at buy price(Actual) * 1.2 ex)1.2 = 120%
# CANCEL_TIME = 60 # afert CANCLE_TIME seconds, cancel all open order and sell market ex) 50 = 50 seconds
AUTO_TRADE = param['AUTO_TRADE']
BUY_COIN_UNIT = param['BUY_COIN_UNIT']
ACCEPT_PRICE_GAP = param['ACCEPT_PRICE_GAP']
IGNORE_GAP_SECONDS = param['IGNORE_GAP_SECONDS']
BUY_PRICE_RATE = param['BUY_PRICE_RATE']
SELL_PRICE_RATE = param['SELL_PRICE_RATE']
LAST_SELL_PRICE_RATE = param['LAST_SELL_PRICE_RATE']
CANCEL_TIME = param['CANCEL_TIME']
dict_price = {}
dict_price_bid = {}
dict_price_last = {}
with open("secrets_bittrex.json") as secrets_file:
secrets = json.load(secrets_file)
secrets_file.close()
bittrex = Bittrex(secrets['key'], secrets['secret'])
class ThreadGetTiker(Thread):
def __init__(self, MarketName):
self.MarketName = MarketName
threading.Thread.__init__(self)
def run(self):
while dict_price[self.MarketName][2]:
try:
current_time = datetime.datetime.now()
current_time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
ticker = bittrex.get_ticker(self.MarketName)
price = float(ticker['result']['Ask'])
price_bid = float(ticker['result']['Bid'])
price_last = float(ticker['result']['Last'])
list_priv = dict_price[self.MarketName][1]
list_curr = [current_time, price]
list_priv_bid = dict_price_bid[self.MarketName][1]
list_curr_bid = [current_time, price_bid]
list_priv_last = dict_price_last[self.MarketName][1]
list_curr_last = [current_time, price_last]
# print(self.MarketName + ' : ' + str(price))
priv_price = list_priv[1]
curr_price = list_curr[1]
priv_price_bid = list_priv_bid[1]
curr_price_bid = list_curr_bid[1]
priv_price_last = list_priv_last[1]
curr_price_last = list_curr_last[1]
# Check Time gap
priv_time = dict_price[self.MarketName][0][0]
curr_time = dict_price[self.MarketName][1][0]
gap_seconds = (curr_time - priv_time).total_seconds()
if gap_seconds < IGNORE_GAP_SECONDS:
# check price gap
gap_price_rate = (curr_price - priv_price) / priv_price
gap_price_rate_bid = (curr_price_bid - priv_price_bid) / priv_price_bid
gap_price_rate_last = (curr_price_last - priv_price_last) / priv_price_last
if gap_price_rate > ACCEPT_PRICE_GAP:
#and gap_price_rate_bid > ACCEPT_PRICE_GAP and gap_price_rate_last > ACCEPT_PRICE_GAP:
printt('#################################### ' + self.MarketName.split('-')[
1] + ' #############################')
price_ask = dict_price[self.MarketName]
price_bid = dict_price_bid[self.MarketName]
price_last = dict_price_last[self.MarketName]
value_str = 'ASK - [%s][%.8f],[%s][%.8f]' % (price_ask[0][0], price_ask[0][1], price_ask[1][0], price_ask[1][1])
value_str_bid = 'BID - [%s][%.8f],[%s][%.8f]' % (price_bid[0][0], price_bid[0][1], price_bid[1][0], price_bid[1][1])
value_str_last = 'LAST - [%s][%.8f],[%s][%.8f]' % (price_last[0][0], price_last[0][1], price_last[1][0], price_last[1][1])
printt(self.MarketName + ' : ' + value_str + ' : ' + str('%.8f' % gap_price_rate))
printt(self.MarketName + ' : ' + value_str_bid + ' : ' + str('%.8f' % gap_price_rate_bid))
printt(self.MarketName + ' : ' + value_str_last + ' : ' + str('%.8f' % gap_price_rate_last))
printt('#################################### ' + self.MarketName.split('-')[
1] + ' #############################')
# Real Trading
if AUTO_TRADE:
# close this coin
dict_price.update({self.MarketName: [list_priv, list_curr, False]})
buyResult = buyCoin(self.MarketName, BUY_PRICE_RATE, curr_price)
printt(str(buyResult))
coinName = self.MarketName.split('-')[1]
sellRate = SELL_PRICE_RATE
#if gap_price_rate > 1.0:
# sellRate = 2.0
#elif gap_price_rate > 0.5:
# sellRate = 2.5
sellResult = sellCoin(coinName, sellRate)
printt(str(sellResult))
slack_message = '[' + self.MarketName + '] ' + '\nPREV: ' + priv_time.strftime(
'%m/%d %H:%M:%S') + ' , ' + str('%.8f' % priv_price) + '\nCURR: ' + curr_time.strftime(
'%m/%d %H:%M:%S') + ' , ' + str('%.8f' % curr_price) + '\nGAP: ' + '%.1f' % (
ACCEPT_PRICE_GAP * 100) + '\nUNIT: ' + '%.3f' % BUY_COIN_UNIT + 'BTC\nHOST: ' + socket.gethostname() + '\nCATCH : %0.8f' % gap_price_rate + '\nBUY_PRICE_RATE : %.2f' % BUY_PRICE_RATE + '\nSELL_PRICE_RATE : %.2f' % sellRate
printt(slack_message)
slack.notify(text=slack_message)
break
slack_message = '[' + self.MarketName + '] ' + '\nPREV: ' + priv_time.strftime(
'%m/%d %H:%M:%S') + ' , ' + str('%.8f' % priv_price) + '\nCURR: ' + curr_time.strftime(
'%m/%d %H:%M:%S') + ' , ' + str('%.8f' % curr_price) + '\nGAP: ' + '%.1f' % (
ACCEPT_PRICE_GAP * 100) + '\nUNIT: ' + '%.3f' % BUY_COIN_UNIT + 'BTC\nHOST: ' + socket.gethostname() + '\nCATCH : %0.8f' % gap_price_rate + '\nBUY_PRICE_RATE : %.2f' % BUY_PRICE_RATE + '\nSELL_PRICE_RATE : %.2f' % SELL_PRICE_RATE
slack.notify(text=slack_message)
dict_price.update({self.MarketName: [list_priv, list_curr, True]})
dict_price_bid.update({self.MarketName: [list_priv_bid, list_curr_bid, True]})
dict_price_last.update({self.MarketName: [list_priv_last, list_curr_last, True]})
except:
print(self.MarketName + ' : error')
traceback.print_exc()
time.sleep(1.1)
def buyCoin(coinName, rate, curr_price):
askPrice = curr_price * rate
qty = round(float(BUY_COIN_UNIT / askPrice), 8)
printt('BUY - ' + coinName + ':' + str('%.8f' % askPrice) + ':' + str('%.8f' % qty))
buyResult = bittrex.buy_limit(coinName, qty, askPrice)['result']
return buyResult
def sellCoin(coinName, rate):
start_time = datetime.datetime.now()
# {'success': True, 'message': '', 'result': {'Currency': 'ANS', 'Balance': None, 'Available': None, 'Pending': None, 'CryptoAddress': None}}
printt('sellCoin :' + coinName)
# number of coin
balance = bittrex.get_balance(coinName)
loop_count = 0
sell_count = 0
while True:
if balance['result']['Available'] == None or balance['result']['Available'] == 0.0:
balance = bittrex.get_balance(coinName)
else:
if sell_count == 0:
coinAvail = '%.8f' % float(balance['result']['Available'])
history = bittrex.get_order_history('BTC-' + coinName, 0)
buy_actual_price = history['result'][0]['PricePerUnit']
bidPrice = '%.8f' % (buy_actual_price * rate)
sellResult = bittrex.sell_limit('BTC-' + coinName, coinAvail, bidPrice)['result']
printt('sell price : ' + bidPrice + ', sell unit : ' + coinAvail + ', sell_count %d' % sell_count)
sell_count += 1
else:
coinAvail = '%.8f' % float(balance['result']['Available'])
bidPrice = '%.8f' % (0.0006 / float(coinAvail))
printt('sell price : ' + bidPrice + ', sell unit : ' + coinAvail + ', sell market price count %d' % sell_count)
sellResult = bittrex.sell_limit('BTC-' + coinName, coinAvail, bidPrice)['result']
sell_count += 1
loop_count += 1
time.sleep(0.1)
if loop_count >= 100:
printt("LOOP COUNT 100 BREAK")
break
curr_time = datetime.datetime.now()
during_seconds = (curr_time - start_time).total_seconds()
if during_seconds > CANCEL_TIME:
loop_count2 = 0
while True:
openOrder = bittrex.get_open_orders('BTC-' + coinName)
for order in openOrder['result']:
bittrex.cancel(order['OrderUuid'])
printt('BTC-' + coinName + ' CANCEL : ' + order['OrderUuid'])
balance = bittrex.get_balance(coinName)
if balance['result']['Available'] != None and balance['result']['Available'] != 0.0:
coinAvail = '%.8f' % float(balance['result']['Available'])
bidPrice = '%.8f' % (buy_actual_price * LAST_SELL_PRICE_RATE)
printt('sell price : ' + bidPrice + ', sell unit : ' + coinAvail)
sellResult = bittrex.sell_limit('BTC-' + coinName, coinAvail, bidPrice)['result']
loop_count2 += 1
printt("After %d seconds Try %d / 20 to Cancel and Sell Market Price" % (CANCEL_TIME, loop_count2))
if(loop_count2 >= 20):
printt("LOOP COUNT2 10 BREAK")
break
break
return sellResult
def printt(str):
currnet_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(currnet_time + ' - ' + str)
writeLogFile(str)
def writeLogFile(str):
currnet_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logfileName = currnet_time[:10].replace('-', '') + '.log'
f = open('./logs/' + logfileName, 'a')
f.writelines(currnet_time + ' - ' + str + '\n')
f.close()
return 0
def isExcludedCoin(MarketName):
print("MarketName : " + MarketName)
with open("excluded_coin_list.json") as f:
excludedCoinName = json.load(f)
if MarketName in excludedCoinName['coin']:
print("Excluded")
return True
else:
print("Included")
return False
if __name__ == "__main__":
result = bittrex.get_markets()
printt(str(result))
for coin in result['result']:
MarketName = coin['MarketName']
if 'BTC-' in MarketName and coin['IsActive'] and isExcludedCoin(MarketName) is not True:
try:
current_time = datetime.datetime.now()
dict_price.update({MarketName: [[current_time, 1], [current_time, 1], True]})
dict_price_bid.update({MarketName: [[current_time, 1], [current_time, 1], True]})
dict_price_last.update({MarketName: [[current_time, 1], [current_time, 1], True]})
ThreadGetTiker(MarketName).start()
except:
print('error : ' + MarketName)
while True:
printt('Program is running')
for key, value in dict_price.items():
# print(key + ' : ' + str('%.8f' % (value[0][1]-value[1][1])/value[0][1]))
if value[0][0] != 0 and value[2]:
price_bid = dict_price_bid[key]
price_last = dict_price_last[key]
rate = (value[1][1] - value[0][1]) / value[0][1]
rate_bid = (price_bid[1][1] - price_bid[0][1]) / price_bid[0][1]
rate_last = (price_bid[1][1] - price_bid[0][1]) / price_bid[0][1]
value_str = 'ASK - [%s][%.8f],[%s][%.8f]' % (value[0][0], value[0][1], value[1][0], value[1][1])
value_str_bid = 'BID - [%s][%.8f],[%s][%.8f]' % (price_bid[0][0], price_bid[0][1], price_bid[1][0], price_bid[1][1])
value_str_last = 'LAST - [%s][%.8f],[%s][%.8f]' % (price_last[0][0], price_last[0][1], price_last[1][0], price_last[1][1])
# printt(key + ' : ' + value_str +' : '+ str('%.8f' % rate))
writeLogFile(key.split('-')[1] + ' : ' + value_str + ' : ' + str('%.8f' % rate))
writeLogFile(key.split('-')[1] + ' : ' + value_str_bid + ' : ' + str('%.8f' % rate_bid))
writeLogFile(key.split('-')[1] + ' : ' + value_str_last + ' : ' + str('%.8f' % rate_last))
"""
if rate > ACCEPT_PRICE_GAP:
printt('#################################### ' + key.split('-')[1] + ' #############################')
printt('#################################### ' + key.split('-')[1] + ' #############################')
printt(key.split('-')[1] + ' : ' + value_str + ' : ' + str('%.8f' % rate))
printt(key.split('-')[1] + ' : ' + value_str_bid + ' : ' + str('%.8f' % rate_bid))
printt('#################################### ' + key.split('-')[1] + ' #############################')
printt('#################################### ' + key.split('-')[1] + ' #############################')
"""
time.sleep(2)