PnL Calculator (Profit/Loss) in Mobile Crypto App

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
PnL Calculator (Profit/Loss) in Mobile Crypto App
Medium
~3-5 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1054
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

P&L Calculator Implementation in Mobile Crypto App

P&L in crypto is not just (sell_price - buy_price) × quantity. Multiple buys at different prices, fees in different tokens, realized and unrealized P&L, tax accounting methods (FIFO vs LIFO vs average cost) — each factor changes the final number. Calculation error surfaces when filing tax return.

P&L Calculation Models

Three accounting methods giving different results for same trade set:

FIFO (First In, First Out) — sell earliest purchased units first. Standard for most jurisdictions.

LIFO (Last In, First Out) — sell latest purchased. Advantageous on rising market (sell expensively purchased recently).

Average Cost — divide total position cost by quantity. Simpler to understand, allowed in some countries.

Real-world example — difference is clear:

  • Buy 1: 1 BTC at $20,000
  • Buy 2: 1 BTC at $30,000
  • Sell: 1 BTC at $35,000
Method Cost Basis P&L
FIFO $20,000 +$15,000
LIFO $30,000 +$5,000
Average Cost $25,000 +$10,000

Implement all three, with switching in settings:

abstract class PnLMethod {
  PnLResult calculate(List<Trade> buys, List<Trade> sells);
}

class FifoMethod implements PnLMethod {
  @override
  PnLResult calculate(List<Trade> buys, List<Trade> sells) {
    final buyQueue = Queue<({double price, double qty, DateTime date})>();
    for (final buy in buys) {
      buyQueue.add((price: buy.price, qty: buy.quantity, date: buy.date));
    }

    double realizedPnL = 0;
    double totalFees = 0;

    for (final sell in sells) {
      var remaining = sell.quantity;
      totalFees += sell.feeInBase; // convert fee to base asset

      while (remaining > 0 && buyQueue.isNotEmpty) {
        final buy = buyQueue.first;
        final matched = min(remaining, buy.qty);

        realizedPnL += matched * (sell.price - buy.price);

        if (matched >= buy.qty) {
          buyQueue.removeFirst();
        } else {
          buyQueue.first = (price: buy.price, qty: buy.qty - matched, date: buy.date);
        }
        remaining -= matched;
      }
    }

    // Unrealized P&L on remaining queue
    final unrealizedCostBasis = buyQueue.fold(0.0, (sum, b) => sum + b.price * b.qty);
    final unrealizedQuantity = buyQueue.fold(0.0, (sum, b) => sum + b.qty);

    return PnLResult(
      realizedPnL: realizedPnL - totalFees,
      unrealizedCostBasis: unrealizedCostBasis,
      unrealizedQuantity: unrealizedQuantity,
      totalFees: totalFees,
    );
  }
}

Fee Accounting

Fees cut P&L, non-trivial to account for. On exchanges fees can be:

  • In quote currency (sold BTC/USDT — fee in USDT, deducted from received amount)
  • In base currency (fee in BTC — reduces received amount)
  • In third token (BNB on Binance with fee discount enabled)

For correct P&L — convert all fees to quote currency at trade-time rate:

double normalizeFeeToCurrency(Trade trade, double feeTokenPriceAtTime) {
  if (trade.feeAsset == trade.quoteCurrency) {
    return trade.fee; // already correct currency
  }
  // Fee in different token (BNB etc.)
  return trade.fee * feeTokenPriceAtTime;
}

Price feeTokenPriceAtTime — from CoinGecko historical API or Binance Klines for exact trade date.

Unrealized P&L in Real-Time

unrealizedPnL = (currentPrice - avgEntryPrice) * holdingQuantity

To update real-time subscribe to WebSocket ticker. avgEntryPrice after each buy:

// On new buy recalculate average price (average cost method)
void addPosition(double buyPrice, double quantity) {
  final newTotalCost = (_totalQuantity * _avgEntryPrice) + (buyPrice * quantity);
  _totalQuantity += quantity;
  _avgEntryPrice = newTotalCost / _totalQuantity;
}

double get unrealizedPnL => (_currentPrice - _avgEntryPrice) * _totalQuantity;
double get unrealizedPnLPercent => (unrealizedPnL / (_avgEntryPrice * _totalQuantity)) * 100;

ValueNotifier<double> for _currentPrice — on price change only P&L recalculates, not entire screen.

UI: Displaying P&L Clearly

Three information blocks on one screen:

  1. Unrealized P&L — current position, updates in real-time. Large font, green/red color, absolute value + percent.

  2. Realized P&L — total from closed trades for selected period. Less critical for monitoring, important for taxes.

  3. Breakdown — table per pair with entry price, quantity, current price, P&L.

Method switcher (FIFO/LIFO/Average Cost) — in settings, with warning that method change recalculates entire history.

Tax Export

CSV format with columns: Date, Pair, Type (buy/sell), Price, Quantity, Fee, Fee Currency, Realized P&L, Method. Basic format for most crypto tax services (Koinly, CoinTracker) on import.

Scope of Work

  • Three calculation method implementation (FIFO, LIFO, Average Cost) with switching
  • Fee accounting in different currencies
  • Unrealized P&L with real-time updates via WebSocket
  • Realized P&L from trade history
  • Breakdown per trading pair
  • CSV export for tax reporting

Timeframe

Basic calculator (one method, manual input): 3–5 days. Full with three methods, exchange API, real-time and export: 2–3 weeks. Cost calculated individually.