def perform_rebalance_task(rebalanceTicker, rebalanceSellAmount, elgibleTicker, elgibleBuyAmount): coinSellIncomplete = True coinBuyIncomplete = True coinSellRetryCount = 0 coinBuyRetryCount = 0 coinSellFailed = False sellOrderUUID = "" buyOrderUUID = "" indexInfo = DatabaseManager.get_index_info_model() retryLimit = indexInfo.OrderRetryAmount rebalanceTickerGainModel = DatabaseManager.get_realized_gain_model( rebalanceTicker) elgibleCoinTicker = DatabaseManager.get_ticker_model(elgibleTicker + "/BTC") em = ExchangeManager() partial_fill_amount = 0 partial_filled = False if rebalanceTicker != "BTC" and rebalanceTicker != "btc": 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 DatabaseManager.update_realized_gain_model( rebalanceTicker, rebalanceTickerGainModel.RealizedGain + ((order_filled_amount * rebalanceCoinTicker.BTCVal) / indexInfo.TotalBTCVal) * 100) 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 DatabaseManager.update_realized_gain_model( rebalanceTicker, rebalanceTickerGainModel.RealizedGain + ((order_filled_amount * rebalanceCoinTicker.BTCVal) / indexInfo.TotalBTCVal) * 100) 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 if coinSellFailed: logger.info("Sell of coin " + rebalanceTicker + " failed after " + str(coinSellRetryCount) + " attempts") else: while coinBuyIncomplete: if coinBuyRetryCount >= retryLimit: coinBuyIncomplete = False logger.info("Buying of coin " + rebalanceTicker + " failed after " + str(coinBuyRetryCount) + " attempts") break # Cancel Order else: if CondexConfig.DEBUG == True: logger.debug("Putting in buy order") else: logger.info("Buying " + str(elgibleBuyAmount) + " of " + elgibleTicker + " at " + str(elgibleCoinTicker.BTCVal)) if partial_filled == True: buyOrderUUID = em.create_buy_order( elgibleTicker, partial_fill_amount / elgibleCoinTicker.BTCVal, elgibleCoinTicker.BTCVal)['id'] else: buyOrderUUID = em.create_buy_order( elgibleTicker, elgibleBuyAmount, elgibleCoinTicker.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 " + elgibleTicker + " for " + str(order_result['price'])) coinBuyIncomplete = False if rebalanceTicker == "BTC" or rebalanceTicker == "btc": DatabaseManager.update_realized_gain_model( rebalanceTicker, rebalanceTickerGainModel.RealizedGain + ((order_filled_amount * elgibleCoinTicker.BTCVal) / indexInfo.TotalBTCVal) * 100) elif ( order_filled_amount * elgibleCoinTicker.BTCVal ) > CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT and order_result[ 'status'] == "open": em.cancel_order(buyOrderUUID) logger.debug("Bought partial of coin " + elgibleCoinTicker + " for " + str(order_result['price'])) coinBuyIncomplete = False if rebalanceTicker == "BTC" or rebalanceTicker == "btc": DatabaseManager.update_realized_gain_model( rebalanceTicker, rebalanceTickerGainModel.RealizedGain + ((order_filled_amount * elgibleCoinTicker.BTCVal) / indexInfo.TotalBTCVal) * 100) 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 # Delete the locks if CondexConfig.DEBUG != True: DatabaseManager.delete_coin_lock_model(rebalanceTicker) DatabaseManager.delete_coin_lock_model(elgibleTicker)
def perform_buy_task(elgibleTicker, elgibleBuyAmount): coinBuyIncomplete = True coinBuyRetryCount = 0 buyOrderUUID = "" indexInfo = DatabaseManager.get_index_info_model() retryLimit = indexInfo.OrderRetryAmount elgibleCoinTicker = DatabaseManager.get_ticker_model(elgibleTicker + "/BTC") em = ExchangeManager() partial_fill_amount = 0 partial_filled = False DatabaseManager.create_coin_lock_model(elgibleTicker) while coinBuyIncomplete: if coinBuyRetryCount >= retryLimit: coinBuyIncomplete = False logger.info("Buying of coin " + rebalanceTicker + " failed after " + str(coinBuyRetryCount) + " attempts") break # Cancel Order else: if CondexConfig.DEBUG == True: logger.debug("Putting in buy order") else: logger.info("Buying " + str(elgibleBuyAmount) + " of " + elgibleTicker + " at " + str(elgibleCoinTicker.BTCVal)) buyOrderUUID = em.create_buy_order( elgibleTicker, elgibleBuyAmount, elgibleCoinTicker.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 " + elgibleTicker + " for " + str(order_result['price'])) coinBuyIncomplete = False elif ( order_filled_amount * elgibleCoinTicker.BTCVal ) > CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT and order_result[ 'status'] == "open": em.cancel_order(buyOrderUUID) logger.debug("Bought partial of coin " + elgibleCoinTicker + " 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 # Delete the locks if CondexConfig.DEBUG != True: DatabaseManager.delete_coin_lock_model(elgibleTicker)
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)