Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 4 min read

Building a Multi-Country Real Estate Underwriting Engine in Pure JS (Canadian Semi-Annual Mortgages, OSFI B-20, US 30yr & French LMNP)

Almost every real estate calculator on the web makes the same shortcut: it takes a US 30-year monthly-compounded mortgage formula (r / 12), slaps on a flat 2% closing cost, and calls it a day. The moment you underwrite

Building a Multi-Country Real Estate Underwriting Engine in Pure JS (Canadian Semi-Annual Mortgages, OSFI B-20, US 30yr & French LMNP)

Almost every real estate calculator on the web makes the same shortcut: it takes a US 30-year monthly-compounded mortgage formula (r / 12), slaps on a flat 2% closing cost, and calls it a day.

The moment you underwrite a duplex in Montreal, a triplex in Toronto, a single-family rental in Chicago, and a furnished apartment in Lyon inside the same codebase, that shortcut produces numbers that are off by hundreds of dollars per month and thousands of dollars at closing.

When I built PlexPulse (a free client-side Chrome and Firefox extension + interactive underwriting calculator for Canada, the US, and France), I had to model every regional rule in a single zero-dependency JavaScript module (finance.js) that runs in < 1 ms right inside listing pages.

PlexPulse real-time underwriting overlay on a Canadian Centris multiplex listing

Here is the math, the edge cases, and the pure JS implementation behind a genuinely accurate multi-country underwriting engine.

1. Canadian Semi-Annual Compounding vs. US/FR Monthly Compounding

In the United States and France, fixed-rate mortgages compound monthly (r_nominal / 12). In Canada, under the federal Interest Act, fixed-rate residential mortgages must be compounded semi-annually, not in advance:

r_monthly_ca = (1 + r / 2)^(2/12) - 1 = (1 + r / 2)^(1/6) - 1

At 5.00% nominal on a $600,000 / 25-year loan:

  • Naive monthly compounding (0.41667%): $3,507.54/mo
  • Exact Canadian semi-annual compounding (0.41239%): $3,489.63/mo
function getMonthlyRate(annualRatePct, compounding) {
  const rate = Number(annualRatePct) || 0;
  if (rate <= 0) return 0;
  if (compounding === 'monthly') return rate / 100 / 12;
  // Canadian legal semi-annual compounding: (1 + r/2)^(2/12) - 1
  return Math.pow(1 + rate / 100 / 2, 1 / 6) - 1;
}

function getMonthlyMortgageFactor(annualRatePct, amortizationYears, compounding) {
  const n = Math.max(1, Number(amortizationYears) || 25) * 12;
  const r = getMonthlyRate(annualRatePct, compounding);
  if (r === 0) return 1 / n;
  return r / (1 - Math.pow(1 + r, -n));
}

Simulating the first 12 monthly payments directly gives both exact Year-1 principal paydown and exact Year-1 deductible interest for French LMNP RΓ©el tax math:

function calcYear1PrincipalPaydown(principal, annualRatePct, amortizationYears, compounding) {
  let balance = Number(principal) || 0;
  if (balance <= 0) return 0;
  const r = getMonthlyRate(annualRatePct, compounding);
  const payment = balance * getMonthlyMortgageFactor(annualRatePct, amortizationYears, compounding);
  let totalPrincipal = 0;
  for (let month = 0; month < 12 && balance > 0; month += 1) {
    const interest = balance * r;
    const principalPaid = Math.min(balance, Math.max(0, payment - interest));
    balance -= principalPaid;
    totalPrincipal += principalPaid;
  }
  return totalPrincipal;
}

2. Mortgage Insurance Across 3 Jurisdictions (CMHC, US PMI & French Borrower Insurance)

