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")