def create_index_coin_model(ticker, desiredPercentage, distanceFromTarget, locked): with internal_database.execution_context(): try: IndexedCoinModel.create(Ticker=ticker, DesiredPercentage=round(desiredPercentage, 2), DistanceFromTarget=round(distanceFromTarget, 2), Locked=locked) return True except IntegrityError: return False except Exception as e: logger.exception(e)
def export_market_cap_index(self, top_n): to_retrieve = int(top_n) + 20 url = "https://api.coinmarketcap.com/v1/ticker/?limit=" + str( to_retrieve) response = requests.get(url) if response.status_code != 200: logger.error("There was a problem retrieving the market cap data") return market_cap = response.json() global_response = requests.get( 'https://api.coinmarketcap.com/v1/global/') if global_response.status_code != 200: logger.error("There was a problem retrieving the market cap data") return total_market_cap = float( global_response.json()['total_market_cap_usd']) coin_objs = [] total_percentage = 0 for coin in market_cap: if len(coin_objs) >= int(top_n): break else: if (self.coin_supported_check(str(coin['symbol']).upper())): coin_obj = IndexedCoinModel() coin_obj.Ticker = coin['symbol'] market_percent = round( (float(coin['market_cap_usd']) / total_market_cap) * 100, 2) coin_obj.DesiredPercentage = market_percent total_percentage += market_percent coin_objs.append(coin_obj) indexJson = "[" coins = [] adjusted_total = 0 for coin_obj in coin_objs: adjusted_coin_percentage = round( (coin_obj.DesiredPercentage / total_percentage) * 100, 2) if adjusted_coin_percentage + adjusted_total > 100: adjusted_coin_percentage = 100 - adjusted_total coin_obj.DesiredPercentage = adjusted_coin_percentage adjusted_total += adjusted_coin_percentage coin_json = jsonpickle.encode(coin_obj) # logger.debug(coin_json) coins.append(coin_json) indexJson += ",".join(coins) indexJson += "]" with open("index.json", "w") as file_object: file_object.write(indexJson)
def show_stats(self): indexInfo = IndexInfoModel.get(id=1) # Create the Index Table cointTableData = [[ 'Coin', 'Amount', 'BTC Val', 'USD Val', 'Desired %', 'Locked', 'Active %', 'U Gain %', 'R Gain %' ]] for coin in IndexedCoinModel.select(): coinBalance = CoinBalanceModel.get( CoinBalanceModel.Coin == coin.Ticker) realizedGainModel = DatabaseManager.get_realized_gain_model( coin.Ticker) newArray = [ coin.Ticker, coinBalance.TotalCoins, coinBalance.BTCBalance, round(coinBalance.USDBalance, 2), coin.DesiredPercentage, coin.Locked, coin.CurrentPercentage, coin.UnrealizedGain, realizedGainModel.RealizedGain, ] cointTableData.append(newArray) # Create the summary table summary_table_data = [[ 'Active', 'Index Count', 'BTC Val', 'USD Val', 'Unrealized Gain %', 'Realized Gain %' ]] summary_table_data.append([ True, len(IndexedCoinModel.select()), indexInfo.TotalBTCVal, indexInfo.TotalUSDVal, round(indexInfo.TotalUnrealizedGain, 2), round(indexInfo.TotalRealizedGain, 2) ]) coin_table = AsciiTable(cointTableData) summary_table = AsciiTable(summary_table_data) sys.stdout.write("\nCurrent Index Summary\n") sys.stdout.write(summary_table.table) sys.stdout.write("\nCurrent Index Table\n") sys.stdout.write(coin_table.table) sys.stdout.write('\n')
def create_index_coin_model(ticker, desiredPercentage, currentPercentage, unRealizedGain, locked): try: IndexedCoinModel.create( Ticker=ticker, DesiredPercentage=round(desiredPercentage, 2), CurrentPercentage=round(currentPercentage, 2), UnrealizedGain=round(unRealizedGain, 2), Locked=locked) return True except IntegrityError: return False except Exception as e: logger.exception(e)
def show_index(self): graphArray = [] for coin in IndexedCoinModel.select(): graphArray.append((coin.Ticker, coin.DesiredPercentage)) pyGraph = Pyasciigraph(line_length=50, min_graph_length=50, separator_length=4, multivalue=False, human_readable='si', graphsymbol='*', float_format='{0:,.2f}') thresholds = { 15: Gre, 30: Blu, 50: Yel, 60: Red, } data = hcolor(graphArray, thresholds) sys.stdout.write("\n") for line in pyGraph.graph('Index Distribution', data=data): sys.stdout.write(line + "\n") sys.stdout.write("\n")
def get_index_coin_model(ticker): try: return IndexedCoinModel.get(IndexedCoinModel.Ticker == ticker) except Exception as e: # Model dosen't exist #logger.exception(e) return None
def show_stats(self): indexInfo = IndexInfoModel.get(id=1) # Create the Index Table cointTableData = [[ 'Coin', 'Amount', 'BTC Val', 'USD Val', 'Locked', 'Desired %', 'Current %', 'Off Target %' ]] for coin in IndexedCoinModel.select(): coinBalance = CoinBalanceModel.get( CoinBalanceModel.Coin == coin.Ticker) newArray = [ coin.Ticker, coinBalance.TotalCoins, coinBalance.BTCBalance, round(coinBalance.USDBalance, 2), coin.Locked, coin.DesiredPercentage, coinBalance.get_current_percentage(indexInfo.TotalBTCVal), coin.get_percent_from_coin_target(coinBalance, indexInfo.TotalBTCVal) ] cointTableData.append(newArray) # Create the summary table summary_table_data = [[ 'Active', 'Index Count', 'BTC Val', 'USD Val', ]] summary_table_data.append([ True, len(IndexedCoinModel.select()), indexInfo.TotalBTCVal, indexInfo.TotalUSDVal ]) coin_table = AsciiTable(cointTableData) summary_table = AsciiTable(summary_table_data) sys.stdout.write("\nCurrent Index Summary\n") sys.stdout.write(summary_table.table) sys.stdout.write("\nCurrent Index Table\n") sys.stdout.write(coin_table.table) sys.stdout.write('\n')
def get_index_coin_model(ticker): with internal_database.execution_context(): try: return IndexedCoinModel.get(IndexedCoinModel.Ticker == ticker) except Exception as e: # Model dosen't exist #logger.exception(e) return None
def delete_index_coin_model(ticker): try: indexCoinModel = IndexedCoinModel.get( IndexedCoinModel.Ticker == ticker) indexCoinModel.delete_instance() return True except Exception as e: # Model dosen't exist #logger.exception(e) return False
def update_index_coin_model(ticker, desiredPercentage, distanceFromTarget, locked): with internal_database.execution_context(): try: indexedCoin = IndexedCoinModel.get(IndexedCoinModel.Ticker == ticker) indexedCoin.DesiredPercentage = round(desiredPercentage, 2) indexedCoin.DistanceFromTarget = round(distanceFromTarget, 2) indexedCoin.Locked = locked indexedCoin.save() return True except IntegrityError: return False except Exception as e: logger.exception(e)
def update_index_coin_model(ticker, desiredPercentage, currentPercentage, unRealizedGain, locked): try: indexedCoin = IndexedCoinModel.get( IndexedCoinModel.Ticker == ticker) indexedCoin.DesiredPercentage = round(desiredPercentage, 2) indexedCoin.CurrentPercentage = round(currentPercentage, 2) indexedCoin.UnrealizedGain = round(unRealizedGain, 2) indexedCoin.Locked = locked indexedCoin.save() return True except IntegrityError: return False except Exception as e: logger.exception(e)
def get_all_index_coin_models(): with internal_database.execution_context(): try: return IndexedCoinModel.select() except Exception as e: logger.exception(e)
def get_all_index_coin_models(): try: return IndexedCoinModel.select() except Exception as e: logger.exception(e)