Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Immo·Ka9# File : immoka/mortgage/providers/bank_of_canada.py10# Module : immoka11# Repository : https://git.spboucher.ai/immo-ka12# Created : 2026-08-2413# Last Modified : 2026-08-2414#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author : Simon-Pierre Boucher20# Position : Professor of Finance21# Institution : Université du Québec en Outaouais (UQO)22# Department : Département des sciences administratives23# Organization : SP Boucher24#25# Website : https://spboucher.ai26# Git : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General : info@spboucher.ai33# Professional : contact@spboucher.ai34# Direct : simon@spboucher.ai35# Research : research@spboucher.ai36# Development : dev@spboucher.ai37# Support : support@spboucher.ai38# Security/Admin : admin@spboucher.ai39# Notifications : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification : Public66# Distribution : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License : None specified (no license file in this repository)80# Maintainer : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# -----------------------------------------------------------------------------88# Immo-Ka — Agrégateur de maisons à vendre (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# mortgage/providers/bank_of_canada.py : Banque du Canada — API Valet91# (officielle, publique, JSON). Séries de référence : taux hypothécaire92# conventionnel affiché 1/3/5 ans, taux directeur, rendement obligataire93# 5 ans. purpose="unknown" : séries de RÉFÉRENCE, exclues du comparateur94# de prêteurs (la BdC ne prête pas aux particuliers).95# -----------------------------------------------------------------------------96from __future__ import annotations9798from .base import RateProvider99100SERIES_URL = ("https://www.bankofcanada.ca/valet/observations/"101 "V80691333,V80691334,V80691335,V39079,BD.CDN.5YR.DQ.YLD/"102 "json?recent=10")103104# série -> (terme mois, nom de produit)105MORTGAGE_SERIES = {106 "V80691333": (12, "Taux hypothécaire conventionnel affiché — 1 an"),107 "V80691334": (36, "Taux hypothécaire conventionnel affiché — 3 ans"),108 "V80691335": (60, "Taux hypothécaire conventionnel affiché — 5 ans"),109}110CONTEXT_SERIES = {111 "V39079": "Taux cible du financement à un jour",112 "BD.CDN.5YR.DQ.YLD": "Rendement obligataire 5 ans — Gouvernement du Canada",113}114115116class BankOfCanadaProvider(RateProvider):117 provider_id = "bank_of_canada"118 institution = "Banque du Canada"119 source_url = "https://www.bankofcanada.ca/rates/interest-rates/"120 request_delay = 0.5121122 def fetch(self) -> list[dict]:123 return self.parse(self.get(SERIES_URL).text)124125 def parse(self, payload: str) -> list[dict]:126 import json127 data = json.loads(payload)128 observations = data.get("observations") or []129 # Séries à fréquences mélangées : garder la DERNIÈRE valeur par série.130 latest: dict[str, tuple[str, float]] = {}131 for obs in observations:132 d = obs.get("d", "")133 for sid, cell in obs.items():134 if sid == "d" or not isinstance(cell, dict):135 continue136 v = cell.get("v")137 if v in (None, ""):138 continue139 try:140 latest[sid] = (d, float(v))141 except ValueError:142 continue143 out: list[dict] = []144 for sid, (term, name) in MORTGAGE_SERIES.items():145 if sid not in latest:146 continue147 d, v = latest[sid]148 out.append(self.make_product(149 rate=v, rate_type="fixed", term_months=term, kind="posted",150 product_name=name, purpose="unknown",151 conditions=f"Série Valet {sid} — observation du {d} "152 "(moyenne hebdomadaire des taux affichés)",153 raw={"series": sid, "date": d, "value": v}))154 for sid, name in CONTEXT_SERIES.items():155 if sid not in latest:156 continue157 d, v = latest[sid]158 out.append(self.make_product(159 rate=v, rate_type="other", term_months=12, kind="posted",160 product_name=name, purpose="unknown",161 conditions=f"Série Valet {sid} — observation du {d}",162 raw={"series": sid, "date": d, "value": v}))163 return out164