Market Trigger Rate Structure How It Is Paid
Canada (CMHC/SCHL) < 20% down, owner-occupied, 1–4 units, < $1.5M 4.0% (<10%), 3.1% (10–14.99%), 2.8% (15–19.99%), +0.2% if > 25y Premium capitalized into mortgage; 9% QC PST / 8% ON RST paid in cash at closing
US (PMI) < 20% down ~0.85%/yr of base loan Monthly cash expense
France (Assurance emprunteur) Practically all loans ~0.34%/yr of base loan Monthly cash expense + tax-deductible under LMNP RΓ©el
function calcCmhcInsurance(price, downPct, isOwnerOccupied, amortYears, units, jurisdiction) {
  const ineligible = !isOwnerOccupied || price >= 1500000 || units >= 5;
  if (price <= 0 || downPct >= 20 || ineligible) {
    return { ratePct: 0, premium: 0, taxOnPremium: 0 };
  }
  let baseRatePct = 4.0;
  if (downPct >= 15) baseRatePct = 2.8;
  else if (downPct >= 10) baseRatePct = 3.1;
  const ratePct = baseRatePct + (amortYears > 25 ? 0.2 : 0);
  const premium = price * (1 - downPct / 100) * (ratePct / 100);
  const taxRate =
    (jurisdiction === 'montreal' || jurisdiction === 'quebec') ? 0.09 :
    (jurisdiction === 'ontario' || jurisdiction === 'toronto') ? 0.08 : 0;
  return { ratePct, premium, taxOnPremium: premium * taxRate };
}

3. OSFI B-20 Stress Test, Progressive Transfer Taxes & French LMNP

In Canada, federally regulated lenders qualify borrowers at max(r_contract + 2.0%, 5.25%). Computing stressedMonthlyCashflow and stressedDscr right next to base cashflow exposes deals that break on a 5-year renewal.

Meanwhile, every progressive transfer-tax schedule (Montreal 2026 Taxe de bienvenue with Math.max(price, municipalAssessment), Toronto 2026 Double LTT + luxury MLTT tiers, BC PTT, and French ~8.4% DMTO) reduces cleanly to a 10-line bracket reducer:

function calcProgressiveBrackets(basis, brackets) {
  let tax = 0, prevCap = 0;
  for (const [cap, rate] of brackets) {
    if (basis <= prevCap) break;
    tax += (Math.min(basis, cap) - prevCap) * rate;
    prevCap = cap;
  }
  return tax;
}

PlexPulse French Leboncoin overlay showing DMTO closing costs and LMNP net-net cashflow

For French furnished rentals (LMNP), comparing Micro-BIC (50% flat allowance) against RΓ©gime RΓ©el (deducting opex + Year-1 interest + borrower insurance + ~85% / 30y building amortization at a 47.2% combined 30% TMI + 17.2% social levies rate) shows investors immediately why pre-tax yield hides the real cashflow gap.

4. Solving Max Offer Price Across Discontinuities with Binary Search

Closed-form breakeven formulas break the moment an offer crosses the $1.5M CMHC cliff or progressive transfer-tax brackets. Because monthlyCashflow(P) is strictly monotonically decreasing in price P, a 40-step binary search solves any discontinuous rule set to < $0.01 precision in < 0.1 ms:

function findMaxOfferBinarySearch(baseParams, targetFn, maxPrice = 10_000_000) {
  let lo = 0;
  let hi = Math.max(baseParams.price * 1.5, maxPrice);
  if (!targetFn(analyzeDeal({ ...baseParams, price: 1 }))) return 0;
  for (let i = 0; i < 40; i += 1) {
    const mid = (lo + hi) / 2;
    if (targetFn(analyzeDeal({ ...baseParams, price: mid }))) lo = mid;
    else hi = mid;
  }
  return Math.floor(lo);
}

Try the Live Engine

Full disclosure: I built *PlexPulse** around this exact engine.*

If you have built underwriting models for other markets (UK Stamp Duty / BTL ICR, German Grunderwerbsteuer, or Australian negative gearing), I'd love to compare edge cases in the comments.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.