-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTasks.py
executable file
·405 lines (295 loc) · 15.1 KB
/
Tasks.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
import time
import json
import datetime
from peewee import *
from decimal import *
from celery import Celery
from logzero import logger
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from config.Database import *
from datetime import timedelta
from config import CondexConfig
from Util import Util
from managers.DatabaseManager import DatabaseManager
from managers.ExchangeManager import ExchangeManager
from managers.BalanceManager import BalanceManager
app = Celery('tasks', backend='amqp', broker='amqp://')
app.conf.task_default_queue = 'Condex-Trade-Queue'
app.conf.beat_schedule = {
'supported_coins_task':{
'task':'Tasks.supported_coins_task',
'schedule': timedelta(seconds=45),
'options': {'queue' : 'Condex-Update-Queue'}
},
'wallet_update_task':{
'task':'Tasks.wallet_update_task',
'schedule': timedelta(seconds=50),
'options': {'queue' : 'Condex-Update-Queue'}
},
'increment_rebalance_tick_task':{
'task':'Tasks.increment_rebalance_tick_task',
'schedule': timedelta(seconds=60),
'options': {'queue' : 'Condex-Update-Queue'}
}
}
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
Util.bootstrap()
@app.task(name='Tasks.supported_coins_task')
def supported_coins_task():
em = ExchangeManager()
marketData = em.get_tickers()
btcUsdValue = em.get_btc_usd_value()
supportedCoins = em.get_supported_pairs(marketData)
logger.debug("Starting Coins Update Task")
# First We Update our Supported Market List
for key in supportedCoins:
sliced_pair = ''
if key != 'BTC/USDT':
sliced_pair = key[:-4]
else:
sliced_pair = 'BTC'
DatabaseManager.create_supported_coin_model(sliced_pair)
btcTickerVal = 0.0
usdTickerVal = 0.0
if key != 'BTC/USDT':
btcTickerVal = marketData[key]['info']['Ask']
usdTickerVal = btcUsdValue*btcTickerVal
else:
btcTickerVal = 0.0
usdTickerVal = marketData[key]['info']['Ask']
if DatabaseManager.create_ticker_model(key, round(btcTickerVal,8), round(usdTickerVal,8), datetime.datetime.now()):
#logger.debug("Created Ticker Model - " + key)
pass
else:
if DatabaseManager.update_ticker_model(key, round(btcTickerVal,8), round(usdTickerVal,8), datetime.datetime.now()):
#logger.debug("Updated Ticker Model - " + key)
pass
else:
logger.error("Failed To Update Ticker Model - " + key)
logger.info("Coins Update Task Completed")
def get_ticker(key):
fullTicker = key.Ticker + "/BTC"
if key.Ticker == 'BTC':
fullTicker = 'BTC/USDT'
return DatabaseManager.get_ticker_model(fullTicker)
@app.task(name='Tasks.wallet_update_task')
def wallet_update_task():
em = ExchangeManager()
walletData = em.get_balance()
btcUsdValue = em.get_btc_usd_value()
totalBtcValue = 0.0
logger.info("Starting Wallet Update Task")
logger.debug("Checking Wallet Locks")
walletLockDeleteList = []
# Clear up the wallet locks.
for walletLockModel in DatabaseManager.get_all_wallet_trade_lock_models():
if DatabaseManager.get_coin_lock_model(walletLockModel.Ticker) == None:
walletLockDeleteList.append(walletLockModel.Ticker)
for walletLockTicker in walletLockDeleteList:
DatabaseManager.delete_wallet_trade_lock_model(walletLockTicker)
for key in DatabaseManager.get_all_supported_coin_models():
btcbalance = 0.0
usdBalance = 0.0
totalCoins = None
tickerModel = get_ticker(key)
try:
btcbalance = walletData[key.Ticker]['total'] * tickerModel.BTCVal
totalCoins = walletData[key.Ticker]['total']
usdBalance = btcUsdValue*btcbalance
except:
btcbalance = 0.0
totalCoins = 0.0
if key.Ticker == 'BTC':
btcbalance=walletData[key.Ticker]['total']
usdBalance=btcUsdValue*btcbalance
indexedCoin = DatabaseManager.get_index_coin_model(key.Ticker)
if indexedCoin is not None:
totalBtcValue = totalBtcValue + btcbalance
if DatabaseManager.create_coin_balance_model(key.Ticker, btcbalance, usdBalance, totalCoins, datetime.datetime.now()):
#logger.debug("Created Coin Balance Model - " + key.Ticker)
pass
else:
if DatabaseManager.update_coin_balance_model(key.Ticker, btcbalance, btcUsdValue*btcbalance, totalCoins, datetime.datetime.now()):
#logger.debug("Updated Coin Balance Model - " + key.Ticker)
pass
else:
logger.error("Failed Update Coin Balance Model - " + key.Ticker)
totalUnrealizedGain = 0.0
totalRealizedGain = 0.0
for key in DatabaseManager.get_all_supported_coin_models():
tickerModel = get_ticker(key)
coinBalance = DatabaseManager.get_coin_balance_model(key.Ticker)
indexedCoin = DatabaseManager.get_index_coin_model(key.Ticker)
if indexedCoin is not None:
if DatabaseManager.update_index_coin_model(
indexedCoin.Ticker,
indexedCoin.DesiredPercentage,
indexedCoin.get_distance_from_target(coinBalance,totalBtcValue),
indexedCoin.Locked):
logger.debug("Updated Indexed Coin Model - " + indexedCoin.Ticker)
else:
logger.error("Failed To Update Indexed Coin Model - " + indexedCoin.Ticker)
indexInfo = DatabaseManager.get_index_info_model()
if DatabaseManager.update_index_info_model(indexInfo.Active, totalBtcValue, btcUsdValue * totalBtcValue,
indexInfo.BalanceThreshold, indexInfo.OrderTimeout,
indexInfo.OrderRetryAmount, indexInfo.RebalanceTickSetting):
logger.debug("Updated Index Info Model")
else:
logger.error("Failed To Update Index Info Model")
logger.info("Wallet Update Task Completed")
@app.task(name='Tasks.increment_rebalance_tick_task')
def increment_rebalance_tick_task():
indexInfo = DatabaseManager.get_index_info_model()
if indexInfo.Active == True:
rebalanceTick = DatabaseManager.get_rebalance_tick_model()
if rebalanceTick.TickCount >= indexInfo.RebalanceTickSetting:
app.send_task('Tasks.perform_algo_task',args=[], queue='Condex-Update-Queue')
else:
DatabaseManager.update_rebalance_tick_model(rebalanceTick.TickCount + 1)
@app.task(name='Tasks.perform_algo_task')
def perform_algo_task():
balanceManager = BalanceManager()
coinsAboveThreshold = {}
coinsEligibleForIncrease = {}
em = ExchangeManager()
indexInfo = DatabaseManager.get_index_info_model()
try:
if indexInfo.Active == True:
percentage_btc_amount = indexInfo.TotalBTCVal*(indexInfo.BalanceThreshold/100)
logger.debug("Percentage_to_btc_amount: " + str(percentage_btc_amount))
if percentage_btc_amount <= CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT:
logger.debug("Current BTC Threshold Value To Low - " + str(percentage_btc_amount))
else:
# Generate our winners/losers list
for indexedCoin in DatabaseManager.get_all_index_coin_models():
if indexedCoin.Ticker != "BTC":
coinBalance = DatabaseManager.get_coin_balance_model(indexedCoin.Ticker)
coin_off_percent = indexedCoin.get_percent_from_coin_target(coinBalance, indexInfo.TotalBTCVal)
if coin_off_percent >= indexInfo.BalanceThreshold:
coinsAboveThreshold[indexedCoin.Ticker] = coin_off_percent
elif abs(coin_off_percent) >= indexInfo.BalanceThreshold:
coinsEligibleForIncrease[indexedCoin.Ticker] = coin_off_percent
# Sort our tables
coinsAboveThreshold = Util.tuple_list_to_dict(sorted(coinsAboveThreshold.items(), key=lambda pair: pair[1], reverse=True))
coinsEligibleForIncrease = Util.tuple_list_to_dict(sorted(coinsEligibleForIncrease.items(), key=lambda pair: pair[1], reverse=True))
balanceManager.rebalance_coins(coinsAboveThreshold, coinsEligibleForIncrease, app)
except Exception as e:
logger.exception(e)
@app.task(name='Tasks.perform_buy_task')
def perform_buy_task(eligibleTicker, eligibleBuyAmount):
coinBuyIncomplete = True
coinBuyRetryCount = 0
buyOrderUUID = ""
indexInfo = DatabaseManager.get_index_info_model()
retryLimit = indexInfo.OrderRetryAmount
eligibleCoinTicker = DatabaseManager.get_ticker_model(eligibleTicker+"/BTC")
em = ExchangeManager()
try:
partial_fill_amount = 0
partial_filled = False
DatabaseManager.create_coin_lock_model(eligibleTicker)
while coinBuyIncomplete:
if coinBuyRetryCount >= retryLimit:
coinBuyIncomplete = False
logger.info("Buying of coin " + eligibleTicker + " failed after " + str(coinBuyRetryCount) + " attempts")
break
# Cancel Order
else:
if CondexConfig.DEBUG == True:
logger.debug("Putting in buy order")
else:
logger.info("Buying %s of %s at %s", eligibleBuyAmount, eligibleTicker, eligibleCoinTicker.BTCVal)
if partial_filled == True:
buyOrderUUID = em.create_buy_order(eligibleTicker, partial_fill_amount/eligibleCoinTicker.BTCVal, eligibleCoinTicker.BTCVal)['id']
else:
buyOrderUUID = em.create_buy_order(eligibleTicker, eligibleBuyAmount, eligibleCoinTicker.BTCVal)['id']
time.sleep(60*indexInfo.OrderTimeout)
# Check order succeded through
if CondexConfig.DEBUG == True:
logger.debug("Fetching order")
coinBuyIncomplete = False
else:
order_result = em.fetch_order(buyOrderUUID)
order_filled_amount = order_result['filled']
if order_result['status'] == "closed":
logger.info("Bought coin " + eligibleTicker + " for " + str(order_result['price']))
coinBuyIncomplete = False
elif (order_filled_amount*eligibleCoinTicker.BTCVal) > CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT and order_result['status'] == "open":
em.cancel_order(buyOrderUUID)
logger.debug("Bought partial of coin " + eligibleCoinTicker + " for " + str(order_result['price']))
coinBuyIncomplete = False
else:
coinBuyRetryCount = coinBuyRetryCount + 1
if CondexConfig.DEBUG == True:
logger.debug("Canceling buy order")
else:
try:
em.cancel_order(buyOrderUUID)
except:
coinBuyIncomplete = False
pass # order failed to cancel got filled previously
logger.debug("Buy Order Timeout Reached")
time.sleep(10) #Magic Number
except Exception as e:
logger.exception(e)
finally:
if CondexConfig.DEBUG != True:
DatabaseManager.delete_coin_lock_model(eligibleTicker)
@app.task(name='Tasks.perform_sell_task')
def perform_sell_task(rebalanceTicker, rebalanceSellAmount):
coinSellIncomplete = True
coinSellRetryCount = 0
sellOrderUUID = ""
indexInfo = DatabaseManager.get_index_info_model()
retryLimit = indexInfo.OrderRetryAmount
eligibleCoinTicker = DatabaseManager.get_ticker_model(rebalanceTicker+"/BTC")
em = ExchangeManager()
try:
partial_fill_amount = 0
partial_filled = False
DatabaseManager.create_coin_lock_model(rebalanceTicker)
while coinSellIncomplete:
if coinSellRetryCount >= retryLimit:
coinSellFailed = True
coinSellIncomplete = False
break
# Cancel Order
else:
rebalanceCoinTicker = DatabaseManager.get_ticker_model(rebalanceTicker+"/BTC")
if CondexConfig.DEBUG == True:
logger.info("Placing Sell Order For " + rebalanceTicker+"/BTC")
else:
logger.info("Selling " + str(rebalanceSellAmount) + " of " + rebalanceTicker + " at " + str(rebalanceCoinTicker.BTCVal))
sellOrderUUID = em.create_sell_order(rebalanceTicker, rebalanceSellAmount, rebalanceCoinTicker.BTCVal)['id']
time.sleep(60*indexInfo.OrderTimeout)
# Check order succeded through
if CondexConfig.DEBUG == True:
logger.debug("Fetching order")
coinSellIncomplete = False
else:
order_result = em.fetch_order(sellOrderUUID)
order_filled_amount = order_result['filled']
if order_result['status'] == "closed":
logger.debug("Sold coin " + rebalanceTicker + " for " + str(order_result['price']))
coinSellIncomplete = False
elif (order_filled_amount*rebalanceCoinTicker.BTCVal) > CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT and order_result['status'] == "open":
em.cancel_order(sellOrderUUID)
logger.debug("Sold partial of coin " + rebalanceTicker + " for " + str(order_result['price']))
coinSellIncomplete = False
partial_filled = True
partial_fill_amount = order_filled_amount*rebalanceCoinTicker.BTCVal
else:
coinSellRetryCount = coinSellRetryCount + 1
if CondexConfig.DEBUG == True:
logger.debug("Canceling sell order")
else:
em.cancel_order(sellOrderUUID)
logger.debug("Sell Order Timeout Reached")
time.sleep(10) #Magic Number
except Exception as e:
logger.exception(e)
finally:
if CondexConfig.DEBUG != True:
DatabaseManager.delete_coin_lock_model(rebalanceTicker)