def pnl_total(): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed and get balance for last avail.date curr = current_user.currency today = get_last_price_dt() balances = calc_balance(current_user, curr, logger) # Calc total portfolio value tot_value = 0 for coin in balances: tot_value += coin.value tot_value = round(tot_value, 2) # Get total cost value tot_cost = calc_tot_pf_cost(current_user, logger, today) tot_PL = round(tot_value - tot_cost, 2) tot_value = num_2_str(tot_value, curr, 0) tot_cost = num_2_str(tot_cost, curr, 0) tot_PL = num_2_str(tot_PL, curr, 0) ts = get_price_timestamp() return render_template('pnl_total.html', balances=balances, title='Balance', tot_value=tot_value, tot_cost=tot_cost, tot_PL=tot_PL, curr=curr, timestamp=ts, today=today)
def balance_exch(): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed curr = current_user.currency # Obtener balance, eliminar fiats y agrupar por exchange last_price_dt = get_last_price_dt() crypto_portfolio = Portfolio.query.filter_by(user_id=current_user.id, date=last_price_dt).all() split_portf = split_portf_by_exch(crypto_portfolio) split_portf = OrderedDict(sorted(split_portf.items())) exch_summary = calc_exch_summary(split_portf, curr) # Calc total values & format results total_value = 0 total_value_btc = 0 for exch in exch_summary: total_value += exch_summary[exch]['value'] total_value_btc += exch_summary[exch]['value_btc'] exch_summary[exch]['value'] = num_2_str(exch_summary[exch]['value'], curr, 0) exch_summary[exch]['value_btc'] = num_2_str( exch_summary[exch]['value_btc'], 'BTC', 4) total_value = num_2_str(total_value, curr, 0) total_value_btc = num_2_str(total_value_btc, 'BTC', 4) ts = get_price_timestamp() # Render page return render_template('balance_exch.html', split_portf=split_portf, curr=curr, exch_summary=exch_summary, total_value=total_value, total_value_btc=total_value_btc, timestamp=ts, title='Balance por Exchange')
def pnl_history(): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed portf_evol = calc_pnl_history(current_user, logger) ts = get_price_timestamp() return render_template('pnl_history.html', portf_evol=portf_evol, timestamp=ts, title='Histórico de Pérdidas y Ganancias')
def pnl_trades(): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed curr = current_user.currency trades = TradePL.query.filter_by(user_id=current_user.id).all() ts = get_price_timestamp() return render_template('pnl_trades.html', trades=trades, curr=curr, timestamp=ts, title='Pérdidas & Ganancias por Operación')
def balance(date): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed if date == 'Today': dt = get_last_price_dt() else: try: dt = datetime.datetime.strptime(date, "%Y-%m-%d") except Exception as e: dt = get_last_price_dt() flash( "Por favor, seleccione un formato correcto de fecha: " "'YYYY-MM-DD'", 'danger') curr = current_user.currency crypto_portfolio = Portfolio.query.filter_by(user_id=current_user.id, date=dt).all() if not crypto_portfolio: flash( "No se ha encontrado información sobre el portfolio para " "la fecha '{}'".format(date), 'danger') dt = get_last_price_dt() crypto_portfolio = Portfolio.query.filter_by(user_id=current_user.id, date=dt).all() # Prepare list of coins & exchanges to be used later exchanges = [] coins = [] for pos in crypto_portfolio: if pos.coin not in coins: coins.append(pos.coin) if pos.exchange not in exchanges: exchanges.append(pos.exchange) exchanges = sorted(exchanges) coins = sorted(coins) # Loop to calculate Totals total_value = 0 total_value_btc = 0 for pos in crypto_portfolio: total_value += pos.value total_value_btc += pos.value_btc # Loop to calculate Exchange summary exch_value = 0 exch_value_btc = 0 exch_summary = {} for exch in exchanges: for pos in crypto_portfolio: if exch == pos.exchange: exch_value += pos.value exch_value_btc += pos.value_btc perc = "{:.2%}".format(exch_value / total_value) exch_value = num_2_str(exch_value, curr, 0) exch_value_btc = num_2_str(exch_value_btc, 'BTC', 4) exch_summary.update({ exch: { 'value': exch_value, 'value_btc': exch_value_btc, 'perc': perc } }) exch_value = 0 exch_value_btc = 0 # Loop to calculate Coin summary coin_amount = 0 coin_value = 0 coin_value_btc = 0 coin_summary = {} for coin in coins: for pos in crypto_portfolio: if coin == pos.coin: coin_amount += pos.amount coin_value += pos.value coin_value_btc += pos.value_btc perc = "{:.2%}".format(coin_value / total_value) coin_value = num_2_str(coin_value, curr, 0) coin_value_btc = num_2_str(coin_value_btc, 'BTC', 4) coin_amount = "{:20,.8f}".format(coin_amount) coin_summary.update({ coin: { 'amount': coin_amount, 'value': coin_value, 'value_btc': coin_value_btc, 'perc': perc } }) coin_amount = 0 coin_value = 0 coin_value_btc = 0 # Format Totals total_value = num_2_str(total_value, curr, 0) total_value_btc = num_2_str(total_value_btc, 'BTC', 4) ts = get_price_timestamp() # Render website return render_template('balance.html', date=dt, exch_summary=exch_summary, coin_summary=coin_summary, total_value=total_value, total_value_btc=total_value_btc, timestamp=ts, title='Balance Histórico')
def dashboard(): # If user has no operations in DB, jump to start if not check_if_ops(current_user): return redirect(url_for('start')) # If user has operations, proceed curr = current_user.currency # Generate information for Portfolio Summary pf_delta = calc_portfolio_deltas(current_user, logger) # Generate information for Coin-Table today = get_last_price_dt() balances = calc_balance(current_user, curr, logger) coin_dashbord = {} for bal in balances: if bal.value == 0: continue coin = bal.coin if coin not in Params.CURRENCIES: curr_delta = PriceDelta.query.filter_by(coin=coin, currency=curr).first() btc_delta = PriceDelta.query.filter_by(coin=coin, currency='BTC').first() price = db.session.query(Price.price)\ .filter_by(coin=coin, currency=curr, date=today)\ .first() if price: price = price[0] else: price_btc = '-' price_btc = db.session.query(Price.price)\ .filter_by(coin=coin, currency='BTC', date=today).first() if price_btc: price_btc = price_btc[0] else: price_btc = '-' if curr_delta and btc_delta: price = num_2_str(price, curr) price_btc = num_2_str(price_btc, 'BTC') value = num_2_str(bal.value, curr, 0) value_btc = num_2_str(bal.value_btc, 'BTC', 4) perc = num_2_perc(bal.perc, 2) curr_1d = num_2_perc(curr_delta.delta_1d, 2) curr_7d = num_2_perc(curr_delta.delta_7d, 2) curr_1m = num_2_perc(curr_delta.delta_1m, 2) curr_3m = num_2_perc(curr_delta.delta_3m, 2) curr_6m = num_2_perc(curr_delta.delta_6m, 2) curr_1y = num_2_perc(curr_delta.delta_1y, 2) curr_2y = num_2_perc(curr_delta.delta_2y, 2) btc_1d = num_2_perc(btc_delta.delta_1d, 2) btc_7d = num_2_perc(btc_delta.delta_7d, 2) btc_1m = num_2_perc(btc_delta.delta_1m, 2) btc_3m = num_2_perc(btc_delta.delta_3m, 2) btc_6m = num_2_perc(btc_delta.delta_6m, 2) btc_1y = num_2_perc(btc_delta.delta_1y, 2) btc_2y = num_2_perc(btc_delta.delta_2y, 2) coin_dashbord.update({ coin: { "Value": value, "ValueBTC": value_btc, "Price": price, "PriceBTC": price_btc, "Perc": perc, "curr_1d": curr_1d, "curr_7d": curr_7d, "curr_1m": curr_1m, "curr_3m": curr_3m, "curr_6m": curr_6m, "curr_1y": curr_1y, "curr_2y": curr_2y, "btc_1d": btc_1d, "btc_7d": btc_7d, "btc_1m": btc_1m, "btc_3m": btc_3m, "btc_6m": btc_6m, "btc_1y": btc_1y, "btc_2y": btc_2y } }) ts = get_price_timestamp() return render_template('dashboard.html', pf_delta=pf_delta, coin_dashbord=coin_dashbord, timestamp=ts, title='Dashboard')