def add_todays_balances(): """ Reads balances from balances table, and updates the balancehistory table accordingly """ with create_connection() as conn: cursor = conn.cursor() query = """INSERT INTO 'cbalancehistory' (account, date, token, balance, balance_btc, balance_eur,balance_usd,balance_jpy) VALUES (?,?,?,?,?,?,?,?);""" # Delete previous balances from today todays_balances = get_balances_from_last_day() if len(todays_balances) > 0: for balance_history in todays_balances: _id = balance_history[0] delete_balance_from_id(_id) # Write today's balances accounts = cbalances.get_all_entries() for acc in accounts: # Prepare account, token, balance, *_ = acc date = int(datetime.today().timestamp()) b_btc = prices.to_btc(token, balance) b_eur = prices.btc_to_fiat(b_btc, currency='eur') b_usd = prices.btc_to_fiat(b_btc, currency='usd') b_jpy = prices.btc_to_fiat(b_btc, currency='jpy') # Insert cursor.execute( query, (account, date, token, balance, b_btc, b_eur, b_usd, b_jpy)) conn.commit()
def tokenChanged(self, tokensymbol): """ If a Token is selected, we show the total amount of it on all accounts """ # Getting token id from coingecko's list if tokensymbol == '': # No token selected return try: token_name = prices.symbol_to_id(tokensymbol) except KeyError: # Not in Coingecko's list logging.warning( f"Selected symbol {tokensymbol} is not in coingecko's stored list") token_name = tokensymbol # Updating token info self.token_or_account_name.setText( token_name[0].upper()+token_name[1:]) # Updating token balances tokenbalance = round(cbalances.get_total_token_balance(tokensymbol), 6) self.token_or_account_balance.setText( str(tokenbalance)+' '+tokensymbol) self.token_or_account_balance.show() tokenbalancebtc = round(prices.to_btc(tokensymbol, tokenbalance), 8) self.token_or_account_balance_btc.setText(str(tokenbalancebtc)+" BTC") if tokensymbol in ["BTC", "btc"]: self.token_or_account_balance_btc.setText("") tokenbalancefiat = prices.btc_to_fiat(tokenbalancebtc) self.token_or_account_balance_fiat.setText( str(tokenbalancefiat) + " {}".format(self.FIAT_CURRENCY.upper()))
def accountChanged(self, accountname): """ If an account is selected, we show the total amount of all of its tokens """ # Updating account name self.token_or_account_name.setText(accountname) # Updating account balances acc_token_balances = cbalances.get_entries_with_account(accountname) total_in_btc = 0 for atb in acc_token_balances: token = atb[1] amount = atb[2] amount_btc = prices.to_btc(token, amount) total_in_btc += amount_btc total_in_fiat = prices.btc_to_fiat( total_in_btc, currency=self.FIAT_CURRENCY) total_in_btc = round(total_in_btc, 8) total_in_fiat = int(round(total_in_fiat, 0)) self.token_or_account_balance.hide() # Not useful here self.token_or_account_balance_btc.setText(str(total_in_btc) + " BTC") self.token_or_account_balance_fiat.setText( str(total_in_fiat) + " " + self.FIAT_CURRENCY.upper())
def allMode(self): """ If 'All' is selected, we show the full balance of all tokens in all accounts """ self.token_or_account_name.setText(self.tr("All Coins")) totalbalancebtc, totalbalancefiat = 0, 0 for t in cbalances.get_all_tokens(): tokenbalance = cbalances.get_total_token_balance(t) tokenbalance_btc = prices.to_btc(t, tokenbalance) totalbalancebtc += tokenbalance_btc totalbalancefiat += prices.btc_to_fiat( tokenbalance_btc, currency=self.FIAT_CURRENCY) self.token_or_account_balance.hide() self.token_or_account_balance_btc.setText( str(round(totalbalancebtc, 8))+" BTC") self.token_or_account_balance_fiat.setText( str(round(totalbalancefiat, 2))+" " + self.FIAT_CURRENCY)
def updateWithAll(self): """ Sets the table in 'All' mode, which shows all accounts and tokens """ self.setSortingEnabled(False) self.clear() self.setColumnCount(8) self.setHorizontalHeaderLabels( ['Account', 'Token', 'Balance', 'Balance(BTC)', 'Balance({})'.format(self.FIAT_CURRENCY), 'Type', 'KYC', 'Description']) rows_to_insert = cbalances.get_all_entries() self.setRowCount(len(rows_to_insert)) for numrow, row in enumerate(rows_to_insert): # Account self.setItem(numrow, 0, QTableWidgetItem(row[0])) # Token self.setItem(numrow, 1, QTableWidgetItem(row[1].upper())) # Balance item = QTableWidgetItem() item.setData(0, row[2]) self.setItem(numrow, 2, item) # Balance(BTC) balance_in_btc = prices.to_btc(row[1], row[2]) item = QTableWidgetItem() item.setData(0, balance_in_btc) self.setItem(numrow, 3, item) # Balance (Fiat) balance_in_fiat = prices.btc_to_fiat( balance_in_btc, currency=self.FIAT_CURRENCY) item = QTableWidgetItem() item.setData(0, balance_in_fiat) self.setItem(numrow, 4, item) # Type item = formatTypeItem(QTableWidgetItem(str(row[3]))) self.setItem(numrow, 5, item) # KYC item = formatKYCItem(QTableWidgetItem(str(row[4]))) self.setItem(numrow, 6, item) # Description self.setItem(numrow, 7, QTableWidgetItem(str(row[5]))) self.resizeColumnsToContents() self.setSortingEnabled(True)
def updateWithAccount(self, account): """Only shows database entries where account=account""" self.setSortingEnabled(False) self.clear() self.setColumnCount(7) self.setHorizontalHeaderLabels( ['Token', 'Balance', 'Balance(BTC)', 'Balance({})'.format(self.FIAT_CURRENCY), 'Type', 'KYC', 'Description']) rows_to_insert = cbalances.get_entries_with_account(account) self.setRowCount(len(rows_to_insert)) for numrow, row in enumerate(rows_to_insert): # Token self.setItem(numrow, 0, QTableWidgetItem(row[1].upper())) # Balance item = QTableWidgetItem() item.setData(0, row[2]) self.setItem(numrow, 1, item) # Balance (BTC) balance_in_btc = prices.to_btc(row[1], row[2]) item = QTableWidgetItem() item.setData(0, balance_in_btc) self.setItem(numrow, 2, item) # Balance (Fiat) balance_in_fiat = prices.btc_to_fiat( balance_in_btc, currency=self.FIAT_CURRENCY) item = QTableWidgetItem() item.setData(0, balance_in_fiat) self.setItem(numrow, 3, item) # Type item = formatTypeItem(QTableWidgetItem(str(row[3]))) self.setItem(numrow, 4, item) # KYC item = formatKYCItem(QTableWidgetItem(str(row[4]))) self.setItem(numrow, 5, item) # Description self.setItem(numrow, 6, QTableWidgetItem(str(row[5]))) self.resizeColumnsToContents() self.setSortingEnabled(True)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setObjectName("totalwealthwidget") # Background image self.setStyleSheet( GradientStyle.get(self.objectName(), "#F06814", "#E43E53")) # Size self.setMaximumSize(645, 213) # ------------ Content ------------ self.layout = QVBoxLayout() self.layout.setSpacing(0) # --Header--- self.header = QLabel(self.tr("Total Wealth")) self.header.setMaximumHeight(80) self.header.setStyleSheet( "background-color: rgba(0,0,0,0);margin-top:25") self.header.setFont(QFont('Roboto', pointSize=40, weight=QFont.Bold)) self.header.setAlignment(Qt.AlignCenter) self.layout.addWidget(self.header, Qt.AlignCenter) # -- Amount Layout -- self.amountlyt = QHBoxLayout() self.amountlyt.setSpacing(0) # - Amount Label - totalwealth_portfolio = balances.get_total_balance_all_accounts() totalwealth_cportfolio = prices.btc_to_fiat( cbalances.get_total_balance_all_accounts()) totalwealth_amount = int(totalwealth_portfolio + totalwealth_cportfolio) self.amount_label = QLabel(str(totalwealth_amount)) self.amount_label.setStyleSheet( "background-color: rgba(0,0,0,0);margin-left:60; margin-right: 0") font = QFont() font.setFamily("Inter") font.setWeight(QFont.Medium) font.setPointSize(40) self.amount_label.setFont(font) self.amount_label.setAlignment(Qt.AlignVCenter | Qt.AlignRight) # - Fiat Label - self.fiat_label = QLabel(confighandler.get_fiat_currency().upper()) self.fiat_label.setMaximumHeight(40) self.fiat_label.setStyleSheet( "background-color: rgba(0,0,0,0);margin-top:15;margin-left:0;margin-right:0" ) font = QFont() font.setFamily("Inter") font.setPointSize(18) self.fiat_label.setFont(font) self.fiat_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) # - Chart - self.chart = TotalWealthHistoryChartView() data = dbhandler.get_wealth_by_day() self.chart.setupChartWithData(data) # - Percentage - # The percentage is the difference between the first balance history and the last one first_balance = dbhandler.get_first_balance() last_balance = dbhandler.get_last_balance() if first_balance != 0: percentage = ((last_balance / first_balance) - 1) * 100 if percentage > 0: percentage_str = "+{}%".format(round(percentage, 1)) else: percentage_str = "{}%".format(round(percentage, 1)) else: # No first balance, so no change percentage_str = "na" self.percentage_label = QLabel(percentage_str) self.percentage_label.setStyleSheet( "background-color: rgba(0,0,0,0);margin-right:60") font = QFont() font.setFamily("Inter") font.setPointSize(24) self.percentage_label.setFont(font) self.percentage_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) self.amountlyt.addWidget(self.amount_label) self.amountlyt.addWidget(self.fiat_label) self.amountlyt.addWidget(self.chart) self.amountlyt.addWidget(self.percentage_label) self.layout.addLayout(self.amountlyt) self.setLayout(self.layout)
def get_total_balance_all_accounts_fiat(): """ Returns the sum of all the balances of all the accounts on this table """ return prices.btc_to_fiat(get_total_balance_all_accounts())
def get_total_account_balance_fiat(account): """ Returns the total balance from all tokens from a certain account expressed in fiat """ return prices.btc_to_fiat(get_total_account_balance(account))
def get_all_accounts_with_amount_fiat(): """ Returns tuples as (account, total amount in fiat)""" data = get_all_accounts_with_amount() return [(d[0], prices.btc_to_fiat(d[1])) for d in data]
def get_total_token_balance_fiat(token: str): """ Sums all token balances from all accounts Expressed in fiat """ return prices.btc_to_fiat(get_total_token_balance(token))