-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_universe.threads.py
265 lines (223 loc) · 8.88 KB
/
find_universe.threads.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
import ccxt
import ttm
from datetime import datetime
from pprint import pprint
import time
import threading
#####
# # #### # # ###### # ####
# # # ## # # # # #
# # # # # # ##### # #
# # # # # # # # # ###
# # # # # ## # # # #
##### #### # # # # ####
max_threads = 6
data_folder = 'output/find_universe'
exchanges = [
'aax', 'aofex', 'ascendex', 'bequant', 'bibox', 'bigone', 'binance', 'binancecoinm', 'binanceus',
'binanceusdm', 'bit2c', 'bitbank', 'bitbay', 'bitbns', 'bitcoincom', 'bitfinex', 'bitfinex2',
'bitflyer', 'bitforex', 'bitget', 'bithumb', 'bitmart', 'bitmex', 'bitpanda', 'bitso', 'bitstamp',
'bitstamp1', 'bittrex', 'bitvavo', 'bitz', 'bl3p', 'braziliex', 'btcalpha', 'btcbox', 'btcmarkets',
'btctradeua', 'btcturk', 'buda', 'bw', 'bybit', 'bytetrade', 'cdax', 'cex', 'coinbase',
'coinbaseprime', 'coinbasepro', 'coincheck', 'coinegg', 'coinex', 'coinfalcon', 'coinfloor',
'coinmarketcap', 'coinmate', 'coinone', 'coinspot', 'crex24', 'currencycom', 'delta', 'deribit',
'digifinex', 'equos', 'exmo', 'exx', 'flowbtc', 'ftx', 'gateio', 'gemini', 'gopax', 'hbtc',
'hitbtc', 'hollaex', 'huobijp', 'huobipro', 'idex', 'independentreserve', 'indodax', 'itbit',
'kraken', 'kucoin', 'kuna', 'latoken', 'lbank', 'liquid', 'luno', 'lykke', 'mercado', 'mixcoins',
'ndax', 'novadax', 'oceanex', 'okcoin', 'okex', 'okex5', 'paymium', 'phemex', 'poloniex', 'probit',
'qtrade', 'rightbtc', 'ripio', 'southxchange', 'stex', 'therock', 'tidebit', 'tidex', 'timex',
'upbit', 'vcc', 'wavesexchange', 'whitebit', 'xena', 'yobit', 'zaif', 'zb',
]
blacklist = [
'okex', 'okex5', # no API for new accounts
'latoken', # precision max 10 satoshi (X.XXXX BTC) TODO send my 30 USD back
]
simulation_trade_amounts = {
'BTC': 0.001,
'EUR': 42.0,
'USD': 50.0,
'USDT': 50.0,
'USxD': 50.0,
'AUD': 65.0,
'AQ': 331.0,
'QC': 333.0,
'HKD': 388.0,
'JPY': 5330.0,
'UAH': 1398.0,
'KRW': 50000.0,
}
###
# # # # #####
# ## # # #
# # # # # #
# # # # # #
# # ## # #
### # # # #
storage = ttm.storage.JSONFile(data_folder + '/results.json') # storage for strategy data
cache = ttm.storage.JSONFile('cache-universe.json') # storage for performance optimalisation
logger = ttm.logger.Multi(
ttm.logger.Console(min_priority=0),
ttm.logger.CSVFile(data_folder + '/log.csv', min_priority=1),
ttm.logger.CSVFile(data_folder + '/log-all.csv', min_priority=0),
)
logger.set_pair('BTC/USDT')
storage_results = ttm.storage.ThreadSafeJSONFile(data_folder + '/results.json')
storage_exceptions = ttm.storage.ThreadSafeJSONFile(data_folder + '/exceptions.json')
# #
# # # #### ##### # #
# # # # # # # # #
# # # # # # # ####
# # # # # ##### # #
# # # # # # # # #
## ## #### # # # #
def simulate(exchange_name):
global storage, storage_results, storage_exceptions
global cache, logger
global blacklist
try:
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' ' + exchange_name)
#
# 1) Prepare everything
#
exchange = ttm.Tools.get_class('ccxt.' + exchange_name)({
'enableRateLimit': False,
})
if not exchange.has['createMarketOrder']:
raise Exception("%s doesn't support market orders." % exchange_name)
pairs = ttm.Tools.get_pairs(exchange) #; print('# PAIRS:') ; pprint(pairs) ; print(len(pairs))
if len(pairs) == 0:
raise Exception("Exchange '%s' has no pairs for trade." % exchange_name)
endpoint = ttm.Tools.find_popular_quote(pairs)
if endpoint not in simulation_trade_amounts:
raise Exception("No simulation trade amount for '%s' was specified." % endpoint)
trade_amount = simulation_trade_amounts[endpoint]
bot = ttm.bot.Real(
exchange,
ttm.strategy.Universe(
exchange_pairs = pairs,
endpoint = endpoint,
executor = ttm.strategy.universe.Executor(),
),
storage,
cache,
logger
)
#
# 2) Scan for current options
#
paths = bot.strategy.scanner.full_scan(
pairs,
endpoint,
path_length = 4,
trade_amount = trade_amount,
min_result_after_fees = trade_amount * 1.00,
min_worse_result = trade_amount * 1.00,
min_bids_count = 3,
min_asks_count = 3,
)
# Save results into statistics
# - init stats
all_stats = storage_results.open()
if exchange_name not in all_stats:
all_stats[exchange_name] = {
'rounds': 0,
'result_coef': 1.0,
'result_coef_fee_free': 1.0,
'simulation_result_coef': 1.0,
'worse_result_coef': 1.0,
'pairs_count': len(pairs),
'paths_count': 0,
'paths': {},
}
# - count stats
all_stats[exchange_name]['rounds'] += 1
for path_key, path_data in paths.items():
result_coef = path_data['result_amount'] / trade_amount
result_coef_fee_free = path_data['result_amount_fee_free'] / trade_amount
simulation_result_coef = path_data['simulation'][-1]['result_amount'] / path_data['simulation'][0]['result_amount']
worse_result_coef = path_data['simulation'][-1]['worse_result_amount'] / path_data['simulation'][0]['result_amount']
print(" • %s: %f" % (path_key, simulation_result_coef))
all_stats[exchange_name]['result_coef'] *= result_coef
all_stats[exchange_name]['result_coef_fee_free'] *= result_coef_fee_free
all_stats[exchange_name]['simulation_result_coef'] *= simulation_result_coef
all_stats[exchange_name]['worse_result_coef'] *= worse_result_coef
if path_key not in all_stats[exchange_name]['paths']:
all_stats[exchange_name]['paths'][path_key] = {
'rounds': 0,
'result_coef': 1.0,
'result_coef_fee_free': 1.0,
'simulation_result_coef': 1.0,
'worse_result_coef': 1.0,
'trade_amount': trade_amount,
'last_result_amount': None,
'last_result_amount_fee_free': None,
'last_fees': 0.0,
'last_result_coef': None,
'last_result_coef_fee_free': None,
'last_simulation_result_coef': None,
'last_worse_result_coef': None,
'datetime': [],
'steps': [],
'simulation': [],
'order_books': {},
}
all_stats[exchange_name]['paths'][path_key]['rounds'] += 1
all_stats[exchange_name]['paths'][path_key]['result_coef'] *= result_coef
all_stats[exchange_name]['paths'][path_key]['result_coef_fee_free'] *= result_coef_fee_free
all_stats[exchange_name]['paths'][path_key]['simulation_result_coef'] *= simulation_result_coef
all_stats[exchange_name]['paths'][path_key]['worse_result_coef'] *= worse_result_coef
all_stats[exchange_name]['paths'][path_key]['datetime'].append(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
all_stats[exchange_name]['paths'][path_key]['last_result_amount'] = path_data['result_amount']
all_stats[exchange_name]['paths'][path_key]['last_result_amount_fee_free'] = path_data['result_amount_fee_free']
all_stats[exchange_name]['paths'][path_key]['last_fees'] = path_data['result_amount_fee_free'] - path_data['result_amount']
all_stats[exchange_name]['paths'][path_key]['last_result_coef'] = result_coef
all_stats[exchange_name]['paths'][path_key]['last_result_coef_fee_free'] = result_coef_fee_free
all_stats[exchange_name]['paths'][path_key]['last_simulation_result_coef'] = simulation_result_coef
all_stats[exchange_name]['paths'][path_key]['last_worse_result_coef'] = worse_result_coef
all_stats[exchange_name]['paths'][path_key]['steps'] = path_data['steps']
all_stats[exchange_name]['paths'][path_key]['simulation'] = path_data['simulation']
all_stats[exchange_name]['paths'][path_key]['order_books'] = path_data['order_books']
all_stats[exchange_name]['paths_count'] = len(all_stats[exchange_name]['paths'])
# - save stats
storage_results.save(all_stats)
except Exception as e:
# Log exception
exceptions = storage_exceptions.open()
if exchange_name not in exceptions:
exceptions[exchange_name] = []
exception_message = "%s: %s" % (type(e).__name__, str(e)[0:255])
if exception_message not in exceptions[exchange_name]:
exceptions[exchange_name].append(exception_message)
storage_exceptions.save(exceptions)
# Blacklist exchange
blacklist.append(exchange_name)
threads_count = 0
def simulate_wrapper(*args, **kwargs):
global threads_count
threads_count += 1
simulate(*args, **kwargs)
threads_count -= 1
# Never-ending work...
i = 0
while True:
for exchange_name in exchanges:
# Blacklist?
if exchange_name in blacklist:
continue
# Limit running threads
while True:
if threads_count < max_threads:
break
time.sleep(0.05)
# Simulate
# simulate_wrapper(exchange_name)
thread = threading.Thread(target=simulate, args=[exchange_name])
thread.start()
# Sort results
i += 1
if i % 100 == 0:
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' SORTING RESULTS...')
all_stats = storage_results.open()
all_stats = {k:all_stats[k] for k in sorted(all_stats, key=lambda k: all_stats[k]['simulation_result_coef'], reverse=True)}
storage_results.save(all_stats)
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' SORTED...')