def index_stop_command(self): indexInfo = DatabaseManager.get_index_info_model() DatabaseManager.update_index_info_model(False, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, indexInfo.BalanceThreshold, indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, indexInfo.RebalanceTickSetting) logger.info("Index Management Stopped.")
def index_rebalance_tick_update(self, tickcount): if isinstance(int(tickcount), (float, int, complex, long)): DatabaseManager.update_index_info_model( indexInfo.Active, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, indexInfo.TotalRealizedGain, indexInfo.TotalUnrealizedGain, round(float(percentage), 2), indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, int(tickcount)) logger.info("Index rebalance time set to " + str(tickcount) + " minutes.") else: logger.warn("Tick count isn't a number")
def index_rebalance_tick_update(self, tickcount): indexInfo = DatabaseManager.get_index_info_model() if isinstance(int(tickcount), (float, int, complex, int)): DatabaseManager.update_index_info_model(True, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, indexInfo.BalanceThreshold, indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, int(tickcount)) logger.info("Index rebalance time set to " + str(tickcount) + " minutes.") else: logger.warn("Tick count isn't a number")
def index_start_command(self): totalIndexPercentage = 0.0 indexInfo = DatabaseManager.get_index_info_model() indexCoins = DatabaseManager.get_all_index_coin_models() for coin in indexCoins: totalIndexPercentage = totalIndexPercentage + coin.DesiredPercentage if totalIndexPercentage == 100: for iCoin in indexCoins: if iCoin.Ticker != "BTC": coinTicker = DatabaseManager.get_ticker_model( iCoin.Ticker.upper() + "/BTC") sys.stdout.write(coinTicker) sys.stdout.write(iCoin.Ticker) sys.stdout.write('\n') percentage_btc_amount = (indexInfo.TotalBTCVal / 100) * iCoin.DesiredPercentage amountToBuy = percentage_btc_amount / coinTicker.BTCVal 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: #buy app.send_task('Tasks.perform_buy_task', args=[iCoin.Ticker.upper(), amountToBuy]) DatabaseManager.update_index_info_model( True, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, indexInfo.TotalRealizedGain, indexInfo.TotalUnrealizedGain, indexInfo.BalanceThreshold, indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, indexInfo.RebalanceTickSetting) else: logger.warn("Index is currently unbalanced please rebuild")
def index_threshold_update(self, percentage): if isinstance(float(percentage), (float, int, complex, int)): indexInfo = DatabaseManager.get_index_info_model() percentage_btc_amount = indexInfo.TotalBTCVal * ( float(percentage) / 100) if percentage_btc_amount <= CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT: logger.error("Desired BTC Threshold Value Too Low - " + str(percentage)) else: DatabaseManager.update_index_info_model( True, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, round(float(percentage), 2), indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, indexInfo.RebalanceTickSetting) logger.info("Index threshold set to " + str(round(float(percentage), 2))) else: logger.warn("Percentage isn't a number")
def index_gen_command(self): totalIndexPercentage = 0.0 indexInfo = DatabaseManager.get_index_info_model() indexCoins = DatabaseManager.get_all_index_coin_models() for iCoin in indexCoins: if iCoin.Ticker != "BTC": coinTicker = DatabaseManager.get_ticker_model( iCoin.Ticker.upper() + "/BTC") percentage_btc_amount = (indexInfo.TotalBTCVal / 100) * iCoin.DesiredPercentage amountToBuy = percentage_btc_amount / coinTicker.BTCVal 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: #buy app.send_task('Tasks.perform_buy_task', args=[iCoin.Ticker.upper(), amountToBuy]) DatabaseManager.update_index_info_model(True, indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, indexInfo.BalanceThreshold, indexInfo.OrderTimeout, indexInfo.OrderRetryAmount, indexInfo.RebalanceTickSetting)
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") for key in DatabaseManager.get_all_supported_coin_models(): btcbalance = 0.0 usdBalance = 0.0 totalCoins = None fullTicker = key.Ticker + "/BTC" if key.Ticker == 'BTC': fullTicker = 'BTC/USDT' tickerModel = DatabaseManager.get_ticker_model(fullTicker) 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) totalUnrealizeGain = 0.0 totalRealizedGain = 0.0 for key in DatabaseManager.get_all_supported_coin_models(): coinBalance = DatabaseManager.get_coin_balance_model(key.Ticker) indexedCoin = DatabaseManager.get_index_coin_model(key.Ticker) realizedGainModel = DatabaseManager.get_realized_gain_model(key.Ticker) if indexedCoin is not None: if DatabaseManager.update_index_coin_model( indexedCoin.Ticker, indexedCoin.DesiredPercentage, (coinBalance.BTCBalance / totalBtcValue) * 100, ((coinBalance.BTCBalance / totalBtcValue) * 100) - indexedCoin.DesiredPercentage, indexedCoin.Locked): totalUnrealizeGain = totalUnrealizeGain + ( ((coinBalance.BTCBalance / totalBtcValue) * 100) - indexedCoin.DesiredPercentage) totalRealizedGain = totalRealizedGain + realizedGainModel.RealizedGain logger.debug("Total unrealized gain - " + str(totalUnrealizeGain)) 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() totalUnrealizeGain = totalUnrealizeGain if DatabaseManager.update_index_info_model( indexInfo.Active, totalBtcValue, btcUsdValue * totalBtcValue, totalRealizedGain, totalUnrealizeGain, 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")
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")