def index_equal_weight(self): """Set portfolio to equal weight""" totalPercentage = 0 totalLockedPercentage = 0.0 totalUnlockedPercentage = 100 totalUnlockedCoinsCount = 0 averagePercentage = 0 indexedCoins = DatabaseManager.get_all_index_coin_models() for inCoins in indexedCoins: if inCoins.Locked == True: totalLockedPercentage = totalLockedPercentage + inCoins.DesiredPercentage else: totalUnlockedCoinsCount = totalUnlockedCoinsCount + 1 totalPercentage = totalPercentage + totalLockedPercentage totalUnlockedPercentage = totalUnlockedPercentage - totalLockedPercentage averagePercentage = round( totalUnlockedPercentage / totalUnlockedCoinsCount, 2) logger.info("Setting default allocation to " + str(averagePercentage)) for inCoins in indexedCoins: if inCoins.Locked == False: totalPercentage = totalPercentage + averagePercentage if totalPercentage + averagePercentage > 100: desiredPercentage = 100 - totalPercentage else: desiredPercentage = averagePercentage DatabaseManager.update_index_coin_model( inCoins.Ticker, float(desiredPercentage), 0.0, False)
def index_remove_coin(self, coin): if (coin.upper() == "BTC"): logger.warn("You cannot remove BTC from your index.") return indexedCoin = DatabaseManager.get_index_coin_model(coin.upper()) btcCoin = DatabaseManager.get_index_coin_model('BTC') percentageToAdd = indexedCoin.DesiredPercentage if self.coin_supported_check(coin.upper()): if DatabaseManager.delete_index_coin_model(coin.upper()): # Add percentage back to BTC model DatabaseManager.update_index_coin_model( btcCoin.Ticker, btcCoin.DesiredPercentage + percentageToAdd, btcCoin.DistanceFromTarget, btcCoin.Locked) logger.info("Coin " + coin.upper() + " removed from index") else: # Already Exist logger.warn("Coin not in index") else: logger.warn("Coin not supported")
def index_add_coin(self, coin, percentage=1.0, locked=False): lockCoin = False totalLockedPercentage = 0.0 totalUnlockedPercentage = 0.0 totalUnlockedCoinsCount = 0 indexInfo = DatabaseManager.get_index_info_model() indexedCoins = DatabaseManager.get_all_index_coin_models() if locked == "true" or locked == "True": lockCoin = True for inCoins in indexedCoins: if inCoins.Locked == True: totalLockedPercentage = totalLockedPercentage + inCoins.DesiredPercentage else: totalUnlockedPercentage = totalUnlockedPercentage + inCoins.DesiredPercentage totalUnlockedCoinsCount = totalUnlockedCoinsCount + 1 if totalUnlockedPercentage > float(percentage): if self.coin_supported_check(coin.upper()): percentageToRemove = float( percentage) / totalUnlockedCoinsCount for iCoin in indexedCoins: if iCoin.Locked != True: DatabaseManager.update_index_coin_model( iCoin.Ticker, iCoin.DesiredPercentage - percentageToRemove, iCoin.DistanceFromTarget, iCoin.Locked) if isinstance(float(percentage), (float, int, complex, int)): if DatabaseManager.create_index_coin_model( coin.upper(), float(percentage), 0.0, lockCoin): logger.info("Coin " + coin.upper() + " added to index") else: # Already Exist logger.warn("Coin already in index") else: logger.warn("Percentage isn't a number") else: logger.warn("Coin not supported") else: logger.warn("Not Enough Unlocked Percentage")
def index_update_coin(self, coin, percentage, locked): lockCoin = False totalLockedPercentage = 0.0 totalUnlockedPercentage = 0.0 totalUnlockedCoinsCount = 0 indexInfo = DatabaseManager.get_index_info_model() indexedCoins = DatabaseManager.get_all_index_coin_models() indexedCoin = DatabaseManager.get_index_coin_model(coin.upper()) for inCoins in indexedCoins: if inCoins.Ticker != coin.upper(): if inCoins.Locked == True: totalLockedPercentage = totalLockedPercentage + inCoins.DesiredPercentage else: totalUnlockedPercentage = totalUnlockedPercentage + inCoins.DesiredPercentage totalUnlockedCoinsCount = totalUnlockedCoinsCount + 1 if len(indexedCoins) > 1: if totalUnlockedCoinsCount > 0: if locked == "true" or locked == "True": lockCoin = True percentage_btc_amount = indexInfo.TotalBTCVal * ( float(percentage) / 100) if percentage_btc_amount >= CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT: if float(percentage) > indexedCoin.DesiredPercentage: if totalUnlockedPercentage > float(percentage): if self.coin_supported_check(coin.upper()): percentageToAdd = 0.0 if totalUnlockedCoinsCount > 0: percentageToAdd = float( indexedCoin.DesiredPercentage - float(percentage) ) / totalUnlockedCoinsCount else: percentageToAdd = float( indexedCoin.DesiredPercentage - float(percentage)) for iCoin in indexedCoins: if iCoin.Ticker != coin.upper(): if iCoin.Locked != True: DatabaseManager.update_index_coin_model( iCoin.Ticker, iCoin.DesiredPercentage - percentageToAdd, iCoin.DistanceFromTarget, iCoin.Locked) if isinstance(float(percentage), (float, int, complex, long)): if DatabaseManager.update_index_coin_model( coin.upper(), float(percentage), 0.0, lockCoin): logger.info("Coin " + coin.upper() + " updated in index") else: # Already Exist logger.warn("Coin already in index") else: logger.warn("Percentage isn't a number") else: logger.warn("Coin not supported") else: logger.warn("Not Enough Unlocked Percentage") else: ## NEW BLOCK if self.coin_supported_check(coin.upper()): percentageToAdd = 0.0 if totalUnlockedCoinsCount > 0: percentageToAdd = float( indexedCoin.DesiredPercentage - float( percentage)) / totalUnlockedCoinsCount else: percentageToAdd = float( indexedCoin.DesiredPercentage - float(percentage)) for iCoin in indexedCoins: if iCoin.Ticker != coin.upper(): if iCoin.Locked != True: DatabaseManager.update_index_coin_model( iCoin.Ticker, iCoin.DesiredPercentage + percentageToAdd, iCoin.DistanceFromTarget, iCoin.Locked) if isinstance(float(percentage), (float, int, complex, int)): if DatabaseManager.update_index_coin_model( coin.upper(), float(percentage), 0.0, lockCoin): logger.info("Coin " + coin.upper() + " updated in index") else: # Already Exist logger.warn("Coin already in index") else: logger.warn("Percentage isn't a number") else: logger.warn("Coin not supported") else: logger.warn( "Specified percentage below current bittrex trade value" ) else: logger.warn( "Currently no unlocked coins to transfer free value") else: logger.warn( "Please add another coin to your index before updating a given coin" )
def index_add_coin(self, coin, percentage, locked): lockCoin = False totalLockedPercentage = 0.0 totalUnlockedPercentage = 0.0 totalUnlockedCoinsCount = 0 indexInfo = DatabaseManager.get_index_info_model() indexedCoins = DatabaseManager.get_all_index_coin_models() if locked == "true" or locked == "True": lockCoin = True percentage_btc_amount = indexInfo.TotalBTCVal * (float(percentage) / 100) if percentage_btc_amount >= CondexConfig.BITTREX_MIN_BTC_TRADE_AMOUNT: for inCoins in indexedCoins: if inCoins.Locked == True: totalLockedPercentage = totalLockedPercentage + inCoins.DesiredPercentage else: totalUnlockedPercentage = totalUnlockedPercentage + inCoins.DesiredPercentage totalUnlockedCoinsCount = totalUnlockedCoinsCount + 1 if totalUnlockedPercentage > float(percentage): if self.coin_supported_check(coin.upper()): percentageToRemove = float( percentage) / totalUnlockedCoinsCount for iCoin in indexedCoins: if iCoin.Locked != True: DatabaseManager.update_index_coin_model( iCoin.Ticker, iCoin.DesiredPercentage - percentageToRemove, iCoin.CurrentPercentage, iCoin.UnrealizedGain, iCoin.Locked) if isinstance(float(percentage), (float, int, complex, long)): if DatabaseManager.create_index_coin_model( coin.upper(), float(percentage), 0.0, 0.0, lockCoin): DatabaseManager.create_realized_gain_model( coin.upper(), 0.0) logger.info("Coin " + coin.upper() + " added to index") else: # Already Exist logger.warn("Coin already in index") else: logger.warn("Percentage isn't a number") else: logger.warn("Coin not supported") else: logger.warn("Not Enough Unlocked Percentage") else: logger.warn( "Specified percentage below current bittrex trade value")
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")